@@ -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