package edu.vt.marian.search; import java.util.*; import java.io.*; import edu.vt.marian.common.*; /** * A quicksort object which can be used to sort the elements of * an enumeration in either non-increasing or non-decreasing order. * The only requirement is that each element of the object being * sorted implements the Sortable interface. Basically, this means * an object provides a compare() method which can be used to * determine whether one object is less than, equal to, or greater * than another object. * * @author Paul Mather * @version 0.9 */ public class WtdObjQuickSort extends QuickSort { /** * The ubiquitous debug object */ private Debug debug; /** * Make a new object to quicksort the supplied enumeration. * Here, we simply invoke the superclass constructor, aside * from setting the value of our local Debug object. * @param elts the enumeration to be sorted * @param sortType the type of order in which to sort: * Sortable.INCREASING or Sortable.DESCREASING. * @param debug the debug object * @see edu.vt.marian.common.Sortable * @see edu.vt.marian.common.QuickSort * @see java.util.Vector * @see java.util.Enumeration */ public WtdObjQuickSort(Enumeration elts, int sortType, Debug debug) { super(elts, sortType); this.debug = debug; } /** * Return a weighted object set enumeration containing the * elements contained in this object in sorted order. Basically, * this method simply invokes quicksort on the local vector, and * then returns an enumeration of the objects in that vector after * quicksort has sorted them. * @return a weighted object set enumeration of the sorted objects * @see edu.vt.marian.common.QuickSort#quicksort * @see edu.vt.marian.search.VectorWtdObjSetEnum */ public WtdObjSetEnumeration sortedWtdObjSetElements() { VectorWtdObjSet v; Enumeration enum; WtdObj obj; // Perform quicksort on local vector quicksort(0, objects.size()-1); // Return an enumeration of the now-sorted vector // (This is lazy, but it avoids having to re-implement // VectorWtdObjSetEnum from VectorWtdObjSet...) v = new VectorWtdObjSet(objects.size(),this.debug); enum = objects.elements(); while( true ) { try { obj = (WtdObj) enum.nextElement(); } catch( NoSuchElementException e ) { break; } v.add( obj ); } return v.elements(); } }