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

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * The Reversing class contains generic reversing algorithms.
  8.  * <p>
  9.  * @see jgl.examples.ReversingExamples
  10.  * @version 1.1
  11.  * @author ObjectSpace, Inc.
  12.  */
  13.  
  14. public final class Reversing
  15.   {
  16.   private Reversing()
  17.     {
  18.     }
  19.  
  20.   /**
  21.    * Reverse a sequence. The time complexity is linear and the space complexity is
  22.    * constant.
  23.    * @param first An iterator positioned at the first element of the sequence.
  24.    * @param last An iterator positioned immediately after the last element of the sequence.
  25.    */
  26.   public static void reverse( BidirectionalIterator first, BidirectionalIterator last )
  27.     {
  28.     if ( !(first.getContainer() instanceof Sequence) )
  29.       throw new IllegalArgumentException( "iterator containers must be a Sequence" );
  30.  
  31.     if( first instanceof RandomAccessIterator )
  32.       {
  33.       RandomAccessIterator firstx = (RandomAccessIterator) first.clone();
  34.       RandomAccessIterator lastx = (RandomAccessIterator) last.clone();
  35.  
  36.       while( firstx.less( lastx ) )
  37.         {
  38.         lastx.retreat();
  39.         Swapping.iterSwap( firstx, lastx );
  40.         firstx.advance();
  41.         }
  42.       }
  43.     else
  44.       {
  45.       BidirectionalIterator firstx = (BidirectionalIterator) first.clone();
  46.       BidirectionalIterator lastx = (BidirectionalIterator) last.clone();
  47.  
  48.       while( true )
  49.         {
  50.         if( firstx.equals( lastx ) )
  51.           return;
  52.  
  53.         lastx.retreat();
  54.  
  55.         if( firstx.equals( lastx ) )
  56.           return;
  57.  
  58.         Swapping.iterSwap( firstx, lastx );
  59.         firstx.advance();
  60.         }
  61.       }
  62.     }
  63.  
  64.   /**
  65.    * Reverse a container. The time complexity is linear and the space complexity is
  66.    * constant.
  67.    * @param container The container to reverse.
  68.    */
  69.   public static void reverse( Container container )
  70.     {
  71.     reverse( (BidirectionalIterator) container.start(), (BidirectionalIterator) container.finish() );
  72.     }
  73.  
  74.   /**
  75.    * Copy the reverse of a sequence into another sequence of the same size. The
  76.    * time complexity is linear and the space complexity is constant. 
  77.    * @param first An iterator positioned at the first element of the input sequence.
  78.    * @param last An iterator positioned immediately after the last element of the input sequence.
  79.    * @param result An iterator positioned at the first element of the output sequence.
  80.    * @return An iterator positioned immediately after the last element of the output sequence.
  81.    */
  82.   public static OutputIterator reverseCopy( BidirectionalIterator first, BidirectionalIterator last, OutputIterator result )
  83.     {
  84.     if ( !(first.getContainer() instanceof Sequence) )
  85.       throw new IllegalArgumentException( "iterator containers must be a Sequence" );
  86.  
  87.     if ( !(last.getContainer() instanceof Sequence) )
  88.       throw new IllegalArgumentException( "iterator containers must be a Sequence" );
  89.  
  90.     BidirectionalIterator lastx = (BidirectionalIterator) last.clone();
  91.     OutputIterator resultx = (OutputIterator) result.clone();
  92.  
  93.     while( !first.equals( lastx ) )
  94.       {
  95.       lastx.retreat();
  96.       resultx.put( lastx.get() );
  97.       resultx.advance();
  98.       }
  99.  
  100.     return resultx;
  101.     }
  102.  
  103.   /**
  104.    * Copy the reverse of a container into a sequence. The time complexity is linear and 
  105.    * the space complexity is constant. 
  106.    * @param input The input container.
  107.    * @param result An iterator positioned at the first element of the output sequence.
  108.    * @return An iterator positioned immediately after the last element of the output sequence.
  109.    */
  110.   public static OutputIterator reverseCopy( Container input, OutputIterator result )
  111.     {
  112.     return reverseCopy( (BidirectionalIterator) input.start(), (BidirectionalIterator) input.finish(), result );
  113.     }
  114.  
  115.   /**
  116.    * Copy the reverse of a container into another container. The time complexity is 
  117.    * linear and the space complexity is constant. 
  118.    * @param source The source container.
  119.    * @param destination The destination container.
  120.    * @return An iterator positioned immediately after the last element of the output sequence.
  121.    */
  122.   public static void reverseCopy( Container source, Container destination )
  123.     {
  124.     reverseCopy( (BidirectionalIterator) source.start(), (BidirectionalIterator) source.finish(), new InsertIterator( destination ) );
  125.     }
  126.   }
  127.  
  128.