4033dcc19c443568fd4c9f2d03a37fd54a4e7347
[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 /*
20  * Copied to Jalview September 2016.
21  * Only the members of this class required for SparseIntArray were copied.
22  * Method binarySearch(short[] array, int size, short value) added to support
23  * SparseShortArray.
24  */
25 class ContainerHelpers
26 {
27   static final boolean[] EMPTY_BOOLEANS = new boolean[0];
28
29   static final int[] EMPTY_INTS = new int[0];
30
31   static final long[] EMPTY_LONGS = new long[0];
32
33   static final Object[] EMPTY_OBJECTS = new Object[0];
34
35   // This is Arrays.binarySearch(), but doesn't do any argument validation.
36   static int binarySearch(int[] array, int size, int value)
37   {
38     int lo = 0;
39     int hi = size - 1;
40     while (lo <= hi)
41     {
42       final int mid = (lo + hi) >>> 1;
43       final int midVal = array[mid];
44       if (midVal < value)
45       {
46         lo = mid + 1;
47       }
48       else if (midVal > value)
49       {
50         hi = mid - 1;
51       }
52       else
53       {
54         return mid; // value found
55       }
56     }
57     return ~lo; // value not present
58   }
59
60   static int binarySearch(long[] array, int size, long value)
61   {
62     int lo = 0;
63     int hi = size - 1;
64     while (lo <= hi)
65     {
66       final int mid = (lo + hi) >>> 1;
67       final long midVal = array[mid];
68       if (midVal < value)
69       {
70         lo = mid + 1;
71       }
72       else if (midVal > value)
73       {
74         hi = mid - 1;
75       }
76       else
77       {
78         return mid; // value found
79       }
80     }
81     return ~lo; // value not present
82   }
83
84   // This is Arrays.binarySearch(), but doesn't do any argument validation.
85   static int binarySearch(short[] array, int size, short value)
86   {
87     int lo = 0;
88     int hi = size - 1;
89     while (lo <= hi)
90     {
91       final int mid = (lo + hi) >>> 1;
92       final short midVal = array[mid];
93       if (midVal < value)
94       {
95         lo = mid + 1;
96       }
97       else if (midVal > value)
98       {
99         hi = mid - 1;
100       }
101       else
102       {
103         return mid; // value found
104       }
105     }
106     return ~lo; // value not present
107   }
108 }