use of cern.colt.function.IntComparator in project tdq-studio-se by Talend.
the class Sorting method sort.
/**
*Sorts the matrix slices according to the order induced by the specified comparator.
*The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.
*The algorithm compares two slices (2-d matrices) at a time, determinining whether one is smaller, equal or larger than the other.
*To sort ranges use sub-ranging views. To sort by other dimensions, use dice views. To sort descending, use flip views ...
*<p>
*<b>Example:</b>
*<pre>
*// sort by sum of values in a slice
*DoubleMatrix2DComparator comp = new DoubleMatrix2DComparator() {
* public int compare(DoubleMatrix2D a, DoubleMatrix2D b) {
* double as = a.zSum(); double bs = b.zSum();
* return as < bs ? -1 : as == bs ? 0 : 1;
* }
*};
*sorted = quickSort(matrix,comp);
*</pre>
*
*@param matrix the matrix to be sorted.
*@param c the comparator to determine the order.
*@return a new matrix view having slices sorted as specified.
* <b>Note that the original matrix is left unaffected.</b>
*/
public DoubleMatrix3D sort(final DoubleMatrix3D matrix, final DoubleMatrix2DComparator c) {
// indexes to reorder instead of matrix itself
int[] sliceIndexes = new int[matrix.slices()];
for (int i = sliceIndexes.length; --i >= 0; ) sliceIndexes[i] = i;
// precompute views for speed
final DoubleMatrix2D[] views = new DoubleMatrix2D[matrix.slices()];
for (int i = views.length; --i >= 0; ) views[i] = matrix.viewSlice(i);
IntComparator comp = new IntComparator() {
public int compare(int a, int b) {
// return c.compare(matrix.viewSlice(a), matrix.viewSlice(b));
return c.compare(views[a], views[b]);
}
};
runSort(sliceIndexes, 0, sliceIndexes.length, comp);
// take all rows and columns in the original order
return matrix.viewSelection(sliceIndexes, null, null);
}
use of cern.colt.function.IntComparator in project tdq-studio-se by Talend.
the class Sorting method sort.
/**
*Sorts the vector into ascending order, according to the <i>natural ordering</i>.
*The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.
*To sort ranges use sub-ranging views. To sort descending, use flip views ...
*<p>
*<b>Example:</b>
*<table border="1" cellspacing="0">
* <tr nowrap>
* <td valign="top"><tt> 7, 1, 3, 1<br>
* </tt></td>
* <td valign="top">
* <p><tt> ==> 1, 1, 3, 7<br>
* The vector IS NOT SORTED.<br>
* The new VIEW IS SORTED.</tt></p>
* </td>
* </tr>
*</table>
*
*@param vector the vector to be sorted.
*@return a new sorted vector (matrix) view.
* <b>Note that the original matrix is left unaffected.</b>
*/
public DoubleMatrix1D sort(final DoubleMatrix1D vector) {
// row indexes to reorder instead of matrix itself
int[] indexes = new int[vector.size()];
for (int i = indexes.length; --i >= 0; ) indexes[i] = i;
IntComparator comp = new IntComparator() {
public int compare(int a, int b) {
double av = vector.getQuick(a);
double bv = vector.getQuick(b);
// swap NaNs to the end
if (av != av || bv != bv)
return compareNaN(av, bv);
return av < bv ? -1 : (av == bv ? 0 : 1);
}
};
runSort(indexes, 0, indexes.length, comp);
return vector.viewSelection(indexes);
}
use of cern.colt.function.IntComparator in project tdq-studio-se by Talend.
the class Sorting method sort.
/**
*Sorts the matrix rows according to the order induced by the specified comparator.
*The returned view is backed by this matrix, so changes in the returned view are reflected in this matrix, and vice-versa.
*The algorithm compares two rows (1-d matrices) at a time, determinining whether one is smaller, equal or larger than the other.
*To sort ranges use sub-ranging views. To sort columns by rows, use dice views. To sort descending, use flip views ...
*<p>
*<b>Example:</b>
*<pre>
*// sort by sum of values in a row
*DoubleMatrix1DComparator comp = new DoubleMatrix1DComparator() {
* public int compare(DoubleMatrix1D a, DoubleMatrix1D b) {
* double as = a.zSum(); double bs = b.zSum();
* return as < bs ? -1 : as == bs ? 0 : 1;
* }
*};
*sorted = quickSort(matrix,comp);
*</pre>
*
*@param matrix the matrix to be sorted.
*@param c the comparator to determine the order.
*@return a new matrix view having rows sorted as specified.
* <b>Note that the original matrix is left unaffected.</b>
*/
public DoubleMatrix2D sort(final DoubleMatrix2D matrix, final DoubleMatrix1DComparator c) {
// row indexes to reorder instead of matrix itself
int[] rowIndexes = new int[matrix.rows()];
for (int i = rowIndexes.length; --i >= 0; ) rowIndexes[i] = i;
// precompute views for speed
final DoubleMatrix1D[] views = new DoubleMatrix1D[matrix.rows()];
for (int i = views.length; --i >= 0; ) views[i] = matrix.viewRow(i);
IntComparator comp = new IntComparator() {
public int compare(int a, int b) {
// return c.compare(matrix.viewRow(a), matrix.viewRow(b));
return c.compare(views[a], views[b]);
}
};
runSort(rowIndexes, 0, rowIndexes.length, comp);
// take all columns in the original order
return matrix.viewSelection(rowIndexes, null);
}
use of cern.colt.function.IntComparator in project tdq-studio-se by Talend.
the class GenericSortingTest method demo1.
/**
* Just a demo.
*/
public static void demo1() {
final int[] x;
final double[] y;
final double[] z;
x = new int[] { 3, 2, 1 };
y = new double[] { 3.0, 2.0, 1.0 };
z = new double[] { 6.0, 7.0, 8.0 };
Swapper swapper = new Swapper() {
public void swap(int a, int b) {
int t1;
double t2, t3;
t1 = x[a];
x[a] = x[b];
x[b] = t1;
t2 = y[a];
y[a] = y[b];
y[b] = t2;
t3 = z[a];
z[a] = z[b];
z[b] = t3;
}
};
IntComparator comp = new IntComparator() {
public int compare(int a, int b) {
return x[a] == x[b] ? 0 : (x[a] < x[b] ? -1 : 1);
}
};
System.out.println("before:");
System.out.println("X=" + Arrays.toString(x));
System.out.println("Y=" + Arrays.toString(y));
System.out.println("Z=" + Arrays.toString(z));
int from = 0;
int to = x.length;
GenericSorting.quickSort(from, to, comp, swapper);
System.out.println("after:");
System.out.println("X=" + Arrays.toString(x));
System.out.println("Y=" + Arrays.toString(y));
System.out.println("Z=" + Arrays.toString(z));
System.out.println("\n\n");
}
use of cern.colt.function.IntComparator in project tdq-studio-se by Talend.
the class GenericSortingTest method demo2.
/**
* Just a demo.
*/
public static void demo2() {
final int[] x;
final double[] y;
final double[] z;
x = new int[] { 6, 7, 8, 9 };
y = new double[] { 3.0, 2.0, 1.0, 3.0 };
z = new double[] { 5.0, 4.0, 4.0, 1.0 };
Swapper swapper = new Swapper() {
public void swap(int a, int b) {
int t1;
double t2, t3;
t1 = x[a];
x[a] = x[b];
x[b] = t1;
t2 = y[a];
y[a] = y[b];
y[b] = t2;
t3 = z[a];
z[a] = z[b];
z[b] = t3;
}
};
IntComparator comp = new IntComparator() {
public int compare(int a, int b) {
if (y[a] == y[b])
return z[a] == z[b] ? 0 : (z[a] < z[b] ? -1 : 1);
return y[a] < y[b] ? -1 : 1;
}
};
System.out.println("before:");
System.out.println("X=" + Arrays.toString(x));
System.out.println("Y=" + Arrays.toString(y));
System.out.println("Z=" + Arrays.toString(z));
int from = 0;
int to = x.length;
GenericSorting.quickSort(from, to, comp, swapper);
System.out.println("after:");
System.out.println("X=" + Arrays.toString(x));
System.out.println("Y=" + Arrays.toString(y));
System.out.println("Z=" + Arrays.toString(z));
System.out.println("\n\n");
}
Aggregations