Skip to content

Commit bdc5df4

Browse files
committed
uh-oh
1 parent ae0bbbd commit bdc5df4

File tree

13 files changed

+4916
-0
lines changed

13 files changed

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

0 commit comments

Comments
 (0)