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

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * A DequeIterator is a random access iterator that allows you to iterate through
  8.  * the contents of a Deque.
  9.  * <p>
  10.  * @see jgl.RandomAccessIterator
  11.  * @version 1.1
  12.  * @author ObjectSpace, Inc.
  13.  */
  14.  
  15. public final class DequeIterator implements RandomAccessIterator
  16.   {
  17.   Deque myDeque;
  18.   int myBlockIndex;
  19.   int myMapIndex;
  20.  
  21.   /**
  22.    * Construct myself to be an iterator with no associated data structure or position.
  23.    */
  24.   public DequeIterator()
  25.     {
  26.     }
  27.  
  28.   /**
  29.    * Construct myself to be a copy of an existing iterator.
  30.    * @param iterator The iterator to copy.
  31.    */
  32.   public DequeIterator( DequeIterator iterator )
  33.     {
  34.     myDeque = iterator.myDeque;
  35.     myMapIndex = iterator.myMapIndex;
  36.     myBlockIndex = iterator.myBlockIndex;
  37.     }
  38.  
  39.   /**
  40.    * Construct myself to be positioned at a particular map and block index
  41.    * within a specified Deque.
  42.    */
  43.   DequeIterator( Deque deque, int blockIndex, int mapIndex )
  44.     {
  45.     myDeque = deque;
  46.     myBlockIndex = blockIndex;
  47.     myMapIndex = mapIndex;
  48.     }
  49.  
  50.   /**
  51.    * Return a clone of myself.
  52.    */
  53.   public Object clone()
  54.     {
  55.     return new DequeIterator( this );
  56.     }
  57.  
  58.   /**
  59.    * Return true if I'm positioned at the first item of my input stream.
  60.    */
  61.   public boolean atBegin()
  62.     {
  63.     return equals( myDeque.myStart );
  64.     }
  65.  
  66.   /**
  67.    * Return true if I'm positioned after the last item in my input stream.
  68.    */
  69.   public boolean atEnd()
  70.     {
  71.     return equals( myDeque.myFinish );
  72.     }
  73.  
  74.   /**
  75.    * Return true if there are more elements in my input stream.
  76.    */
  77.   public boolean hasMoreElements()
  78.     {
  79.     return !equals( myDeque.myFinish );
  80.     }
  81.  
  82.   /**
  83.    * Advance by one.
  84.    */
  85.   public void advance()
  86.     {
  87.     if( ++myBlockIndex == Deque.BLOCK_SIZE )
  88.       {
  89.       ++myMapIndex;
  90.       myBlockIndex = 0;
  91.       }
  92.     }
  93.  
  94.   /**
  95.    * Advance by a specified amount.
  96.    * @param n The amount to advance.
  97.    */
  98.   public void advance( int n )
  99.     {
  100.     myBlockIndex += n;
  101.  
  102.     if( myBlockIndex >= Deque.BLOCK_SIZE )
  103.       {
  104.       int jump = myBlockIndex / Deque.BLOCK_SIZE;
  105.       myMapIndex += jump;
  106.       myBlockIndex %= Deque.BLOCK_SIZE;
  107.       }
  108.     else if( myBlockIndex < 0 )
  109.       {
  110.       int jump = ( Deque.BLOCK_SIZE - 1 - myBlockIndex ) / Deque.BLOCK_SIZE;
  111.       myMapIndex -= jump;
  112.       myBlockIndex += jump * Deque.BLOCK_SIZE;
  113.       }
  114.     }
  115.  
  116.   /**
  117.    * Retreat by one.
  118.    */
  119.   public void retreat()
  120.     {
  121.     if( --myBlockIndex == -1 )
  122.       {
  123.       --myMapIndex;
  124.       myBlockIndex = Deque.BLOCK_SIZE - 1;
  125.       }
  126.     }
  127.  
  128.   /**
  129.    * Retreat by a specified amount.
  130.    * @param n The amount to retreat.
  131.    */
  132.   public void retreat( int n )
  133.     {
  134.     advance( -n );
  135.     }
  136.  
  137.   /**
  138.    * Return the next element in my input stream.
  139.    */
  140.   public Object nextElement()
  141.     {
  142.     Object object = myDeque.myMap[ myMapIndex ][ myBlockIndex++ ];
  143.  
  144.     if( myBlockIndex == Deque.BLOCK_SIZE )
  145.       {
  146.       ++myMapIndex;
  147.       myBlockIndex = 0;
  148.       }
  149.  
  150.     return object;
  151.     }
  152.  
  153.   DequeIterator copy( int i )
  154.     {
  155.     DequeIterator tmp = new DequeIterator( this );
  156.     tmp.advance( i );
  157.     return tmp;
  158.     }
  159.  
  160.   /**
  161.    * Return the distance from myself to another iterator.
  162.    * I should be before the specified iterator.
  163.    * @param iterator The iterator to compare myself against.
  164.    */
  165.   public int distance( ForwardIterator iter )
  166.     {
  167.     DequeIterator iterator = (DequeIterator) iter;
  168.     int gap = iterator.myBlockIndex - myBlockIndex;
  169.     return myMapIndex == iterator.myMapIndex 
  170.       ? gap
  171.       : Deque.BLOCK_SIZE * (iterator.myMapIndex - myMapIndex) + gap;
  172.     }
  173.  
  174.   /**
  175.    * Return my current index.
  176.    */
  177.   public int index()
  178.     {
  179.     return myDeque.myStart.distance( this );
  180.     }
  181.  
  182.   /**
  183.    * Return the object at my current position.
  184.    */
  185.   public Object get()
  186.     {
  187.     return myDeque.myMap[ myMapIndex ][ myBlockIndex ];
  188.     }
  189.  
  190.   /**
  191.    * Set the object at my current position to a specified value.
  192.    * @param object The object to be written at my current position.
  193.    */
  194.   public void put( Object object )
  195.     {
  196.     myDeque.myMap[ myMapIndex ][ myBlockIndex ] = object;
  197.     }
  198.  
  199.   /**
  200.    * Return the object that is a specified distance from my current position.
  201.    * @param offset The offset from my current position.
  202.    */
  203.   public Object get( int offset )
  204.     {
  205.     int oldMapIndex = myMapIndex;
  206.     int oldBlockIndex = myBlockIndex;
  207.     advance( offset );
  208.     Object object = get();
  209.     myMapIndex = oldMapIndex;
  210.     myBlockIndex = oldBlockIndex;
  211.     return object;
  212.     }
  213.  
  214.   /**
  215.    * Write an object at a specified distance from my current position.
  216.    * @param offset The offset from my current position.
  217.    * @param object The object to write.
  218.    */
  219.   public void put( int offset, Object object )
  220.     {
  221.     int oldMapIndex = myMapIndex;
  222.     int oldBlockIndex = myBlockIndex;
  223.     advance( offset );
  224.     put( object );
  225.     myMapIndex = oldMapIndex;
  226.     myBlockIndex = oldBlockIndex;
  227.     }
  228.  
  229.   /**
  230.    * Return true if a specified object is the same kind of iterator as me 
  231.    * and is positioned at the same element.
  232.    * @param object Any object.
  233.    */
  234.   public boolean equals( Object object )
  235.     {
  236.     return object instanceof DequeIterator && equals( (DequeIterator) object );
  237.     }
  238.  
  239.   /**
  240.    * Return true if iterator is positioned at the same element as me.
  241.    * @param iterator The iterator to compare myself against.
  242.    */
  243.   public boolean equals( DequeIterator iterator )
  244.     {
  245.     return myBlockIndex == iterator.myBlockIndex && myMapIndex == iterator.myMapIndex && myDeque == iterator.myDeque;
  246.     }
  247.  
  248.   /**
  249.    * Return true if I'm before a specified iterator.
  250.    * @param iterator The iterator to compare myself against.
  251.    */
  252.   public boolean less( RandomAccessIterator iterator )
  253.     {
  254.     DequeIterator iter = (DequeIterator) iterator;
  255.     return( myMapIndex < iter.myMapIndex || (myMapIndex == iter.myMapIndex && myBlockIndex < iter.myBlockIndex ));
  256.     }
  257.  
  258.   /**
  259.    * Return my associated container.
  260.    */
  261.   public Container getContainer() 
  262.     {
  263.     return myDeque;
  264.     }
  265.   }
  266.  
  267.