propane 3.4.2-java → 3.8.0-java

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