remaining cleanup and array() -> toArray() changes

This commit is contained in:
Ben Fry
2022-08-03 21:31:33 -04:00
parent 4f03a0b413
commit e47c20b6b1
3 changed files with 65 additions and 191 deletions
+37 -142
View File
@@ -51,13 +51,13 @@ public class IntList implements Iterable<Integer> {
/**
* 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<Object> iter) {
public IntList(Iterable<Object> 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<Integer> {
/**
* 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<Integer> {
// 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<Integer> {
// 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<Integer> {
/** 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<Integer> {
}
// 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<Integer> {
/** 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<Integer> {
}
// !!! 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<Integer, Integer>();
// 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<Integer> {
* @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<Integer> {
return false;
}
/**
* Add one to a value.
*
@@ -560,6 +512,7 @@ public class IntList implements Iterable<Integer> {
// 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<Integer> {
}
// 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<Integer> {
* @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<Integer> {
* 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<Integer> {
@Override
public Iterator<Integer> iterator() {
// public Iterator<Integer> valueIterator() {
return new Iterator<Integer>() {
return new Iterator<>() {
int index = -1;
public void remove() {
@@ -793,6 +730,12 @@ public class IntList implements Iterable<Integer> {
}
@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<Integer> {
* @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<Integer> {
}
// 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<Integer> {
}
// /**
// * 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);
+2 -2
View File
@@ -61,9 +61,9 @@ public class LongList implements Iterable<Long> {
* @nowebref
*/
@SuppressWarnings("unused")
public LongList(Iterable<Object> iter) {
public LongList(Iterable<Object> iterable) {
this(10);
for (Object o : iter) {
for (Object o : iterable) {
if (o == null) {
append(0); // missing value default
} else if (o instanceof Number) {
+26 -47
View File
@@ -57,9 +57,6 @@ public class StringList implements Iterable<String> {
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<String> {
*
* @nowebref
*/
public StringList(Iterable<String> iter) {
public StringList(Iterable<String> iterable) {
this(10);
for (String s : iter) {
for (String s : iterable) {
append(s);
}
}
@@ -114,7 +111,7 @@ public class StringList implements Iterable<String> {
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<String> {
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<String> {
// 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<String> {
// 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<String> {
// 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<String> {
// 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<String> {
}
// !!! 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<Integer, Integer>();
// 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<String> {
}
// 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<String> {
* @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<String> {
* 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<String> {
@Override
public Iterator<String> iterator() {
// return valueIterator();
// }
//
//
// public Iterator<String> valueIterator() {
return new Iterator<String>() {
return new Iterator<>() {
int index = -1;
public void remove() {
@@ -696,6 +663,12 @@ public class StringList implements Iterable<String> {
}
@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<String> {
* @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<String> {
}
@SuppressWarnings("unused")
public StringList getSubset(int start) {
return getSubset(start, count - start);
}