Search in sources :

Example 16 with IntComparator

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 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 cells at a time, determinining whether one is smaller, equal or larger than the other.
 *To sort ranges use sub-ranging views. To sort descending, use flip views ...
 *<p>
 *<b>Example:</b>
 *<pre>
 *// sort by sinus of cells
 *ObjectComparator comp = new ObjectComparator() {
 *&nbsp;&nbsp;&nbsp;public int compare(Object a, Object b) {
 *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Object as = Math.sin(a); Object bs = Math.sin(b);
 *&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return as < bs ? -1 : as == bs ? 0 : 1;
 *&nbsp;&nbsp;&nbsp;}
 *};
 *sorted = quickSort(vector,comp);
 *</pre>
 *
 *@param vector the vector to be sorted.
 *@param c the comparator to determine the order.
 *@return a new matrix view sorted as specified.
 *		<b>Note that the original vector (matrix) is left unaffected.</b>
 */
public ObjectMatrix1D sort(final ObjectMatrix1D vector, final java.util.Comparator c) {
    // 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) {
            return c.compare(vector.getQuick(a), vector.getQuick(b));
        }
    };
    runSort(indexes, 0, indexes.length, comp);
    return vector.viewSelection(indexes);
}
Also used : IntComparator(cern.colt.function.IntComparator)

Aggregations

IntComparator (cern.colt.function.IntComparator)16 DoubleMatrix1D (cern.colt.matrix.DoubleMatrix1D)4 ObjectMatrix1D (cern.colt.matrix.ObjectMatrix1D)4 DenseDoubleMatrix1D (cern.colt.matrix.impl.DenseDoubleMatrix1D)3 Swapper (cern.colt.Swapper)2 DoubleMatrix2D (cern.colt.matrix.DoubleMatrix2D)1 ObjectMatrix2D (cern.colt.matrix.ObjectMatrix2D)1