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