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

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * The MinMax class contains generic min/max algorithms.
  8.  * <p>
  9.  * @see jgl.examples.MinMaxExamples
  10.  * @version 1.1
  11.  * @author ObjectSpace, Inc.
  12.  */
  13.  
  14. public final class MinMax
  15.   {
  16.   private MinMax()
  17.     {
  18.     }
  19.  
  20.   /**
  21.    * Find the maximum element in a sequence. Compare objects based on the hash code.
  22.    * The time complexity is linear and the space complexity is constant.
  23.    * @param first An iterator positioned at the first element in the sequence.
  24.    * @param last An iterator positioned immediately after the last element in the sequence.
  25.    * @return An iterator positioned at the maximum element in the sequence.
  26.    */
  27.   public static InputIterator maxElement( InputIterator first, InputIterator last )
  28.     {
  29.     return maxElement( first, last, new HashComparator() );
  30.     }
  31.  
  32.   /**
  33.    * Find the maximum element in a container. Compare objects based on the hash code.
  34.    * The time complexity is linear and the space complexity is constant.
  35.    * @param container The container.
  36.    * @param last An iterator positioned immediately after the last element in the sequence.
  37.    * @return An iterator positioned at the maximum element in the container.
  38.    */
  39.   public static InputIterator maxElement( Container container )
  40.     {
  41.     return maxElement( container.start(), container.finish(), new HashComparator() );
  42.     }
  43.  
  44.   /**
  45.    * Find the maximum element in a sequence. Use a comparator to perform the comparison.
  46.    * The time complexity is linear and the space complexity is constant.
  47.    * @param first An iterator positioned at the first element in the sequence.
  48.    * @param last An iterator positioned immediately after the last element in the sequence.
  49.    * @param comparator A binary predicate that returns true if the first operand is "less" than the second operand.
  50.    * @return An iterator positioned at the maximum element in the sequence.
  51.    */
  52.   public static InputIterator maxElement( InputIterator first, InputIterator last, BinaryPredicate comparator )
  53.     {
  54.     InputIterator firstx = (InputIterator) first.clone();
  55.  
  56.     if( firstx.equals( last ) )
  57.       return firstx;
  58.  
  59.     InputIterator result = (InputIterator) firstx.clone();
  60.     firstx.advance();
  61.  
  62.     while( !firstx.equals( last ) )
  63.       {
  64.       if( comparator.execute( result.get(), firstx.get() ) )
  65.         result = (InputIterator) firstx.clone();
  66.  
  67.       firstx.advance();
  68.       }
  69.  
  70.     return result;
  71.   }
  72.  
  73.   /**
  74.    * Find the maximum element in a container. Use a comparator to perform the comparison.
  75.    * The time complexity is linear and the space complexity is constant.
  76.    * @param container The container.
  77.    * @param comparator A binary predicate that returns true if the first operand is "less" than the second operand.
  78.    * @return An iterator positioned at the maximum element in the container.
  79.    */
  80.   public static InputIterator maxElement( Container container, BinaryPredicate comparator )
  81.     {
  82.     return maxElement( container.start(), container.finish(), comparator );
  83.     }
  84.  
  85.   /**
  86.    * Find the minimum element in a sequence. Compare objects based on the hash code.
  87.    * The time complexity is linear and the space complexity is constant.
  88.    * @param first An iterator positioned at the first element in the sequence.
  89.    * @param last An iterator positioned immediately after the last element in the sequence.
  90.    * @return An iterator positioned at the minimum element in the sequence.
  91.    */
  92.   public static InputIterator minElement( InputIterator first, InputIterator last )
  93.     {
  94.     return minElement( first, last, new HashComparator() );
  95.     }
  96.  
  97.   /**
  98.    * Find the minimum element in a container. Compare objects based on the hash code.
  99.    * The time complexity is linear and the space complexity is constant.
  100.    * @param container The container.
  101.    * @return An iterator positioned at the minimum element in the container.
  102.    */
  103.   public static InputIterator minElement( Container container )
  104.     {
  105.     return minElement( container.start(), container.finish(), new HashComparator() );
  106.     }
  107.  
  108.   /**
  109.    * Find the minimum element in a sequence. Use a comparator to perform the comparison.
  110.    * The time complexity is linear and the space complexity is constant.
  111.    * @param first An iterator positioned at the first element in the sequence.
  112.    * @param last An iterator positioned immediately after the last element in the sequence.
  113.    * @param comparator A binary predicate that returns true if the first operand is "less" than the second operand.
  114.    * @return An iterator positioned at the minimum element in the sequence.
  115.    */
  116.   public static InputIterator minElement( InputIterator first, InputIterator last, BinaryPredicate comparator )
  117.     {
  118.     InputIterator firstx = (InputIterator) first.clone();
  119.  
  120.     if( firstx.equals( last ) )
  121.       return firstx;
  122.  
  123.     InputIterator result = (InputIterator) firstx.clone();
  124.     firstx.advance();
  125.  
  126.     while( !firstx.equals( last ) )
  127.       {
  128.       if( comparator.execute( firstx.get(), result.get() ) )
  129.         result = (InputIterator) firstx.clone();
  130.  
  131.       firstx.advance();
  132.       }
  133.  
  134.     return result;
  135.     }
  136.  
  137.   /**
  138.    * Find the minimum element in a container. Use a comparator to perform the comparison.
  139.    * The time complexity is linear and the space complexity is constant.
  140.    * @param container The container.
  141.    * @param comparator A binary predicate that returns true if the first operand is "less" than the second operand.
  142.    * @return An iterator positioned at the minimum element in the container.
  143.    */
  144.   public static InputIterator minElement( Container container, BinaryPredicate comparator )
  145.     {
  146.     return minElement( container.start(), container.finish(), comparator );
  147.     }
  148.   }
  149.  
  150.