forked from processing/processing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFloatHash.java
More file actions
300 lines (231 loc) · 6.12 KB
/
Copy pathFloatHash.java
File metadata and controls
300 lines (231 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package processing.data;
import java.io.*;
import java.util.HashMap;
import processing.core.PApplet;
/**
* A simple table class to use a String as a lookup for an float value.
*/
public class FloatHash {
/** Number of elements in the table */
public int count;
/**
* List of keys, available for sake of speed,
* but should be manipulated (consider it read-only).
*/
protected String[] keys;
/**
* List of values, available for sake of speed,
* but should be manipulated (consider it read-only).
*/
protected float[] values;
/** Internal implementation for faster lookups */
private HashMap<String, Integer> indices = new HashMap<String, Integer>();
public FloatHash() {
count = 0;
keys = new String[10];
values = new float[10];
}
public FloatHash(int length) {
count = 0;
keys = new String[length];
values = new float[length];
}
public FloatHash(PApplet parent, String filename) {
String[] lines = parent.loadStrings(filename);
keys = new String[lines.length];
values = new float[lines.length];
// boolean csv = (lines[0].indexOf('\t') == -1);
for (int i = 0; i < lines.length; i++) {
// String[] pieces = csv ? Table.splitLineCSV(lines[i]) : PApplet.split(lines[i], '\t');
String[] pieces = PApplet.split(lines[i], '\t');
if (pieces.length == 2) {
keys[count] = pieces[0];
values[count] = PApplet.parseFloat(pieces[1]);
count++;
}
}
}
public int getCount() {
return count;
}
public String key(int index) {
return keys[index];
}
protected void crop() {
if (count != keys.length) {
keys = PApplet.subset(keys, 0, count);
values = PApplet.subset(values, 0, count);
}
}
/**
* Return the internal array being used to store the keys. Allocated but
* unused entries will be removed. This array should not be modified.
*/
public String[] keys() {
crop();
return keys;
}
/**
* Return a copy of the internal keys array. This array can be modified.
*/
public String[] keyArray() {
return keyArray(null);
}
public String[] keyArray(String[] outgoing) {
if (outgoing == null || outgoing.length != count) {
outgoing = new String[count];
}
System.arraycopy(keys, 0, outgoing, 0, count);
return outgoing;
}
public float value(int index) {
return values[index];
}
public float[] values() {
crop();
return values;
}
public int[] valueArray() {
int[] outgoing = new int[count];
System.arraycopy(values, 0, outgoing, 0, count);
return outgoing;
}
public float get(String what) {
int index = index(what);
if (index == -1) return 0;
return values[index];
}
public void set(String who, int amount) {
int index = index(who);
if (index == -1) {
create(who, amount);
} else {
values[index] = amount;
}
}
public void add(String who, int amount) {
int index = index(who);
if (index == -1) {
create(who, amount);
} else {
values[index] += amount;
}
}
public void increment(String who) {
int index = index(who);
if (index == -1) {
create(who, 1);
} else {
values[index]++;
}
}
public int index(String what) {
Integer found = indices.get(what);
return (found == null) ? -1 : found.intValue();
}
protected void create(String what, int much) {
if (count == keys.length) {
keys = PApplet.expand(keys);
// String ktemp[] = new String[count << 1];
// System.arraycopy(keys, 0, ktemp, 0, count);
// keys = ktemp;
values = PApplet.expand(values);
// float vtemp[] = new float[count << 1];
// System.arraycopy(values, 0, vtemp, 0, count);
// values = vtemp;
}
indices.put(what, new Integer(count));
keys[count] = what;
values[count] = much;
count++;
}
public void print() {
write(new PrintWriter(System.out));
}
public void write(PrintWriter writer) {
for (int i = 0; i < count; i++) {
writer.println(keys[i] + "\t" + values[i]);
}
writer.flush();
}
public void remove(String which) {
removeIndex(index(which));
}
public void removeIndex(int which) {
//System.out.println("index is " + which + " and " + keys[which]);
indices.remove(keys[which]);
for (int i = which; i < count-1; i++) {
keys[i] = keys[i+1];
values[i] = values[i+1];
indices.put(keys[i], i);
}
count--;
keys[count] = null;
values[count] = 0;
}
public void swap(int a, int b) {
String tkey = keys[a];
float tvalue = values[a];
keys[a] = keys[b];
values[a] = values[b];
keys[b] = tkey;
values[b] = tvalue;
indices.put(keys[a], new Integer(a));
indices.put(keys[b], new Integer(b));
}
public void sortKeys() {
Sort s = new Sort() {
@Override
public int size() {
return count;
}
@Override
public float compare(int a, int b) {
int result = keys[a].compareToIgnoreCase(keys[b]);
if (result != 0) {
return result;
}
return values[b] - values[a];
}
@Override
public void swap(int a, int b) {
FloatHash.this.swap(a, b);
}
};
s.run();
}
/**
* Sort by values in descending order (largest value will be at [0]).
*/
public void sortValues() {
sortValues(true, true);
}
public void sortValues(final boolean descending) {
sortValues(descending, true);
}
// ascending puts the largest value at the end
// descending puts the largest value at 0
public void sortValues(final boolean descending, final boolean tiebreaker) {
Sort s = new Sort() {
@Override
public int size() {
return count;
}
@Override
public float compare(int a, int b) {
float diff = values[b] - values[a];
if (tiebreaker) {
if (diff == 0) {
diff = keys[a].compareToIgnoreCase(keys[b]);
}
}
return descending ? diff : -diff;
}
@Override
public void swap(int a, int b) {
FloatHash.this.swap(a, b);
}
};
s.run();
}
}