From e47c20b6b1f9b416f1c74db22a4e700913a74db5 Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Wed, 3 Aug 2022 21:31:33 -0400 Subject: [PATCH] remaining cleanup and array() -> toArray() changes --- core/src/processing/data/IntList.java | 179 +++++------------------ core/src/processing/data/LongList.java | 4 +- core/src/processing/data/StringList.java | 73 ++++----- 3 files changed, 65 insertions(+), 191 deletions(-) diff --git a/core/src/processing/data/IntList.java b/core/src/processing/data/IntList.java index 7b15b9da5..6ef0b3f8b 100644 --- a/core/src/processing/data/IntList.java +++ b/core/src/processing/data/IntList.java @@ -51,13 +51,13 @@ public class IntList implements Iterable { /** * Construct an IntList from an iterable pile of objects. - * For instance, a float array, an array of strings, who knows). - * Un-parseable or null values will be set to 0. + * For instance, a float array, an array of strings, who knows. + * Un-parsable or null values will be set to 0. * @nowebref */ - public IntList(Iterable iter) { + public IntList(Iterable iterable) { this(10); - for (Object o : iter) { + for (Object o : iterable) { if (o == null) { append(0); // missing value default } else if (o instanceof Number) { @@ -72,7 +72,7 @@ public class IntList implements Iterable { /** * Construct an IntList from a random pile of objects. - * Un-parseable or null values will be set to zero. + * Un-parsable or null values will be set to zero. */ public IntList(Object... items) { final int missingValue = 0; // nuts, can't be last/final/second arg @@ -233,6 +233,7 @@ public class IntList implements Iterable { // Remove the first instance of a particular value, // and return the index at which it was found. + @SuppressWarnings("unused") public int removeValue(int value) { int index = index(value); if (index != -1) { @@ -245,6 +246,7 @@ public class IntList implements Iterable { // Remove all instances of a particular value, // and return the number of values found and removed + @SuppressWarnings("unused") public int removeValues(int value) { int ii = 0; for (int i = 0; i < count; i++) { @@ -287,6 +289,7 @@ public class IntList implements Iterable { /** Add this value, but only if it's not already in the list. */ + @SuppressWarnings("unused") public void appendUnique(int value) { if (!hasValue(value)) { append(value); @@ -294,36 +297,6 @@ public class IntList implements Iterable { } -// public void insert(int index, int value) { -// if (index+1 > count) { -// if (index+1 < data.length) { -// } -// } -// if (index >= data.length) { -// data = PApplet.expand(data, index+1); -// data[index] = value; -// count = index+1; -// -// } else if (count == data.length) { -// if (index >= count) { -// //int[] temp = new int[count << 1]; -// System.arraycopy(data, 0, temp, 0, index); -// temp[index] = value; -// System.arraycopy(data, index, temp, index+1, count - index); -// data = temp; -// -// } else { -// // data[] has room to grow -// // for() loop believed to be faster than System.arraycopy over itself -// for (int i = count; i > index; --i) { -// data[i] = data[i-1]; -// } -// data[index] = value; -// count++; -// } -// } - - public void insert(int index, int value) { insert(index, new int[] { value }); } @@ -408,15 +381,6 @@ public class IntList implements Iterable { /** Return the first index of a particular value. */ public int index(int what) { - /* - if (indexCache != null) { - try { - return indexCache.get(what); - } catch (Exception e) { // not there - return -1; - } - } - */ for (int i = 0; i < count; i++) { if (data[i] == what) { return i; @@ -426,15 +390,6 @@ public class IntList implements Iterable { } - // !!! TODO this is not yet correct, because it's not being reset when - // the rest of the entries are changed -// protected void cacheIndices() { -// indexCache = new HashMap(); -// for (int i = 0; i < count; i++) { -// indexCache.put(data[i], i); -// } -// } - /** * Check if a number is a part of the data structure. * @@ -442,10 +397,6 @@ public class IntList implements Iterable { * @webBrief Check if a number is a part of the list */ public boolean hasValue(int value) { -// if (indexCache == null) { -// cacheIndices(); -// } -// return index(what) != -1; for (int i = 0; i < count; i++) { if (data[i] == value) { return true; @@ -454,6 +405,7 @@ public class IntList implements Iterable { return false; } + /** * Add one to a value. * @@ -560,6 +512,7 @@ public class IntList implements Iterable { // returns the index of the minimum value. // if there are ties, it returns the first one found. + @SuppressWarnings("unused") public int minIndex() { checkMinMax("minIndex"); int value = data[0]; @@ -667,23 +620,6 @@ public class IntList implements Iterable { } - // use insert() -// public void splice(int index, int value) { -// } - - -// public void subset(int start) { -// subset(start, count - start); -// } -// -// -// public void subset(int start, int num) { -// for (int i = 0; i < num; i++) { -// data[i] = data[i+start]; -// } -// count = num; -// } - /** * Reverse the order of the list. * @@ -707,6 +643,7 @@ public class IntList implements Iterable { * @webref intlist:method * @webBrief Randomize the order of the list elements */ + @SuppressWarnings("unused") public void shuffle() { Random r = new Random(); int num = count; @@ -724,6 +661,7 @@ public class IntList implements Iterable { * Randomize the list order using the random() function from the specified * sketch, allowing shuffle() to use its current randomSeed() setting. */ + @SuppressWarnings("unused") public void shuffle(PApplet sketch) { int num = count; while (num > 1) { @@ -773,8 +711,7 @@ public class IntList implements Iterable { @Override public Iterator iterator() { -// public Iterator valueIterator() { - return new Iterator() { + return new Iterator<>() { int index = -1; public void remove() { @@ -793,6 +730,12 @@ public class IntList implements Iterable { } + @Deprecated + public int[] array() { + return toArray(); + } + + /** * Create a new array with a copy of all the values. * @@ -800,17 +743,22 @@ public class IntList implements Iterable { * @webref intlist:method * @webBrief Create a new array with a copy of all the values */ - public int[] array() { - return array(null); + public int[] toArray() { + return toArray(null); + } + + + @Deprecated + public int[] array(int[] array) { + return toArray(array); } /** - * Copy values into the specified array. If the specified array is null or - * not the same size, a new array will be allocated. - * @param array + * Copy values into the specified array. If the specified array is + * null or not the same size, a new array will be allocated. */ - public int[] array(int[] array) { + public int[] toArray(int[] array) { if (array == null || array.length != count) { array = new int[count]; } @@ -819,55 +767,13 @@ public class IntList implements Iterable { } -// public int[] toIntArray() { -// int[] outgoing = new int[count]; -// for (int i = 0; i < count; i++) { -// outgoing[i] = (int) data[i]; -// } -// return outgoing; -// } - - -// public long[] toLongArray() { -// long[] outgoing = new long[count]; -// for (int i = 0; i < count; i++) { -// outgoing[i] = (long) data[i]; -// } -// return outgoing; -// } - - -// public float[] toFloatArray() { -// float[] outgoing = new float[count]; -// System.arraycopy(data, 0, outgoing, 0, count); -// return outgoing; -// } - - -// public double[] toDoubleArray() { -// double[] outgoing = new double[count]; -// for (int i = 0; i < count; i++) { -// outgoing[i] = data[i]; -// } -// return outgoing; -// } - - -// public String[] toStringArray() { -// String[] outgoing = new String[count]; -// for (int i = 0; i < count; i++) { -// outgoing[i] = String.valueOf(data[i]); -// } -// return outgoing; -// } - - /** - * Returns a normalized version of this array. Called getPercent() for - * consistency with the Dict classes. It's a getter method because it needs - * to returns a new list (because IntList/Dict can't do percentages or - * normalization in place on int values). + * Returns a normalized version of this array. Called getPercent() + * for consistency with the Dict classes. It's a getter method because + * it needs to return a new list (because IntList/Dict can't do + * percentages or normalization in place on int values). */ + @SuppressWarnings("unused") public FloatList getPercent() { double sum = 0; for (float value : array()) { @@ -882,24 +788,13 @@ public class IntList implements Iterable { } -// /** -// * Count the number of times each entry is found in this list. -// * Converts each entry to a String so it can be used as a key. -// */ -// public IntDict getTally() { -// IntDict outgoing = new IntDict(); -// for (int i = 0; i < count; i++) { -// outgoing.increment(String.valueOf(data[i])); -// } -// return outgoing; -// } - - + @SuppressWarnings("unused") public IntList getSubset(int start) { return getSubset(start, count - start); } + @SuppressWarnings("unused") public IntList getSubset(int start, int num) { int[] subset = new int[num]; System.arraycopy(data, start, subset, 0, num); diff --git a/core/src/processing/data/LongList.java b/core/src/processing/data/LongList.java index 068b47b81..f78edf9e5 100644 --- a/core/src/processing/data/LongList.java +++ b/core/src/processing/data/LongList.java @@ -61,9 +61,9 @@ public class LongList implements Iterable { * @nowebref */ @SuppressWarnings("unused") - public LongList(Iterable iter) { + public LongList(Iterable iterable) { this(10); - for (Object o : iter) { + for (Object o : iterable) { if (o == null) { append(0); // missing value default } else if (o instanceof Number) { diff --git a/core/src/processing/data/StringList.java b/core/src/processing/data/StringList.java index e83f866bc..f7b6e6b4d 100644 --- a/core/src/processing/data/StringList.java +++ b/core/src/processing/data/StringList.java @@ -57,9 +57,6 @@ public class StringList implements Iterable { data = new String[count]; int index = 0; for (Object o : items) { -// // Not gonna go with null values staying that way because perhaps -// // the most common case here is to immediately call join() or similar. -// data[index++] = String.valueOf(o); // Keep null values null (because join() will make non-null anyway) if (o != null) { // leave null values null data[index] = o.toString(); @@ -75,9 +72,9 @@ public class StringList implements Iterable { * * @nowebref */ - public StringList(Iterable iter) { + public StringList(Iterable iterable) { this(10); - for (String s : iter) { + for (String s : iterable) { append(s); } } @@ -114,7 +111,7 @@ public class StringList implements Iterable { data = temp; } else if (length > count) { - Arrays.fill(data, count, length, 0); + Arrays.fill(data, count, length, null); } count = length; } @@ -192,11 +189,6 @@ public class StringList implements Iterable { throw new ArrayIndexOutOfBoundsException(index); } String entry = data[index]; -// int[] outgoing = new int[count - 1]; -// System.arraycopy(data, 0, outgoing, 0, index); -// count--; -// System.arraycopy(data, index + 1, outgoing, 0, count - index); -// data = outgoing; for (int i = index; i < count-1; i++) { data[i] = data[i+1]; } @@ -206,6 +198,7 @@ public class StringList implements Iterable { // Remove the first instance of a particular value and return its index. + @SuppressWarnings("unused") public int removeValue(String value) { if (value == null) { for (int i = 0; i < count; i++) { @@ -226,6 +219,7 @@ public class StringList implements Iterable { // Remove all instances of a particular value and return the count removed. + @SuppressWarnings("unused") public int removeValues(String value) { int ii = 0; if (value == null) { @@ -248,6 +242,7 @@ public class StringList implements Iterable { // replace the first value that matches, return the index that was replaced + @SuppressWarnings("unused") public int replaceValue(String value, String newValue) { if (value == null) { for (int i = 0; i < count; i++) { @@ -269,6 +264,7 @@ public class StringList implements Iterable { // replace all values that match, return the count of those replaced + @SuppressWarnings("unused") public int replaceValues(String value, String newValue) { int changed = 0; if (value == null) { @@ -457,15 +453,6 @@ public class StringList implements Iterable { } - // !!! TODO this is not yet correct, because it's not being reset when - // the rest of the entries are changed -// protected void cacheIndices() { -// indexCache = new HashMap(); -// for (int i = 0; i < count; i++) { -// indexCache.put(data[i], i); -// } -// } - /** * Check if a value is a part of the list * @@ -536,23 +523,6 @@ public class StringList implements Iterable { } - // use insert() -// public void splice(int index, int value) { -// } - - -// public void subset(int start) { -// subset(start, count - start); -// } -// -// -// public void subset(int start, int num) { -// for (int i = 0; i < num; i++) { -// data[i] = data[i+start]; -// } -// count = num; -// } - /** * Reverse the order of the list * @@ -576,6 +546,7 @@ public class StringList implements Iterable { * @webref stringlist:method * @webBrief Randomize the order of the list elements */ + @SuppressWarnings("unused") public void shuffle() { Random r = new Random(); int num = count; @@ -593,6 +564,7 @@ public class StringList implements Iterable { * Randomize the list order using the random() function from the specified * sketch, allowing shuffle() to use its current randomSeed() setting. */ + @SuppressWarnings("unused") public void shuffle(PApplet sketch) { int num = count; while (num > 1) { @@ -672,12 +644,7 @@ public class StringList implements Iterable { @Override public Iterator iterator() { -// return valueIterator(); -// } -// -// -// public Iterator valueIterator() { - return new Iterator() { + return new Iterator<>() { int index = -1; public void remove() { @@ -696,6 +663,12 @@ public class StringList implements Iterable { } + @Deprecated + public String[] array() { + return toArray(); + } + + /** * Create a new array with a copy of all the values. * @@ -703,17 +676,22 @@ public class StringList implements Iterable { * @webref stringlist:method * @webBrief Create a new array with a copy of all the values */ - public String[] array() { - return array(null); + public String[] toArray() { + return toArray(null); + } + + + @Deprecated + public String[] array(String[] array) { + return toArray(array); } /** * Copy values into the specified array. If the specified array is null or * not the same size, a new array will be allocated. - * @param array */ - public String[] array(String[] array) { + public String[] toArray(String[] array) { if (array == null || array.length != count) { array = new String[count]; } @@ -722,6 +700,7 @@ public class StringList implements Iterable { } + @SuppressWarnings("unused") public StringList getSubset(int start) { return getSubset(start, count - start); }