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