added resize() to IntDict, FloatDict, StringDict

This commit is contained in:
Ben Fry
2017-10-10 20:57:27 -04:00
parent 2327009d88
commit 65b138de31
3 changed files with 96 additions and 12 deletions

View File

@@ -137,6 +137,29 @@ public class StringDict {
}
/**
* Resize the internal data, this can only be used to shrink the list.
* Helpful for situations like sorting and then grabbing the top 50 entries.
*/
public void resize(int length) {
if (length > count) {
throw new IllegalArgumentException("resize() can only be used to shrink the dictionary");
}
if (length < 1) {
throw new IllegalArgumentException("resize(" + length + ") is too small, use 1 or higher");
}
String[] newKeys = new String[length];
String[] newValues = new String[length];
PApplet.arrayCopy(keys, newKeys, length);
PApplet.arrayCopy(values, newValues, length);
keys = newKeys;
values = newValues;
count = length;
resetIndices();
}
/**
* Remove all entries.
*
@@ -149,6 +172,14 @@ public class StringDict {
}
private void resetIndices() {
indices = new HashMap<String, Integer>(count);
for (int i = 0; i < count; i++) {
indices.put(keys[i], i);
}
}
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
@@ -515,10 +546,7 @@ public class StringDict {
s.run();
// Set the indices after sort/swaps (performance fix 160411)
indices = new HashMap<String, Integer>();
for (int i = 0; i < count; i++) {
indices.put(keys[i], i);
}
resetIndices();
}