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

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * The Swapping class contains generic swapping algorithms.
  8.  * <p>
  9.  * @see jgl.examples.SwappingExamples
  10.  * @version 1.1
  11.  * @author ObjectSpace, Inc.
  12.  */
  13.  
  14. public final class Swapping
  15.   {
  16.   private Swapping()
  17.     {
  18.     }
  19.  
  20.   /**
  21.    * Swap the objects referenced by two iterators.
  22.    * @param iterator1 The first iterator.
  23.    * @param iterator2 The second iterator.
  24.    **/
  25.   public static void iterSwap( ForwardIterator iterator1, ForwardIterator iterator2 )
  26.     {
  27.     Object tmp = iterator1.get();
  28.     iterator1.put( iterator2.get() );
  29.     iterator2.put( tmp );
  30.     }
  31.  
  32.   /**
  33.    * Swap the elements in one sequence with the elements in another sequence of the
  34.    * same size.
  35.    * @param first1 An iterator positioned at the first element of the first sequence.
  36.    * @param last1 An iterator positioned immediately after the last element of the first sequence.
  37.    * @param first2 An iterator positioned at the first element of the second sequence.
  38.    * @return An iterator positioned immediately after the last element in the second sequence.
  39.    */
  40.   public static ForwardIterator swapRanges( ForwardIterator first1, ForwardIterator last1, ForwardIterator first2 )
  41.     {
  42.     ForwardIterator first1x = (ForwardIterator) first1.clone();
  43.     ForwardIterator first2x = (ForwardIterator) first2.clone();
  44.  
  45.     while( !first1x.equals( last1 ) )
  46.       {
  47.       iterSwap( first1x, first2x );
  48.       first1x.advance();
  49.       first2x.advance();
  50.       }
  51.  
  52.     return first2x;
  53.     }
  54.  
  55.   /**
  56.    * Swap the elements in one container with the elements in another container of the
  57.    * same size.
  58.    * @param container1 The first container.
  59.    * @param container2 The second container.
  60.    * @return An iterator positioned immediately after the last element in the second container.
  61.    */
  62.   public static ForwardIterator swapRanges( Container container1, Container container2 )
  63.     {
  64.     return swapRanges( container1.start(), container1.finish(), container2.start() );
  65.     }
  66.   }
  67.  
  68.