diff --git a/core/src/processing/data/FloatHash.java b/core/src/processing/data/FloatHash.java index 644ced16c..96c5c0c85 100644 --- a/core/src/processing/data/FloatHash.java +++ b/core/src/processing/data/FloatHash.java @@ -68,6 +68,12 @@ public class FloatHash { } + /** Remove all entries. */ + public void clear() { + count = 0; + } + + public String key(int index) { return keys[index]; } diff --git a/core/src/processing/data/FloatList.java b/core/src/processing/data/FloatList.java index 597388b6e..43697b173 100644 --- a/core/src/processing/data/FloatList.java +++ b/core/src/processing/data/FloatList.java @@ -170,7 +170,7 @@ public class FloatList { */ - public void crop() { + private void crop() { if (count != data.length) { data = PApplet.subset(data, 0, count); } diff --git a/core/src/processing/data/IntHash.java b/core/src/processing/data/IntHash.java index 6c098fff8..34d5991e4 100644 --- a/core/src/processing/data/IntHash.java +++ b/core/src/processing/data/IntHash.java @@ -91,12 +91,18 @@ public class IntHash { } + /** Remove all entries. */ + public void clear() { + count = 0; + } + + public String key(int index) { return keys[index]; } - protected void crop() { + private void crop() { if (count != keys.length) { keys = PApplet.subset(keys, 0, count); values = PApplet.subset(values, 0, count); @@ -104,38 +110,39 @@ public class IntHash { } -// /** -// * Return the internal array being used to store the keys. Allocated but -// * unused entries will be removed. This array should not be modified. -// */ -// public String[] keys() { -// crop(); -// return keys; -// } + /** + * Return the internal array being used to store the keys. Allocated but + * unused entries will be removed. This array should not be modified. + */ + public String[] keys() { + crop(); + return keys; + } - public Iterable keys() { - return new Iterable() { +// public Iterable keys() { +// return new Iterable() { +// +// @Override +// public Iterator iterator() { + public Iterator keyIterator() { + return new Iterator() { + int index = -1; - @Override - public Iterator iterator() { - return new Iterator() { - int index = -1; + public void remove() { + removeIndex(index); + } - public void remove() { - removeIndex(index); - } + public String next() { + return key(++index); + } - public String next() { - return key(++index); - } - - public boolean hasNext() { - return index+1 < size(); - } - }; + public boolean hasNext() { + return index+1 < size(); } }; +// } +// }; } diff --git a/core/src/processing/data/IntList.java b/core/src/processing/data/IntList.java index c72b8a39a..cba31c852 100644 --- a/core/src/processing/data/IntList.java +++ b/core/src/processing/data/IntList.java @@ -1,13 +1,21 @@ package processing.data; import java.util.Arrays; +import java.util.Iterator; import java.util.Random; import processing.core.PApplet; +// splice, slice, subset, concat, reverse + +// trim, join for String versions + + /** - * Helper class for a list of ints. + * Helper class for a list of ints. By design (for efficiency), functions like + * sort() and shuffle() always act on the list itself. To get a sorted copy, + * use list.copy().sort(). */ public class IntList { protected int count; @@ -48,14 +56,13 @@ public class IntList { public void resize(int length) { - if (length > count) { -// // make sure the entries in data[] that are past 'count' are set to zero -// for (int i = count; i < data.length; i++) { -// data[i] = 0; -// } -// data = PApplet.expand(data, length); + if (length > data.length) { int[] temp = new int[length]; System.arraycopy(data, 0, temp, 0, count); + data = temp; + + } else if (length > count) { + Arrays.fill(data, count, length, 0); } count = length; } @@ -108,7 +115,7 @@ public class IntList { } - /** remove the first instance of a particular value */ + /** Remove the first instance of a particular value */ public boolean removeValue(int value) { int index = index(value); if (index != -1) { @@ -119,9 +126,21 @@ public class IntList { } - /** - * Add a new entry to the list. - */ + /** Remove all instances of a particular value */ + public boolean removeValues(int value) { + int ii = 0; + for (int i = 0; i < count; i++) { + if (data[i] != value) { + data[ii++] = data[i]; + } + } + boolean changed = count == ii; + count = ii; + return changed; + } + + + /** Add a new entry to the list. */ public void append(int value) { if (count == data.length) { data = PApplet.expand(data); @@ -130,6 +149,20 @@ public class IntList { } + public void append(int[] values) { + for (int v : values) { + append(v); + } + } + + + public void append(IntList list) { + for (int v : list.values()) { // will concat the list... + append(v); + } + } + + // public void insert(int index, int value) { // if (index+1 > count) { // if (index+1 < data.length) { @@ -162,8 +195,11 @@ public class IntList { // same as splice public void insert(int index, int[] values) { - if (index < 0 || index >= count) { - throw new IllegalArgumentException("Index " + index + " is outside this list's size"); + if (index < 0) { + throw new IllegalArgumentException("insert() index cannot be negative: it was " + index); + } + if (index >= values.length) { + throw new IllegalArgumentException("insert() index " + index + " is past the end of this list"); } int[] temp = new int[count + values.length]; @@ -183,6 +219,13 @@ public class IntList { // count = index + values.length; // } data = temp; + } + + + public void insert(int index, IntList list) { + insert(index, list.values()); + } + // below are aborted attempts at more optimized versions of the code // that are harder to read and debug... @@ -225,9 +268,9 @@ public class IntList { // data[index] = value; // count++; // } - } + /** Return the first index of a particular value. */ public int index(int what) { /* if (indexCache != null) { @@ -257,7 +300,7 @@ public class IntList { // } - public boolean contains(int value) { + public boolean hasValue(int value) { // if (indexCache == null) { // cacheIndices(); // } @@ -271,22 +314,17 @@ public class IntList { } - public void inc(int index) { + public void increment(int index) { data[index]++; } - public void inc(int index, int amount) { + public void add(int index, int amount) { data[index] += amount; } - public void dec(int index) { - data[index]--; - } - - - public void dec(int index, int amount) { + public void sub(int index, int amount) { data[index] -= amount; } @@ -301,34 +339,6 @@ public class IntList { } -// public void inc(int amt) { -// for (int i = 0; i < count; i++) { -// data[i] += amt; -// } -// } -// -// -// public void dec(int amt) { -// for (int i = 0; i < count; i++) { -// data[i] -= amt; -// } -// } -// -// -// public void mul(int amt) { -// for (int i = 0; i < count; i++) { -// data[i] *= amt; -// } -// } -// -// -// public void div(int amt) { -// for (int i = 0; i < count; i++) { -// data[i] /= amt; -// } -// } - - public int min() { if (count == 0) { throw new ArrayIndexOutOfBoundsException("Cannot use min() on IntList of length 0."); @@ -353,14 +363,14 @@ public class IntList { } - /** Sorts the array in place. To get a sorted copy, use list.copy().sort(). */ + /** Sorts the array in place. */ public void sort() { Arrays.sort(data, 0, count); } /** reverse sort, orders values from highest to lowest */ - public void rsort() { + public void sortReverse() { new Sort() { @Override public int size() { @@ -400,25 +410,17 @@ public class IntList { } - public void concat(int[] values) { - - } - - - public void concat(IntList list) { - - } - - public void reverse() { - + int ii = count - 1; + for (int i = 0; i < count/2; i++) { + int t = data[i]; + data[i] = data[ii]; + data[ii] = t; + --ii; + } } - // splice, slice, subset, concat, reverse - - // trim, join for String versions - /** * Randomize the order of the list elements. Note that this does not * obey the randomSeed() function in PApplet. @@ -460,8 +462,9 @@ public class IntList { /** - * Returns the actual array being used to store the data. - * Suitable for iterating, but do not modify. + * Returns the actual array being used to store the data. Suitable for + * iterating with a for() loop, but modifying the list could cause terrible + * things to happen. */ public int[] values() { crop(); @@ -469,14 +472,31 @@ public class IntList { } + public Iterator valueIterator() { + return new Iterator() { + int index = -1; + + public void remove() { + IntList.this.remove(index); + } + + public Integer next() { + return data[++index]; + } + + public boolean hasNext() { + return index+1 < count; + } + }; + } + + /** * Create a new array with a copy of all the values. * @return an array sized by the length of the list with each of the values. */ - public int[] getArray() { - int[] outgoing = new int[count]; - System.arraycopy(data, 0, outgoing, 0, count); - return outgoing; + public int[] valueArray() { + return valueArray(null); } @@ -484,7 +504,54 @@ public class IntList { * Copy as many values as possible into the specified array. * @param array */ - public void getArray(int[] array) { - System.arraycopy(data, 0, array, 0, Math.min(count, array.length)); + public int[] valueArray(int[] array) { + if (array == null || array.length != count) { + array = new int[count]; + } + System.arraycopy(data, 0, array, 0, count); + return array; + } + + + 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; } } \ No newline at end of file diff --git a/core/src/processing/data/JSONObject.java b/core/src/processing/data/JSONObject.java index 464c3ea07..f6eece65b 100644 --- a/core/src/processing/data/JSONObject.java +++ b/core/src/processing/data/JSONObject.java @@ -36,7 +36,6 @@ SOFTWARE. import java.io.File; import java.io.IOException; -import java.io.OutputStream; import java.io.PrintWriter; import java.io.StringWriter; import java.io.Writer; @@ -741,7 +740,7 @@ public class JSONObject { * * @return An iterator of the keys. */ - public Iterator keys() { + public Iterator keyIterator() { // return this.keySet().iterator(); return map.keySet().iterator(); } @@ -752,7 +751,7 @@ public class JSONObject { * * @return A keySet. */ - public Set keySet() { + public Set keys() { return this.map.keySet(); } @@ -1419,17 +1418,17 @@ public class JSONObject { // } - protected boolean save(OutputStream output) { - return save(PApplet.createWriter(output)); - } +// protected boolean save(OutputStream output) { +// return save(PApplet.createWriter(output)); +// } public boolean save(File file, String options) { - return save(PApplet.createWriter(file)); + return write(PApplet.createWriter(file)); } - public boolean save(PrintWriter output) { + public boolean write(PrintWriter output) { output.print(format(2)); output.flush(); return true; @@ -1580,18 +1579,18 @@ public class JSONObject { } - /** - * Write the contents of the JSONObject as JSON text to a writer. - * For compactness, no whitespace is added. - *

- * Warning: This method assumes that the data structure is acyclical. - * - * @return The writer. - * @throws JSONException - */ - protected Writer write(Writer writer) { - return this.write(writer, 0, 0); - } +// /** +// * Write the contents of the JSONObject as JSON text to a writer. +// * For compactness, no whitespace is added. +// *

+// * Warning: This method assumes that the data structure is acyclical. +// * +// * @return The writer. +// * @throws JSONException +// */ +// protected Writer write(Writer writer) { +// return this.write(writer, 0, 0); +// } static final Writer writeValue(Writer writer, Object value, @@ -1649,7 +1648,7 @@ public class JSONObject { try { boolean commanate = false; final int length = this.size(); - Iterator keys = this.keys(); + Iterator keys = this.keyIterator(); writer.write('{'); int actualFactor = (indentFactor == -1) ? 0 : indentFactor; diff --git a/core/src/processing/data/StringHash.java b/core/src/processing/data/StringHash.java index 630105bdd..bfffd959f 100644 --- a/core/src/processing/data/StringHash.java +++ b/core/src/processing/data/StringHash.java @@ -66,6 +66,12 @@ public class StringHash { } + /** Remove all entries. */ + public void clear() { + count = 0; + } + + public String key(int index) { return keys[index]; } diff --git a/core/todo.txt b/core/todo.txt index b07724256..b1943275e 100644 --- a/core/todo.txt +++ b/core/todo.txt @@ -48,6 +48,32 @@ _ using Iterable for rows(), keys(), etc _ generally more efficient for Table, but slower for FloatHash and IntHash _ could use an int array instead, but a bit hokey in places +replace? (especially for NaN) +match? find? on StringList? + +Iterable, Iterator, or [] returned for keys(), rows(), etc. + list, dict, json, table are each more efficient at different things + keys(), rows(), etc. should return something Iterable + whether that means an array or an actual Iterator or even a Set + that way we can do what's most efficient + add nodes() (no) or children() to XML? + then we add keyIterator() and others to handle the other case (deletion) +means that JSON.keys() -> JSON.keyIterator(), JSON.keySet() -> JSON.keys() + +what should they all return? + remove -> true/false based on whether it found something? + remove all -> the number removed? + +with removeValue(), add removeValues() for all values that match? + also support NaN here + +hasAttribute in XML, containsKey in java's Map, hasKey in JSON + hasChildren() is another precedent + -> hasKey/hasValue is better; fewest changes and most descriptive + -> contains() is nicer, but containsKey() is weird, often not 'containing' + +listAttributes() in XML is like array from keys() etc in our data classes + List: remove(), append(), index(), etc all use values removeIndex(index) is the other otherwise remove() would be the only one that dealt with indices