From 65b138de31987bba35dd709327b8d8f5624414e2 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Tue, 10 Oct 2017 20:57:27 -0400 Subject: [PATCH] added resize() to IntDict, FloatDict, StringDict --- core/src/processing/data/FloatDict.java | 36 +++++++++++++++++++++--- core/src/processing/data/IntDict.java | 36 +++++++++++++++++++++--- core/src/processing/data/StringDict.java | 36 +++++++++++++++++++++--- 3 files changed, 96 insertions(+), 12 deletions(-) diff --git a/core/src/processing/data/FloatDict.java b/core/src/processing/data/FloatDict.java index dc35f4b99..3ef440595 100644 --- a/core/src/processing/data/FloatDict.java +++ b/core/src/processing/data/FloatDict.java @@ -115,6 +115,29 @@ public class FloatDict { } + /** + * 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]; + float[] newValues = new float[length]; + PApplet.arrayCopy(keys, newKeys, length); + PApplet.arrayCopy(values, newValues, length); + keys = newKeys; + values = newValues; + count = length; + resetIndices(); + } + + /** * Remove all entries. * @@ -127,6 +150,14 @@ public class FloatDict { } + private void resetIndices() { + indices = new HashMap(count); + for (int i = 0; i < count; i++) { + indices.put(keys[i], i); + } + } + + // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . @@ -725,10 +756,7 @@ public class FloatDict { s.run(); // Set the indices after sort/swaps (performance fix 160411) - indices = new HashMap(); - for (int i = 0; i < count; i++) { - indices.put(keys[i], i); - } + resetIndices(); } diff --git a/core/src/processing/data/IntDict.java b/core/src/processing/data/IntDict.java index 6ef168b46..c3e684fab 100644 --- a/core/src/processing/data/IntDict.java +++ b/core/src/processing/data/IntDict.java @@ -116,6 +116,29 @@ public class IntDict { } + /** + * 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]; + int[] newValues = new int[length]; + PApplet.arrayCopy(keys, newKeys, length); + PApplet.arrayCopy(values, newValues, length); + keys = newKeys; + values = newValues; + count = length; + resetIndices(); + } + + /** * Remove all entries. * @@ -128,6 +151,14 @@ public class IntDict { } + private void resetIndices() { + indices = new HashMap(count); + for (int i = 0; i < count; i++) { + indices.put(keys[i], i); + } + } + + // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . @@ -694,10 +725,7 @@ public class IntDict { s.run(); // Set the indices after sort/swaps (performance fix 160411) - indices = new HashMap(); - for (int i = 0; i < count; i++) { - indices.put(keys[i], i); - } + resetIndices(); } diff --git a/core/src/processing/data/StringDict.java b/core/src/processing/data/StringDict.java index e34d4663f..6204ef523 100644 --- a/core/src/processing/data/StringDict.java +++ b/core/src/processing/data/StringDict.java @@ -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(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(); - for (int i = 0; i < count; i++) { - indices.put(keys[i], i); - } + resetIndices(); }