net.sf.jga.algorithms
Class Compare

java.lang.Object
  extended by net.sf.jga.algorithms.Compare

public class Compare
extends java.lang.Object

Algorithms and utilities that compare two sets of input. The inputs may be arrays, collections, or iterations (although, in the case of iterations, the comparison also consumes elements from the iteration).

Copyright © 2007 David Hall


Nested Class Summary
static class Compare.IteratorComparator<T>
           
 
Constructor Summary
Compare()
           
 
Method Summary
static
<T extends java.lang.Comparable<? super T>>
int
compare(java.lang.Iterable<? extends T> c1, java.lang.Iterable<? extends T> c2)
          Compares the contents of the two iterable resources using a default comparator.
static
<T> int
compare(java.lang.Iterable<? extends T> c1, java.lang.Iterable<? extends T> c2, BinaryFunctor<T,T,java.lang.Boolean> bf)
          Compares the contents of the two iterable resources using a binary predicate that returns TRUE when its first argument is less than its second.
static
<T> int
compare(java.lang.Iterable<? extends T> c1, java.lang.Iterable<? extends T> c2, java.util.Comparator<? super T> comp)
          Compares the contents of the two iterable resources.
static
<T extends java.lang.Comparable<? super T>>
int
compare(java.util.Iterator<? extends T> i1, java.util.Iterator<? extends T> i2)
          Compares the contents of the two iterations using a default comparator.
static
<T> int
compare(java.util.Iterator<? extends T> i1, java.util.Iterator<? extends T> i2, BinaryFunctor<T,T,java.lang.Boolean> bf)
          Compares the contents of the two iterations using a predicate that returns TRUE if its first argument is less than its second.
static
<T> int
compare(java.util.Iterator<? extends T> i1, java.util.Iterator<? extends T> i2, java.util.Comparator<? super T> comp)
          Compares the contents of the two iterations using the given comparator.
static
<T extends java.lang.Comparable<? super T>>
int
compare(T[] a1, T[] a2)
          Compares the contents of the two arrays using a default comparator.
static
<T> int
compare(T[] a1, T[] a2, BinaryFunctor<T,T,java.lang.Boolean> bf)
          Compares the contents of the two arrays using a binary predicate that returns TRUE when its first argument is less than its second.
static
<T> int
compare(T[] a1, T[] a2, java.util.Comparator<T> comp)
          Compares the contents of the two arrays using the given comparator.
static
<T> boolean
equal(java.lang.Iterable<? extends T> c1, java.lang.Iterable<? extends T> c2)
          Returns true if the two resources are equal, using the equals() method to compare the elements of each resource
static
<T> boolean
equal(java.lang.Iterable<? extends T> c1, java.lang.Iterable<? extends T> c2, BinaryFunctor<T,T,java.lang.Boolean> eq)
          Returns true if the two resources are equal, using the given functor to compare the elements in each resources.
static
<T> boolean
equal(java.lang.Iterable<? extends T> c1, java.lang.Iterable<? extends T> c2, java.util.Comparator<T> comp)
          Returns true if the two resources are equal, using the given comparator to compare the elements in each resource
static
<T extends java.lang.Comparable<? super T>>
boolean
equal(java.util.Iterator<? extends T> i1, java.util.Iterator<? extends T> i2)
          Returns true if the two iterations are equal, using the Comparable interface to compare elements in the iterations.
static
<T> boolean
equal(java.util.Iterator<? extends T> i1, java.util.Iterator<? extends T> i2, BinaryFunctor<T,T,java.lang.Boolean> eq)
          Returns true if the two iterations are equal, using the given BinaryFunctor to compare elements in the iterations.
static
<T> boolean
equal(java.util.Iterator<? extends T> i1, java.util.Iterator<? extends T> i2, java.util.Comparator<T> comp)
          Returns true if the two iterations are equal, using the given Comparator to compare elements in the iterations.
static
<T> boolean
equal(T[] a1, T[] a2)
          Returns true if the two arrays are equal, using the equals() method to compare the elements of each array
static
<T> boolean
equal(T[] a1, T[] a2, BinaryFunctor<T,T,java.lang.Boolean> eq)
          Returns true if the two arrays are equal, using the given functor to compare the elements in each arrays.
static
<T> boolean
equal(T[] a1, T[] a2, java.util.Comparator<T> comp)
          Returns true if the two arrays are equal, using the given comparator to compare the elements in each array
static
<T extends java.lang.Comparable<? super T>>
boolean
lessThan(java.lang.Iterable<? extends T> c1, java.lang.Iterable<? extends T> c2)
          Returns true if the first resource is lexically less than the second, using the default comparison operation to compare the elements in each resource.
static
<T> boolean
lessThan(java.lang.Iterable<? extends T> c1, java.lang.Iterable<? extends T> c2, BinaryFunctor<T,T,java.lang.Boolean> lt)
          Returns true if the first resource is lexically less than the second, using the given operator to compare the elements in each collection.
static
<T> boolean
lessThan(java.lang.Iterable<? extends T> c1, java.lang.Iterable<? extends T> c2, java.util.Comparator<T> comp)
          Returns true if the first resource is lexically less than the second, using the given comparator to compare the elements in each resource.
static
<T extends java.lang.Comparable<? super T>>
boolean
lessThan(java.util.Iterator<? extends T> i1, java.util.Iterator<? extends T> i2)
          Returns true if the first iterator is lexically less than the second, using the default comparison operation to compare the elements in each iterator.
static
<T> boolean
lessThan(java.util.Iterator<? extends T> i1, java.util.Iterator<? extends T> i2, BinaryFunctor<T,T,java.lang.Boolean> lt)
          Returns true if the first iterator is lexically less than the second, using the given operator to compare the elements in each iterator.
static
<T> boolean
lessThan(java.util.Iterator<? extends T> i1, java.util.Iterator<? extends T> i2, java.util.Comparator<T> comp)
          Returns true if the first iterator is lexically less than the second, using the given comparator to compare the elements in each iterator.
static
<T extends java.lang.Comparable<? super T>>
boolean
lessThan(T[] a1, T[] a2)
          Returns true if the first array is lexically less than the second, using the default comparison operation to compare the elements in each array.
static
<T> boolean
lessThan(T[] a1, T[] a2, BinaryFunctor<T,T,java.lang.Boolean> lt)
          Returns true if the first array is lexically less than the second, using the given operator to compare the elements in each collection.
static
<T> boolean
lessThan(T[] a1, T[] a2, java.util.Comparator<T> comp)
          Returns true if the first array is lexically less than the second, using the given comparator to compare the elements in each array.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Compare

public Compare()
Method Detail

equal

public static <T> boolean equal(T[] a1,
                                T[] a2)
Returns true if the two arrays are equal, using the equals() method to compare the elements of each array

Returns:
true if the two arrays are equal

equal

public static <T> boolean equal(T[] a1,
                                T[] a2,
                                java.util.Comparator<T> comp)
Returns true if the two arrays are equal, using the given comparator to compare the elements in each array

Returns:
true if the two arrays are equal.

equal

public static <T> boolean equal(T[] a1,
                                T[] a2,
                                BinaryFunctor<T,T,java.lang.Boolean> eq)
Returns true if the two arrays are equal, using the given functor to compare the elements in each arrays. The functor is expected to evaluate its two argments and return true if they are "equal", therefore this method returns true if the arrays contain the same number of elements and if the functor returns true for all pairs of elements.

Returns:
true if the two collections are equal

lessThan

public static <T extends java.lang.Comparable<? super T>> boolean lessThan(T[] a1,
                                                                           T[] a2)
Returns true if the first array is lexically less than the second, using the default comparison operation to compare the elements in each array.

Returns:
true if c1 < c2

lessThan

public static <T> boolean lessThan(T[] a1,
                                   T[] a2,
                                   java.util.Comparator<T> comp)
Returns true if the first array is lexically less than the second, using the given comparator to compare the elements in each array.

Returns:
true if c1 < c2

lessThan

public static <T> boolean lessThan(T[] a1,
                                   T[] a2,
                                   BinaryFunctor<T,T,java.lang.Boolean> lt)
Returns true if the first array is lexically less than the second, using the given operator to compare the elements in each collection. The first is less than the second if it is not longer than the second and if the first corresponding element that is not equal is less.

Returns:
true if c1 < c2

compare

public static <T extends java.lang.Comparable<? super T>> int compare(T[] a1,
                                                                      T[] a2)
Compares the contents of the two arrays using a default comparator. An element from each array is passed to the comparator, and if they are not equal, the resource that produced the lesser of the two elements is determined to be the lesser of the two resources. If the elements are equal, then the next pair of elements if passed. If one of the two arrays is exhausted before a pair of unequal elements is found, then it is considered to be the lesser.

Returns:
a negative integer if a1 < a2, a positive integer if a1 > a2, 0 if a1 == a2

compare

public static <T> int compare(T[] a1,
                              T[] a2,
                              BinaryFunctor<T,T,java.lang.Boolean> bf)
Compares the contents of the two arrays using a binary predicate that returns TRUE when its first argument is less than its second. An element from each array is compared, and if they are not equal, the resource that produced the lesser of the two elements is determined to be the lesser. If the elements are equal, then the next pair of elements is passed. If one of the two arrays is exhausted before a pair of unequal elements is found, then it is considered to be the lesser.

Returns:
a negative integer if a1 < a2, a positive integer if a1 > a2, 0 if a1 == a2

compare

public static <T> int compare(T[] a1,
                              T[] a2,
                              java.util.Comparator<T> comp)
Compares the contents of the two arrays using the given comparator. An element from each array is passed to the given comparator, and if they are not equal, the array that produced the lesser of the two elements is determined to be the lesser. If the elements are equal, then the next pair of elements if passed. If one of the two arrays is exhausted before a pair of unequal elements is found, then it is considered to be the lesser.

Returns:
a negative integer if a1 < a2, a positive integer if a1 > a2, 0 if a1 == a2

equal

public static <T> boolean equal(java.lang.Iterable<? extends T> c1,
                                java.lang.Iterable<? extends T> c2)
Returns true if the two resources are equal, using the equals() method to compare the elements of each resource

Returns:
true if the two resources are equal

equal

public static <T> boolean equal(java.lang.Iterable<? extends T> c1,
                                java.lang.Iterable<? extends T> c2,
                                java.util.Comparator<T> comp)
Returns true if the two resources are equal, using the given comparator to compare the elements in each resource

Returns:
true if the two resources are equal.

equal

public static <T> boolean equal(java.lang.Iterable<? extends T> c1,
                                java.lang.Iterable<? extends T> c2,
                                BinaryFunctor<T,T,java.lang.Boolean> eq)
Returns true if the two resources are equal, using the given functor to compare the elements in each resources. The functor is expected to evaluate its two argments and return true if they are "equal", therefore this method returns true if the resources contain the same number of elements and if the functor returns true for all pairs of elements.

Returns:
true if the two collections are equal

lessThan

public static <T extends java.lang.Comparable<? super T>> boolean lessThan(java.lang.Iterable<? extends T> c1,
                                                                           java.lang.Iterable<? extends T> c2)
Returns true if the first resource is lexically less than the second, using the default comparison operation to compare the elements in each resource.

Returns:
true if c1 < c2

lessThan

public static <T> boolean lessThan(java.lang.Iterable<? extends T> c1,
                                   java.lang.Iterable<? extends T> c2,
                                   java.util.Comparator<T> comp)
Returns true if the first resource is lexically less than the second, using the given comparator to compare the elements in each resource.

Returns:
true if c1 < c2

lessThan

public static <T> boolean lessThan(java.lang.Iterable<? extends T> c1,
                                   java.lang.Iterable<? extends T> c2,
                                   BinaryFunctor<T,T,java.lang.Boolean> lt)
Returns true if the first resource is lexically less than the second, using the given operator to compare the elements in each collection. The first is less than the second if it is not longer than the second and if the first corresponding element that is not equal is less.

Returns:
true if c1 < c2

compare

public static <T extends java.lang.Comparable<? super T>> int compare(java.lang.Iterable<? extends T> c1,
                                                                      java.lang.Iterable<? extends T> c2)
Compares the contents of the two iterable resources using a default comparator. An element from each resource is passed to the comparator, and if they are not equal, the resource that produced the lesser of the two elements is determined to be the lesser. If the elements are equal, then the next pair of elements if passed. If one of the two resources is exhausted before a pair of unequal elements is found, then it is considered to be the lesser.

Returns:
a negative integer if c1 < c2, a positive integer if c1 > c2, 0 if c1 == c2

compare

public static <T> int compare(java.lang.Iterable<? extends T> c1,
                              java.lang.Iterable<? extends T> c2,
                              BinaryFunctor<T,T,java.lang.Boolean> bf)
Compares the contents of the two iterable resources using a binary predicate that returns TRUE when its first argument is less than its second. An element from each resource is passed to the given comparator, and if they are not equal, the resource that produced the lesser of the two elements is determined to be the lesser. If the elements are equal, then the next pair of elements if passed. If one of the two resources is exhausted before a pair of unequal elements is found, then it is considered to be the lesser.

Returns:
a negative integer if c1 < c2, a positive integer if c1 > c2, 0 if c1 == c2

compare

public static <T> int compare(java.lang.Iterable<? extends T> c1,
                              java.lang.Iterable<? extends T> c2,
                              java.util.Comparator<? super T> comp)
Compares the contents of the two iterable resources. An element from each resources is passed to the given comparator, and if they are not equal, the resource that produced the lesser of the two elements is determined to be the lesser. If the elements are equal, then the next pair of elements if passed. If one of the two resources is exhausted before a pair of unequal elements is found, then it is considered to be the lesser.

Returns:
a negative integer if c1 < c2, a positive integer if c1 > c2, 0 if c1 == c2

equal

public static <T extends java.lang.Comparable<? super T>> boolean equal(java.util.Iterator<? extends T> i1,
                                                                        java.util.Iterator<? extends T> i2)
Returns true if the two iterations are equal, using the Comparable interface to compare elements in the iterations.

Returns:
true if the two iterations are equal

equal

public static <T> boolean equal(java.util.Iterator<? extends T> i1,
                                java.util.Iterator<? extends T> i2,
                                java.util.Comparator<T> comp)
Returns true if the two iterations are equal, using the given Comparator to compare elements in the iterations.

Returns:
true if the two iterations are equal

equal

public static <T> boolean equal(java.util.Iterator<? extends T> i1,
                                java.util.Iterator<? extends T> i2,
                                BinaryFunctor<T,T,java.lang.Boolean> eq)
Returns true if the two iterations are equal, using the given BinaryFunctor to compare elements in the iterations.

Returns:
true if the two iterations are equal

lessThan

public static <T extends java.lang.Comparable<? super T>> boolean lessThan(java.util.Iterator<? extends T> i1,
                                                                           java.util.Iterator<? extends T> i2)
Returns true if the first iterator is lexically less than the second, using the default comparison operation to compare the elements in each iterator.

Returns:
true if the first iteration is less than the second

lessThan

public static <T> boolean lessThan(java.util.Iterator<? extends T> i1,
                                   java.util.Iterator<? extends T> i2,
                                   java.util.Comparator<T> comp)
Returns true if the first iterator is lexically less than the second, using the given comparator to compare the elements in each iterator.

Returns:
true if the first iteration is less than the second

lessThan

public static <T> boolean lessThan(java.util.Iterator<? extends T> i1,
                                   java.util.Iterator<? extends T> i2,
                                   BinaryFunctor<T,T,java.lang.Boolean> lt)
Returns true if the first iterator is lexically less than the second, using the given operator to compare the elements in each iterator. The first is less than the second if it is not longer than the second and if the first corresponding element that is not equal is less.

Returns:
true if the first iteration is less than the second

compare

public static <T extends java.lang.Comparable<? super T>> int compare(java.util.Iterator<? extends T> i1,
                                                                      java.util.Iterator<? extends T> i2)
Compares the contents of the two iterations using a default comparator. An element from each iteration is passed to the comparator, and if they are not equal, the iteration that produced the lesser of the two elements is determined to be the lesser. If the elements are equal, then the next pair of elements if passed. If one of the two iterations is exhausted before a pair of unequal elements is found, then it is considered to be the lesser. If both iterations are exhausted before a pair of unequal elements is found, then the iterations are determined to be equal.

Neither iteration will be advanced past the point where an unequal pair is found, but the elements that were unequal will have been consumed.

Returns:
a negative integer if i1 < i2, a positive integer if i1 > i2, 0 if i1 == i2

compare

public static <T> int compare(java.util.Iterator<? extends T> i1,
                              java.util.Iterator<? extends T> i2,
                              BinaryFunctor<T,T,java.lang.Boolean> bf)
Compares the contents of the two iterations using a predicate that returns TRUE if its first argument is less than its second. An element from each iteration is passed to the comparator, and if they are not equal, the iteration that produced the lesser of the two elements is determined to be the lesser. If the elements are equal, then the next pair of elements if passed. If one of the two iterations is exhausted before a pair of unequal elements is found, then it is considered to be the lesser. If both iterations are exhausted before a pair of unequal elements is found, then the iterations are determined to be equal.

Neither iteration will be advanced past the point where an unequal pair is found, but the elements that were unequal will have been consumed.

Returns:
a negative integer if i1 < i2, a positive integer if i1 > i2, 0 if i1 == i2

compare

public static <T> int compare(java.util.Iterator<? extends T> i1,
                              java.util.Iterator<? extends T> i2,
                              java.util.Comparator<? super T> comp)
Compares the contents of the two iterations using the given comparator. An element from each iteration is passed to the comparator, and if they are not equal, the iteration that produced the lesser of the two elements is determined to be the lesser. If the elements are equal, then the next pair of elements if passed. If one of the two iterations is exhausted before a pair of unequal elements is found, then it is considered to be the lesser. If both iterations are exhausted before a pair of unequal elements is found, then the iterations are determined to be equal.

Neither iteration will be advanced past the point where an unequal pair is found, but the elements that were unequal will have been consumed.

Returns:
-1 if i1 < i2, 1 if i1 > i2, 0 if i1 == i2


Copyright © 2002-2006 David A. Hall. All Rights Reserved.