From 59b9cd573ab2ca675fc26d4ec3f3c58c6a795cfe Mon Sep 17 00:00:00 2001 From: Ben Fry Date: Mon, 29 Apr 2013 09:45:30 -0400 Subject: [PATCH] fixing sort() methods so they are actually, you know, accurate... also ironing out Table bits --- core/src/processing/data/FloatDict.java | 14 +- core/src/processing/data/FloatList.java | 23 ++- core/src/processing/data/IntDict.java | 14 +- core/src/processing/data/IntList.java | 23 ++- core/src/processing/data/StringDict.java | 16 ++- core/src/processing/data/StringList.java | 37 +++-- core/src/processing/data/Table.java | 173 ++++++++++++++++------- 7 files changed, 225 insertions(+), 75 deletions(-) diff --git a/core/src/processing/data/FloatDict.java b/core/src/processing/data/FloatDict.java index 5167cf35c..9b5128118 100644 --- a/core/src/processing/data/FloatDict.java +++ b/core/src/processing/data/FloatDict.java @@ -63,6 +63,16 @@ public class FloatDict { } + public FloatDict(String[] keys, float[] values) { + if (keys.length != values.length) { + throw new IllegalArgumentException("key and value arrays must be the same length"); + } + this.keys = keys; + this.values = values; + count = keys.length; + } + + public int size() { return count; } @@ -485,7 +495,7 @@ public class FloatDict { diff = keys[a].compareToIgnoreCase(keys[b]); } } - return reverse ? diff : -diff; + return reverse ? -diff : diff; } @Override @@ -532,7 +542,7 @@ public class FloatDict { @Override public String toString() { StringBuilder sb = new StringBuilder(); - sb.append(getClass().getName() + " size= " + size() + " { "); + sb.append(getClass().getSimpleName() + " size=" + size() + " { "); for (int i = 0; i < size(); i++) { if (i != 0) { sb.append(", "); diff --git a/core/src/processing/data/FloatList.java b/core/src/processing/data/FloatList.java index d04b1f1f3..cd6205e3e 100644 --- a/core/src/processing/data/FloatList.java +++ b/core/src/processing/data/FloatList.java @@ -467,14 +467,14 @@ public class FloatList implements Iterable { @Override public float compare(int a, int b) { - return data[a] - data[b]; + return data[b] - data[a]; } @Override public void swap(int a, int b) { - int temp = a; - a = b; - b = temp; + float temp = data[a]; + data[a] = data[b]; + data[b] = temp; } }.run(); } @@ -605,4 +605,19 @@ public class FloatList implements Iterable { System.arraycopy(data, 0, array, 0, count); return array; } + + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName() + " size=" + size() + " [ "); + for (int i = 0; i < size(); i++) { + if (i != 0) { + sb.append(", "); + } + sb.append(i + ": " + data[i]); + } + sb.append(" ]"); + return sb.toString(); + } } \ No newline at end of file diff --git a/core/src/processing/data/IntDict.java b/core/src/processing/data/IntDict.java index dfa079980..64e15452d 100644 --- a/core/src/processing/data/IntDict.java +++ b/core/src/processing/data/IntDict.java @@ -86,6 +86,16 @@ public class IntDict { } + public IntDict(String[] keys, int[] values) { + if (keys.length != values.length) { + throw new IllegalArgumentException("key and value arrays must be the same length"); + } + this.keys = keys; + this.values = values; + count = keys.length; + } + + public int size() { return count; } @@ -375,7 +385,7 @@ public class IntDict { diff = keys[a].compareToIgnoreCase(keys[b]); } } - return reverse ? diff : -diff; + return reverse ? -diff : diff; } @Override @@ -414,7 +424,7 @@ public class IntDict { @Override public String toString() { StringBuilder sb = new StringBuilder(); - sb.append(getClass().getName() + " size= " + size() + " { "); + sb.append(getClass().getSimpleName() + " size=" + size() + " { "); for (int i = 0; i < size(); i++) { if (i != 0) { sb.append(", "); diff --git a/core/src/processing/data/IntList.java b/core/src/processing/data/IntList.java index e0f454452..21e8217ff 100644 --- a/core/src/processing/data/IntList.java +++ b/core/src/processing/data/IntList.java @@ -379,14 +379,14 @@ public class IntList implements Iterable { @Override public float compare(int a, int b) { - return data[a] - data[b]; + return data[b] - data[a]; } @Override public void swap(int a, int b) { - int temp = a; - a = b; - b = temp; + int temp = data[a]; + data[a] = data[b]; + data[b] = temp; } }.run(); } @@ -556,4 +556,19 @@ public class IntList implements Iterable { // } // return outgoing; // } + + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName() + " size=" + size() + " [ "); + for (int i = 0; i < size(); i++) { + if (i != 0) { + sb.append(", "); + } + sb.append(i + ": " + data[i]); + } + sb.append(" ]"); + return sb.toString(); + } } \ No newline at end of file diff --git a/core/src/processing/data/StringDict.java b/core/src/processing/data/StringDict.java index a07bbb3fb..5f591915e 100644 --- a/core/src/processing/data/StringDict.java +++ b/core/src/processing/data/StringDict.java @@ -61,6 +61,16 @@ public class StringDict { } + public StringDict(String[] keys, String[] values) { + if (keys.length != values.length) { + throw new IllegalArgumentException("key and value arrays must be the same length"); + } + this.keys = keys; + this.values = values; + count = keys.length; + } + + public int size() { return count; } @@ -312,7 +322,7 @@ public class StringDict { diff = keys[a].compareToIgnoreCase(keys[b]); } } - return reverse ? diff : -diff; + return reverse ? -diff : diff; } @Override @@ -351,12 +361,12 @@ public class StringDict { @Override public String toString() { StringBuilder sb = new StringBuilder(); - sb.append(getClass().getName() + " size= " + size() + " { "); + sb.append(getClass().getSimpleName() + " size=" + size() + " { "); for (int i = 0; i < size(); i++) { if (i != 0) { sb.append(", "); } - sb.append("\"" + keys[i] + "\": " + values[i]); + sb.append("\"" + keys[i] + "\": \"" + values[i] + "\""); } sb.append(" }"); return sb.toString(); diff --git a/core/src/processing/data/StringList.java b/core/src/processing/data/StringList.java index 6d761d2dc..ed7ed2968 100644 --- a/core/src/processing/data/StringList.java +++ b/core/src/processing/data/StringList.java @@ -464,9 +464,9 @@ public class StringList implements Iterable { @Override public void swap(int a, int b) { - int temp = a; - a = b; - b = temp; + String temp = data[a]; + data[a] = data[b]; + data[b] = temp; } }.run(); } @@ -599,11 +599,9 @@ public class StringList implements Iterable { } - /** Remove all non-unique entries. */ - public void unique() { - IntDict cheat = getTally(); - data = cheat.keyArray(); - count = cheat.size(); + /** Get a list of all unique entries. */ + public String[] getUnique() { + return getTally().keyArray(); } @@ -625,4 +623,27 @@ public class StringList implements Iterable { } return outgoing; } + + +// public void println() { +// for (int i = 0; i < count; i++) { +// System.out.println("[" + i + "] " + data[i]); +// } +// System.out.flush(); +// } + + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(getClass().getSimpleName() + " size=" + size() + " [ "); + for (int i = 0; i < size(); i++) { + if (i != 0) { + sb.append(", "); + } + sb.append(i + ": \"" + data[i] + "\""); + } + sb.append(" ]"); + return sb.toString(); + } } \ No newline at end of file diff --git a/core/src/processing/data/Table.java b/core/src/processing/data/Table.java index f8e229f64..f36b29d22 100644 --- a/core/src/processing/data/Table.java +++ b/core/src/processing/data/Table.java @@ -3044,62 +3044,131 @@ public class Table { // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . - // replaced with StringList.fromUnique(String[] list) -// public String[] listUnique(String column) { -// return listUnique(getColumnIndex(column)); -// } + // TODO maybe these aren't needed. better to use getStringList().getUnique()? - -// IntHash found = IntHash.fromTally(getStringColumn(column)); -// return found.keyArray(); - - // replaced with StringList.fromUnique(String[] list) -// public String[] listUnique(int column) { -// HashMapSucks found = new HashMapSucks(); -// -// for (int row = 0; row < getRowCount(); row++) { -// found.check(getString(row, column)); -// } -// String[] outgoing = new String[found.size()]; -// found.keySet().toArray(outgoing); -// return outgoing; -// } - - - // replaced with StringDict.fromTally(String[] list) -// public HashMap tallyUnique(String columnName) { -// return tallyUnique(getColumnIndex(columnName)); -// } - - - // replaced with StringDict.fromTally(String[] list) -// public HashMap tallyUnique(int column) { -// HashMapSucks outgoing = new HashMapSucks(); -// for (int row = 0; row < rowCount; row++) { -// String entry = getString(row, column); -// if (entry != null) { -// outgoing.increment(entry); -// } -// } -// return outgoing; -// } - - - /** - * Return an object that maps the String values in one column back to the - * row from which they came. For instance, if the "name" of each row is - * found in the first column, getColumnRowLookup(0) would return an object - * that would map each name back to its row. - */ - protected HashMap getRowLookup(int col) { - HashMap outgoing = new HashMap(); - for (int row = 0; row < getRowCount(); row++) { - outgoing.put(getString(row, col), row); - } - return outgoing; + public String[] getUnique(String columnName) { + return getUnique(getColumnIndex(columnName)); } + public String[] getUnique(int column) { + StringList list = new StringList(getStringColumn(column)); + return list.getUnique(); + } + + + public IntDict getTally(String columnName) { + return getTally(getColumnIndex(columnName)); + } + + + public IntDict getTally(int column) { + StringList list = new StringList(getStringColumn(column)); + return list.getTally(); + } + + + public IntDict getOrder(String columnName) { + return getOrder(getColumnIndex(columnName)); + } + + + public IntDict getOrder(int column) { + StringList list = new StringList(getStringColumn(column)); + return list.getOrder(); + } + + + // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . + + + public IntList getIntList(String columnName) { + return new IntList(getIntColumn(columnName)); + } + + + public IntList getIntList(int column) { + return new IntList(getIntColumn(column)); + } + + + public FloatList getFloatList(String columnName) { + return new FloatList(getFloatColumn(columnName)); + } + + + public FloatList getFloatList(int column) { + return new FloatList(getFloatColumn(column)); + } + + + public StringList getStringList(String columnName) { + return new StringList(getStringColumn(columnName)); + } + + + public StringList getStringList(int column) { + return new StringList(getStringColumn(column)); + } + + + // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . + + + public IntDict getIntDict(String keyColumnName, String valueColumnName) { + return new IntDict(getStringColumn(keyColumnName), + getIntColumn(valueColumnName)); + } + + + public IntDict getIntDict(int keyColumn, int valueColumn) { + return new IntDict(getStringColumn(keyColumn), + getIntColumn(valueColumn)); + } + + + public FloatDict getFloatDict(String keyColumnName, String valueColumnName) { + return new FloatDict(getStringColumn(keyColumnName), + getFloatColumn(valueColumnName)); + } + + + public FloatDict getFloatDict(int keyColumn, int valueColumn) { + return new FloatDict(getStringColumn(keyColumn), + getFloatColumn(valueColumn)); + } + + + public StringDict getStringDict(String keyColumnName, String valueColumnName) { + return new StringDict(getStringColumn(keyColumnName), + getStringColumn(valueColumnName)); + } + + + public StringDict getStringDict(int keyColumn, int valueColumn) { + return new StringDict(getStringColumn(keyColumn), + getStringColumn(valueColumn)); + } + + + // . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . + + +// /** +// * Return an object that maps the String values in one column back to the +// * row from which they came. For instance, if the "name" of each row is +// * found in the first column, getColumnRowLookup(0) would return an object +// * that would map each name back to its row. +// */ +// protected HashMap getRowLookup(int col) { +// HashMap outgoing = new HashMap(); +// for (int row = 0; row < getRowCount(); row++) { +// outgoing.put(getString(row, col), row); +// } +// return outgoing; +// } + + // incomplete, basically this is silly to write all this repetitive code when // it can be implemented in ~3 lines of code... // /**