1 |
|
package jalview.ext.android; |
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
|
21 |
|
|
22 |
|
|
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
|
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
@link@link |
38 |
|
|
39 |
|
|
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
|
|
| 41% |
Uncovered Elements: 105 (178) |
Complexity: 46 |
Complexity Density: 0.4 |
|
50 |
|
public class SparseIntArray implements Cloneable |
51 |
|
{ |
52 |
|
private int[] mKeys; |
53 |
|
|
54 |
|
private int[] mValues; |
55 |
|
|
56 |
|
private int mSize; |
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
61 |
2 |
public SparseIntArray()... |
62 |
|
{ |
63 |
2 |
this(10); |
64 |
|
} |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
|
|
|
| 66.7% |
Uncovered Elements: 3 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
73 |
4 |
public SparseIntArray(int initialCapacity)... |
74 |
|
{ |
75 |
4 |
if (initialCapacity == 0) |
76 |
|
{ |
77 |
0 |
mKeys = ContainerHelpers.EMPTY_INTS; |
78 |
0 |
mValues = ContainerHelpers.EMPTY_INTS; |
79 |
|
} |
80 |
|
else |
81 |
|
{ |
82 |
4 |
initialCapacity = idealIntArraySize(initialCapacity); |
83 |
4 |
mKeys = new int[initialCapacity]; |
84 |
4 |
mValues = new int[initialCapacity]; |
85 |
|
} |
86 |
4 |
mSize = 0; |
87 |
|
} |
88 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
89 |
0 |
@Override... |
90 |
|
public SparseIntArray clone() |
91 |
|
{ |
92 |
0 |
SparseIntArray clone = null; |
93 |
0 |
try |
94 |
|
{ |
95 |
0 |
clone = (SparseIntArray) super.clone(); |
96 |
0 |
clone.mKeys = mKeys.clone(); |
97 |
0 |
clone.mValues = mValues.clone(); |
98 |
|
} catch (CloneNotSupportedException cnse) |
99 |
|
{ |
100 |
|
|
101 |
|
} |
102 |
0 |
return clone; |
103 |
|
} |
104 |
|
|
105 |
|
|
106 |
|
|
107 |
|
|
108 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
109 |
2 |
public int get(int key)... |
110 |
|
{ |
111 |
2 |
return get(key, 0); |
112 |
|
} |
113 |
|
|
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
118 |
2 |
public int get(int key, int valueIfKeyNotFound)... |
119 |
|
{ |
120 |
2 |
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
121 |
2 |
if (i < 0) |
122 |
|
{ |
123 |
0 |
return valueIfKeyNotFound; |
124 |
|
} |
125 |
|
else |
126 |
|
{ |
127 |
2 |
return mValues[i]; |
128 |
|
} |
129 |
|
} |
130 |
|
|
131 |
|
|
132 |
|
|
133 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
134 |
0 |
public void delete(int key)... |
135 |
|
{ |
136 |
0 |
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
137 |
0 |
if (i >= 0) |
138 |
|
{ |
139 |
0 |
removeAt(i); |
140 |
|
} |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
146 |
0 |
public void removeAt(int index)... |
147 |
|
{ |
148 |
0 |
System.arraycopy(mKeys, index + 1, mKeys, index, mSize - (index + 1)); |
149 |
0 |
System.arraycopy(mValues, index + 1, mValues, index, mSize |
150 |
|
- (index + 1)); |
151 |
0 |
mSize--; |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
|
|
| 58.3% |
Uncovered Elements: 10 (24) |
Complexity: 4 |
Complexity Density: 0.22 |
|
158 |
7 |
public void put(int key, int value)... |
159 |
|
{ |
160 |
7 |
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
161 |
7 |
if (i >= 0) |
162 |
|
{ |
163 |
0 |
mValues[i] = value; |
164 |
|
} |
165 |
|
else |
166 |
|
{ |
167 |
7 |
i = ~i; |
168 |
7 |
if (mSize >= mKeys.length) |
169 |
|
{ |
170 |
0 |
int n = idealIntArraySize(mSize + 1); |
171 |
0 |
int[] nkeys = new int[n]; |
172 |
0 |
int[] nvalues = new int[n]; |
173 |
|
|
174 |
0 |
System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length); |
175 |
0 |
System.arraycopy(mValues, 0, nvalues, 0, mValues.length); |
176 |
0 |
mKeys = nkeys; |
177 |
0 |
mValues = nvalues; |
178 |
|
} |
179 |
7 |
if (mSize - i != 0) |
180 |
|
{ |
181 |
|
|
182 |
1 |
System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i); |
183 |
1 |
System.arraycopy(mValues, i, mValues, i + 1, mSize - i); |
184 |
|
} |
185 |
7 |
mKeys[i] = key; |
186 |
7 |
mValues[i] = value; |
187 |
7 |
mSize++; |
188 |
|
} |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
195 |
0 |
public int size()... |
196 |
|
{ |
197 |
0 |
return mSize; |
198 |
|
} |
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
|
204 |
|
|
205 |
|
|
206 |
|
|
207 |
|
|
208 |
|
|
209 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
210 |
0 |
public int keyAt(int index)... |
211 |
|
{ |
212 |
0 |
return mKeys[index]; |
213 |
|
} |
214 |
|
|
215 |
|
|
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
|
221 |
|
|
222 |
|
|
223 |
|
|
224 |
|
|
225 |
|
|
226 |
|
|
227 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
228 |
0 |
public int valueAt(int index)... |
229 |
|
{ |
230 |
0 |
return mValues[index]; |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
|
@link |
235 |
|
|
236 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
237 |
0 |
public int indexOfKey(int key)... |
238 |
|
{ |
239 |
0 |
return ContainerHelpers.binarySearch(mKeys, mSize, key); |
240 |
|
} |
241 |
|
|
242 |
|
|
243 |
|
@link |
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
248 |
0 |
public int indexOfValue(int value)... |
249 |
|
{ |
250 |
0 |
for (int i = 0; i < mSize; i++) |
251 |
|
{ |
252 |
0 |
if (mValues[i] == value) |
253 |
|
{ |
254 |
0 |
return i; |
255 |
|
} |
256 |
|
} |
257 |
0 |
return -1; |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
263 |
0 |
public void clear()... |
264 |
|
{ |
265 |
0 |
mSize = 0; |
266 |
|
} |
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
|
|
|
| 0% |
Uncovered Elements: 19 (19) |
Complexity: 4 |
Complexity Density: 0.27 |
|
272 |
0 |
public void append(int key, int value)... |
273 |
|
{ |
274 |
0 |
if (mSize != 0 && key <= mKeys[mSize - 1]) |
275 |
|
{ |
276 |
0 |
put(key, value); |
277 |
0 |
return; |
278 |
|
} |
279 |
0 |
int pos = mSize; |
280 |
0 |
if (pos >= mKeys.length) |
281 |
|
{ |
282 |
0 |
int n = idealIntArraySize(pos + 1); |
283 |
0 |
int[] nkeys = new int[n]; |
284 |
0 |
int[] nvalues = new int[n]; |
285 |
|
|
286 |
0 |
System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length); |
287 |
0 |
System.arraycopy(mValues, 0, nvalues, 0, mValues.length); |
288 |
0 |
mKeys = nkeys; |
289 |
0 |
mValues = nvalues; |
290 |
|
} |
291 |
0 |
mKeys[pos] = key; |
292 |
0 |
mValues[pos] = value; |
293 |
0 |
mSize = pos + 1; |
294 |
|
} |
295 |
|
|
296 |
|
|
297 |
|
|
298 |
|
|
299 |
|
@param |
300 |
|
@return |
301 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
302 |
4 |
public static int idealIntArraySize(int need)... |
303 |
|
{ |
304 |
4 |
return idealByteArraySize(need * 4) / 4; |
305 |
|
} |
306 |
|
|
307 |
|
|
308 |
|
|
309 |
|
|
310 |
|
@param |
311 |
|
@return |
312 |
|
|
|
|
| 75% |
Uncovered Elements: 2 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
313 |
4 |
public static int idealByteArraySize(int need)... |
314 |
|
{ |
315 |
8 |
for (int i = 4; i < 32; i++) |
316 |
|
{ |
317 |
8 |
if (need <= (1 << i) - 12) |
318 |
|
{ |
319 |
4 |
return (1 << i) - 12; |
320 |
|
} |
321 |
|
} |
322 |
|
|
323 |
0 |
return need; |
324 |
|
} |
325 |
|
|
326 |
|
|
327 |
|
@inheritDoc |
328 |
|
|
329 |
|
|
330 |
|
|
331 |
|
|
|
|
| 0% |
Uncovered Elements: 20 (20) |
Complexity: 4 |
Complexity Density: 0.29 |
|
332 |
0 |
@Override... |
333 |
|
public String toString() |
334 |
|
{ |
335 |
0 |
if (size() <= 0) |
336 |
|
{ |
337 |
0 |
return "{}"; |
338 |
|
} |
339 |
0 |
StringBuilder buffer = new StringBuilder(mSize * 28); |
340 |
0 |
buffer.append('{'); |
341 |
0 |
for (int i = 0; i < mSize; i++) |
342 |
|
{ |
343 |
0 |
if (i > 0) |
344 |
|
{ |
345 |
0 |
buffer.append(", "); |
346 |
|
} |
347 |
0 |
int key = keyAt(i); |
348 |
0 |
buffer.append(key); |
349 |
0 |
buffer.append('='); |
350 |
0 |
int value = valueAt(i); |
351 |
0 |
buffer.append(value); |
352 |
|
} |
353 |
0 |
buffer.append('}'); |
354 |
0 |
return buffer.toString(); |
355 |
|
} |
356 |
|
|
357 |
|
|
358 |
|
|
359 |
|
|
360 |
|
|
361 |
|
|
362 |
|
@param |
363 |
|
|
364 |
|
@return |
365 |
|
|
366 |
|
|
367 |
|
|
|
|
| 60.7% |
Uncovered Elements: 11 (28) |
Complexity: 4 |
Complexity Density: 0.18 |
|
368 |
7 |
public int add(int key, int toAdd)... |
369 |
|
{ |
370 |
7 |
int newValue = toAdd; |
371 |
7 |
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
372 |
7 |
if (i >= 0) |
373 |
|
{ |
374 |
6 |
checkOverflow(mValues[i], toAdd); |
375 |
4 |
mValues[i] += toAdd; |
376 |
4 |
newValue = mValues[i]; |
377 |
|
} |
378 |
|
else |
379 |
|
{ |
380 |
1 |
i = ~i; |
381 |
1 |
if (mSize >= mKeys.length) |
382 |
|
{ |
383 |
0 |
int n = idealIntArraySize(mSize + 1); |
384 |
0 |
int[] nkeys = new int[n]; |
385 |
0 |
int[] nvalues = new int[n]; |
386 |
0 |
System.arraycopy(mKeys, 0, nkeys, 0, mKeys.length); |
387 |
0 |
System.arraycopy(mValues, 0, nvalues, 0, mValues.length); |
388 |
0 |
mKeys = nkeys; |
389 |
0 |
mValues = nvalues; |
390 |
|
} |
391 |
1 |
if (mSize - i != 0) |
392 |
|
{ |
393 |
0 |
System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i); |
394 |
0 |
System.arraycopy(mValues, i, mValues, i + 1, mSize - i); |
395 |
|
} |
396 |
1 |
mKeys[i] = key; |
397 |
1 |
mValues[i] = toAdd; |
398 |
1 |
mSize++; |
399 |
|
} |
400 |
5 |
return newValue; |
401 |
|
} |
402 |
|
|
403 |
|
|
404 |
|
|
405 |
|
|
406 |
|
|
407 |
|
@param |
408 |
|
@param |
409 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (14) |
Complexity: 7 |
Complexity Density: 1.17 |
|
410 |
21 |
static void checkOverflow(int value, int addend)... |
411 |
|
{ |
412 |
|
|
413 |
|
|
414 |
|
|
415 |
21 |
if (addend > 0) |
416 |
|
{ |
417 |
9 |
if (value > 0 && Integer.MAX_VALUE - value < addend) |
418 |
|
{ |
419 |
4 |
throw new ArithmeticException("Integer overflow adding " + addend |
420 |
|
+ " to " + value); |
421 |
|
} |
422 |
|
} |
423 |
12 |
else if (addend < 0) |
424 |
|
{ |
425 |
10 |
if (value < 0 && Integer.MIN_VALUE - value > addend) |
426 |
|
{ |
427 |
4 |
throw new ArithmeticException("Integer underflow adding " + addend |
428 |
|
+ " to " + value); |
429 |
|
} |
430 |
|
} |
431 |
|
} |
432 |
|
} |