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

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * The Removing class contains generic removing algorithms.
  8.  * <p>
  9.  * @see jgl.examples.RemovingExamples
  10.  * @version 1.1
  11.  * @author ObjectSpace, Inc.
  12.  */
  13.  
  14. public final class Removing
  15.   {
  16.   private Removing()
  17.     {
  18.     }
  19.  
  20.   /**
  21.    * Remove all occurrences of an object from a sequence. The size of the sequence 
  22.    * is not altered; if n elements are removed, the last n elements of the sequence 
  23.    * will have undefined values. The time complexity is linear and the space complexity 
  24.    * is constant.
  25.    * @param first An iterator positioned at the first element of the sequence.
  26.    * @param last An iterator positioned immediately after the last element of the sequence.
  27.    * @param object The object to remove.
  28.    * @return An iterator positioned immediately after the last remaining element.
  29.    */
  30.   public static ForwardIterator remove( ForwardIterator first, ForwardIterator last, Object object )
  31.     {
  32.     if ( !(first.getContainer() instanceof Sequence) )
  33.       throw new IllegalArgumentException( "iterator containers must be a Sequence" );
  34.  
  35.     first = (ForwardIterator) Finding.find( first, last, object );
  36.  
  37.     if( first.equals( last ) )
  38.       {
  39.       return first;
  40.       }
  41.     else
  42.       {
  43.       ForwardIterator i = (ForwardIterator) first.clone();
  44.       i.advance();
  45.       return (ForwardIterator) removeCopy( i, last, first, object );
  46.       }
  47.     }
  48.  
  49.   /**
  50.    * Remove all occurrences of an object from a sequence. The size of the container 
  51.    * is not altered; if n elements are removed, the last n elements of the container 
  52.    * will have undefined values. The time complexity is linear and the space complexity 
  53.    * is constant.
  54.    * @param container The container.
  55.    * @param object The object to remove.
  56.    * @return An iterator positioned immediately after the last remaining element.
  57.    */
  58.   public static ForwardIterator remove( Sequence container, Object object )
  59.     {
  60.     return remove( container.start(), container.finish(), object );
  61.     }
  62.  
  63.   /**
  64.    * Remove all objects in a sequence that satisfy a predicate from a sequence. 
  65.    * The size of the sequence is not altered; if n elements are removed, the last n 
  66.    * elements of the sequence will have undefined values. The time complexity is linear 
  67.    * and the space complexity is constant.
  68.    * @param first An iterator positioned at the first element of the sequence.
  69.    * @param last An iterator positioned immediately after the last element of the sequence.
  70.    * @param predicate A unary predicate.
  71.    * @return An iterator positioned immediately after the last remaining element.
  72.    */
  73.   public static ForwardIterator removeIf( ForwardIterator first, ForwardIterator last, UnaryPredicate predicate )
  74.     {
  75.     if ( !(first.getContainer() instanceof Sequence) )
  76.       throw new IllegalArgumentException( "iterator containers must be a Sequence" );
  77.  
  78.     first = (ForwardIterator) Finding.findIf( first, last, predicate );
  79.  
  80.     if( first.equals( last ) )
  81.       {
  82.       return first;
  83.       }
  84.     else
  85.       {
  86.       ForwardIterator i = (ForwardIterator) first.clone();
  87.       i.advance();
  88.       return (ForwardIterator) removeCopyIf( i, last, first, predicate );
  89.       }
  90.     }
  91.  
  92.   /**
  93.    * Remove all objects in a sequence that satisfy a predicate from a container. 
  94.    * The size of the container is not altered; if n elements are removed, the last n 
  95.    * elements of the container will have undefined values. The time complexity is linear 
  96.    * and the space complexity is constant.
  97.    * @param container The container.
  98.    * @param predicate A unary predicate.
  99.    * @return An iterator positioned immediately after the last remaining element.
  100.    */
  101.   public static ForwardIterator removeIf( Sequence container, UnaryPredicate predicate )
  102.     {
  103.     return removeIf( container.start(), container.finish(), predicate );
  104.     }
  105.  
  106.   /**
  107.    * Copy one sequence to another sequence, skipping any occurrences of a particular 
  108.    * object. The time complexity is linear and the space complexity is constant.
  109.    * @param first An iterator positioned at the first element of the input sequence.
  110.    * @param last An iterator positioned immediately after the last element of the input sequence.
  111.    * @param result An iterator positioned at the first element of the output sequence.
  112.    * @param object The object to remove.
  113.    * @return An iterator positioned immediately after the last element of the output sequence.
  114.    */
  115.   public static OutputIterator removeCopy( InputIterator first, InputIterator last, OutputIterator result, Object object )
  116.     {
  117.     InputIterator firstx = (InputIterator) first.clone();
  118.     OutputIterator resultx = (OutputIterator) result.clone();
  119.  
  120.     while( !firstx.equals( last ) )
  121.       {
  122.       if( !firstx.get().equals( object ) )
  123.         {
  124.         resultx.put( firstx.get() );
  125.         resultx.advance();
  126.         }
  127.  
  128.       firstx.advance();
  129.       }
  130.  
  131.     return resultx;
  132.     }
  133.  
  134.   /**
  135.    * Copy a container to a sequence, skipping any occurrences of a particular 
  136.    * object. The time complexity is linear and the space complexity is constant.
  137.    * @param container The source container.
  138.    * @param result An iterator positioned at the first element of the output sequence.
  139.    * @param object The object to remove.
  140.    * @return An iterator positioned immediately after the last element of the output sequence.
  141.    */
  142.   public static OutputIterator removeCopy( Container container, OutputIterator result, Object object )
  143.     {
  144.     return removeCopy( container.start(), container.finish(), result, object );
  145.     }
  146.  
  147.   /**
  148.    * Copy one container to another container, skipping any occurrences of a particular 
  149.    * object. The time complexity is linear and the space complexity is constant.
  150.    * @param source The source container.
  151.    * @param destination The destination container.
  152.    * @param object The object to remove.
  153.    */
  154.   public static void removeCopy( Container source, Container destination, Object object )
  155.     {
  156.     removeCopy( source.start(), source.finish(), new InsertIterator( destination ), object );
  157.     }
  158.  
  159.   /**
  160.    * Copy one sequence to another sequence, skipping all objects that satisfy a predicate.
  161.    * The time complexity is linear and the space complexity is constant.
  162.    * @param first An iterator positioned at the first element of the input sequence.
  163.    * @param last An iterator positioned immediately after the last element of the input sequence.
  164.    * @param result An iterator positioned at the first element of the output sequence.
  165.    * @param predicate A UnaryPredicate.
  166.    * @return An iterator positioned immediately after the last element of the output sequence.
  167.    */
  168.   public static OutputIterator removeCopyIf( InputIterator first, InputIterator last, OutputIterator result, UnaryPredicate predicate )
  169.     {
  170.     InputIterator firstx = (InputIterator) first.clone();
  171.     OutputIterator resultx = (OutputIterator) result.clone();
  172.  
  173.     while( !firstx.equals( last ) )
  174.       {
  175.       if( !predicate.execute( firstx.get() ) )
  176.         {
  177.         resultx.put( firstx.get() );
  178.         resultx.advance();
  179.         }
  180.  
  181.       firstx.advance();
  182.       }
  183.  
  184.     return resultx;
  185.     }
  186.  
  187.   /**
  188.    * Copy a container to a sequence, skipping all objects that satisfy a predicate.
  189.    * The time complexity is linear and the space complexity is constant.
  190.    * @param container The source container.
  191.    * @param result An iterator positioned at the first element of the output sequence.
  192.    * @param predicate A UnaryPredicate.
  193.    * @return An iterator positioned immediately after the last element of the output sequence.
  194.    */
  195.   public static OutputIterator removeCopyIf( Container container, OutputIterator result, UnaryPredicate predicate )
  196.     {
  197.     return removeCopyIf( container.start(), container.finish(), result, predicate );
  198.     }
  199.  
  200.   /**
  201.    * Copy one container to another container, skipping all objects that satisfy a predicate.
  202.    * The time complexity is linear and the space complexity is constant.
  203.    * @param source The source container.
  204.    * @param destination The destination container.
  205.    * @param predicate A unary predicate.
  206.    */
  207.   public static void removeCopyIf( Container source, Container destination, UnaryPredicate predicate )
  208.     {
  209.     removeCopyIf( source.start(), source.finish(), new InsertIterator( destination ), predicate );
  210.     }
  211.   }
  212.  
  213.