picrate 0.0.2-java

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +47 -0
  3. data/.mvn/extensions.xml +9 -0
  4. data/.mvn/wrapper/maven-wrapper.properties +1 -0
  5. data/.travis.yml +10 -0
  6. data/CHANGELOG.md +4 -0
  7. data/LICENSE.md +165 -0
  8. data/README.md +51 -0
  9. data/Rakefile +59 -0
  10. data/bin/picrate +8 -0
  11. data/docs/.gitignore +6 -0
  12. data/docs/_config.yml +30 -0
  13. data/docs/_includes/footer.html +38 -0
  14. data/docs/_includes/head.html +16 -0
  15. data/docs/_includes/header.html +27 -0
  16. data/docs/_includes/icon-github.html +1 -0
  17. data/docs/_includes/icon-github.svg +1 -0
  18. data/docs/_includes/icon-twitter.html +1 -0
  19. data/docs/_includes/icon-twitter.svg +1 -0
  20. data/docs/_includes/navigation.html +24 -0
  21. data/docs/_layouts/default.html +20 -0
  22. data/docs/_layouts/page.html +14 -0
  23. data/docs/_layouts/post.html +15 -0
  24. data/docs/_posts/2018-05-06-getting_started.md +8 -0
  25. data/docs/_posts/2018-05-06-install_jruby.md +35 -0
  26. data/docs/_sass/_base.scss +206 -0
  27. data/docs/_sass/_layout.scss +242 -0
  28. data/docs/_sass/_syntax-highlighting.scss +71 -0
  29. data/docs/about.md +10 -0
  30. data/docs/css/main.scss +38 -0
  31. data/docs/favicon.ico +0 -0
  32. data/docs/feed.xml +30 -0
  33. data/docs/index.html +38 -0
  34. data/lib/picrate.rb +10 -0
  35. data/lib/picrate/app.rb +187 -0
  36. data/lib/picrate/creators/sketch_class.rb +57 -0
  37. data/lib/picrate/creators/sketch_factory.rb +12 -0
  38. data/lib/picrate/creators/sketch_writer.rb +21 -0
  39. data/lib/picrate/helper_methods.rb +214 -0
  40. data/lib/picrate/helpers/numeric.rb +9 -0
  41. data/lib/picrate/library.rb +69 -0
  42. data/lib/picrate/library_loader.rb +53 -0
  43. data/lib/picrate/native_folder.rb +35 -0
  44. data/lib/picrate/native_loader.rb +27 -0
  45. data/lib/picrate/runner.rb +81 -0
  46. data/lib/picrate/version.rb +4 -0
  47. data/library/boids/boids.rb +209 -0
  48. data/library/chooser/chooser.rb +19 -0
  49. data/library/control_panel/control_panel.rb +182 -0
  50. data/library/library_proxy/README.md +99 -0
  51. data/library/library_proxy/library_proxy.rb +14 -0
  52. data/library/slider/slider.rb +42 -0
  53. data/library/vector_utils/vector_utils.rb +69 -0
  54. data/library/video_event/video_event.rb +3 -0
  55. data/license.txt +508 -0
  56. data/picrate.gemspec +35 -0
  57. data/pom.rb +122 -0
  58. data/pom.xml +214 -0
  59. data/src/main/java/japplemenubar/JAppleMenuBar.java +88 -0
  60. data/src/main/java/japplemenubar/libjAppleMenuBar.jnilib +0 -0
  61. data/src/main/java/monkstone/ColorUtil.java +115 -0
  62. data/src/main/java/monkstone/MathToolModule.java +236 -0
  63. data/src/main/java/monkstone/PicrateLibrary.java +47 -0
  64. data/src/main/java/monkstone/core/LibraryProxy.java +127 -0
  65. data/src/main/java/monkstone/fastmath/Deglut.java +122 -0
  66. data/src/main/java/monkstone/fastmath/package-info.java +6 -0
  67. data/src/main/java/monkstone/filechooser/Chooser.java +48 -0
  68. data/src/main/java/monkstone/noise/SimplexNoise.java +465 -0
  69. data/src/main/java/monkstone/slider/CustomHorizontalSlider.java +168 -0
  70. data/src/main/java/monkstone/slider/CustomVerticalSlider.java +182 -0
  71. data/src/main/java/monkstone/slider/SimpleHorizontalSlider.java +149 -0
  72. data/src/main/java/monkstone/slider/SimpleSlider.java +196 -0
  73. data/src/main/java/monkstone/slider/SimpleVerticalSlider.java +163 -0
  74. data/src/main/java/monkstone/slider/Slider.java +67 -0
  75. data/src/main/java/monkstone/slider/SliderBar.java +277 -0
  76. data/src/main/java/monkstone/slider/SliderGroup.java +78 -0
  77. data/src/main/java/monkstone/slider/WheelHandler.java +35 -0
  78. data/src/main/java/monkstone/vecmath/AppRender.java +87 -0
  79. data/src/main/java/monkstone/vecmath/JRender.java +56 -0
  80. data/src/main/java/monkstone/vecmath/ShapeRender.java +87 -0
  81. data/src/main/java/monkstone/vecmath/package-info.java +20 -0
  82. data/src/main/java/monkstone/vecmath/vec2/Vec2.java +757 -0
  83. data/src/main/java/monkstone/vecmath/vec2/package-info.java +6 -0
  84. data/src/main/java/monkstone/vecmath/vec3/Vec3.java +727 -0
  85. data/src/main/java/monkstone/vecmath/vec3/package-info.java +6 -0
  86. data/src/main/java/monkstone/videoevent/VideoInterface.java +42 -0
  87. data/src/main/java/monkstone/videoevent/package-info.java +20 -0
  88. data/src/main/java/processing/awt/PGraphicsJava2D.java +3098 -0
  89. data/src/main/java/processing/awt/PShapeJava2D.java +401 -0
  90. data/src/main/java/processing/awt/PSurfaceAWT.java +1660 -0
  91. data/src/main/java/processing/core/PApplet.java +17647 -0
  92. data/src/main/java/processing/core/PConstants.java +1033 -0
  93. data/src/main/java/processing/core/PFont.java +1250 -0
  94. data/src/main/java/processing/core/PGraphics.java +9614 -0
  95. data/src/main/java/processing/core/PImage.java +3608 -0
  96. data/src/main/java/processing/core/PMatrix.java +347 -0
  97. data/src/main/java/processing/core/PMatrix2D.java +694 -0
  98. data/src/main/java/processing/core/PMatrix3D.java +1153 -0
  99. data/src/main/java/processing/core/PShape.java +4332 -0
  100. data/src/main/java/processing/core/PShapeOBJ.java +544 -0
  101. data/src/main/java/processing/core/PShapeSVG.java +1987 -0
  102. data/src/main/java/processing/core/PStyle.java +208 -0
  103. data/src/main/java/processing/core/PSurface.java +242 -0
  104. data/src/main/java/processing/core/PSurfaceNone.java +479 -0
  105. data/src/main/java/processing/core/PVector.java +1140 -0
  106. data/src/main/java/processing/data/FloatDict.java +829 -0
  107. data/src/main/java/processing/data/FloatList.java +912 -0
  108. data/src/main/java/processing/data/IntDict.java +796 -0
  109. data/src/main/java/processing/data/IntList.java +913 -0
  110. data/src/main/java/processing/data/JSONArray.java +1260 -0
  111. data/src/main/java/processing/data/JSONObject.java +2282 -0
  112. data/src/main/java/processing/data/JSONTokener.java +435 -0
  113. data/src/main/java/processing/data/Sort.java +46 -0
  114. data/src/main/java/processing/data/StringDict.java +601 -0
  115. data/src/main/java/processing/data/StringList.java +775 -0
  116. data/src/main/java/processing/data/Table.java +4923 -0
  117. data/src/main/java/processing/data/TableRow.java +198 -0
  118. data/src/main/java/processing/data/XML.java +1149 -0
  119. data/src/main/java/processing/event/Event.java +108 -0
  120. data/src/main/java/processing/event/KeyEvent.java +70 -0
  121. data/src/main/java/processing/event/MouseEvent.java +149 -0
  122. data/src/main/java/processing/event/TouchEvent.java +57 -0
  123. data/src/main/java/processing/javafx/PGraphicsFX2D.java +354 -0
  124. data/src/main/java/processing/opengl/FontTexture.java +379 -0
  125. data/src/main/java/processing/opengl/FrameBuffer.java +503 -0
  126. data/src/main/java/processing/opengl/LinePath.java +623 -0
  127. data/src/main/java/processing/opengl/LineStroker.java +685 -0
  128. data/src/main/java/processing/opengl/PGL.java +3366 -0
  129. data/src/main/java/processing/opengl/PGraphics2D.java +615 -0
  130. data/src/main/java/processing/opengl/PGraphics3D.java +281 -0
  131. data/src/main/java/processing/opengl/PGraphicsOpenGL.java +13634 -0
  132. data/src/main/java/processing/opengl/PJOGL.java +1966 -0
  133. data/src/main/java/processing/opengl/PShader.java +1478 -0
  134. data/src/main/java/processing/opengl/PShapeOpenGL.java +5234 -0
  135. data/src/main/java/processing/opengl/PSurfaceJOGL.java +1315 -0
  136. data/src/main/java/processing/opengl/Texture.java +1670 -0
  137. data/src/main/java/processing/opengl/VertexBuffer.java +88 -0
  138. data/src/main/java/processing/opengl/cursors/arrow.png +0 -0
  139. data/src/main/java/processing/opengl/cursors/cross.png +0 -0
  140. data/src/main/java/processing/opengl/cursors/hand.png +0 -0
  141. data/src/main/java/processing/opengl/cursors/license.txt +27 -0
  142. data/src/main/java/processing/opengl/cursors/move.png +0 -0
  143. data/src/main/java/processing/opengl/cursors/text.png +0 -0
  144. data/src/main/java/processing/opengl/cursors/wait.png +0 -0
  145. data/src/main/java/processing/opengl/shaders/ColorFrag.glsl +32 -0
  146. data/src/main/java/processing/opengl/shaders/ColorVert.glsl +34 -0
  147. data/src/main/java/processing/opengl/shaders/LightFrag.glsl +33 -0
  148. data/src/main/java/processing/opengl/shaders/LightVert-vc4.glsl +154 -0
  149. data/src/main/java/processing/opengl/shaders/LightVert.glsl +151 -0
  150. data/src/main/java/processing/opengl/shaders/LineFrag.glsl +32 -0
  151. data/src/main/java/processing/opengl/shaders/LineVert.glsl +100 -0
  152. data/src/main/java/processing/opengl/shaders/MaskFrag.glsl +40 -0
  153. data/src/main/java/processing/opengl/shaders/PointFrag.glsl +32 -0
  154. data/src/main/java/processing/opengl/shaders/PointVert.glsl +56 -0
  155. data/src/main/java/processing/opengl/shaders/TexFrag.glsl +37 -0
  156. data/src/main/java/processing/opengl/shaders/TexLightFrag.glsl +37 -0
  157. data/src/main/java/processing/opengl/shaders/TexLightVert-vc4.glsl +160 -0
  158. data/src/main/java/processing/opengl/shaders/TexLightVert.glsl +157 -0
  159. data/src/main/java/processing/opengl/shaders/TexVert.glsl +38 -0
  160. data/src/main/resources/icon/icon-1024.png +0 -0
  161. data/src/main/resources/icon/icon-128.png +0 -0
  162. data/src/main/resources/icon/icon-16.png +0 -0
  163. data/src/main/resources/icon/icon-256.png +0 -0
  164. data/src/main/resources/icon/icon-32.png +0 -0
  165. data/src/main/resources/icon/icon-48.png +0 -0
  166. data/src/main/resources/icon/icon-512.png +0 -0
  167. data/src/main/resources/icon/icon-64.png +0 -0
  168. data/src/main/resources/license.txt +508 -0
  169. data/test/create_test.rb +68 -0
  170. data/test/deglut_spec_test.rb +24 -0
  171. data/test/helper_methods_test.rb +58 -0
  172. data/test/math_tool_test.rb +75 -0
  173. data/test/respond_to_test.rb +215 -0
  174. data/test/sketches/key_event.rb +37 -0
  175. data/test/sketches/library/my_library/my_library.rb +32 -0
  176. data/test/test_helper.rb +3 -0
  177. data/test/vecmath_spec_test.rb +522 -0
  178. data/vendors/Rakefile +127 -0
  179. metadata +289 -0
@@ -0,0 +1,796 @@
1
+ package processing.data;
2
+
3
+ import java.io.*;
4
+ import java.util.HashMap;
5
+ import java.util.Iterator;
6
+
7
+ import processing.core.PApplet;
8
+
9
+
10
+ /**
11
+ * A simple class to use a String as a lookup for an int value.
12
+ *
13
+ * @webref data:composite
14
+ * @see FloatDict
15
+ * @see StringDict
16
+ */
17
+ public class IntDict {
18
+
19
+ /** Number of elements in the table */
20
+ protected int count;
21
+
22
+ protected String[] keys;
23
+ protected int[] values;
24
+
25
+ /** Internal implementation for faster lookups */
26
+ private HashMap<String, Integer> indices = new HashMap<>();
27
+
28
+
29
+ public IntDict() {
30
+ count = 0;
31
+ keys = new String[10];
32
+ values = new int[10];
33
+ }
34
+
35
+
36
+ /**
37
+ * Create a new lookup with a specific size. This is more efficient than not
38
+ * specifying a size. Use it when you know the rough size of the thing you're creating.
39
+ *
40
+ * @nowebref
41
+ */
42
+ public IntDict(int length) {
43
+ count = 0;
44
+ keys = new String[length];
45
+ values = new int[length];
46
+ }
47
+
48
+
49
+ /**
50
+ * Read a set of entries from a Reader that has each key/value pair on
51
+ * a single line, separated by a tab.
52
+ *
53
+ * @nowebref
54
+ */
55
+ public IntDict(BufferedReader reader) {
56
+ String[] lines = PApplet.loadStrings(reader);
57
+ keys = new String[lines.length];
58
+ values = new int[lines.length];
59
+
60
+ for (int i = 0; i < lines.length; i++) {
61
+ String[] pieces = PApplet.split(lines[i], '\t');
62
+ if (pieces.length == 2) {
63
+ keys[count] = pieces[0];
64
+ values[count] = PApplet.parseInt(pieces[1]);
65
+ indices.put(pieces[0], count);
66
+ count++;
67
+ }
68
+ }
69
+ }
70
+
71
+ /**
72
+ * @nowebref
73
+ */
74
+ public IntDict(String[] keys, int[] values) {
75
+ if (keys.length != values.length) {
76
+ throw new IllegalArgumentException("key and value arrays must be the same length");
77
+ }
78
+ this.keys = keys;
79
+ this.values = values;
80
+ count = keys.length;
81
+ for (int i = 0; i < count; i++) {
82
+ indices.put(keys[i], i);
83
+ }
84
+ }
85
+
86
+
87
+ /**
88
+ * Constructor to allow (more intuitive) inline initialization, e.g.:
89
+ * <pre>
90
+ * new FloatDict(new Object[][] {
91
+ * { "key1", 1 },
92
+ * { "key2", 2 }
93
+ * });
94
+ * </pre>
95
+ */
96
+ public IntDict(Object[][] pairs) {
97
+ count = pairs.length;
98
+ this.keys = new String[count];
99
+ this.values = new int[count];
100
+ for (int i = 0; i < count; i++) {
101
+ keys[i] = (String) pairs[i][0];
102
+ values[i] = (Integer) pairs[i][1];
103
+ indices.put(keys[i], i);
104
+ }
105
+ }
106
+
107
+
108
+ /**
109
+ * Returns the number of key/value pairs
110
+ *
111
+ * @webref intdict: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) {
125
+ throw new IllegalArgumentException("resize() can only be used to shrink the dictionary");
126
+ }
127
+ if (length < 1) {
128
+ throw new IllegalArgumentException("resize(" + length + ") is too small, use 1 or higher");
129
+ }
130
+
131
+ String[] newKeys = new String[length];
132
+ int[] newValues = new int[length];
133
+ PApplet.arrayCopy(keys, newKeys, length);
134
+ PApplet.arrayCopy(values, newValues, length);
135
+ keys = newKeys;
136
+ values = newValues;
137
+ count = length;
138
+ resetIndices();
139
+ }
140
+
141
+
142
+ /**
143
+ * Remove all entries.
144
+ *
145
+ * @webref intdict:method
146
+ * @brief Remove all entries
147
+ */
148
+ public void clear() {
149
+ count = 0;
150
+ indices = new HashMap<>();
151
+ }
152
+
153
+
154
+ private void resetIndices() {
155
+ indices = new HashMap<>(count);
156
+ for (int i = 0; i < count; i++) {
157
+ indices.put(keys[i], i);
158
+ }
159
+ }
160
+
161
+
162
+ // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
163
+
164
+
165
+ public class Entry {
166
+ public String key;
167
+ public int value;
168
+
169
+ Entry(String key, int value) {
170
+ this.key = key;
171
+ this.value = value;
172
+ }
173
+ }
174
+
175
+
176
+ public Iterable<Entry> entries() {
177
+ return new Iterable<Entry>() {
178
+
179
+ public Iterator<Entry> iterator() {
180
+ return entryIterator();
181
+ }
182
+ };
183
+ }
184
+
185
+
186
+ public Iterator<Entry> entryIterator() {
187
+ return new Iterator<Entry>() {
188
+ int index = -1;
189
+
190
+ public void remove() {
191
+ removeIndex(index);
192
+ index--;
193
+ }
194
+
195
+ public Entry next() {
196
+ ++index;
197
+ Entry e = new Entry(keys[index], values[index]);
198
+ return e;
199
+ }
200
+
201
+ public boolean hasNext() {
202
+ return index+1 < size();
203
+ }
204
+ };
205
+ }
206
+
207
+
208
+ // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
209
+
210
+
211
+ public String key(int index) {
212
+ return keys[index];
213
+ }
214
+
215
+
216
+ protected void crop() {
217
+ if (count != keys.length) {
218
+ keys = PApplet.subset(keys, 0, count);
219
+ values = PApplet.subset(values, 0, count);
220
+ }
221
+ }
222
+
223
+
224
+ public Iterable<String> keys() {
225
+ return new Iterable<String>() {
226
+
227
+ @Override
228
+ public Iterator<String> iterator() {
229
+ return keyIterator();
230
+ }
231
+ };
232
+ }
233
+
234
+
235
+ // Use this to iterate when you want to be able to remove elements along the way
236
+ public Iterator<String> keyIterator() {
237
+ return new Iterator<String>() {
238
+ int index = -1;
239
+
240
+ public void remove() {
241
+ removeIndex(index);
242
+ index--;
243
+ }
244
+
245
+ public String next() {
246
+ return key(++index);
247
+ }
248
+
249
+ public boolean hasNext() {
250
+ return index+1 < size();
251
+ }
252
+ };
253
+ }
254
+
255
+
256
+ /**
257
+ * Return a copy of the internal keys array. This array can be modified.
258
+ *
259
+ * @webref intdict:method
260
+ * @brief Return a copy of the internal keys array
261
+ */
262
+ public String[] keyArray() {
263
+ crop();
264
+ return keyArray(null);
265
+ }
266
+
267
+
268
+ public String[] keyArray(String[] outgoing) {
269
+ if (outgoing == null || outgoing.length != count) {
270
+ outgoing = new String[count];
271
+ }
272
+ System.arraycopy(keys, 0, outgoing, 0, count);
273
+ return outgoing;
274
+ }
275
+
276
+
277
+ public int value(int index) {
278
+ return values[index];
279
+ }
280
+
281
+
282
+ /**
283
+ * @webref intdict:method
284
+ * @brief Return the internal array being used to store the values
285
+ */
286
+ public Iterable<Integer> values() {
287
+ return new Iterable<Integer>() {
288
+
289
+ @Override
290
+ public Iterator<Integer> iterator() {
291
+ return valueIterator();
292
+ }
293
+ };
294
+ }
295
+
296
+
297
+ public Iterator<Integer> valueIterator() {
298
+ return new Iterator<Integer>() {
299
+ int index = -1;
300
+
301
+ public void remove() {
302
+ removeIndex(index);
303
+ index--;
304
+ }
305
+
306
+ public Integer next() {
307
+ return value(++index);
308
+ }
309
+
310
+ public boolean hasNext() {
311
+ return index+1 < size();
312
+ }
313
+ };
314
+ }
315
+
316
+
317
+ /**
318
+ * Create a new array and copy each of the values into it.
319
+ *
320
+ * @webref intdict:method
321
+ * @brief Create a new array and copy each of the values into it
322
+ */
323
+ public int[] valueArray() {
324
+ crop();
325
+ return valueArray(null);
326
+ }
327
+
328
+
329
+ /**
330
+ * Fill an already-allocated array with the values (more efficient than
331
+ * creating a new array each time). If 'array' is null, or not the same
332
+ * size as the number of values, a new array will be allocated and returned.
333
+ *
334
+ * @param array values to copy into the array
335
+ */
336
+ public int[] valueArray(int[] array) {
337
+ if (array == null || array.length != size()) {
338
+ array = new int[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 intdict:method
349
+ * @brief Return a value for the specified key
350
+ */
351
+ public int 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 int get(String key, int alternate) {
361
+ int index = index(key);
362
+ if (index == -1) return alternate;
363
+ return values[index];
364
+ }
365
+
366
+
367
+ /**
368
+ * Create a new key/value pair or change the value of one.
369
+ *
370
+ * @webref intdict:method
371
+ * @brief Create a new key/value pair or change the value of one
372
+ */
373
+ public void set(String key, int 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, int 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 intdict: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
+ * Increase the value associated with a specific key by 1.
403
+ *
404
+ * @webref intdict:method
405
+ * @brief Increase the value of a specific key value by 1
406
+ */
407
+ public void increment(String key) {
408
+ add(key, 1);
409
+ }
410
+
411
+
412
+ /**
413
+ * Merge another dictionary into this one. Calling this increment()
414
+ * since it doesn't make sense in practice for the other dictionary types,
415
+ * even though it's technically an add().
416
+ */
417
+ public void increment(IntDict dict) {
418
+ for (int i = 0; i < dict.count; i++) {
419
+ add(dict.key(i), dict.value(i));
420
+ }
421
+ }
422
+
423
+
424
+ /**
425
+ * @webref intdict:method
426
+ * @brief Add to a value
427
+ */
428
+ public void add(String key, int amount) {
429
+ int index = index(key);
430
+ if (index == -1) {
431
+ create(key, amount);
432
+ } else {
433
+ values[index] += amount;
434
+ }
435
+ }
436
+
437
+
438
+ /**
439
+ * @webref intdict:method
440
+ * @brief Subtract from a value
441
+ */
442
+ public void sub(String key, int amount) {
443
+ add(key, -amount);
444
+ }
445
+
446
+
447
+ /**
448
+ * @webref intdict:method
449
+ * @brief Multiply a value
450
+ */
451
+ public void mult(String key, int amount) {
452
+ int index = index(key);
453
+ if (index != -1) {
454
+ values[index] *= amount;
455
+ }
456
+ }
457
+
458
+
459
+ /**
460
+ * @webref intdict:method
461
+ * @brief Divide a value
462
+ */
463
+ public void div(String key, int amount) {
464
+ int index = index(key);
465
+ if (index != -1) {
466
+ values[index] /= amount;
467
+ }
468
+ }
469
+
470
+
471
+ private void checkMinMax(String functionName) {
472
+ if (count == 0) {
473
+ String msg =
474
+ String.format("Cannot use %s() on an empty %s.",
475
+ functionName, getClass().getSimpleName());
476
+ throw new RuntimeException(msg);
477
+ }
478
+ }
479
+
480
+
481
+ // return the index of the minimum value
482
+ public int minIndex() {
483
+ //checkMinMax("minIndex");
484
+ if (count == 0) return -1;
485
+
486
+ int index = 0;
487
+ int value = values[0];
488
+ for (int i = 1; i < count; i++) {
489
+ if (values[i] < value) {
490
+ index = i;
491
+ value = values[i];
492
+ }
493
+ }
494
+ return index;
495
+ }
496
+
497
+
498
+ // return the key for the minimum value
499
+ public String minKey() {
500
+ checkMinMax("minKey");
501
+ int index = minIndex();
502
+ if (index == -1) {
503
+ return null;
504
+ }
505
+ return keys[index];
506
+ }
507
+
508
+
509
+ // return the minimum value, or throw an error if there are no values
510
+ public int minValue() {
511
+ checkMinMax("minValue");
512
+ return values[minIndex()];
513
+ }
514
+
515
+
516
+ // return the index of the max value
517
+ public int maxIndex() {
518
+ //checkMinMax("maxIndex");
519
+ if (count == 0) {
520
+ return -1;
521
+ }
522
+ int index = 0;
523
+ int value = values[0];
524
+ for (int i = 1; i < count; i++) {
525
+ if (values[i] > value) {
526
+ index = i;
527
+ value = values[i];
528
+ }
529
+ }
530
+ return index;
531
+ }
532
+
533
+
534
+ /** return the key corresponding to the maximum value or null if no entries */
535
+ public String maxKey() {
536
+ //checkMinMax("maxKey");
537
+ int index = maxIndex();
538
+ if (index == -1) {
539
+ return null;
540
+ }
541
+ return keys[index];
542
+ }
543
+
544
+
545
+ // return the maximum value or throw an error if zero length
546
+ public int maxValue() {
547
+ checkMinMax("maxIndex");
548
+ return values[maxIndex()];
549
+ }
550
+
551
+
552
+ public int sum() {
553
+ long amount = sumLong();
554
+ if (amount > Integer.MAX_VALUE) {
555
+ throw new RuntimeException("sum() exceeds " + Integer.MAX_VALUE + ", use sumLong()");
556
+ }
557
+ if (amount < Integer.MIN_VALUE) {
558
+ throw new RuntimeException("sum() less than " + Integer.MIN_VALUE + ", use sumLong()");
559
+ }
560
+ return (int) amount;
561
+ }
562
+
563
+
564
+ public long sumLong() {
565
+ long sum = 0;
566
+ for (int i = 0; i < count; i++) {
567
+ sum += values[i];
568
+ }
569
+ return sum;
570
+ }
571
+
572
+
573
+ public int index(String what) {
574
+ Integer found = indices.get(what);
575
+ return (found == null) ? -1 : found.intValue();
576
+ }
577
+
578
+
579
+ protected void create(String what, int much) {
580
+ if (count == keys.length) {
581
+ keys = PApplet.expand(keys);
582
+ values = PApplet.expand(values);
583
+ }
584
+ indices.put(what, Integer.valueOf(count));
585
+ keys[count] = what;
586
+ values[count] = much;
587
+ count++;
588
+ }
589
+
590
+ /**
591
+ * @webref intdict:method
592
+ * @brief Remove a key/value pair
593
+ */
594
+ public int remove(String key) {
595
+ int index = index(key);
596
+ if (index != -1) {
597
+ removeIndex(index);
598
+ }
599
+ return index;
600
+ }
601
+
602
+
603
+ public String removeIndex(int index) {
604
+ if (index < 0 || index >= count) {
605
+ throw new ArrayIndexOutOfBoundsException(index);
606
+ }
607
+ //System.out.println("index is " + which + " and " + keys[which]);
608
+ String key = keys[index];
609
+ indices.remove(keys[index]);
610
+ for (int i = index; i < count-1; i++) {
611
+ keys[i] = keys[i+1];
612
+ values[i] = values[i+1];
613
+ indices.put(keys[i], i);
614
+ }
615
+ count--;
616
+ keys[count] = null;
617
+ values[count] = 0;
618
+ return key;
619
+ }
620
+
621
+
622
+ public void swap(int a, int b) {
623
+ String tkey = keys[a];
624
+ int tvalue = values[a];
625
+ keys[a] = keys[b];
626
+ values[a] = values[b];
627
+ keys[b] = tkey;
628
+ values[b] = tvalue;
629
+
630
+ // indices.put(keys[a], Integer.valueOf(a));
631
+ // indices.put(keys[b], Integer.valueOf(b));
632
+ }
633
+
634
+
635
+ /**
636
+ * Sort the keys alphabetically (ignoring case). Uses the value as a
637
+ * tie-breaker (only really possible with a key that has a case change).
638
+ *
639
+ * @webref intdict:method
640
+ * @brief Sort the keys alphabetically
641
+ */
642
+ public void sortKeys() {
643
+ sortImpl(true, false, true);
644
+ }
645
+
646
+ /**
647
+ * Sort the keys alphabetically in reverse (ignoring case). Uses the value as a
648
+ * tie-breaker (only really possible with a key that has a case change).
649
+ *
650
+ * @webref intdict:method
651
+ * @brief Sort the keys alphabetically in reverse
652
+ */
653
+ public void sortKeysReverse() {
654
+ sortImpl(true, true, true);
655
+ }
656
+
657
+
658
+ /**
659
+ * Sort by values in ascending order. The smallest value will be at [0].
660
+ *
661
+ * @webref intdict:method
662
+ * @brief Sort by values in ascending order
663
+ */
664
+ public void sortValues() {
665
+ sortValues(true);
666
+ }
667
+
668
+
669
+ /**
670
+ * Set true to ensure that the order returned is identical. Slightly
671
+ * slower because the tie-breaker for identical values compares the keys.
672
+ * @param stable
673
+ */
674
+ public void sortValues(boolean stable) {
675
+ sortImpl(false, false, stable);
676
+ }
677
+
678
+
679
+ /**
680
+ * Sort by values in descending order. The largest value will be at [0].
681
+ *
682
+ * @webref intdict:method
683
+ * @brief Sort by values in descending order
684
+ */
685
+ public void sortValuesReverse() {
686
+ sortValuesReverse(true);
687
+ }
688
+
689
+
690
+ public void sortValuesReverse(boolean stable) {
691
+ sortImpl(false, true, stable);
692
+ }
693
+
694
+
695
+ protected void sortImpl(final boolean useKeys, final boolean reverse,
696
+ final boolean stable) {
697
+ Sort s = new Sort() {
698
+ @Override
699
+ public int size() {
700
+ return count;
701
+ }
702
+
703
+ @Override
704
+ public float compare(int a, int b) {
705
+ int diff = 0;
706
+ if (useKeys) {
707
+ diff = keys[a].compareToIgnoreCase(keys[b]);
708
+ if (diff == 0) {
709
+ diff = values[a] - values[b];
710
+ }
711
+ } else { // sort values
712
+ diff = values[a] - values[b];
713
+ if (diff == 0 && stable) {
714
+ diff = keys[a].compareToIgnoreCase(keys[b]);
715
+ }
716
+ }
717
+ return reverse ? -diff : diff;
718
+ }
719
+
720
+ @Override
721
+ public void swap(int a, int b) {
722
+ IntDict.this.swap(a, b);
723
+ }
724
+ };
725
+ s.run();
726
+
727
+ // Set the indices after sort/swaps (performance fix 160411)
728
+ resetIndices();
729
+ }
730
+
731
+
732
+ /**
733
+ * Sum all of the values in this dictionary, then return a new FloatDict of
734
+ * each key, divided by the total sum. The total for all values will be ~1.0.
735
+ * @return an IntDict with the original keys, mapped to their pct of the total
736
+ */
737
+ public FloatDict getPercent() {
738
+ double sum = sum(); // a little more accuracy
739
+ FloatDict outgoing = new FloatDict();
740
+ for (int i = 0; i < size(); i++) {
741
+ double percent = value(i) / sum;
742
+ outgoing.set(key(i), (float) percent);
743
+ }
744
+ return outgoing;
745
+ }
746
+
747
+
748
+ /** Returns a duplicate copy of this object. */
749
+ public IntDict copy() {
750
+ IntDict outgoing = new IntDict(count);
751
+ System.arraycopy(keys, 0, outgoing.keys, 0, count);
752
+ System.arraycopy(values, 0, outgoing.values, 0, count);
753
+ for (int i = 0; i < count; i++) {
754
+ outgoing.indices.put(keys[i], i);
755
+ }
756
+ outgoing.count = count;
757
+ return outgoing;
758
+ }
759
+
760
+
761
+ public void print() {
762
+ for (int i = 0; i < size(); i++) {
763
+ System.out.println(keys[i] + " = " + values[i]);
764
+ }
765
+ }
766
+
767
+
768
+ /**
769
+ * Write tab-delimited entries out to
770
+ * @param writer
771
+ */
772
+ public void write(PrintWriter writer) {
773
+ for (int i = 0; i < count; i++) {
774
+ writer.println(keys[i] + "\t" + values[i]);
775
+ }
776
+ writer.flush();
777
+ }
778
+
779
+
780
+ /**
781
+ * Return this dictionary as a String in JSON format.
782
+ */
783
+ public String toJSON() {
784
+ StringList items = new StringList();
785
+ for (int i = 0; i < count; i++) {
786
+ items.append(JSONObject.quote(keys[i])+ ": " + values[i]);
787
+ }
788
+ return "{ " + items.join(", ") + " }";
789
+ }
790
+
791
+
792
+ @Override
793
+ public String toString() {
794
+ return getClass().getSimpleName() + " size=" + size() + " " + toJSON();
795
+ }
796
+ }