kiwi.util
Class VectorUtils

java.lang.Object
  extended bykiwi.util.VectorUtils

public class VectorUtils
extends java.lang.Object

This is a utility class that provides methods for performing common operations on sorted and unsorted vectors. Some of these operations are taken from set theory, while others are basic list operations.

Author:
Mark Lindner, PING Software Group

Field Summary
private  Comparator comparator
           
private  MergeSort sorter
           
 
Constructor Summary
VectorUtils()
          Construct a new VectorUtils object with the default comparator.
VectorUtils(Comparator comparator)
          Construct a new VectorUtils object with the specified comparator.
 
Method Summary
static java.util.Vector append(java.util.Vector v1, java.util.Vector v2)
          Append one vector to another vector.
 java.lang.Object binarySearch(java.util.Vector v, java.lang.Object o)
          Perform a binary search on a presumably sorted vector.
 boolean compare(java.util.Vector v1, java.util.Vector v2, boolean sorted)
          Compare two vectors.
 java.util.Vector difference(java.util.Vector v1, java.util.Vector v2, boolean sorted)
          Compute the difference between two vectors.
 java.util.Vector intersection(java.util.Vector v1, java.util.Vector v2, boolean sorted)
          Compute the intersection of two vectors.
 java.lang.Object linearSearch(java.util.Vector v, java.lang.Object o)
          Perform a linear search on a vector.
static void print(java.util.Vector v)
          Print the contents of a vector.
 java.lang.Object search(java.util.Vector v, java.lang.Object o, boolean sorted)
          Search for an object in a vector.
 void setComparator(Comparator comparator)
          Set the comparator to be used by the methods in this class.
 java.util.Vector sort(java.util.Vector v)
          Sort a vector.
 java.util.Vector union(java.util.Vector v1, java.util.Vector v2, boolean sorted)
          Compute the union of two vectors.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

comparator

private Comparator comparator

sorter

private MergeSort sorter
Constructor Detail

VectorUtils

public VectorUtils()
Construct a new VectorUtils object with the default comparator. The default comparator is the HashCodeComparator.

See Also:
HashCodeComparator

VectorUtils

public VectorUtils(Comparator comparator)
Construct a new VectorUtils object with the specified comparator.

Parameters:
comparator - The comparator to use.
Method Detail

setComparator

public void setComparator(Comparator comparator)
Set the comparator to be used by the methods in this class.

Parameters:
comparator - The comparator to use.

print

public static void print(java.util.Vector v)
Print the contents of a vector. This method displays a list of all items in a vector. For each item, the class name of the object type is listed, followed by the string representation of the object.

Parameters:
v - The vector to print.

search

public java.lang.Object search(java.util.Vector v,
                               java.lang.Object o,
                               boolean sorted)
Search for an object in a vector. If the vector is sorted, a binary search is performed. Otherwise, a linear search is performed.

Parameters:
v - The vector to search.
o - The object to search for.
sorted - A flag specifying whether the vector is sorted.
Returns:
The object, if found, and null otherwise.

linearSearch

public java.lang.Object linearSearch(java.util.Vector v,
                                     java.lang.Object o)
Perform a linear search on a vector.

Parameters:
v - The vector to search.
o - The object to search for.
Returns:
The object, if found, and null otherwise.

binarySearch

public java.lang.Object binarySearch(java.util.Vector v,
                                     java.lang.Object o)
Perform a binary search on a presumably sorted vector. If the specified vector is not sorted, the results are undefined.

Parameters:
v - The vector to search.
o - The object to search for.
Returns:
The object, if found, and null otherwise.

sort

public java.util.Vector sort(java.util.Vector v)
Sort a vector. Merge-sorts the given vector, returning a new vector.

Parameters:
v - The vector to sort.
Returns:
A vector that includes all items from v in sorted order.

compare

public boolean compare(java.util.Vector v1,
                       java.util.Vector v2,
                       boolean sorted)
Compare two vectors. The comparison is done using the currently set comparator.

Parameters:
v1 - The first vector.
v2 - The second vector.
sorted - A flag specifying whether the vectors are sorted.
Returns:
true if the vectors are "equal" and false otherwise.

union

public java.util.Vector union(java.util.Vector v1,
                              java.util.Vector v2,
                              boolean sorted)
Compute the union of two vectors. The union of two vectors consists of all items unique to one vector and all items unique to the other vector.

Parameters:
v1 - The first vector.
v2 - The second vector.
sorted - A flag specifying whether the vectors are sorted.
Returns:
A vector consisting of all items in v1 and all items in v2 that are not also in v1.

intersection

public java.util.Vector intersection(java.util.Vector v1,
                                     java.util.Vector v2,
                                     boolean sorted)
Compute the intersection of two vectors. The intersection of two vectors is a vector of elements that appear in both vectors.

Parameters:
v1 - The first vector.
v2 - The second vector.
sorted - A flag specifying whether the vectors are sorted.
Returns:
A vector that includes all items that are in both v1 and v2.

difference

public java.util.Vector difference(java.util.Vector v1,
                                   java.util.Vector v2,
                                   boolean sorted)
Compute the difference between two vectors. The difference between two vectors is a vector consisting of all items in one vector that are not in the other vector.

Parameters:
v1 - The first vector.
v2 - The second vector.
sorted - A flag specifying whether the vectors are sorted.
Returns:
A vector that includes all elements from v1 that are not in v2.

append

public static java.util.Vector append(java.util.Vector v1,
                                      java.util.Vector v2)
Append one vector to another vector.

Parameters:
v1 - The original vector.
v2 - The vector of items to append to v1.
Returns:
v1