Merge branch 'documentation/JAL-3407_2.11.1_release' into releases/Release_2_11_1_Branch
[jalview.git] / src / jalview / ext / android / ContainerHelpers.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.ext.android;
22
23 /*
24  * Copyright (C) 2013 The Android Open Source Project
25  *
26  * Licensed under the Apache License, Version 2.0 (the "License");
27  * you may not use this file except in compliance with the License.
28  * You may obtain a copy of the License at
29  *
30  *      http://www.apache.org/licenses/LICENSE-2.0
31  *
32  * Unless required by applicable law or agreed to in writing, software
33  * distributed under the License is distributed on an "AS IS" BASIS,
34  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
35  * See the License for the specific language governing permissions and
36  * limitations under the License.
37  */
38
39 /*
40  * Copied to Jalview September 2016.
41  * Only the members of this class required for SparseIntArray were copied.
42  * Change Log:
43  * Sep 2016: Method binarySearch(short[] array, int size, short value) added to support
44  * SparseShortArray.
45  * Jan 2017: EMPTY_DOUBLES added
46  */
47 class ContainerHelpers
48 {
49   static final boolean[] EMPTY_BOOLEANS = new boolean[0];
50
51   static final int[] EMPTY_INTS = new int[0];
52
53   static final double[] EMPTY_DOUBLES = new double[0];
54
55   static final long[] EMPTY_LONGS = new long[0];
56
57   static final Object[] EMPTY_OBJECTS = new Object[0];
58
59   // This is Arrays.binarySearch(), but doesn't do any argument validation.
60   static int binarySearch(int[] array, int size, int 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 int 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   static int binarySearch(long[] array, int size, long value)
85   {
86     int lo = 0;
87     int hi = size - 1;
88     while (lo <= hi)
89     {
90       final int mid = (lo + hi) >>> 1;
91       final long midVal = array[mid];
92       if (midVal < value)
93       {
94         lo = mid + 1;
95       }
96       else if (midVal > value)
97       {
98         hi = mid - 1;
99       }
100       else
101       {
102         return mid; // value found
103       }
104     }
105     return ~lo; // value not present
106   }
107
108   // This is Arrays.binarySearch(), but doesn't do any argument validation.
109   static int binarySearch(short[] array, int size, short value)
110   {
111     int lo = 0;
112     int hi = size - 1;
113     while (lo <= hi)
114     {
115       final int mid = (lo + hi) >>> 1;
116       final short midVal = array[mid];
117       if (midVal < value)
118       {
119         lo = mid + 1;
120       }
121       else if (midVal > value)
122       {
123         hi = mid - 1;
124       }
125       else
126       {
127         return mid; // value found
128       }
129     }
130     return ~lo; // value not present
131   }
132 }