propane 3.9.0-java → 3.10.0-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (150) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +1 -1
  3. data/CHANGELOG.md +2 -2
  4. data/README.md +3 -3
  5. data/Rakefile +6 -6
  6. data/lib/java/japplemenubar/JAppleMenuBar.java +88 -0
  7. data/lib/java/japplemenubar/libjAppleMenuBar.jnilib +0 -0
  8. data/lib/java/monkstone/ColorUtil.java +127 -0
  9. data/lib/java/monkstone/MathToolModule.java +287 -0
  10. data/lib/java/monkstone/PropaneLibrary.java +46 -0
  11. data/lib/java/monkstone/core/LibraryProxy.java +136 -0
  12. data/lib/java/monkstone/fastmath/DegLutTables.java +111 -0
  13. data/lib/java/monkstone/fastmath/Deglut.java +71 -0
  14. data/lib/java/monkstone/fastmath/package-info.java +6 -0
  15. data/lib/java/monkstone/filechooser/Chooser.java +39 -0
  16. data/{src/main → lib}/java/monkstone/noise/FastTerrain.java +0 -0
  17. data/{src/main → lib}/java/monkstone/noise/Noise.java +0 -0
  18. data/{src/main → lib}/java/monkstone/noise/NoiseGenerator.java +0 -0
  19. data/{src/main → lib}/java/monkstone/noise/NoiseMode.java +0 -0
  20. data/lib/java/monkstone/noise/OpenSimplex2F.java +881 -0
  21. data/lib/java/monkstone/noise/OpenSimplex2S.java +1106 -0
  22. data/{src/main → lib}/java/monkstone/noise/SmoothTerrain.java +0 -0
  23. data/lib/java/monkstone/slider/CustomHorizontalSlider.java +164 -0
  24. data/lib/java/monkstone/slider/CustomVerticalSlider.java +178 -0
  25. data/lib/java/monkstone/slider/SimpleHorizontalSlider.java +145 -0
  26. data/lib/java/monkstone/slider/SimpleSlider.java +166 -0
  27. data/lib/java/monkstone/slider/SimpleVerticalSlider.java +157 -0
  28. data/lib/java/monkstone/slider/Slider.java +61 -0
  29. data/lib/java/monkstone/slider/SliderBar.java +245 -0
  30. data/lib/java/monkstone/slider/SliderGroup.java +56 -0
  31. data/lib/java/monkstone/slider/WheelHandler.java +35 -0
  32. data/lib/java/monkstone/vecmath/GfxRender.java +86 -0
  33. data/lib/java/monkstone/vecmath/JRender.java +56 -0
  34. data/lib/java/monkstone/vecmath/ShapeRender.java +87 -0
  35. data/lib/java/monkstone/vecmath/package-info.java +20 -0
  36. data/lib/java/monkstone/vecmath/vec2/Vec2.java +802 -0
  37. data/lib/java/monkstone/vecmath/vec2/package-info.java +6 -0
  38. data/lib/java/monkstone/vecmath/vec3/Vec3.java +727 -0
  39. data/lib/java/monkstone/vecmath/vec3/package-info.java +6 -0
  40. data/lib/java/monkstone/videoevent/CaptureEvent.java +27 -0
  41. data/lib/java/monkstone/videoevent/MovieEvent.java +32 -0
  42. data/lib/java/monkstone/videoevent/package-info.java +20 -0
  43. data/lib/java/processing/awt/PGraphicsJava2D.java +3040 -0
  44. data/lib/java/processing/awt/PImageAWT.java +377 -0
  45. data/lib/java/processing/awt/PShapeJava2D.java +387 -0
  46. data/lib/java/processing/awt/PSurfaceAWT.java +1581 -0
  47. data/lib/java/processing/awt/ShimAWT.java +581 -0
  48. data/lib/java/processing/core/PApplet.java +15156 -0
  49. data/lib/java/processing/core/PConstants.java +523 -0
  50. data/lib/java/processing/core/PFont.java +1126 -0
  51. data/lib/java/processing/core/PGraphics.java +8600 -0
  52. data/lib/java/processing/core/PImage.java +3377 -0
  53. data/lib/java/processing/core/PMatrix.java +208 -0
  54. data/lib/java/processing/core/PMatrix2D.java +562 -0
  55. data/lib/java/processing/core/PMatrix3D.java +890 -0
  56. data/lib/java/processing/core/PShape.java +3561 -0
  57. data/lib/java/processing/core/PShapeOBJ.java +483 -0
  58. data/lib/java/processing/core/PShapeSVG.java +2016 -0
  59. data/lib/java/processing/core/PStyle.java +63 -0
  60. data/lib/java/processing/core/PSurface.java +198 -0
  61. data/lib/java/processing/core/PSurfaceNone.java +431 -0
  62. data/lib/java/processing/core/PVector.java +1066 -0
  63. data/lib/java/processing/core/ThinkDifferent.java +115 -0
  64. data/lib/java/processing/data/DoubleDict.java +850 -0
  65. data/lib/java/processing/data/DoubleList.java +928 -0
  66. data/lib/java/processing/data/FloatDict.java +847 -0
  67. data/lib/java/processing/data/FloatList.java +936 -0
  68. data/lib/java/processing/data/IntDict.java +807 -0
  69. data/lib/java/processing/data/IntList.java +936 -0
  70. data/lib/java/processing/data/JSONArray.java +1260 -0
  71. data/lib/java/processing/data/JSONObject.java +2282 -0
  72. data/lib/java/processing/data/JSONTokener.java +435 -0
  73. data/lib/java/processing/data/LongDict.java +802 -0
  74. data/lib/java/processing/data/LongList.java +937 -0
  75. data/lib/java/processing/data/Sort.java +46 -0
  76. data/lib/java/processing/data/StringDict.java +613 -0
  77. data/lib/java/processing/data/StringList.java +800 -0
  78. data/lib/java/processing/data/Table.java +4936 -0
  79. data/lib/java/processing/data/TableRow.java +198 -0
  80. data/lib/java/processing/data/XML.java +1156 -0
  81. data/lib/java/processing/dxf/RawDXF.java +404 -0
  82. data/lib/java/processing/event/Event.java +125 -0
  83. data/lib/java/processing/event/KeyEvent.java +70 -0
  84. data/lib/java/processing/event/MouseEvent.java +114 -0
  85. data/lib/java/processing/event/TouchEvent.java +57 -0
  86. data/lib/java/processing/javafx/PGraphicsFX2D.java +32 -0
  87. data/lib/java/processing/javafx/PSurfaceFX.java +173 -0
  88. data/lib/java/processing/net/Client.java +744 -0
  89. data/lib/java/processing/net/Server.java +388 -0
  90. data/lib/java/processing/opengl/FontTexture.java +378 -0
  91. data/lib/java/processing/opengl/FrameBuffer.java +513 -0
  92. data/lib/java/processing/opengl/LinePath.java +627 -0
  93. data/lib/java/processing/opengl/LineStroker.java +681 -0
  94. data/lib/java/processing/opengl/PGL.java +3483 -0
  95. data/lib/java/processing/opengl/PGraphics2D.java +615 -0
  96. data/lib/java/processing/opengl/PGraphics3D.java +281 -0
  97. data/lib/java/processing/opengl/PGraphicsOpenGL.java +13753 -0
  98. data/lib/java/processing/opengl/PJOGL.java +2008 -0
  99. data/lib/java/processing/opengl/PShader.java +1484 -0
  100. data/lib/java/processing/opengl/PShapeOpenGL.java +5269 -0
  101. data/lib/java/processing/opengl/PSurfaceJOGL.java +1385 -0
  102. data/lib/java/processing/opengl/Texture.java +1696 -0
  103. data/lib/java/processing/opengl/VertexBuffer.java +88 -0
  104. data/lib/java/processing/opengl/cursors/arrow.png +0 -0
  105. data/lib/java/processing/opengl/cursors/cross.png +0 -0
  106. data/lib/java/processing/opengl/cursors/hand.png +0 -0
  107. data/lib/java/processing/opengl/cursors/license.txt +27 -0
  108. data/lib/java/processing/opengl/cursors/move.png +0 -0
  109. data/lib/java/processing/opengl/cursors/text.png +0 -0
  110. data/lib/java/processing/opengl/cursors/wait.png +0 -0
  111. data/lib/java/processing/opengl/shaders/ColorFrag.glsl +32 -0
  112. data/lib/java/processing/opengl/shaders/ColorVert.glsl +34 -0
  113. data/lib/java/processing/opengl/shaders/LightFrag.glsl +33 -0
  114. data/lib/java/processing/opengl/shaders/LightVert.glsl +151 -0
  115. data/lib/java/processing/opengl/shaders/LineFrag.glsl +32 -0
  116. data/lib/java/processing/opengl/shaders/LineVert.glsl +100 -0
  117. data/lib/java/processing/opengl/shaders/MaskFrag.glsl +40 -0
  118. data/lib/java/processing/opengl/shaders/PointFrag.glsl +32 -0
  119. data/lib/java/processing/opengl/shaders/PointVert.glsl +56 -0
  120. data/lib/java/processing/opengl/shaders/TexFrag.glsl +37 -0
  121. data/lib/java/processing/opengl/shaders/TexLightFrag.glsl +37 -0
  122. data/lib/java/processing/opengl/shaders/TexLightVert.glsl +157 -0
  123. data/lib/java/processing/opengl/shaders/TexVert.glsl +38 -0
  124. data/lib/java/processing/pdf/PGraphicsPDF.java +581 -0
  125. data/lib/java/processing/svg/PGraphicsSVG.java +378 -0
  126. data/lib/propane/app.rb +8 -13
  127. data/lib/propane/version.rb +1 -1
  128. data/mvnw +3 -3
  129. data/mvnw.cmd +2 -2
  130. data/pom.rb +7 -2
  131. data/pom.xml +14 -2
  132. data/propane.gemspec +2 -2
  133. data/src/main/java/monkstone/FastNoiseModuleJava.java +127 -0
  134. data/src/main/java/monkstone/MathToolModule.java +30 -30
  135. data/src/main/java/monkstone/PropaneLibrary.java +2 -0
  136. data/src/main/java/monkstone/SmoothNoiseModuleJava.java +127 -0
  137. data/src/main/java/monkstone/fastmath/DegLutTables.java +15 -15
  138. data/src/main/java/monkstone/filechooser/Chooser.java +1 -1
  139. data/src/main/java/monkstone/noise/OpenSimplex2F.java +752 -820
  140. data/src/main/java/monkstone/noise/OpenSimplex2S.java +1138 -1106
  141. data/src/main/java/monkstone/slider/WheelHandler.java +1 -1
  142. data/src/main/java/monkstone/vecmath/JRender.java +6 -6
  143. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +20 -19
  144. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +12 -12
  145. data/src/main/java/processing/awt/PGraphicsJava2D.java +11 -3
  146. data/src/main/java/processing/core/PApplet.java +89 -89
  147. data/src/main/java/processing/core/PConstants.java +155 -163
  148. data/src/main/java/processing/opengl/PJOGL.java +6 -5
  149. data/vendors/Rakefile +1 -1
  150. metadata +136 -19
@@ -0,0 +1,847 @@
1
+ package processing.data;
2
+
3
+ import java.io.*;
4
+ import java.util.HashMap;
5
+ import java.util.Iterator;
6
+ import java.util.NoSuchElementException;
7
+
8
+ import processing.core.PApplet;
9
+
10
+
11
+ /**
12
+ * A simple table class to use a String as a lookup for an float value.
13
+ *
14
+ * @webref data:composite
15
+ * @see IntDict
16
+ * @see StringDict
17
+ */
18
+ public class FloatDict {
19
+
20
+ /** Number of elements in the table */
21
+ protected int count;
22
+
23
+ protected String[] keys;
24
+ protected float[] values;
25
+
26
+ /** Internal implementation for faster lookups */
27
+ private HashMap<String, Integer> indices = new HashMap<>();
28
+
29
+
30
+ public FloatDict() {
31
+ count = 0;
32
+ keys = new String[10];
33
+ values = new float[10];
34
+ }
35
+
36
+
37
+ /**
38
+ * Create a new lookup with a specific size. This is more efficient than not
39
+ * specifying a size. Use it when you know the rough size of the thing you're creating.
40
+ *
41
+ * @nowebref
42
+ */
43
+ public FloatDict(int length) {
44
+ count = 0;
45
+ keys = new String[length];
46
+ values = new float[length];
47
+ }
48
+
49
+
50
+ /**
51
+ * Read a set of entries from a Reader that has each key/value pair on
52
+ * a single line, separated by a tab.
53
+ *
54
+ * @nowebref
55
+ */
56
+ public FloatDict(BufferedReader reader) {
57
+ String[] lines = PApplet.loadStrings(reader);
58
+ keys = new String[lines.length];
59
+ values = new float[lines.length];
60
+
61
+ for (int i = 0; i < lines.length; i++) {
62
+ String[] pieces = PApplet.split(lines[i], '\t');
63
+ if (pieces.length == 2) {
64
+ keys[count] = pieces[0];
65
+ values[count] = PApplet.parseFloat(pieces[1]);
66
+ indices.put(pieces[0], count);
67
+ count++;
68
+ }
69
+ }
70
+ }
71
+
72
+
73
+ /**
74
+ * @nowebref
75
+ */
76
+ public FloatDict(String[] keys, float[] values) {
77
+ if (keys.length != values.length) {
78
+ throw new IllegalArgumentException("key and value arrays must be the same length");
79
+ }
80
+ this.keys = keys;
81
+ this.values = values;
82
+ count = keys.length;
83
+ for (int i = 0; i < count; i++) {
84
+ indices.put(keys[i], i);
85
+ }
86
+ }
87
+
88
+
89
+ /**
90
+ * Constructor to allow (more intuitive) inline initialization, e.g.:
91
+ * <pre>
92
+ * new FloatDict(new Object[][] {
93
+ * { "key1", 1 },
94
+ * { "key2", 2 }
95
+ * });
96
+ * </pre>
97
+ */
98
+ public FloatDict(Object[][] pairs) {
99
+ count = pairs.length;
100
+ this.keys = new String[count];
101
+ this.values = new float[count];
102
+ for (int i = 0; i < count; i++) {
103
+ keys[i] = (String) pairs[i][0];
104
+ values[i] = (Float) pairs[i][1];
105
+ indices.put(keys[i], i);
106
+ }
107
+ }
108
+
109
+
110
+ /**
111
+ * @webref floatdict:method
112
+ * @brief Returns the number of key/value pairs
113
+ */
114
+ public int size() {
115
+ return count;
116
+ }
117
+
118
+
119
+ /**
120
+ * Resize the internal data, this can only be used to shrink the list.
121
+ * Helpful for situations like sorting and then grabbing the top 50 entries.
122
+ */
123
+ public void resize(int length) {
124
+ if (length == count) return;
125
+
126
+ if (length > count) {
127
+ throw new IllegalArgumentException("resize() can only be used to shrink the dictionary");
128
+ }
129
+ if (length < 1) {
130
+ throw new IllegalArgumentException("resize(" + length + ") is too small, use 1 or higher");
131
+ }
132
+
133
+ String[] newKeys = new String[length];
134
+ float[] newValues = new float[length];
135
+ PApplet.arrayCopy(keys, newKeys, length);
136
+ PApplet.arrayCopy(values, newValues, length);
137
+ keys = newKeys;
138
+ values = newValues;
139
+ count = length;
140
+ resetIndices();
141
+ }
142
+
143
+
144
+ /**
145
+ * Remove all entries.
146
+ *
147
+ * @webref floatdict:method
148
+ * @brief Remove all entries
149
+ */
150
+ public void clear() {
151
+ count = 0;
152
+ indices = new HashMap<>();
153
+ }
154
+
155
+
156
+ private void resetIndices() {
157
+ indices = new HashMap<>(count);
158
+ for (int i = 0; i < count; i++) {
159
+ indices.put(keys[i], i);
160
+ }
161
+ }
162
+
163
+
164
+ // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
165
+
166
+
167
+ public class Entry {
168
+ public String key;
169
+ public float value;
170
+
171
+ Entry(String key, float value) {
172
+ this.key = key;
173
+ this.value = value;
174
+ }
175
+ }
176
+
177
+
178
+ public Iterable<Entry> entries() {
179
+ return new Iterable<Entry>() {
180
+
181
+ public Iterator<Entry> iterator() {
182
+ return entryIterator();
183
+ }
184
+ };
185
+ }
186
+
187
+
188
+ public Iterator<Entry> entryIterator() {
189
+ return new Iterator<Entry>() {
190
+ int index = -1;
191
+
192
+ public void remove() {
193
+ removeIndex(index);
194
+ index--;
195
+ }
196
+
197
+ public Entry next() {
198
+ ++index;
199
+ Entry e = new Entry(keys[index], values[index]);
200
+ return e;
201
+ }
202
+
203
+ public boolean hasNext() {
204
+ return index+1 < size();
205
+ }
206
+ };
207
+ }
208
+
209
+
210
+ // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
211
+
212
+
213
+ public String key(int index) {
214
+ return keys[index];
215
+ }
216
+
217
+
218
+ protected void crop() {
219
+ if (count != keys.length) {
220
+ keys = PApplet.subset(keys, 0, count);
221
+ values = PApplet.subset(values, 0, count);
222
+ }
223
+ }
224
+
225
+
226
+ public Iterable<String> keys() {
227
+ return new Iterable<String>() {
228
+
229
+ @Override
230
+ public Iterator<String> iterator() {
231
+ return keyIterator();
232
+ }
233
+ };
234
+ }
235
+
236
+
237
+ // Use this to iterate when you want to be able to remove elements along the way
238
+ public Iterator<String> keyIterator() {
239
+ return new Iterator<String>() {
240
+ int index = -1;
241
+
242
+ public void remove() {
243
+ removeIndex(index);
244
+ index--;
245
+ }
246
+
247
+ public String next() {
248
+ return key(++index);
249
+ }
250
+
251
+ public boolean hasNext() {
252
+ return index+1 < size();
253
+ }
254
+ };
255
+ }
256
+
257
+
258
+ /**
259
+ * Return a copy of the internal keys array. This array can be modified.
260
+ *
261
+ * @webref floatdict:method
262
+ * @brief Return a copy of the internal keys array
263
+ */
264
+ public String[] keyArray() {
265
+ crop();
266
+ return keyArray(null);
267
+ }
268
+
269
+
270
+ public String[] keyArray(String[] outgoing) {
271
+ if (outgoing == null || outgoing.length != count) {
272
+ outgoing = new String[count];
273
+ }
274
+ System.arraycopy(keys, 0, outgoing, 0, count);
275
+ return outgoing;
276
+ }
277
+
278
+
279
+ public float value(int index) {
280
+ return values[index];
281
+ }
282
+
283
+
284
+ /**
285
+ * @webref floatdict:method
286
+ * @brief Return the internal array being used to store the values
287
+ */
288
+ public Iterable<Float> values() {
289
+ return new Iterable<Float>() {
290
+
291
+ @Override
292
+ public Iterator<Float> iterator() {
293
+ return valueIterator();
294
+ }
295
+ };
296
+ }
297
+
298
+
299
+ public Iterator<Float> valueIterator() {
300
+ return new Iterator<Float>() {
301
+ int index = -1;
302
+
303
+ public void remove() {
304
+ removeIndex(index);
305
+ index--;
306
+ }
307
+
308
+ public Float next() {
309
+ return value(++index);
310
+ }
311
+
312
+ public boolean hasNext() {
313
+ return index+1 < size();
314
+ }
315
+ };
316
+ }
317
+
318
+
319
+ /**
320
+ * Create a new array and copy each of the values into it.
321
+ *
322
+ * @webref floatdict:method
323
+ * @brief Create a new array and copy each of the values into it
324
+ */
325
+ public float[] valueArray() {
326
+ crop();
327
+ return valueArray(null);
328
+ }
329
+
330
+
331
+ /**
332
+ * Fill an already-allocated array with the values (more efficient than
333
+ * creating a new array each time). If 'array' is null, or not the same
334
+ * size as the number of values, a new array will be allocated and returned.
335
+ */
336
+ public float[] valueArray(float[] array) {
337
+ if (array == null || array.length != size()) {
338
+ array = new float[count];
339
+ }
340
+ System.arraycopy(values, 0, array, 0, count);
341
+ return array;
342
+ }
343
+
344
+
345
+ /**
346
+ * Return a value for the specified key.
347
+ *
348
+ * @webref floatdict:method
349
+ * @brief Return a value for the specified key
350
+ */
351
+ public float get(String key) {
352
+ int index = index(key);
353
+ if (index == -1) {
354
+ throw new IllegalArgumentException("No key named '" + key + "'");
355
+ }
356
+ return values[index];
357
+ }
358
+
359
+
360
+ public float get(String key, float alternate) {
361
+ int index = index(key);
362
+ if (index == -1) {
363
+ return alternate;
364
+ }
365
+ return values[index];
366
+ }
367
+
368
+
369
+ /**
370
+ * @webref floatdict:method
371
+ * @brief Create a new key/value pair or change the value of one
372
+ */
373
+ public void set(String key, float amount) {
374
+ int index = index(key);
375
+ if (index == -1) {
376
+ create(key, amount);
377
+ } else {
378
+ values[index] = amount;
379
+ }
380
+ }
381
+
382
+
383
+ public void setIndex(int index, String key, float value) {
384
+ if (index < 0 || index >= count) {
385
+ throw new ArrayIndexOutOfBoundsException(index);
386
+ }
387
+ keys[index] = key;
388
+ values[index] = value;
389
+ }
390
+
391
+
392
+ /**
393
+ * @webref floatdict:method
394
+ * @brief Check if a key is a part of the data structure
395
+ */
396
+ public boolean hasKey(String key) {
397
+ return index(key) != -1;
398
+ }
399
+
400
+
401
+ /**
402
+ * @webref floatdict:method
403
+ * @brief Add to a value
404
+ */
405
+ public void add(String key, float amount) {
406
+ int index = index(key);
407
+ if (index == -1) {
408
+ create(key, amount);
409
+ } else {
410
+ values[index] += amount;
411
+ }
412
+ }
413
+
414
+
415
+ /**
416
+ * @webref floatdict:method
417
+ * @brief Subtract from a value
418
+ */
419
+ public void sub(String key, float amount) {
420
+ add(key, -amount);
421
+ }
422
+
423
+
424
+ /**
425
+ * @webref floatdict:method
426
+ * @brief Multiply a value
427
+ */
428
+ public void mult(String key, float amount) {
429
+ int index = index(key);
430
+ if (index != -1) {
431
+ values[index] *= amount;
432
+ }
433
+ }
434
+
435
+
436
+ /**
437
+ * @webref floatdict:method
438
+ * @brief Divide a value
439
+ */
440
+ public void div(String key, float amount) {
441
+ int index = index(key);
442
+ if (index != -1) {
443
+ values[index] /= amount;
444
+ }
445
+ }
446
+
447
+
448
+ private void checkMinMax(String functionName) {
449
+ if (count == 0) {
450
+ String msg =
451
+ String.format("Cannot use %s() on an empty %s.",
452
+ functionName, getClass().getSimpleName());
453
+ throw new RuntimeException(msg);
454
+ }
455
+ }
456
+
457
+
458
+ /**
459
+ * @webref floatlist:method
460
+ * @brief Return the smallest value
461
+ */
462
+ public int minIndex() {
463
+ //checkMinMax("minIndex");
464
+ if (count == 0) return -1;
465
+
466
+ // Will still return NaN if there are 1 or more entries, and they're all NaN
467
+ float m = Float.NaN;
468
+ int mi = -1;
469
+ for (int i = 0; i < count; i++) {
470
+ // find one good value to start
471
+ if (values[i] == values[i]) {
472
+ m = values[i];
473
+ mi = i;
474
+
475
+ // calculate the rest
476
+ for (int j = i+1; j < count; j++) {
477
+ float d = values[j];
478
+ if ((d == d) && (d < m)) {
479
+ m = values[j];
480
+ mi = j;
481
+ }
482
+ }
483
+ break;
484
+ }
485
+ }
486
+ return mi;
487
+ }
488
+
489
+
490
+ // return the key for the minimum value
491
+ public String minKey() {
492
+ checkMinMax("minKey");
493
+ int index = minIndex();
494
+ if (index == -1) {
495
+ return null;
496
+ }
497
+ return keys[index];
498
+ }
499
+
500
+
501
+ // return the minimum value, or throw an error if there are no values
502
+ public float minValue() {
503
+ checkMinMax("minValue");
504
+ int index = minIndex();
505
+ if (index == -1) {
506
+ return Float.NaN;
507
+ }
508
+ return values[index];
509
+ }
510
+
511
+
512
+ /**
513
+ * @webref floatlist:method
514
+ * @brief Return the largest value
515
+ */
516
+ // The index of the entry that has the max value. Reference above is incorrect.
517
+ public int maxIndex() {
518
+ //checkMinMax("maxIndex");
519
+ if (count == 0) {
520
+ return -1;
521
+ }
522
+ // Will still return NaN if there is 1 or more entries, and they're all NaN
523
+ float m = Float.NaN;
524
+ int mi = -1;
525
+ for (int i = 0; i < count; i++) {
526
+ // find one good value to start
527
+ if (values[i] == values[i]) {
528
+ m = values[i];
529
+ mi = i;
530
+
531
+ // calculate the rest
532
+ for (int j = i+1; j < count; j++) {
533
+ float d = values[j];
534
+ if (!Float.isNaN(d) && (d > m)) {
535
+ m = values[j];
536
+ mi = j;
537
+ }
538
+ }
539
+ break;
540
+ }
541
+ }
542
+ return mi;
543
+ }
544
+
545
+
546
+ /** The key for a max value; null if empty or everything is NaN (no max). */
547
+ public String maxKey() {
548
+ //checkMinMax("maxKey");
549
+ int index = maxIndex();
550
+ if (index == -1) {
551
+ return null;
552
+ }
553
+ return keys[index];
554
+ }
555
+
556
+
557
+ /** The max value. (Or NaN if no entries or they're all NaN.) */
558
+ public float maxValue() {
559
+ //checkMinMax("maxValue");
560
+ int index = maxIndex();
561
+ if (index == -1) {
562
+ return Float.NaN;
563
+ }
564
+ return values[index];
565
+ }
566
+
567
+
568
+ public float sum() {
569
+ double amount = sumDouble();
570
+ if (amount > Float.MAX_VALUE) {
571
+ throw new RuntimeException("sum() exceeds " + Float.MAX_VALUE + ", use sumDouble()");
572
+ }
573
+ if (amount < -Float.MAX_VALUE) {
574
+ throw new RuntimeException("sum() lower than " + -Float.MAX_VALUE + ", use sumDouble()");
575
+ }
576
+ return (float) amount;
577
+ }
578
+
579
+
580
+ public double sumDouble() {
581
+ double sum = 0;
582
+ for (int i = 0; i < count; i++) {
583
+ sum += values[i];
584
+ }
585
+ return sum;
586
+ }
587
+
588
+
589
+ public int index(String what) {
590
+ Integer found = indices.get(what);
591
+ return (found == null) ? -1 : found.intValue();
592
+ }
593
+
594
+
595
+ protected void create(String what, float much) {
596
+ if (count == keys.length) {
597
+ keys = PApplet.expand(keys);
598
+ values = PApplet.expand(values);
599
+ }
600
+ indices.put(what, Integer.valueOf(count));
601
+ keys[count] = what;
602
+ values[count] = much;
603
+ count++;
604
+ }
605
+
606
+
607
+ /**
608
+ * @webref floatdict:method
609
+ * @brief Remove a key/value pair
610
+ */
611
+ public float remove(String key) {
612
+ int index = index(key);
613
+ if (index == -1) {
614
+ throw new NoSuchElementException("'" + key + "' not found");
615
+ }
616
+ float value = values[index];
617
+ removeIndex(index);
618
+ return value;
619
+ }
620
+
621
+
622
+ public float removeIndex(int index) {
623
+ if (index < 0 || index >= count) {
624
+ throw new ArrayIndexOutOfBoundsException(index);
625
+ }
626
+ float value = values[index];
627
+ indices.remove(keys[index]);
628
+ for (int i = index; i < count-1; i++) {
629
+ keys[i] = keys[i+1];
630
+ values[i] = values[i+1];
631
+ indices.put(keys[i], i);
632
+ }
633
+ count--;
634
+ keys[count] = null;
635
+ values[count] = 0;
636
+ return value;
637
+ }
638
+
639
+
640
+ public void swap(int a, int b) {
641
+ String tkey = keys[a];
642
+ float tvalue = values[a];
643
+ keys[a] = keys[b];
644
+ values[a] = values[b];
645
+ keys[b] = tkey;
646
+ values[b] = tvalue;
647
+
648
+ // indices.put(keys[a], Integer.valueOf(a));
649
+ // indices.put(keys[b], Integer.valueOf(b));
650
+ }
651
+
652
+
653
+ /**
654
+ * Sort the keys alphabetically (ignoring case). Uses the value as a
655
+ * tie-breaker (only really possible with a key that has a case change).
656
+ *
657
+ * @webref floatdict:method
658
+ * @brief Sort the keys alphabetically
659
+ */
660
+ public void sortKeys() {
661
+ sortImpl(true, false, true);
662
+ }
663
+
664
+
665
+ /**
666
+ * @webref floatdict:method
667
+ * @brief Sort the keys alphabetically in reverse
668
+ */
669
+ public void sortKeysReverse() {
670
+ sortImpl(true, true, true);
671
+ }
672
+
673
+
674
+ /**
675
+ * Sort by values in descending order (largest value will be at [0]).
676
+ *
677
+ * @webref floatdict:method
678
+ * @brief Sort by values in ascending order
679
+ */
680
+ public void sortValues() {
681
+ sortValues(true);
682
+ }
683
+
684
+
685
+ /**
686
+ * Set true to ensure that the order returned is identical. Slightly
687
+ * slower because the tie-breaker for identical values compares the keys.
688
+ * @param stable
689
+ */
690
+ public void sortValues(boolean stable) {
691
+ sortImpl(false, false, stable);
692
+ }
693
+
694
+
695
+ /**
696
+ * @webref floatdict:method
697
+ * @brief Sort by values in descending order
698
+ */
699
+ public void sortValuesReverse() {
700
+ sortValuesReverse(true);
701
+ }
702
+
703
+
704
+ public void sortValuesReverse(boolean stable) {
705
+ sortImpl(false, true, stable);
706
+ }
707
+
708
+
709
+ protected void sortImpl(final boolean useKeys, final boolean reverse,
710
+ final boolean stable) {
711
+ Sort s = new Sort() {
712
+ @Override
713
+ public int size() {
714
+ if (useKeys) {
715
+ return count; // don't worry about NaN values
716
+
717
+ } else if (count == 0) { // skip the NaN check, it'll AIOOBE
718
+ return 0;
719
+
720
+ } else { // first move NaN values to the end of the list
721
+ int right = count - 1;
722
+ while (values[right] != values[right]) {
723
+ right--;
724
+ if (right == -1) {
725
+ return 0; // all values are NaN
726
+ }
727
+ }
728
+ for (int i = right; i >= 0; --i) {
729
+ if (Float.isNaN(values[i])) {
730
+ swap(i, right);
731
+ --right;
732
+ }
733
+ }
734
+ return right + 1;
735
+ }
736
+ }
737
+
738
+ @Override
739
+ public int compare(int a, int b) {
740
+ float diff = 0;
741
+ if (useKeys) {
742
+ diff = keys[a].compareToIgnoreCase(keys[b]);
743
+ if (diff == 0) {
744
+ diff = values[a] - values[b];
745
+ }
746
+ } else { // sort values
747
+ diff = values[a] - values[b];
748
+ if (diff == 0 && stable) {
749
+ diff = keys[a].compareToIgnoreCase(keys[b]);
750
+ }
751
+ }
752
+ if (diff == 0) {
753
+ return 0;
754
+ } else if (reverse) {
755
+ return diff < 0 ? 1 : -1;
756
+ } else {
757
+ return diff < 0 ? -1 : 1;
758
+ }
759
+ }
760
+
761
+ @Override
762
+ public void swap(int a, int b) {
763
+ FloatDict.this.swap(a, b);
764
+ }
765
+ };
766
+ s.run();
767
+
768
+ // Set the indices after sort/swaps (performance fix 160411)
769
+ resetIndices();
770
+ }
771
+
772
+
773
+ /**
774
+ * Sum all of the values in this dictionary, then return a new FloatDict of
775
+ * each key, divided by the total sum. The total for all values will be ~1.0.
776
+ * @return a FloatDict with the original keys, mapped to their pct of the total
777
+ */
778
+ public FloatDict getPercent() {
779
+ double sum = sum();
780
+ FloatDict outgoing = new FloatDict();
781
+ for (int i = 0; i < size(); i++) {
782
+ double percent = value(i) / sum;
783
+ outgoing.set(key(i), (float) percent);
784
+ }
785
+ return outgoing;
786
+ }
787
+
788
+
789
+ /** Returns a duplicate copy of this object. */
790
+ public FloatDict copy() {
791
+ FloatDict outgoing = new FloatDict(count);
792
+ System.arraycopy(keys, 0, outgoing.keys, 0, count);
793
+ System.arraycopy(values, 0, outgoing.values, 0, count);
794
+ for (int i = 0; i < count; i++) {
795
+ outgoing.indices.put(keys[i], i);
796
+ }
797
+ outgoing.count = count;
798
+ return outgoing;
799
+ }
800
+
801
+
802
+ public void print() {
803
+ for (int i = 0; i < size(); i++) {
804
+ System.out.println(keys[i] + " = " + values[i]);
805
+ }
806
+ }
807
+
808
+
809
+ /**
810
+ * Save tab-delimited entries to a file (TSV format, UTF-8 encoding)
811
+ */
812
+ public void save(File file) {
813
+ PrintWriter writer = PApplet.createWriter(file);
814
+ write(writer);
815
+ writer.close();
816
+ }
817
+
818
+
819
+ /**
820
+ * Write tab-delimited entries out to
821
+ * @param writer
822
+ */
823
+ public void write(PrintWriter writer) {
824
+ for (int i = 0; i < count; i++) {
825
+ writer.println(keys[i] + "\t" + values[i]);
826
+ }
827
+ writer.flush();
828
+ }
829
+
830
+
831
+ /**
832
+ * Return this dictionary as a String in JSON format.
833
+ */
834
+ public String toJSON() {
835
+ StringList items = new StringList();
836
+ for (int i = 0; i < count; i++) {
837
+ items.append(JSONObject.quote(keys[i])+ ": " + values[i]);
838
+ }
839
+ return "{ " + items.join(", ") + " }";
840
+ }
841
+
842
+
843
+ @Override
844
+ public String toString() {
845
+ return getClass().getSimpleName() + " size=" + size() + " " + toJSON();
846
+ }
847
+ }