home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Extras / OSpace / jgl.exe / jgl_2_0 / src / COM / objectspace / jgl / Counting.java < prev    next >
Encoding:
Java Source  |  1997-03-14  |  10.2 KB  |  273 lines

  1. // Copyright(c) 1996,1997 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package COM.objectspace.jgl;
  5.  
  6. /**
  7.  * The Counting class contains generic counting algorithms.
  8.  * <p>
  9.  * @see COM.objectspace.jgl.examples.CountingExamples
  10.  * @version 2.0.2
  11.  * @author ObjectSpace, Inc.
  12.  */
  13.  
  14. public final class Counting
  15.   {
  16.   private Counting()
  17.     {
  18.     }
  19.  
  20.   /**
  21.    * Return the number of elements in a range that match a particular object using
  22.    * equals(). The time complexity is linear and the space complexity is constant.
  23.    * @param first An iterator positioned at the first element in the range.
  24.    * @param last An iterator positioned immediately after the last element in the range.
  25.    * @param object The object to count.
  26.    * @return The number of objects that matched.
  27.    */
  28.   public static int count( InputIterator first, InputIterator last, Object object )
  29.     {
  30.     InputIterator firstx = (InputIterator) first.clone();
  31.     int n = 0;
  32.  
  33.     while ( !firstx.equals( last ) )
  34.       if ( firstx.nextElement().equals( object ) )
  35.         ++n;
  36.  
  37.     return n;
  38.     }
  39.  
  40.   /**
  41.    * Return the number of elements in a container that match a particular object using
  42.    * equals(). The time complexity is linear and the space complexity is constant.
  43.    * @param container The container.
  44.    * @param object The object to count.
  45.    * @return The number of objects that matched.
  46.    */
  47.   public static int count( Container container, Object object )
  48.     {
  49.     return count( container.start(), container.finish(), object );
  50.     }
  51.  
  52.   /**
  53.    * Return the number of elements in a range that satisfy a predicate.
  54.    * The time complexity is linear and the space complexity is constant.
  55.    * @param first An iterator positioned at the first element in the range.
  56.    * @param last An iterator positioned immediately after the last element in the range.
  57.    * @param predicate A unary predicate.
  58.    * @return The number of objects that matched.
  59.    */
  60.   public static int countIf( InputIterator first, InputIterator last, UnaryPredicate predicate )
  61.     {
  62.     InputIterator firstx = (InputIterator) first.clone();
  63.     int n = 0;
  64.  
  65.     while ( !firstx.equals( last ) )
  66.       if ( predicate.execute( firstx.nextElement() ) )
  67.         ++n;
  68.  
  69.     return n;
  70.     }
  71.  
  72.   /**
  73.    * Return the number of elements in a container that satisfy a predicate.
  74.    * The time complexity is linear and the space complexity is constant.
  75.    * @param container The container.
  76.    * @param predicate A unary predicate.
  77.    * @return The number of objects that matched.
  78.    */
  79.   public static int countIf( Container container, UnaryPredicate predicate )
  80.     {
  81.     return countIf( container.start(), container.finish(), predicate );
  82.     }
  83.  
  84.   /**
  85.    * Add the value of each element in a range to an inital value and return the
  86.    * sum.  All elements the iterators represent must be instances of
  87.    * java.lang.Number.  Use PlusNumber( init.getClass() ) to perform the addition.
  88.    * @param first An iterator positioned at the first element in the range.
  89.    * @param last An iterator positioned immediately after the last element in the range.
  90.    * @return The number of objects that matched.
  91.    * @see java.lang.Number
  92.    * @see COM.objectspace.jgl.PlusNumber
  93.    */
  94.   public static Number accumulate( InputIterator first, InputIterator last, Number init )
  95.     {
  96.     return accumulate( first, last, init, new PlusNumber( init.getClass() ) );
  97.     }
  98.  
  99.   /**
  100.    * Add the value of each element in a container to an inital value and return the
  101.    * sum.  All elements the iterators represent must be instances of
  102.    * java.lang.Number.  Use PlusNumber( init.getClass() ) to perform the addition.
  103.    * @param container The container.
  104.    * @return The number of objects that matched.
  105.    * @see java.lang.Number
  106.    * @see COM.objectspace.jgl.PlusNumber
  107.    */
  108.   public static Number accumulate( Container container, Number init )
  109.     {
  110.     return accumulate
  111.       (
  112.       container.start(),
  113.       container.finish(),
  114.       init,
  115.       new PlusNumber( init.getClass() )
  116.       );
  117.     }
  118.  
  119.   /**
  120.    * Add the value of each element in a range to an inital value and return the
  121.    * sum.  All elements the iterators represent must be instances of
  122.    * java.lang.Number.  Use a specified binary function to perform the addition.
  123.    * @param first An iterator positioned at the first element in the range.
  124.    * @param last An iterator positioned immediately after the last element in the range.
  125.    * @param function A binary function.
  126.    * @return The number of objects that matched.
  127.    * @see java.lang.Number
  128.    */
  129.   public static Number accumulate( InputIterator first, InputIterator last, Number init, BinaryFunction function )
  130.     {
  131.     InputIterator firstx = (InputIterator)first.clone();
  132.  
  133.     while ( !firstx.equals( last ) )
  134.       init = (Number)function.execute( init, firstx.nextElement() );
  135.  
  136.     return init;
  137.     }
  138.  
  139.   /**
  140.    * Add the value of each element in a container to an inital value and return the
  141.    * sum.  All elements the iterators represent must be instances of
  142.    * java.lang.Number.  Use a specified binary function to perform the addition.
  143.    * @param container The container.
  144.    * @return The number of objects that matched.
  145.    * @see java.lang.Number
  146.    */
  147.   public static Number accumulate( Container container, Number init, BinaryFunction function )
  148.     {
  149.     return accumulate( container.start(), container.finish(), init, function );
  150.     }
  151.  
  152.   /**
  153.    * Iterate through every element in a range and calculate the difference between
  154.    * each element and its preceding element.
  155.    * Use MinusNumber() to calculate the difference.
  156.    * Assignment back into the original range is allowed.
  157.    * @param first An iterator positioned at the first element in the range.
  158.    * @param last An iterator positioned immediately after the last element in the range.
  159.    * @param result An iterator positioned at the first element of the output range.
  160.    * @return An iterator positioned immediately after the last element of the output range.
  161.    */
  162.   public static OutputIterator adjacentDifference( InputIterator first, InputIterator last, OutputIterator result )
  163.     {
  164.     return adjacentDifference( first, last, result, new MinusNumber() );
  165.     }
  166.  
  167.   /**
  168.    * Iterate through every element in a container and calculate the difference between
  169.    * each element and its preceding element.
  170.    * Use MinusNumber() to calculate the difference.
  171.    * Assignment back into the original range is allowed.
  172.    * @param input The input container.
  173.    * @param result An iterator positioned at the first element of the output range.
  174.    * @return An iterator positioned immediately after the last element of the output range.
  175.    */
  176.   public static OutputIterator adjacentDifference( Container input, OutputIterator result )
  177.     {
  178.     return adjacentDifference
  179.       (
  180.       input.start(),
  181.       input.finish(),
  182.       result,
  183.       new MinusNumber()
  184.       );
  185.     }
  186.  
  187.   /**
  188.    * Iterate through every element in a container and calculate the difference between
  189.    * each element and its preceding element.
  190.    * Use MinusNumber() to calculate the difference.
  191.    * Assignment back into the original range is allowed.
  192.    * @param source The source container.
  193.    * @param destination The destination container.
  194.    * @return An iterator positioned immediately after the last element of the output range.
  195.    */
  196.   public static OutputIterator adjacentDifference( Container source, Container destination )
  197.     {
  198.     return adjacentDifference
  199.       (
  200.       source.start(),
  201.       source.finish(),
  202.       new InsertIterator( destination ),
  203.       new MinusNumber()
  204.       );
  205.     }
  206.  
  207.   /**
  208.    * Iterate through every element in a range and calculate the difference between
  209.    * each element and its preceding element.
  210.    * Assignment back into the original range is allowed.
  211.    * @param first An iterator positioned at the first element in the range.
  212.    * @param last An iterator positioned immediately after the last element in the range.
  213.    * @param result An iterator positioned at the first element of the output range.
  214.    * @param function A binary function.
  215.    * @return An iterator positioned immediately after the last element of the output range.
  216.    */
  217.   public static OutputIterator adjacentDifference( InputIterator first, InputIterator last, OutputIterator result, BinaryFunction function )
  218.     {
  219.     OutputIterator resultx = (OutputIterator)result.clone();
  220.     if ( first.equals( last ) )
  221.       return resultx;
  222.  
  223.     InputIterator firstx = (InputIterator)first.clone();
  224.     resultx.put( firstx.get() );
  225.     resultx.advance();
  226.  
  227.     Object value = firstx.nextElement();
  228.     while ( !firstx.equals( last ) )
  229.       {
  230.       Object tmp = firstx.nextElement();
  231.       resultx.put( function.execute( tmp, value ) );
  232.       resultx.advance();
  233.       value = tmp;
  234.       }
  235.  
  236.     return resultx;
  237.     }
  238.  
  239.   /**
  240.    * Iterate through every element in a container and calculate the difference between
  241.    * each element and its preceding element.
  242.    * Assignment back into the original range is allowed.
  243.    * @param input The input container.
  244.    * @param result An iterator positioned at the first element of the output range.
  245.    * @param function A binary function.
  246.    * @return An iterator positioned immediately after the last element of the output range.
  247.    */
  248.   public static OutputIterator adjacentDifference( Container input, OutputIterator result, BinaryFunction function )
  249.     {
  250.     return adjacentDifference( input.start(), input.finish(), result, function );
  251.     }
  252.  
  253.   /**
  254.    * Iterate through every element in a container and calculate the difference between
  255.    * each element and its preceding element.
  256.    * Assignment back into the original range is allowed.
  257.    * @param source The source container.
  258.    * @param destination The destination container.
  259.    * @param function A binary function.
  260.    * @return An iterator positioned immediately after the last element of the output range.
  261.    */
  262.   public static OutputIterator adjacentDifference( Container source, Container destination, BinaryFunction function )
  263.     {
  264.     return adjacentDifference
  265.       (
  266.       source.start(),
  267.       source.finish(),
  268.       new InsertIterator( destination ),
  269.       function
  270.       );
  271.     }
  272.   }
  273.