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