private int partition(int left, int right, java.util.Comparator <fastmath.Vector> cmp) { int i = left - 1; int j = right; while (true) { // find item on left to swap while (cmp.compare(row(++i), row(right)) > 0) { } // find item on right to swap while (cmp.compare(row(right), row(--j)) > 0) { if (j == left) { break; } } // check if pointers cross if (i >= j) { break; } exch(i, j); } // swap with partition element if (i != right) { exch(i, right); } return(i); }
/** * Returns the index of the least element in {@link #values}, * {@link #set(int) setting} any uninitialized values. * * @throws IllegalStateException */ private int least() { int leastIndex = -1; Object leastObject = null; for (int i = 0; i < values.size(); i++) { if (valueSet.get(i) == false) { set(i); } if (valueSet.get(i)) { if (leastIndex == -1) { leastIndex = i; leastObject = values.get(i); } else { Object curObject = values.get(i); if (comparator.compare(curObject, leastObject) < 0) { leastObject = curObject; leastIndex = i; } } } } return(leastIndex); }
/** * Returns the larger of the given objects according to the given * comparator, returning the second object if the comparator * returns equal. * * @param o1 the first object to compare * @param o2 the second object to compare * @param comparator the sort order to use * @return the larger of the two objects */ public static Object max(Object o1, Object o2, java.util.Comparator <Object> comparator) { if (comparator == null) { comparator = NATURAL_COMPARATOR; } int c = comparator.compare(o1, o2); return((c > 0) ? o1 : o2); }
/** * Compares two objects using the comparator if specified, or the * natural order otherwise. * * @param a the first object * @param b the second object * @return -ve if a less than b, 0 if they are equal, +ve if a greater than b */ internal int compare(Object a, Object b) { if (m_comparator != null) { return(m_comparator.compare(a, b)); } else { return(((java.lang.Comparable <Object>)a).compareTo(b)); } }
//----------------------------------------------------------------------- /** * Perform comparisons on the Objects as per * Comparator.compare(o1,o2). * * @param o1 the first object to compare * @param o2 the second object to compare * @return -1, 0, or 1 * @exception UnsupportedOperationException * if the ComparatorChain does not contain at least one * Comparator */ public int compare(Object o1, Object o2) {//throws UnsupportedOperationException { if (isLockedJ == false) { checkChainIntegrity(); isLockedJ = true; } // iterate over all comparators in the chain java.util.Iterator <Object> comparators = comparatorChain.iterator(); for (int comparatorIndex = 0; comparators.hasNext(); ++comparatorIndex) { java.util.Comparator <Object> comparator = (java.util.Comparator <Object>)comparators.next(); int retval = comparator.compare(o1, o2); if (retval != 0) { // invert the order if it is a reverse sort if (orderingBits.get(comparatorIndex) == true) { if (java.lang.Integer.MIN_VALUE == retval) { retval = java.lang.Integer.MAX_VALUE; } else { retval *= -1; } } return(retval); } } // if comparators are exhausted, return 0 return(0); }
//----------------------------------------------------------------------- /** * Compares two objects in reverse order. * * @param obj1 the first object to compare * @param obj2 the second object to compare * @return negative if obj1 is less, positive if greater, zero if equal */ public int compare(Object obj1, Object obj2) { return(comparator.compare(obj2, obj1)); }