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

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * The Copying class contains generic copying algorithms.
  8.  * <p>
  9.  * @see jgl.examples.CopyingExamples
  10.  * @version 1.1
  11.  * @author ObjectSpace, Inc.
  12.  */
  13.  
  14. public final class Copying
  15.   {
  16.   private Copying()
  17.     {
  18.     }
  19.  
  20.   /**
  21.    * Copy the elements from one range to another range of the same size. 
  22.    * Time complexity is linear and space complexity is constant.
  23.    * @param first An iterator positioned at the first element of the input range.
  24.    * @param last An iterator positioned immediately after the last element of the input range.
  25.    * @param result An iterator positioned at the first element of the output range.
  26.    * @return An iterator positioned immediately after the last element of the output range.
  27.    */
  28.   public static OutputIterator copy( InputIterator first, InputIterator last, OutputIterator result )
  29.     {
  30.     InputIterator firstx = (InputIterator) first.clone();
  31.     OutputIterator resultx = (OutputIterator) result.clone();
  32.  
  33.     while( !firstx.equals( last ) )
  34.       {
  35.       resultx.put( firstx.nextElement() );
  36.       resultx.advance();
  37.       }
  38.  
  39.     return resultx;
  40.     }
  41.  
  42.   /**
  43.    * Copy the elements from a container to a sequence. 
  44.    * Time complexity is linear and space complexity is constant.
  45.    * @param input The input container.
  46.    * @param result An iterator positioned at the first element of the output sequence.
  47.    * @return An iterator positioned immediately after the last element of the output sequence.
  48.    */
  49.   public static OutputIterator copy( Container input, OutputIterator result )
  50.     {
  51.     return copy( input.start(), input.finish(), result );
  52.     }
  53.  
  54.   /**
  55.    * Copy the elements from one container to another container. 
  56.    * Time complexity is linear and space complexity is constant.
  57.    * @param source The source container.
  58.    * @param destination The destination container.
  59.    */
  60.   public static void copy( Container source, Container destination )
  61.     {
  62.     copy( source.start(), source.finish(), new InsertIterator( destination ) );
  63.     }
  64.  
  65.   /**
  66.    * Copy the elements backwards from one range to another range of the same size. 
  67.    * Time complexity is linear and space complexity is constant.
  68.    * @param first An iterator positioned at the first element of the input range.
  69.    * @param last An iterator positioned immediately after the last element of the input range.
  70.    * @param result An iterator positioned immediately after the last element of the output range.
  71.    * @return An iterator positioned at the first element of the output range.
  72.    */
  73.   public static OutputIterator copyBackward( BidirectionalIterator first, BidirectionalIterator last, BidirectionalIterator result )
  74.     {
  75.     BidirectionalIterator lastx = (BidirectionalIterator) last.clone();
  76.     BidirectionalIterator resultx = (BidirectionalIterator) result.clone();
  77.  
  78.     while( !first.equals( lastx ) )
  79.       {
  80.       resultx.retreat();
  81.       lastx.retreat();
  82.       resultx.put( lastx.get() );
  83.       }
  84.  
  85.     return resultx;
  86.     }
  87.   }
  88.  
  89.