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