@@ -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 ));
0 commit comments