home *** CD-ROM | disk | FTP | other *** search
/ ActiveX Programming Unleashed CD / AXU.iso / jgl_1_1 / jgl_1_1.exe / src / PriorityQueue.java < prev    next >
Encoding:
Java Source  |  1996-09-10  |  5.6 KB  |  231 lines

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. import java.util.Enumeration;
  7.  
  8. /**
  9.  * A PriorityQueue is an adapter that allows you to access items in a sorted order.
  10.  * It allows you to specify a comparator that is used to sort the items.
  11.  * <p>
  12.  * A PriorityQueue uses the generic heap algorithms to access its underlying Array
  13.  * as a heap. This means that although objects will always pop in the order determined
  14.  * by the comparator, the objects are not necessarily stored in that order within
  15.  * the heap structure.
  16.  * <p>
  17.  * @see jgl.Array
  18.  * @see jgl.Heap
  19.  * @see jgl.examples.PriorityQueueExamples
  20.  * @version 1.1
  21.  * @author ObjectSpace, Inc.
  22.  */
  23.  
  24. public class PriorityQueue implements Container
  25.   {
  26.   Array myArray;
  27.   BinaryPredicate myComparator;
  28.  
  29.   /**
  30.    * Construct myself to be an empty PriorityQueue.
  31.    * Order elements based on their hash code.
  32.    */
  33.   public PriorityQueue()
  34.     {
  35.     this( new HashComparator() );
  36.     }
  37.  
  38.   /**
  39.    * Construct myself to be an empty PriorityQueue.
  40.    * Order elements using the specified comparator.
  41.    * @param comparator The comparator to be used for comparing elements.
  42.    */
  43.   public PriorityQueue( BinaryPredicate comparator )
  44.     {
  45.     myArray = new Array();
  46.     myComparator = comparator;
  47.     }
  48.  
  49.   /**
  50.    * Construct myself to be a shallow copy of a specified PriorityQueue.
  51.    * A shallow copy is made of its underlying sequence.
  52.    * @param queue The instance of PriorityQueue to be copied.
  53.    */
  54.   public PriorityQueue( PriorityQueue queue )
  55.     {
  56.     myArray = (Array) queue.myArray.clone();
  57.     myComparator = queue.myComparator;
  58.     }
  59.  
  60.   /**
  61.    * Return a string that describes me.
  62.    */
  63.   public synchronized String toString()
  64.     {
  65.     return "PriorityQueue( " + myArray.toString() + " )";
  66.     }
  67.  
  68.   /**
  69.    * Return a shallow copy of myself.
  70.    */
  71.   public synchronized Object clone()
  72.     {
  73.     return new PriorityQueue( this );
  74.     }
  75.  
  76.   /**
  77.    * Become a shallow copy of a specified PriorityQueue.
  78.    * By underlying data structure becomes a shallow copy of the specified 
  79.    * PriorityQueue's data structure.
  80.    * @param queue The PriorityQueue to be copied.
  81.    */
  82.   public synchronized void copy( PriorityQueue queue )
  83.     {
  84.     if( this != queue )
  85.       {
  86.       myArray = (Array) queue.myArray.clone();
  87.       myComparator = queue.myComparator;
  88.       }
  89.     }
  90.  
  91.   /**
  92.    * Return true if object is a PriorityQueue whose underlying sequence is equal to mine.
  93.    * @param object Any object.
  94.    */
  95.   public boolean equals( Object object )
  96.     {
  97.     return object instanceof PriorityQueue && equals( (PriorityQueue) object );
  98.     }
  99.  
  100.   /**
  101.    * Return true if a specified PriorityQueue's sequence is equal to mine.
  102.    * @param queue The PriorityQueue to compare myself against.
  103.    */
  104.   public synchronized boolean equals( PriorityQueue queue )
  105.     {
  106.     return myArray.equals( queue.myArray );
  107.     }
  108.  
  109.   /**
  110.    * Return my hash code for support of hashing containers
  111.    */
  112.   public int hashCode()
  113.     {                             
  114.     return Hashing.orderedHash( this );
  115.     }
  116.   
  117.   /**
  118.    * Return true if I contain no objects.
  119.    */
  120.   public boolean isEmpty()
  121.     {
  122.     return myArray.isEmpty();
  123.     }
  124.  
  125.   /**
  126.    * Return the number of objects that I contain.
  127.    */
  128.   public int size()
  129.     {
  130.     return myArray.size();
  131.     }
  132.  
  133.   /**
  134.    * Return the maximum number of objects that I can contain.
  135.    */
  136.   public int maxSize()
  137.     {
  138.     return myArray.maxSize();
  139.     }
  140.  
  141.   /**
  142.    * Remove all of my objects.
  143.    */
  144.   public synchronized void clear()
  145.     {
  146.     myArray.clear();
  147.     }
  148.  
  149.   /**
  150.    * Return an Enumeration of my elements.
  151.    */
  152.   public synchronized Enumeration elements()
  153.     {
  154.     return myArray.elements();
  155.     }
  156.  
  157.   /**
  158.    * Return an iterator positioned at my first item.
  159.    */
  160.   public synchronized ForwardIterator start()
  161.     {
  162.     return myArray.start();
  163.     }
  164.  
  165.   /**
  166.    * Return an iterator positioned immediately afer my last item.
  167.    */
  168.   public synchronized ForwardIterator finish()
  169.     {
  170.     return myArray.finish();
  171.     }
  172.  
  173.   /**
  174.    * Return my top object.
  175.    * @exception jgl.InvalidOperationException If the PriorityQueue is empty.
  176.    */
  177.   public synchronized Object top()
  178.     {
  179.     return myArray.front();
  180.     }
  181.  
  182.   /**
  183.    * Push an object.  Add always works so return null.
  184.    * @param object The object to push.
  185.    */
  186.   public Object add( Object object )
  187.     {
  188.     push( object );
  189.     return null;
  190.     }
  191.  
  192.   /**
  193.    * Push an object.
  194.    * @param object The object to push.
  195.    */
  196.   public synchronized void push( Object object )
  197.     {
  198.     myArray.pushBack( object );
  199.     Heap.pushHeap( myArray.begin(), myArray.end(), myComparator );
  200.     }
  201.  
  202.   /**
  203.    * Pop the last object that was pushed onto me.
  204.    * @exception jgl.InvalidOperationException If the PriorityQueue is empty.
  205.    */
  206.   public synchronized Object pop()
  207.     {
  208.     if( myArray.isEmpty() ) 
  209.       throw new InvalidOperationException( "PriorityQueue is empty" );
  210.  
  211.     Heap.popHeap( myArray.begin(), myArray.end(), myComparator );
  212.     return myArray.popBack();
  213.     }
  214.  
  215.   /**
  216.    * Swap my contents with another PriorityQueue.
  217.    * @param queue The PriorityQueue that I will swap my contents with.
  218.    */
  219.   public synchronized void swap( PriorityQueue queue )
  220.     {
  221.     Array tmpArray = myArray;
  222.     myArray = queue.myArray;
  223.     queue.myArray = tmpArray;
  224.  
  225.     BinaryPredicate tmpComparator = myComparator;
  226.     myComparator = queue.myComparator;
  227.     queue.myComparator = tmpComparator;
  228.     }
  229.   }
  230.  
  231.