Skip to content

Commit 2f97983

Browse files
committed
fix up getPercentages() -> getPercent()... also add sort() to Table
1 parent 26c697a commit 2f97983

6 files changed

Lines changed: 183 additions & 4 deletions

File tree

core/src/processing/data/FloatDict.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ public float minValue() {
427427
* @webref floatlist:method
428428
* @brief Return the largest value
429429
*/
430+
// The index of the entry that has the max value. Reference above is incorrect.
430431
public int maxIndex() {
431432
checkMinMax("maxIndex");
432433
// Will still return NaN if there is 1 or more entries, and they're all NaN
@@ -453,12 +454,14 @@ public int maxIndex() {
453454
}
454455

455456

457+
/** The key for a max value. */
456458
public String maxKey() {
457459
checkMinMax("maxKey");
458460
return keys[maxIndex()];
459461
}
460462

461463

464+
/** The max value. */
462465
public float maxValue() {
463466
checkMinMax("maxValue");
464467
return values[maxIndex()];
@@ -685,7 +688,7 @@ public void swap(int a, int b) {
685688
* each key, divided by the total sum. The total for all values will be ~1.0.
686689
* @return a Dict with the original keys, mapped to their pct of the total
687690
*/
688-
public FloatDict getPercentages() {
691+
public FloatDict getPercent() {
689692
double sum = 0;
690693
for (float value : valueArray()) {
691694
sum += value;

core/src/processing/data/FloatList.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,26 @@ public float[] array(float[] array) {
708708
}
709709

710710

711+
/**
712+
* Returns a normalized version of this array. Called getPercent() for
713+
* consistency with the Dict classes. It's a getter method because it needs
714+
* to returns a new list (because IntList/Dict can't do percentages or
715+
* normalization in place on int values).
716+
*/
717+
public FloatList getPercent() {
718+
double sum = 0;
719+
for (float value : array()) {
720+
sum += value;
721+
}
722+
FloatList outgoing = new FloatList(count);
723+
for (int i = 0; i < count; i++) {
724+
double percent = data[i] / sum;
725+
outgoing.set(i, (float) percent);
726+
}
727+
return outgoing;
728+
}
729+
730+
711731
public FloatList getSubset(int start) {
712732
return getSubset(start, count - start);
713733
}

core/src/processing/data/IntDict.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ public int maxValue() {
470470
}
471471

472472

473-
// return the key for the maximum value
473+
// return the key corresponding to the maximum value
474474
public String maxKey() {
475475
checkMinMax("maxKey");
476476
return keys[maxIndex()];
@@ -626,7 +626,7 @@ public void swap(int a, int b) {
626626
* each key, divided by the total sum. The total for all values will be ~1.0.
627627
* @return a Dict with the original keys, mapped to their pct of the total
628628
*/
629-
public FloatDict getPercentages() {
629+
public FloatDict getPercent() {
630630
double sum = 0;
631631
for (int value : valueArray()) {
632632
sum += value;

core/src/processing/data/IntList.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ public IntList(Iterable<Integer> iter) {
6060
}
6161

6262

63+
static public IntList fromRange(int stop) {
64+
return fromRange(0, stop);
65+
}
66+
67+
68+
static public IntList fromRange(int start, int stop) {
69+
int count = stop - start;
70+
IntList newbie = new IntList(count);
71+
for (int i = 0; i < count; i++) {
72+
newbie.set(i, start+i);
73+
}
74+
return newbie;
75+
}
76+
77+
6378
/**
6479
* Improve efficiency by removing allocated but unused entries from the
6580
* internal array used to store the data. Set to private, though it could
@@ -698,6 +713,26 @@ public int[] array(int[] array) {
698713
// }
699714

700715

716+
/**
717+
* Returns a normalized version of this array. Called getPercent() for
718+
* consistency with the Dict classes. It's a getter method because it needs
719+
* to returns a new list (because IntList/Dict can't do percentages or
720+
* normalization in place on int values).
721+
*/
722+
public FloatList getPercent() {
723+
double sum = 0;
724+
for (float value : array()) {
725+
sum += value;
726+
}
727+
FloatList outgoing = new FloatList(count);
728+
for (int i = 0; i < count; i++) {
729+
double percent = data[i] / sum;
730+
outgoing.set(i, (float) percent);
731+
}
732+
return outgoing;
733+
}
734+
735+
701736
public IntList getSubset(int start) {
702737
return getSubset(start, count - start);
703738
}

core/src/processing/data/Table.java

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public class Table {
9191

9292
protected RowIterator rowIterator;
9393

94+
// 0 for doubling each time, otherwise the number of rows to increment on
95+
// each expansion.
96+
protected int expandIncrement;
97+
9498

9599
/**
96100
* Creates a new, empty table. Use addRow() to add additional rows.
@@ -1707,6 +1711,7 @@ public int lastRowIndex() {
17071711
return getRowCount() - 1;
17081712
}
17091713

1714+
17101715
/**
17111716
* @webref table:method
17121717
* @brief Removes all rows from a table
@@ -1749,6 +1754,7 @@ public void setRowCount(int newCount) {
17491754
rowCount = newCount;
17501755
}
17511756

1757+
17521758
/**
17531759
* @webref table:method
17541760
* @brief Adds a row to a table
@@ -1760,6 +1766,7 @@ public TableRow addRow() {
17601766
return new RowPointer(this, rowCount - 1);
17611767
}
17621768

1769+
17631770
/**
17641771
* @param source a reference to the original row to be duplicated
17651772
*/
@@ -1793,6 +1800,7 @@ public TableRow addRow(TableRow source) {
17931800
return new RowPointer(this, row);
17941801
}
17951802

1803+
17961804
/**
17971805
* @nowebref
17981806
*/
@@ -3580,7 +3588,119 @@ void read(DataInputStream input) throws IOException {
35803588
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
35813589

35823590

3583-
// TODO maybe these aren't needed. better to use getStringList().getUnique()?
3591+
public void sort(String columnName) {
3592+
sort(getColumnIndex(columnName), false);
3593+
}
3594+
3595+
3596+
public void sort(int column) {
3597+
sort(column, false);
3598+
}
3599+
3600+
3601+
public void sortReverse(String columnName) {
3602+
sort(getColumnIndex(columnName), true);
3603+
}
3604+
3605+
3606+
public void sortReverse(int column) {
3607+
sort(column, true);
3608+
}
3609+
3610+
3611+
protected void sort(final int column, final boolean reverse) {
3612+
final int[] order = IntList.fromRange(getRowCount()).array();
3613+
Sort s = new Sort() {
3614+
3615+
@Override
3616+
public int size() {
3617+
return getRowCount();
3618+
}
3619+
3620+
@Override
3621+
public float compare(int index1, int index2) {
3622+
int a = reverse ? order[index2] : order[index1];
3623+
int b = reverse ? order[index1] : order[index2];
3624+
3625+
switch (getColumnType(column)) {
3626+
case INT:
3627+
return getInt(a, column) - getInt(b, column);
3628+
case LONG:
3629+
return getLong(a, column) - getLong(b, column);
3630+
case FLOAT:
3631+
return getFloat(a, column) - getFloat(b, column);
3632+
case DOUBLE:
3633+
return (float) (getDouble(a, column) - getDouble(b, column));
3634+
case STRING:
3635+
return getString(a, column).compareToIgnoreCase(getString(b, column));
3636+
case CATEGORY:
3637+
return getInt(a, column) - getInt(b, column);
3638+
default:
3639+
throw new IllegalArgumentException("Invalid column type: " + getColumnType(column));
3640+
}
3641+
}
3642+
3643+
@Override
3644+
public void swap(int a, int b) {
3645+
int temp = order[a];
3646+
order[a] = order[b];
3647+
order[b] = temp;
3648+
}
3649+
3650+
};
3651+
s.run();
3652+
3653+
//Object[] newColumns = new Object[getColumnCount()];
3654+
for (int col = 0; col < getColumnCount(); col++) {
3655+
switch (getColumnType(col)) {
3656+
case INT:
3657+
case CATEGORY:
3658+
int[] oldInt = (int[]) columns[col];
3659+
int[] newInt = new int[rowCount];
3660+
for (int row = 0; row < getRowCount(); row++) {
3661+
newInt[row] = oldInt[order[row]];
3662+
}
3663+
columns[col] = newInt;
3664+
break;
3665+
case LONG:
3666+
long[] oldLong = (long[]) columns[col];
3667+
long[] newLong = new long[rowCount];
3668+
for (int row = 0; row < getRowCount(); row++) {
3669+
newLong[row] = oldLong[order[row]];
3670+
}
3671+
columns[col] = newLong;
3672+
break;
3673+
case FLOAT:
3674+
float[] oldFloat = (float[]) columns[col];
3675+
float[] newFloat = new float[rowCount];
3676+
for (int row = 0; row < getRowCount(); row++) {
3677+
newFloat[row] = oldFloat[order[row]];
3678+
}
3679+
columns[col] = newFloat;
3680+
break;
3681+
case DOUBLE:
3682+
double[] oldDouble = (double[]) columns[col];
3683+
double[] newDouble = new double[rowCount];
3684+
for (int row = 0; row < getRowCount(); row++) {
3685+
newDouble[row] = oldDouble[order[row]];
3686+
}
3687+
columns[col] = newDouble;
3688+
break;
3689+
case STRING:
3690+
String[] oldString = (String[]) columns[col];
3691+
String[] newString = new String[rowCount];
3692+
for (int row = 0; row < getRowCount(); row++) {
3693+
newString[row] = oldString[order[row]];
3694+
}
3695+
columns[col] = newString;
3696+
break;
3697+
}
3698+
}
3699+
}
3700+
3701+
3702+
// . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3703+
35843704

35853705
public String[] getUnique(String columnName) {
35863706
return getUnique(getColumnIndex(columnName));

core/todo.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ _ Unable to get TAB key event with P2D/P3D renderer
2828
_ https://github.com/processing/processing/issues/1967
2929

3030
table
31+
X add sort() to Table
3132
_ implement version of Table that takes a dictionary file
3233
_ addRow() is not efficient, probably need to do the doubling
3334
_ or have a setIncrement() function?

0 commit comments

Comments
 (0)