Skip to content

Commit af1bb52

Browse files
committed
added resize() to IntDict, FloatDict, StringDict
1 parent 7b0ed86 commit af1bb52

3 files changed

Lines changed: 96 additions & 12 deletions

File tree

core/src/processing/data/FloatDict.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,29 @@ public int size() {
115115
}
116116

117117

118+
/**
119+
* Resize the internal data, this can only be used to shrink the list.
120+
* Helpful for situations like sorting and then grabbing the top 50 entries.
121+
*/
122+
public void resize(int length) {
123+
if (length > count) {
124+
throw new IllegalArgumentException("resize() can only be used to shrink the dictionary");
125+
}
126+
if (length < 1) {
127+
throw new IllegalArgumentException("resize(" + length + ") is too small, use 1 or higher");
128+
}
129+
130+
String[] newKeys = new String[length];
131+
float[] newValues = new float[length];
132+
PApplet.arrayCopy(keys, newKeys, length);
133+
PApplet.arrayCopy(values, newValues, length);
134+
keys = newKeys;
135+
values = newValues;
136+
count = length;
137+
resetIndices();
138+
}
139+
140+
118141
/**
119142
* Remove all entries.
120143
*
@@ -127,6 +150,14 @@ public void clear() {
127150
}
128151

129152

153+
private void resetIndices() {
154+
indices = new HashMap<String, Integer>(count);
155+
for (int i = 0; i < count; i++) {
156+
indices.put(keys[i], i);
157+
}
158+
}
159+
160+
130161
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
131162

132163

@@ -725,10 +756,7 @@ public void swap(int a, int b) {
725756
s.run();
726757

727758
// Set the indices after sort/swaps (performance fix 160411)
728-
indices = new HashMap<String, Integer>();
729-
for (int i = 0; i < count; i++) {
730-
indices.put(keys[i], i);
731-
}
759+
resetIndices();
732760
}
733761

734762

core/src/processing/data/IntDict.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,29 @@ public int size() {
116116
}
117117

118118

119+
/**
120+
* Resize the internal data, this can only be used to shrink the list.
121+
* Helpful for situations like sorting and then grabbing the top 50 entries.
122+
*/
123+
public void resize(int length) {
124+
if (length > count) {
125+
throw new IllegalArgumentException("resize() can only be used to shrink the dictionary");
126+
}
127+
if (length < 1) {
128+
throw new IllegalArgumentException("resize(" + length + ") is too small, use 1 or higher");
129+
}
130+
131+
String[] newKeys = new String[length];
132+
int[] newValues = new int[length];
133+
PApplet.arrayCopy(keys, newKeys, length);
134+
PApplet.arrayCopy(values, newValues, length);
135+
keys = newKeys;
136+
values = newValues;
137+
count = length;
138+
resetIndices();
139+
}
140+
141+
119142
/**
120143
* Remove all entries.
121144
*
@@ -128,6 +151,14 @@ public void clear() {
128151
}
129152

130153

154+
private void resetIndices() {
155+
indices = new HashMap<String, Integer>(count);
156+
for (int i = 0; i < count; i++) {
157+
indices.put(keys[i], i);
158+
}
159+
}
160+
161+
131162
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
132163

133164

@@ -694,10 +725,7 @@ public void swap(int a, int b) {
694725
s.run();
695726

696727
// Set the indices after sort/swaps (performance fix 160411)
697-
indices = new HashMap<String, Integer>();
698-
for (int i = 0; i < count; i++) {
699-
indices.put(keys[i], i);
700-
}
728+
resetIndices();
701729
}
702730

703731

core/src/processing/data/StringDict.java

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,29 @@ public int size() {
137137
}
138138

139139

140+
/**
141+
* Resize the internal data, this can only be used to shrink the list.
142+
* Helpful for situations like sorting and then grabbing the top 50 entries.
143+
*/
144+
public void resize(int length) {
145+
if (length > count) {
146+
throw new IllegalArgumentException("resize() can only be used to shrink the dictionary");
147+
}
148+
if (length < 1) {
149+
throw new IllegalArgumentException("resize(" + length + ") is too small, use 1 or higher");
150+
}
151+
152+
String[] newKeys = new String[length];
153+
String[] newValues = new String[length];
154+
PApplet.arrayCopy(keys, newKeys, length);
155+
PApplet.arrayCopy(values, newValues, length);
156+
keys = newKeys;
157+
values = newValues;
158+
count = length;
159+
resetIndices();
160+
}
161+
162+
140163
/**
141164
* Remove all entries.
142165
*
@@ -149,6 +172,14 @@ public void clear() {
149172
}
150173

151174

175+
private void resetIndices() {
176+
indices = new HashMap<String, Integer>(count);
177+
for (int i = 0; i < count; i++) {
178+
indices.put(keys[i], i);
179+
}
180+
}
181+
182+
152183
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
153184

154185

@@ -515,10 +546,7 @@ public void swap(int a, int b) {
515546
s.run();
516547

517548
// Set the indices after sort/swaps (performance fix 160411)
518-
indices = new HashMap<String, Integer>();
519-
for (int i = 0; i < count; i++) {
520-
indices.put(keys[i], i);
521-
}
549+
resetIndices();
522550
}
523551

524552

0 commit comments

Comments
 (0)