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