JAL-98 bespoke fast percentage formatter for consensus tooltip
[jalview.git] / src / jalview / ext / android / ContainerHelpers.java
1 package jalview.ext.android;
2
3 /*
4  * Copyright (C) 2013 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 class ContainerHelpers
20 {
21   static final boolean[] EMPTY_BOOLEANS = new boolean[0];
22
23   static final int[] EMPTY_INTS = new int[0];
24
25   static final long[] EMPTY_LONGS = new long[0];
26
27   static final Object[] EMPTY_OBJECTS = new Object[0];
28
29   // This is Arrays.binarySearch(), but doesn't do any argument validation.
30   static int binarySearch(int[] array, int size, int value)
31   {
32     int lo = 0;
33     int hi = size - 1;
34     while (lo <= hi)
35     {
36       final int mid = (lo + hi) >>> 1;
37       final int midVal = array[mid];
38       if (midVal < value)
39       {
40         lo = mid + 1;
41       }
42       else if (midVal > value)
43       {
44         hi = mid - 1;
45       }
46       else
47       {
48         return mid; // value found
49       }
50     }
51     return ~lo; // value not present
52   }
53
54   static int binarySearch(long[] array, int size, long value)
55   {
56     int lo = 0;
57     int hi = size - 1;
58     while (lo <= hi)
59     {
60       final int mid = (lo + hi) >>> 1;
61       final long midVal = array[mid];
62       if (midVal < value)
63       {
64         lo = mid + 1;
65       }
66       else if (midVal > value)
67       {
68         hi = mid - 1;
69       }
70       else
71       {
72         return mid; // value found
73       }
74     }
75     return ~lo; // value not present
76   }
77 }