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

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * The Counting class contains generic counting algorithms.
  8.  * <p>
  9.  * @see jgl.examples.CountingExamples
  10.  * @version 1.1
  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 first An iterator positioned at the first element in the range.
  76.    * @param last An iterator positioned immediately after the last element in the range.
  77.    * @param predicate A unary predicate.
  78.    * @return The number of objects that matched.
  79.    */
  80.   public static int countIf( Container container, UnaryPredicate predicate )
  81.     {
  82.     return countIf( container.start(), container.finish(), predicate );
  83.     }
  84.   }
  85.  
  86.