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