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

  1. // Copyright(c) 1996 ObjectSpace, Inc.
  2. // Portions Copyright(c) 1995, 1996 Hewlett-Packard Company.
  3.  
  4. package jgl;
  5.  
  6. /**
  7.  * The Comparing class contains generic comparison algorithms.
  8.  * <p>
  9.  * @see jgl.examples.ComparingExamples
  10.  * @version 1.1
  11.  * @author ObjectSpace, Inc.
  12.  */
  13.  
  14. public final class Comparing
  15.   {
  16.   private Comparing()
  17.     {
  18.     }
  19.  
  20.   /**
  21.    * Return the median value of three objects, using a comparator to perform
  22.    * the comparisons.
  23.    * @param a The first object.
  24.    * @param b The second object.
  25.    * @param c The third object.
  26.    * @param comparator The comparator object.
  27.    * @return The median value.
  28.    */
  29.   public static Object median( Object a, Object b, Object c, BinaryPredicate comparator )
  30.     {
  31.     if( comparator.execute( a, b ) )
  32.       {
  33.       if( comparator.execute( b, c ) )
  34.         {
  35.         return b;
  36.         }
  37.       else if( comparator.execute( a, c ) )
  38.         {
  39.         return c;
  40.         }
  41.       else
  42.         {
  43.         return a;
  44.         }
  45.       }
  46.     else if( comparator.execute( a, c ) )
  47.       {
  48.       return a;
  49.       }
  50.     else if( comparator.execute( b, c ) )
  51.       {
  52.       return c;
  53.       }
  54.     else
  55.       {
  56.       return b;
  57.       }
  58.     }
  59.  
  60.   /**
  61.    * Scan two sequences and return a pair of iterators that are positioned at the first
  62.    * mismatched elements. Use equals() to perform the comparison. If the first iterator 
  63.    * reaches past the end of the first sequence, stop the scan and return the iterator's 
  64.    * values at that point. Time complexity is linear and space complexity is constant.
  65.    * @param first1 An iterator positioned at the first element of the first sequence.
  66.    * @param last1 An iterator positioned immediately after the last element of the first sequence.
  67.    * @param first2 An iterator positioned at the first element of the second sequence.
  68.    * @return A pair of iterators positioned at the first mismatched elements.
  69.    */
  70.   public static Pair mismatch( InputIterator first1, InputIterator last1, InputIterator first2 )
  71.     {
  72.     InputIterator first1x = (InputIterator) first1.clone();
  73.     InputIterator first2x = (InputIterator) first2.clone();
  74.  
  75.     while( (!first1x.equals( last1 )) && first1x.get().equals( first2x.get()) )
  76.       {
  77.       first1x.advance();
  78.       first2x.advance();
  79.       }
  80.  
  81.     return new Pair( first1x, first2x );
  82.     }
  83.  
  84.   /**
  85.    * Scan two containers and return a pair of iterators that are positioned at the first
  86.    * mismatched elements. Use equals() to perform the comparison. If the first iterator 
  87.    * reaches past the end of the first container, stop the scan and return the iterator's 
  88.    * values at that point. Time complexity is linear and space complexity is constant.
  89.    * @param container1 The first container.
  90.    * @param container2 The second container.
  91.    * @return A pair of iterators positioned at the first mismatched elements.
  92.    */
  93.   public static Pair mismatch( Container container1, Container container2 )
  94.     {
  95.     return mismatch( container1.start(), container1.finish(), container2.start() );
  96.     }
  97.  
  98.   /**
  99.    * Scan two sequences and return a pair of iterators that are positioned at the first
  100.    * mismatched elements. Use the specified predicate to perform the comparison. If the first iterator 
  101.    * reaches past the end of the first sequence, stop the scan and return the iterator's 
  102.    * values at that point. Time complexity is linear and space complexity is constant.
  103.    * @param first1 An iterator positioned at the first element of the first sequence.
  104.    * @param last1 An iterator positioned immediately after the last element of the first sequence.
  105.    * @param first2 An iterator positioned at the first element of the second sequence.
  106.    * @param predicate A binary function.
  107.    * @return A pair of iterators positioned at the first mismatched elements.
  108.    */
  109.   public static Pair mismatch( InputIterator first1, InputIterator last1, InputIterator first2, BinaryPredicate predicate )
  110.     {
  111.     InputIterator first1x = (InputIterator) first1.clone();
  112.     InputIterator first2x = (InputIterator) first2.clone();
  113.  
  114.     while( (!first1x.equals( last1 )) && predicate.execute( first1x.get(), first2x.get() ) )
  115.       {
  116.       first1x.advance();
  117.       first2x.advance();
  118.       }
  119.  
  120.     return new Pair( first1x, first2x );
  121.     }
  122.  
  123.   /**
  124.    * Scan two containers and return a pair of iterators that are positioned at the first
  125.    * mismatched elements. Use the specified predicate to perform the comparison. If the first iterator 
  126.    * reaches past the end of the first container, stop the scan and return the iterator's 
  127.    * values at that point. Time complexity is linear and space complexity is constant.
  128.    * @param container1 The first container.
  129.    * @param container2 The second container.
  130.    * @param predicate A binary predicate.
  131.    * @return A pair of iterators positioned at the first mismatched elements.
  132.    */
  133.   public static Pair mismatch( Container container1, Container container2, BinaryPredicate predicate )
  134.     {
  135.     return mismatch( container1.start(), container1.finish(), container2.start(), predicate );
  136.     }
  137.  
  138.   /**
  139.    * Scan two sequences of the same size and return true if every element in one
  140.    * sequence matches its counterpart using equals(). The time complexity is linear and
  141.    * the space complexity is constant.
  142.    * @param first1 An iterator positioned at the first element of the first sequence.
  143.    * @param last1 An iterator positioned immediately after the last element of the first sequence.
  144.    * @param first2 An iterator positioned at the first element of the second sequence.
  145.    * @return true if the sequences match.
  146.    */
  147.   public static boolean equal( InputIterator first1, InputIterator last1, InputIterator first2 )
  148.     {
  149.     InputIterator first1x = (InputIterator) first1.clone();
  150.     InputIterator first2x = (InputIterator) first2.clone();
  151.  
  152.     while( !first1x.equals( last1 ) )
  153.       {
  154.       if ( !first1x.get().equals( first2x.get() ) )
  155.         return false;
  156.  
  157.       first1x.advance();
  158.       first2x.advance();
  159.       }
  160.  
  161.     return true;
  162.     }
  163.  
  164.   /**
  165.    * Scan two containers and return true if the containers are the same size and every 
  166.    * element in one container matches its counterpart using equals(). The time complexity is 
  167.    * linear and the space complexity is constant.
  168.    * @param container1 The first container.
  169.    * @param container2 The second container.
  170.    * @return true if the containers match.
  171.    */
  172.   public static boolean equal( Container container1, Container container2 )
  173.     {
  174.     return container1.size() == container2.size() && equal( container1.start(), container1.finish(), container2.start() );
  175.     }
  176.  
  177.   /**
  178.    * Return true if one sequence is lexicographically less than another. 
  179.    * Use a HashComparator to compare corresponding elements. The time complexity is
  180.    * linear and the space complexity is constant.
  181.    * @param first1 An iterator positioned at the first element of the first sequence.
  182.    * @param last1 An iterator positioned immediately after the last element of the first sequence.
  183.    * @param first2 An iterator positioned at the first element of the second sequence.
  184.    * @param last2 An iterator positioned immediately after the last element of the second sequence.
  185.    * @return true if the first sequence is lexicographically less than the second.
  186.    */
  187.   public static boolean lexicographicalCompare( InputIterator first1, InputIterator last1, InputIterator first2, InputIterator last2 )
  188.     {
  189.     return lexicographicalCompare( first1, last1, first2, last2, new HashComparator() );
  190.     }
  191.  
  192.   /**
  193.    * Return true if one container is lexicographically less than another. 
  194.    * Use a HashComparator to compare corresponding elements. The time complexity is
  195.    * linear and the space complexity is constant.
  196.    * @param container1 The first container.
  197.    * @param container2 The second container.
  198.    * @return true if the first container is lexicographically less than the second.
  199.    */
  200.   public static boolean lexicographicalCompare( Container container1, Container container2 )
  201.     {
  202.     return lexicographicalCompare( container1.start(), container1.finish(), container2.start(), container2.finish() );
  203.     }  
  204.  
  205.   /**
  206.    * Return true if one sequence is lexicographically less than another. 
  207.    * Use a specified comparator to compare corresponding elements. The time complexity is
  208.    * linear and the space complexity is constant.
  209.    * @param first1 An iterator positioned at the first element of the first sequence.
  210.    * @param last1 An iterator positioned immediately after the last element of the first sequence.
  211.    * @param first2 An iterator positioned at the first element of the second sequence.
  212.    * @param last2 An iterator positioned immediately after the last element of the second sequence.
  213.    * @param comparator A binary predicate that returns true if its first operand is "less" than its second operand.
  214.    * @return true if the first sequence is lexicographically less than the second.
  215.    */
  216.   public static boolean lexicographicalCompare( InputIterator first1, InputIterator last1, InputIterator first2, InputIterator last2, BinaryPredicate comparator )
  217.     {
  218.     InputIterator first1x = (InputIterator) first1.clone();
  219.     InputIterator first2x = (InputIterator) first2.clone();
  220.  
  221.     while( !first1x.equals( last1 ) && !first2x.equals( last2 ) )
  222.       {
  223.       if( comparator.execute( first1x.get(), first2x.get() ) )
  224.         return true;
  225.  
  226.       if( comparator.execute( first2x.get(), first1x.get() ) )
  227.         return false;
  228.  
  229.       first1x.advance();
  230.       first2x.advance();
  231.       }
  232.  
  233.     return first1x.equals( last1 ) && !first2x.equals( last2 );
  234.     }
  235.  
  236.   /**
  237.    * Return true if one container is lexicographically less than another. 
  238.    * Use a specified comparator to compare corresponding elements. The time complexity is
  239.    * linear and the space complexity is constant.
  240.    * @param container1 The first container.
  241.    * @param container2 The second container.
  242.    * @param comparator A binary predicate that returns true if its first operand is "less" than its second operand.
  243.    * @return true if the first container is lexicographically less than the second.
  244.    */
  245.   public static boolean lexicographicalCompare( Container container1, Container container2, BinaryPredicate comparator )
  246.     {
  247.     return lexicographicalCompare( container1.start(), container1.finish(), container2.start(), container2.finish(), comparator );
  248.     }
  249.   }
  250.  
  251.