fixing sort() methods so they are actually, you know, accurate... also ironing out Table bits

This commit is contained in:
Ben Fry
2013-04-29 09:45:30 -04:00
parent d7f2e5298f
commit 59b9cd573a
7 changed files with 225 additions and 75 deletions

View File

@@ -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(", ");

View File

@@ -467,14 +467,14 @@ public class FloatList implements Iterable<Float> {
@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<Float> {
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();
}
}

View File

@@ -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(", ");

View File

@@ -379,14 +379,14 @@ public class IntList implements Iterable<Integer> {
@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<Integer> {
// }
// 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();
}
}

View File

@@ -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();

View File

@@ -464,9 +464,9 @@ public class StringList implements Iterable<String> {
@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<String> {
}
/** 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<String> {
}
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();
}
}

View File

@@ -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<String,Integer> tallyUnique(String columnName) {
// return tallyUnique(getColumnIndex(columnName));
// }
// replaced with StringDict.fromTally(String[] list)
// public HashMap<String,Integer> 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<String,Integer> getRowLookup(int col) {
HashMap<String,Integer> outgoing = new HashMap<String, Integer>();
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<String,Integer> getRowLookup(int col) {
// HashMap<String,Integer> outgoing = new HashMap<String, Integer>();
// 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...
// /**