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 |
|
|
|
|
| 0% |
Uncovered Elements: 184 (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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
81 |
0 |
public SparseDoubleArray()... |
82 |
|
{ |
83 |
0 |
this(10); |
84 |
|
} |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 2 |
Complexity Density: 0.29 |
|
93 |
0 |
public SparseDoubleArray(int initialCapacity)... |
94 |
|
{ |
95 |
0 |
if (initialCapacity == 0) |
96 |
|
{ |
97 |
0 |
mKeys = ContainerHelpers.EMPTY_INTS; |
98 |
0 |
mValues = ContainerHelpers.EMPTY_DOUBLES; |
99 |
|
} |
100 |
|
else |
101 |
|
{ |
102 |
0 |
initialCapacity = idealDoubleArraySize(initialCapacity); |
103 |
0 |
mKeys = new int[initialCapacity]; |
104 |
0 |
mValues = new double[initialCapacity]; |
105 |
|
} |
106 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
146 |
0 |
public double get(int key)... |
147 |
|
{ |
148 |
0 |
return get(key, 0d); |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 2 |
Complexity Density: 0.5 |
|
155 |
0 |
public double get(int key, double valueIfKeyNotFound)... |
156 |
|
{ |
157 |
0 |
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
158 |
0 |
if (i < 0) |
159 |
|
{ |
160 |
0 |
return valueIfKeyNotFound; |
161 |
|
} |
162 |
|
else |
163 |
|
{ |
164 |
0 |
return mValues[i]; |
165 |
|
} |
166 |
|
} |
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
|
|
| 0% |
Uncovered Elements: 5 (5) |
Complexity: 2 |
Complexity Density: 0.67 |
|
171 |
0 |
public void delete(int key)... |
172 |
|
{ |
173 |
0 |
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
174 |
0 |
if (i >= 0) |
175 |
|
{ |
176 |
0 |
removeAt(i); |
177 |
|
} |
178 |
|
} |
179 |
|
|
180 |
|
|
181 |
|
|
182 |
|
|
|
|
| 0% |
Uncovered Elements: 3 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
183 |
0 |
public void removeAt(int index)... |
184 |
|
{ |
185 |
0 |
System.arraycopy(mKeys, index + 1, mKeys, index, mSize - (index + 1)); |
186 |
0 |
System.arraycopy(mValues, index + 1, mValues, index, |
187 |
|
mSize - (index + 1)); |
188 |
0 |
mSize--; |
189 |
|
} |
190 |
|
|
191 |
|
|
192 |
|
|
193 |
|
|
194 |
|
|
|
|
| 0% |
Uncovered Elements: 24 (24) |
Complexity: 4 |
Complexity Density: 0.22 |
|
195 |
0 |
public void put(int key, double value)... |
196 |
|
{ |
197 |
0 |
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
198 |
0 |
if (i >= 0) |
199 |
|
{ |
200 |
0 |
mValues[i] = value; |
201 |
|
} |
202 |
|
else |
203 |
|
{ |
204 |
0 |
i = ~i; |
205 |
0 |
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 |
0 |
if (mSize - i != 0) |
217 |
|
{ |
218 |
|
|
219 |
0 |
System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i); |
220 |
0 |
System.arraycopy(mValues, i, mValues, i + 1, mSize - i); |
221 |
|
} |
222 |
0 |
mKeys[i] = key; |
223 |
0 |
mValues[i] = value; |
224 |
0 |
mSize++; |
225 |
|
} |
226 |
|
} |
227 |
|
|
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
232 |
0 |
public int size()... |
233 |
|
{ |
234 |
0 |
return mSize; |
235 |
|
} |
236 |
|
|
237 |
|
|
238 |
|
|
239 |
|
|
240 |
|
|
241 |
|
|
242 |
|
|
243 |
|
|
244 |
|
|
245 |
|
|
246 |
|
|
247 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
248 |
0 |
public int keyAt(int index)... |
249 |
|
{ |
250 |
0 |
return mKeys[index]; |
251 |
|
} |
252 |
|
|
253 |
|
|
254 |
|
|
255 |
|
|
256 |
|
|
257 |
|
|
258 |
|
|
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
266 |
0 |
public double valueAt(int index)... |
267 |
|
{ |
268 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
341 |
0 |
public static int idealDoubleArraySize(int need)... |
342 |
|
{ |
343 |
0 |
return idealByteArraySize(need * 8) / 8; |
344 |
|
} |
345 |
|
|
346 |
|
|
347 |
|
|
348 |
|
|
349 |
|
@param |
350 |
|
@return |
351 |
|
|
|
|
| 0% |
Uncovered Elements: 8 (8) |
Complexity: 3 |
Complexity Density: 0.75 |
|
352 |
0 |
public static int idealByteArraySize(int need)... |
353 |
|
{ |
354 |
0 |
for (int i = 4; i < 32; i++) |
355 |
|
{ |
356 |
0 |
if (need <= (1 << i) - 12) |
357 |
|
{ |
358 |
0 |
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 |
|
|
|
|
| 0% |
Uncovered Elements: 27 (27) |
Complexity: 4 |
Complexity Density: 0.19 |
|
405 |
0 |
public double add(int key, double toAdd)... |
406 |
|
{ |
407 |
0 |
double newValue = toAdd; |
408 |
0 |
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
409 |
0 |
if (i >= 0) |
410 |
|
{ |
411 |
0 |
mValues[i] += toAdd; |
412 |
0 |
newValue = mValues[i]; |
413 |
|
} |
414 |
|
else |
415 |
|
{ |
416 |
0 |
i = ~i; |
417 |
0 |
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 |
0 |
if (mSize - i != 0) |
428 |
|
{ |
429 |
0 |
System.arraycopy(mKeys, i, mKeys, i + 1, mSize - i); |
430 |
0 |
System.arraycopy(mValues, i, mValues, i + 1, mSize - i); |
431 |
|
} |
432 |
0 |
mKeys[i] = key; |
433 |
0 |
mValues[i] = toAdd; |
434 |
0 |
mSize++; |
435 |
|
} |
436 |
0 |
return newValue; |
437 |
|
} |
438 |
|
|
439 |
|
|
440 |
|
|
441 |
|
|
442 |
|
|
443 |
|
|
444 |
|
@param |
445 |
|
|
446 |
|
@return |
447 |
|
|
|
|
| 0% |
Uncovered Elements: 12 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
448 |
0 |
public double divide(int key, double divisor)... |
449 |
|
{ |
450 |
0 |
double newValue = 0d; |
451 |
0 |
if (divisor == 0d) |
452 |
|
{ |
453 |
0 |
return newValue; |
454 |
|
} |
455 |
0 |
int i = ContainerHelpers.binarySearch(mKeys, mSize, key); |
456 |
0 |
if (i >= 0) |
457 |
|
{ |
458 |
0 |
mValues[i] /= divisor; |
459 |
0 |
newValue = mValues[i]; |
460 |
|
} |
461 |
0 |
return newValue; |
462 |
|
} |
463 |
|
} |