propane 2.9.0-java → 2.9.1-java
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +2 -0
- data/Rakefile +2 -3
- data/lib/.gitignore +1 -0
- data/lib/export.txt +2 -0
- data/lib/propane/version.rb +1 -1
- data/pom.rb +2 -49
- data/pom.xml +1 -64
- data/propane.gemspec +13 -10
- data/src/main/java/processing/awt/PGraphicsJava2D.java +29 -20
- data/src/main/java/processing/core/PApplet.java +12854 -12803
- data/src/main/java/processing/core/PGraphics.java +17 -22
- data/src/main/java/processing/core/PShape.java +42 -20
- data/src/main/java/processing/core/PShapeSVG.java +20 -5
- data/src/main/java/processing/core/PSurfaceNone.java +1 -4
- data/src/main/java/processing/data/DoubleDict.java +848 -0
- data/src/main/java/processing/data/DoubleList.java +928 -0
- data/src/main/java/processing/data/FloatDict.java +18 -2
- data/src/main/java/processing/data/FloatList.java +26 -2
- data/src/main/java/processing/data/IntDict.java +12 -3
- data/src/main/java/processing/data/IntList.java +24 -1
- data/src/main/java/processing/data/JSONObject.java +2 -2
- data/src/main/java/processing/data/LongDict.java +800 -0
- data/src/main/java/processing/data/LongList.java +937 -0
- data/src/main/java/processing/data/Sort.java +1 -1
- data/src/main/java/processing/data/StringDict.java +12 -3
- data/src/main/java/processing/data/StringList.java +25 -2
- data/src/main/java/processing/data/Table.java +16 -5
- data/src/main/java/processing/data/XML.java +8 -1
- data/src/main/java/processing/opengl/PGL.java +23 -16
- data/src/main/java/processing/opengl/PGraphicsOpenGL.java +13 -10
- data/src/main/java/processing/opengl/PJOGL.java +35 -12
- data/src/main/java/processing/opengl/shaders/LightVert-brcm.glsl +154 -0
- data/src/main/java/processing/opengl/shaders/LightVert-vc4.glsl +2 -2
- data/src/main/java/processing/opengl/shaders/TexLightVert-brcm.glsl +160 -0
- data/src/main/java/processing/opengl/shaders/TexLightVert-vc4.glsl +2 -2
- metadata +22 -13
@@ -0,0 +1,928 @@
|
|
1
|
+
package processing.data;
|
2
|
+
|
3
|
+
import java.io.File;
|
4
|
+
import java.io.PrintWriter;
|
5
|
+
import java.util.Arrays;
|
6
|
+
import java.util.Iterator;
|
7
|
+
import java.util.Random;
|
8
|
+
|
9
|
+
import processing.core.PApplet;
|
10
|
+
|
11
|
+
|
12
|
+
/**
|
13
|
+
* Helper class for a list of floats. Lists are designed to have some of the
|
14
|
+
* features of ArrayLists, but to maintain the simplicity and efficiency of
|
15
|
+
* working with arrays.
|
16
|
+
*
|
17
|
+
* Functions like sort() and shuffle() always act on the list itself. To get
|
18
|
+
* a sorted copy, use list.copy().sort().
|
19
|
+
*
|
20
|
+
* @webref data:composite
|
21
|
+
* @see IntList
|
22
|
+
* @see StringList
|
23
|
+
*/
|
24
|
+
public class DoubleList implements Iterable<Double> {
|
25
|
+
int count;
|
26
|
+
double[] data;
|
27
|
+
|
28
|
+
|
29
|
+
public DoubleList() {
|
30
|
+
data = new double[10];
|
31
|
+
}
|
32
|
+
|
33
|
+
|
34
|
+
/**
|
35
|
+
* @nowebref
|
36
|
+
*/
|
37
|
+
public DoubleList(int length) {
|
38
|
+
data = new double[length];
|
39
|
+
}
|
40
|
+
|
41
|
+
|
42
|
+
/**
|
43
|
+
* @nowebref
|
44
|
+
*/
|
45
|
+
public DoubleList(double[] list) {
|
46
|
+
count = list.length;
|
47
|
+
data = new double[count];
|
48
|
+
System.arraycopy(list, 0, data, 0, count);
|
49
|
+
}
|
50
|
+
|
51
|
+
|
52
|
+
/**
|
53
|
+
* Construct an FloatList from an iterable pile of objects.
|
54
|
+
* For instance, a double array, an array of strings, who knows).
|
55
|
+
* Un-parseable or null values will be set to NaN.
|
56
|
+
* @nowebref
|
57
|
+
*/
|
58
|
+
public DoubleList(Iterable<Object> iter) {
|
59
|
+
this(10);
|
60
|
+
for (Object o : iter) {
|
61
|
+
if (o == null) {
|
62
|
+
append(Double.NaN);
|
63
|
+
} else if (o instanceof Number) {
|
64
|
+
append(((Number) o).doubleValue());
|
65
|
+
} else {
|
66
|
+
append(PApplet.parseFloat(o.toString().trim()));
|
67
|
+
}
|
68
|
+
}
|
69
|
+
crop();
|
70
|
+
}
|
71
|
+
|
72
|
+
|
73
|
+
/**
|
74
|
+
* Construct an FloatList from a random pile of objects.
|
75
|
+
* Un-parseable or null values will be set to NaN.
|
76
|
+
*/
|
77
|
+
public DoubleList(Object... items) {
|
78
|
+
// nuts, no good way to pass missingValue to this fn (varargs must be last)
|
79
|
+
final double missingValue = Double.NaN;
|
80
|
+
|
81
|
+
count = items.length;
|
82
|
+
data = new double[count];
|
83
|
+
int index = 0;
|
84
|
+
for (Object o : items) {
|
85
|
+
double value = missingValue;
|
86
|
+
if (o != null) {
|
87
|
+
if (o instanceof Number) {
|
88
|
+
value = ((Number) o).doubleValue();
|
89
|
+
} else {
|
90
|
+
try {
|
91
|
+
value = Double.parseDouble(o.toString().trim());
|
92
|
+
} catch (NumberFormatException nfe) {
|
93
|
+
value = missingValue;
|
94
|
+
}
|
95
|
+
}
|
96
|
+
}
|
97
|
+
data[index++] = value;
|
98
|
+
}
|
99
|
+
}
|
100
|
+
|
101
|
+
|
102
|
+
/**
|
103
|
+
* Improve efficiency by removing allocated but unused entries from the
|
104
|
+
* internal array used to store the data. Set to private, though it could
|
105
|
+
* be useful to have this public if lists are frequently making drastic
|
106
|
+
* size changes (from very large to very small).
|
107
|
+
*/
|
108
|
+
private void crop() {
|
109
|
+
if (count != data.length) {
|
110
|
+
data = PApplet.subset(data, 0, count);
|
111
|
+
}
|
112
|
+
}
|
113
|
+
|
114
|
+
|
115
|
+
/**
|
116
|
+
* Get the length of the list.
|
117
|
+
*
|
118
|
+
* @webref doublelist:method
|
119
|
+
* @brief Get the length of the list
|
120
|
+
*/
|
121
|
+
public int size() {
|
122
|
+
return count;
|
123
|
+
}
|
124
|
+
|
125
|
+
|
126
|
+
public void resize(int length) {
|
127
|
+
if (length > data.length) {
|
128
|
+
double[] temp = new double[length];
|
129
|
+
System.arraycopy(data, 0, temp, 0, count);
|
130
|
+
data = temp;
|
131
|
+
|
132
|
+
} else if (length > count) {
|
133
|
+
Arrays.fill(data, count, length, 0);
|
134
|
+
}
|
135
|
+
count = length;
|
136
|
+
}
|
137
|
+
|
138
|
+
|
139
|
+
/**
|
140
|
+
* Remove all entries from the list.
|
141
|
+
*
|
142
|
+
* @webref doublelist:method
|
143
|
+
* @brief Remove all entries from the list
|
144
|
+
*/
|
145
|
+
public void clear() {
|
146
|
+
count = 0;
|
147
|
+
}
|
148
|
+
|
149
|
+
|
150
|
+
/**
|
151
|
+
* Get an entry at a particular index.
|
152
|
+
*
|
153
|
+
* @webref doublelist:method
|
154
|
+
* @brief Get an entry at a particular index
|
155
|
+
*/
|
156
|
+
public double get(int index) {
|
157
|
+
if (index >= count) {
|
158
|
+
throw new ArrayIndexOutOfBoundsException(index);
|
159
|
+
}
|
160
|
+
return data[index];
|
161
|
+
}
|
162
|
+
|
163
|
+
|
164
|
+
/**
|
165
|
+
* Set the entry at a particular index. If the index is past the length of
|
166
|
+
* the list, it'll expand the list to accommodate, and fill the intermediate
|
167
|
+
* entries with 0s.
|
168
|
+
*
|
169
|
+
* @webref doublelist:method
|
170
|
+
* @brief Set the entry at a particular index
|
171
|
+
*/
|
172
|
+
public void set(int index, double what) {
|
173
|
+
if (index >= count) {
|
174
|
+
data = PApplet.expand(data, index+1);
|
175
|
+
for (int i = count; i < index; i++) {
|
176
|
+
data[i] = 0;
|
177
|
+
}
|
178
|
+
count = index+1;
|
179
|
+
}
|
180
|
+
data[index] = what;
|
181
|
+
}
|
182
|
+
|
183
|
+
|
184
|
+
/** Just an alias for append(), but matches pop() */
|
185
|
+
public void push(double value) {
|
186
|
+
append(value);
|
187
|
+
}
|
188
|
+
|
189
|
+
|
190
|
+
public double pop() {
|
191
|
+
if (count == 0) {
|
192
|
+
throw new RuntimeException("Can't call pop() on an empty list");
|
193
|
+
}
|
194
|
+
double value = get(count-1);
|
195
|
+
count--;
|
196
|
+
return value;
|
197
|
+
}
|
198
|
+
|
199
|
+
|
200
|
+
/**
|
201
|
+
* Remove an element from the specified index.
|
202
|
+
*
|
203
|
+
* @webref doublelist:method
|
204
|
+
* @brief Remove an element from the specified index
|
205
|
+
*/
|
206
|
+
public double remove(int index) {
|
207
|
+
if (index < 0 || index >= count) {
|
208
|
+
throw new ArrayIndexOutOfBoundsException(index);
|
209
|
+
}
|
210
|
+
double entry = data[index];
|
211
|
+
// int[] outgoing = new int[count - 1];
|
212
|
+
// System.arraycopy(data, 0, outgoing, 0, index);
|
213
|
+
// count--;
|
214
|
+
// System.arraycopy(data, index + 1, outgoing, 0, count - index);
|
215
|
+
// data = outgoing;
|
216
|
+
// For most cases, this actually appears to be faster
|
217
|
+
// than arraycopy() on an array copying into itself.
|
218
|
+
for (int i = index; i < count-1; i++) {
|
219
|
+
data[i] = data[i+1];
|
220
|
+
}
|
221
|
+
count--;
|
222
|
+
return entry;
|
223
|
+
}
|
224
|
+
|
225
|
+
|
226
|
+
// Remove the first instance of a particular value,
|
227
|
+
// and return the index at which it was found.
|
228
|
+
public int removeValue(int value) {
|
229
|
+
int index = index(value);
|
230
|
+
if (index != -1) {
|
231
|
+
remove(index);
|
232
|
+
return index;
|
233
|
+
}
|
234
|
+
return -1;
|
235
|
+
}
|
236
|
+
|
237
|
+
|
238
|
+
// Remove all instances of a particular value,
|
239
|
+
// and return the number of values found and removed
|
240
|
+
public int removeValues(int value) {
|
241
|
+
int ii = 0;
|
242
|
+
if (Double.isNaN(value)) {
|
243
|
+
for (int i = 0; i < count; i++) {
|
244
|
+
if (!Double.isNaN(data[i])) {
|
245
|
+
data[ii++] = data[i];
|
246
|
+
}
|
247
|
+
}
|
248
|
+
} else {
|
249
|
+
for (int i = 0; i < count; i++) {
|
250
|
+
if (data[i] != value) {
|
251
|
+
data[ii++] = data[i];
|
252
|
+
}
|
253
|
+
}
|
254
|
+
}
|
255
|
+
int removed = count - ii;
|
256
|
+
count = ii;
|
257
|
+
return removed;
|
258
|
+
}
|
259
|
+
|
260
|
+
|
261
|
+
/** Replace the first instance of a particular value */
|
262
|
+
public boolean replaceValue(double value, double newValue) {
|
263
|
+
if (Double.isNaN(value)) {
|
264
|
+
for (int i = 0; i < count; i++) {
|
265
|
+
if (Double.isNaN(data[i])) {
|
266
|
+
data[i] = newValue;
|
267
|
+
return true;
|
268
|
+
}
|
269
|
+
}
|
270
|
+
} else {
|
271
|
+
int index = index(value);
|
272
|
+
if (index != -1) {
|
273
|
+
data[index] = newValue;
|
274
|
+
return true;
|
275
|
+
}
|
276
|
+
}
|
277
|
+
return false;
|
278
|
+
}
|
279
|
+
|
280
|
+
|
281
|
+
/** Replace all instances of a particular value */
|
282
|
+
public boolean replaceValues(double value, double newValue) {
|
283
|
+
boolean changed = false;
|
284
|
+
if (Double.isNaN(value)) {
|
285
|
+
for (int i = 0; i < count; i++) {
|
286
|
+
if (Double.isNaN(data[i])) {
|
287
|
+
data[i] = newValue;
|
288
|
+
changed = true;
|
289
|
+
}
|
290
|
+
}
|
291
|
+
} else {
|
292
|
+
for (int i = 0; i < count; i++) {
|
293
|
+
if (data[i] == value) {
|
294
|
+
data[i] = newValue;
|
295
|
+
changed = true;
|
296
|
+
}
|
297
|
+
}
|
298
|
+
}
|
299
|
+
return changed;
|
300
|
+
}
|
301
|
+
|
302
|
+
|
303
|
+
|
304
|
+
/**
|
305
|
+
* Add a new entry to the list.
|
306
|
+
*
|
307
|
+
* @webref doublelist:method
|
308
|
+
* @brief Add a new entry to the list
|
309
|
+
*/
|
310
|
+
public void append(double value) {
|
311
|
+
if (count == data.length) {
|
312
|
+
data = PApplet.expand(data);
|
313
|
+
}
|
314
|
+
data[count++] = value;
|
315
|
+
}
|
316
|
+
|
317
|
+
|
318
|
+
public void append(double[] values) {
|
319
|
+
for (double v : values) {
|
320
|
+
append(v);
|
321
|
+
}
|
322
|
+
}
|
323
|
+
|
324
|
+
|
325
|
+
public void append(DoubleList list) {
|
326
|
+
for (double v : list.values()) { // will concat the list...
|
327
|
+
append(v);
|
328
|
+
}
|
329
|
+
}
|
330
|
+
|
331
|
+
|
332
|
+
/** Add this value, but only if it's not already in the list. */
|
333
|
+
public void appendUnique(double value) {
|
334
|
+
if (!hasValue(value)) {
|
335
|
+
append(value);
|
336
|
+
}
|
337
|
+
}
|
338
|
+
|
339
|
+
|
340
|
+
// public void insert(int index, int value) {
|
341
|
+
// if (index+1 > count) {
|
342
|
+
// if (index+1 < data.length) {
|
343
|
+
// }
|
344
|
+
// }
|
345
|
+
// if (index >= data.length) {
|
346
|
+
// data = PApplet.expand(data, index+1);
|
347
|
+
// data[index] = value;
|
348
|
+
// count = index+1;
|
349
|
+
//
|
350
|
+
// } else if (count == data.length) {
|
351
|
+
// if (index >= count) {
|
352
|
+
// //int[] temp = new int[count << 1];
|
353
|
+
// System.arraycopy(data, 0, temp, 0, index);
|
354
|
+
// temp[index] = value;
|
355
|
+
// System.arraycopy(data, index, temp, index+1, count - index);
|
356
|
+
// data = temp;
|
357
|
+
//
|
358
|
+
// } else {
|
359
|
+
// // data[] has room to grow
|
360
|
+
// // for() loop believed to be faster than System.arraycopy over itself
|
361
|
+
// for (int i = count; i > index; --i) {
|
362
|
+
// data[i] = data[i-1];
|
363
|
+
// }
|
364
|
+
// data[index] = value;
|
365
|
+
// count++;
|
366
|
+
// }
|
367
|
+
// }
|
368
|
+
|
369
|
+
|
370
|
+
public void insert(int index, double value) {
|
371
|
+
insert(index, new double[] { value });
|
372
|
+
}
|
373
|
+
|
374
|
+
|
375
|
+
// same as splice
|
376
|
+
public void insert(int index, double[] values) {
|
377
|
+
if (index < 0) {
|
378
|
+
throw new IllegalArgumentException("insert() index cannot be negative: it was " + index);
|
379
|
+
}
|
380
|
+
if (index >= data.length) {
|
381
|
+
throw new IllegalArgumentException("insert() index " + index + " is past the end of this list");
|
382
|
+
}
|
383
|
+
|
384
|
+
double[] temp = new double[count + values.length];
|
385
|
+
|
386
|
+
// Copy the old values, but not more than already exist
|
387
|
+
System.arraycopy(data, 0, temp, 0, Math.min(count, index));
|
388
|
+
|
389
|
+
// Copy the new values into the proper place
|
390
|
+
System.arraycopy(values, 0, temp, index, values.length);
|
391
|
+
|
392
|
+
// if (index < count) {
|
393
|
+
// The index was inside count, so it's a true splice/insert
|
394
|
+
System.arraycopy(data, index, temp, index+values.length, count - index);
|
395
|
+
count = count + values.length;
|
396
|
+
// } else {
|
397
|
+
// // The index was past 'count', so the new count is weirder
|
398
|
+
// count = index + values.length;
|
399
|
+
// }
|
400
|
+
data = temp;
|
401
|
+
}
|
402
|
+
|
403
|
+
|
404
|
+
public void insert(int index, DoubleList list) {
|
405
|
+
insert(index, list.values());
|
406
|
+
}
|
407
|
+
|
408
|
+
|
409
|
+
// below are aborted attempts at more optimized versions of the code
|
410
|
+
// that are harder to read and debug...
|
411
|
+
|
412
|
+
// if (index + values.length >= count) {
|
413
|
+
// // We're past the current 'count', check to see if we're still allocated
|
414
|
+
// // index 9, data.length = 10, values.length = 1
|
415
|
+
// if (index + values.length < data.length) {
|
416
|
+
// // There's still room for these entries, even though it's past 'count'.
|
417
|
+
// // First clear out the entries leading up to it, however.
|
418
|
+
// for (int i = count; i < index; i++) {
|
419
|
+
// data[i] = 0;
|
420
|
+
// }
|
421
|
+
// data[index] =
|
422
|
+
// }
|
423
|
+
// if (index >= data.length) {
|
424
|
+
// int length = index + values.length;
|
425
|
+
// int[] temp = new int[length];
|
426
|
+
// System.arraycopy(data, 0, temp, 0, count);
|
427
|
+
// System.arraycopy(values, 0, temp, index, values.length);
|
428
|
+
// data = temp;
|
429
|
+
// count = data.length;
|
430
|
+
// } else {
|
431
|
+
//
|
432
|
+
// }
|
433
|
+
//
|
434
|
+
// } else if (count == data.length) {
|
435
|
+
// int[] temp = new int[count << 1];
|
436
|
+
// System.arraycopy(data, 0, temp, 0, index);
|
437
|
+
// temp[index] = value;
|
438
|
+
// System.arraycopy(data, index, temp, index+1, count - index);
|
439
|
+
// data = temp;
|
440
|
+
//
|
441
|
+
// } else {
|
442
|
+
// // data[] has room to grow
|
443
|
+
// // for() loop believed to be faster than System.arraycopy over itself
|
444
|
+
// for (int i = count; i > index; --i) {
|
445
|
+
// data[i] = data[i-1];
|
446
|
+
// }
|
447
|
+
// data[index] = value;
|
448
|
+
// count++;
|
449
|
+
// }
|
450
|
+
|
451
|
+
|
452
|
+
/** Return the first index of a particular value. */
|
453
|
+
public int index(double what) {
|
454
|
+
/*
|
455
|
+
if (indexCache != null) {
|
456
|
+
try {
|
457
|
+
return indexCache.get(what);
|
458
|
+
} catch (Exception e) { // not there
|
459
|
+
return -1;
|
460
|
+
}
|
461
|
+
}
|
462
|
+
*/
|
463
|
+
for (int i = 0; i < count; i++) {
|
464
|
+
if (data[i] == what) {
|
465
|
+
return i;
|
466
|
+
}
|
467
|
+
}
|
468
|
+
return -1;
|
469
|
+
}
|
470
|
+
|
471
|
+
|
472
|
+
/**
|
473
|
+
* @webref doublelist:method
|
474
|
+
* @brief Check if a number is a part of the list
|
475
|
+
*/
|
476
|
+
public boolean hasValue(double value) {
|
477
|
+
if (Double.isNaN(value)) {
|
478
|
+
for (int i = 0; i < count; i++) {
|
479
|
+
if (Double.isNaN(data[i])) {
|
480
|
+
return true;
|
481
|
+
}
|
482
|
+
}
|
483
|
+
} else {
|
484
|
+
for (int i = 0; i < count; i++) {
|
485
|
+
if (data[i] == value) {
|
486
|
+
return true;
|
487
|
+
}
|
488
|
+
}
|
489
|
+
}
|
490
|
+
return false;
|
491
|
+
}
|
492
|
+
|
493
|
+
|
494
|
+
private void boundsProblem(int index, String method) {
|
495
|
+
final String msg = String.format("The list size is %d. " +
|
496
|
+
"You cannot %s() to element %d.", count, method, index);
|
497
|
+
throw new ArrayIndexOutOfBoundsException(msg);
|
498
|
+
}
|
499
|
+
|
500
|
+
|
501
|
+
/**
|
502
|
+
* @webref doublelist:method
|
503
|
+
* @brief Add to a value
|
504
|
+
*/
|
505
|
+
public void add(int index, double amount) {
|
506
|
+
if (index < count) {
|
507
|
+
data[index] += amount;
|
508
|
+
} else {
|
509
|
+
boundsProblem(index, "add");
|
510
|
+
}
|
511
|
+
}
|
512
|
+
|
513
|
+
|
514
|
+
/**
|
515
|
+
* @webref doublelist:method
|
516
|
+
* @brief Subtract from a value
|
517
|
+
*/
|
518
|
+
public void sub(int index, double amount) {
|
519
|
+
if (index < count) {
|
520
|
+
data[index] -= amount;
|
521
|
+
} else {
|
522
|
+
boundsProblem(index, "sub");
|
523
|
+
}
|
524
|
+
}
|
525
|
+
|
526
|
+
|
527
|
+
/**
|
528
|
+
* @webref doublelist:method
|
529
|
+
* @brief Multiply a value
|
530
|
+
*/
|
531
|
+
public void mult(int index, double amount) {
|
532
|
+
if (index < count) {
|
533
|
+
data[index] *= amount;
|
534
|
+
} else {
|
535
|
+
boundsProblem(index, "mult");
|
536
|
+
}
|
537
|
+
}
|
538
|
+
|
539
|
+
|
540
|
+
/**
|
541
|
+
* @webref doublelist:method
|
542
|
+
* @brief Divide a value
|
543
|
+
*/
|
544
|
+
public void div(int index, double amount) {
|
545
|
+
if (index < count) {
|
546
|
+
data[index] /= amount;
|
547
|
+
} else {
|
548
|
+
boundsProblem(index, "div");
|
549
|
+
}
|
550
|
+
}
|
551
|
+
|
552
|
+
|
553
|
+
private void checkMinMax(String functionName) {
|
554
|
+
if (count == 0) {
|
555
|
+
String msg =
|
556
|
+
String.format("Cannot use %s() on an empty %s.",
|
557
|
+
functionName, getClass().getSimpleName());
|
558
|
+
throw new RuntimeException(msg);
|
559
|
+
}
|
560
|
+
}
|
561
|
+
|
562
|
+
|
563
|
+
/**
|
564
|
+
* @webref doublelist:method
|
565
|
+
* @brief Return the smallest value
|
566
|
+
*/
|
567
|
+
public double min() {
|
568
|
+
checkMinMax("min");
|
569
|
+
int index = minIndex();
|
570
|
+
return index == -1 ? Double.NaN : data[index];
|
571
|
+
}
|
572
|
+
|
573
|
+
|
574
|
+
public int minIndex() {
|
575
|
+
checkMinMax("minIndex");
|
576
|
+
double m = Double.NaN;
|
577
|
+
int mi = -1;
|
578
|
+
for (int i = 0; i < count; i++) {
|
579
|
+
// find one good value to start
|
580
|
+
if (data[i] == data[i]) {
|
581
|
+
m = data[i];
|
582
|
+
mi = i;
|
583
|
+
|
584
|
+
// calculate the rest
|
585
|
+
for (int j = i+1; j < count; j++) {
|
586
|
+
double d = data[j];
|
587
|
+
if (!Double.isNaN(d) && (d < m)) {
|
588
|
+
m = data[j];
|
589
|
+
mi = j;
|
590
|
+
}
|
591
|
+
}
|
592
|
+
break;
|
593
|
+
}
|
594
|
+
}
|
595
|
+
return mi;
|
596
|
+
}
|
597
|
+
|
598
|
+
|
599
|
+
/**
|
600
|
+
* @webref doublelist:method
|
601
|
+
* @brief Return the largest value
|
602
|
+
*/
|
603
|
+
public double max() {
|
604
|
+
checkMinMax("max");
|
605
|
+
int index = maxIndex();
|
606
|
+
return index == -1 ? Double.NaN : data[index];
|
607
|
+
}
|
608
|
+
|
609
|
+
|
610
|
+
public int maxIndex() {
|
611
|
+
checkMinMax("maxIndex");
|
612
|
+
double m = Double.NaN;
|
613
|
+
int mi = -1;
|
614
|
+
for (int i = 0; i < count; i++) {
|
615
|
+
// find one good value to start
|
616
|
+
if (data[i] == data[i]) {
|
617
|
+
m = data[i];
|
618
|
+
mi = i;
|
619
|
+
|
620
|
+
// calculate the rest
|
621
|
+
for (int j = i+1; j < count; j++) {
|
622
|
+
double d = data[j];
|
623
|
+
if (!Double.isNaN(d) && (d > m)) {
|
624
|
+
m = data[j];
|
625
|
+
mi = j;
|
626
|
+
}
|
627
|
+
}
|
628
|
+
break;
|
629
|
+
}
|
630
|
+
}
|
631
|
+
return mi;
|
632
|
+
}
|
633
|
+
|
634
|
+
|
635
|
+
public double sum() {
|
636
|
+
double sum = 0;
|
637
|
+
for (int i = 0; i < count; i++) {
|
638
|
+
sum += data[i];
|
639
|
+
}
|
640
|
+
return sum;
|
641
|
+
}
|
642
|
+
|
643
|
+
|
644
|
+
/**
|
645
|
+
* Sorts the array in place.
|
646
|
+
*
|
647
|
+
* @webref doublelist:method
|
648
|
+
* @brief Sorts an array, lowest to highest
|
649
|
+
*/
|
650
|
+
public void sort() {
|
651
|
+
Arrays.sort(data, 0, count);
|
652
|
+
}
|
653
|
+
|
654
|
+
|
655
|
+
/**
|
656
|
+
* Reverse sort, orders values from highest to lowest
|
657
|
+
*
|
658
|
+
* @webref doublelist:method
|
659
|
+
* @brief Reverse sort, orders values from highest to lowest
|
660
|
+
*/
|
661
|
+
public void sortReverse() {
|
662
|
+
new Sort() {
|
663
|
+
@Override
|
664
|
+
public int size() {
|
665
|
+
// if empty, don't even mess with the NaN check, it'll AIOOBE
|
666
|
+
if (count == 0) {
|
667
|
+
return 0;
|
668
|
+
}
|
669
|
+
// move NaN values to the end of the list and don't sort them
|
670
|
+
int right = count - 1;
|
671
|
+
while (data[right] != data[right]) {
|
672
|
+
right--;
|
673
|
+
if (right == -1) { // all values are NaN
|
674
|
+
return 0;
|
675
|
+
}
|
676
|
+
}
|
677
|
+
for (int i = right; i >= 0; --i) {
|
678
|
+
double v = data[i];
|
679
|
+
if (v != v) {
|
680
|
+
data[i] = data[right];
|
681
|
+
data[right] = v;
|
682
|
+
--right;
|
683
|
+
}
|
684
|
+
}
|
685
|
+
return right + 1;
|
686
|
+
}
|
687
|
+
|
688
|
+
@Override
|
689
|
+
public int compare(int a, int b) {
|
690
|
+
double diff = data[b] - data[a];
|
691
|
+
return diff == 0 ? 0 : (diff < 0 ? -1 : 1);
|
692
|
+
}
|
693
|
+
|
694
|
+
@Override
|
695
|
+
public void swap(int a, int b) {
|
696
|
+
double temp = data[a];
|
697
|
+
data[a] = data[b];
|
698
|
+
data[b] = temp;
|
699
|
+
}
|
700
|
+
}.run();
|
701
|
+
}
|
702
|
+
|
703
|
+
|
704
|
+
// use insert()
|
705
|
+
// public void splice(int index, int value) {
|
706
|
+
// }
|
707
|
+
|
708
|
+
|
709
|
+
// public void subset(int start) {
|
710
|
+
// subset(start, count - start);
|
711
|
+
// }
|
712
|
+
|
713
|
+
|
714
|
+
// public void subset(int start, int num) {
|
715
|
+
// for (int i = 0; i < num; i++) {
|
716
|
+
// data[i] = data[i+start];
|
717
|
+
// }
|
718
|
+
// count = num;
|
719
|
+
// }
|
720
|
+
|
721
|
+
|
722
|
+
/**
|
723
|
+
* @webref doublelist:method
|
724
|
+
* @brief Reverse the order of the list elements
|
725
|
+
*/
|
726
|
+
public void reverse() {
|
727
|
+
int ii = count - 1;
|
728
|
+
for (int i = 0; i < count/2; i++) {
|
729
|
+
double t = data[i];
|
730
|
+
data[i] = data[ii];
|
731
|
+
data[ii] = t;
|
732
|
+
--ii;
|
733
|
+
}
|
734
|
+
}
|
735
|
+
|
736
|
+
|
737
|
+
/**
|
738
|
+
* Randomize the order of the list elements. Note that this does not
|
739
|
+
* obey the randomSeed() function in PApplet.
|
740
|
+
*
|
741
|
+
* @webref doublelist:method
|
742
|
+
* @brief Randomize the order of the list elements
|
743
|
+
*/
|
744
|
+
public void shuffle() {
|
745
|
+
Random r = new Random();
|
746
|
+
int num = count;
|
747
|
+
while (num > 1) {
|
748
|
+
int value = r.nextInt(num);
|
749
|
+
num--;
|
750
|
+
double temp = data[num];
|
751
|
+
data[num] = data[value];
|
752
|
+
data[value] = temp;
|
753
|
+
}
|
754
|
+
}
|
755
|
+
|
756
|
+
|
757
|
+
/**
|
758
|
+
* Randomize the list order using the random() function from the specified
|
759
|
+
* sketch, allowing shuffle() to use its current randomSeed() setting.
|
760
|
+
*/
|
761
|
+
public void shuffle(PApplet sketch) {
|
762
|
+
int num = count;
|
763
|
+
while (num > 1) {
|
764
|
+
int value = (int) sketch.random(num);
|
765
|
+
num--;
|
766
|
+
double temp = data[num];
|
767
|
+
data[num] = data[value];
|
768
|
+
data[value] = temp;
|
769
|
+
}
|
770
|
+
}
|
771
|
+
|
772
|
+
|
773
|
+
public DoubleList copy() {
|
774
|
+
DoubleList outgoing = new DoubleList(data);
|
775
|
+
outgoing.count = count;
|
776
|
+
return outgoing;
|
777
|
+
}
|
778
|
+
|
779
|
+
|
780
|
+
/**
|
781
|
+
* Returns the actual array being used to store the data. For advanced users,
|
782
|
+
* this is the fastest way to access a large list. Suitable for iterating
|
783
|
+
* with a for() loop, but modifying the list will have terrible consequences.
|
784
|
+
*/
|
785
|
+
public double[] values() {
|
786
|
+
crop();
|
787
|
+
return data;
|
788
|
+
}
|
789
|
+
|
790
|
+
|
791
|
+
/** Implemented this way so that we can use a FloatList in a for loop. */
|
792
|
+
@Override
|
793
|
+
public Iterator<Double> iterator() {
|
794
|
+
// }
|
795
|
+
//
|
796
|
+
//
|
797
|
+
// public Iterator<Float> valueIterator() {
|
798
|
+
return new Iterator<Double>() {
|
799
|
+
int index = -1;
|
800
|
+
|
801
|
+
public void remove() {
|
802
|
+
DoubleList.this.remove(index);
|
803
|
+
index--;
|
804
|
+
}
|
805
|
+
|
806
|
+
public Double next() {
|
807
|
+
return data[++index];
|
808
|
+
}
|
809
|
+
|
810
|
+
public boolean hasNext() {
|
811
|
+
return index+1 < count;
|
812
|
+
}
|
813
|
+
};
|
814
|
+
}
|
815
|
+
|
816
|
+
|
817
|
+
/**
|
818
|
+
* Create a new array with a copy of all the values.
|
819
|
+
* @return an array sized by the length of the list with each of the values.
|
820
|
+
* @webref doublelist:method
|
821
|
+
* @brief Create a new array with a copy of all the values
|
822
|
+
*/
|
823
|
+
public double[] array() {
|
824
|
+
return array(null);
|
825
|
+
}
|
826
|
+
|
827
|
+
|
828
|
+
/**
|
829
|
+
* Copy values into the specified array. If the specified array is null or
|
830
|
+
* not the same size, a new array will be allocated.
|
831
|
+
* @param array
|
832
|
+
*/
|
833
|
+
public double[] array(double[] array) {
|
834
|
+
if (array == null || array.length != count) {
|
835
|
+
array = new double[count];
|
836
|
+
}
|
837
|
+
System.arraycopy(data, 0, array, 0, count);
|
838
|
+
return array;
|
839
|
+
}
|
840
|
+
|
841
|
+
|
842
|
+
/**
|
843
|
+
* Returns a normalized version of this array. Called getPercent() for
|
844
|
+
* consistency with the Dict classes. It's a getter method because it needs
|
845
|
+
* to returns a new list (because IntList/Dict can't do percentages or
|
846
|
+
* normalization in place on int values).
|
847
|
+
*/
|
848
|
+
public DoubleList getPercent() {
|
849
|
+
double sum = 0;
|
850
|
+
for (double value : array()) {
|
851
|
+
sum += value;
|
852
|
+
}
|
853
|
+
DoubleList outgoing = new DoubleList(count);
|
854
|
+
for (int i = 0; i < count; i++) {
|
855
|
+
double percent = data[i] / sum;
|
856
|
+
outgoing.set(i, percent);
|
857
|
+
}
|
858
|
+
return outgoing;
|
859
|
+
}
|
860
|
+
|
861
|
+
|
862
|
+
public DoubleList getSubset(int start) {
|
863
|
+
return getSubset(start, count - start);
|
864
|
+
}
|
865
|
+
|
866
|
+
|
867
|
+
public DoubleList getSubset(int start, int num) {
|
868
|
+
double[] subset = new double[num];
|
869
|
+
System.arraycopy(data, start, subset, 0, num);
|
870
|
+
return new DoubleList(subset);
|
871
|
+
}
|
872
|
+
|
873
|
+
|
874
|
+
public String join(String separator) {
|
875
|
+
if (count == 0) {
|
876
|
+
return "";
|
877
|
+
}
|
878
|
+
StringBuilder sb = new StringBuilder();
|
879
|
+
sb.append(data[0]);
|
880
|
+
for (int i = 1; i < count; i++) {
|
881
|
+
sb.append(separator);
|
882
|
+
sb.append(data[i]);
|
883
|
+
}
|
884
|
+
return sb.toString();
|
885
|
+
}
|
886
|
+
|
887
|
+
|
888
|
+
public void print() {
|
889
|
+
for (int i = 0; i < count; i++) {
|
890
|
+
System.out.format("[%d] %f%n", i, data[i]);
|
891
|
+
}
|
892
|
+
}
|
893
|
+
|
894
|
+
|
895
|
+
/**
|
896
|
+
* Save tab-delimited entries to a file (TSV format, UTF-8 encoding)
|
897
|
+
*/
|
898
|
+
public void save(File file) {
|
899
|
+
PrintWriter writer = PApplet.createWriter(file);
|
900
|
+
write(writer);
|
901
|
+
writer.close();
|
902
|
+
}
|
903
|
+
|
904
|
+
|
905
|
+
/**
|
906
|
+
* Write entries to a PrintWriter, one per line
|
907
|
+
*/
|
908
|
+
public void write(PrintWriter writer) {
|
909
|
+
for (int i = 0; i < count; i++) {
|
910
|
+
writer.println(data[i]);
|
911
|
+
}
|
912
|
+
writer.flush();
|
913
|
+
}
|
914
|
+
|
915
|
+
|
916
|
+
/**
|
917
|
+
* Return this dictionary as a String in JSON format.
|
918
|
+
*/
|
919
|
+
public String toJSON() {
|
920
|
+
return "[ " + join(", ") + " ]";
|
921
|
+
}
|
922
|
+
|
923
|
+
|
924
|
+
@Override
|
925
|
+
public String toString() {
|
926
|
+
return getClass().getSimpleName() + " size=" + size() + " " + toJSON();
|
927
|
+
}
|
928
|
+
}
|