pivot.collections
Class Sequence.Search

java.lang.Object
  extended by pivot.collections.Sequence.Search
Enclosing interface:
Sequence<T>

public static class Sequence.Search
extends java.lang.Object

Contains utility methods for searching sequences.

Author:
gbrown, tvolkert

Constructor Summary
Sequence.Search()
           
 
Method Summary
static
<T extends java.lang.Comparable<? super T>>
int
binarySearch(Sequence<T> sequence, T item)
          Performs a binary search of a sequence for the given comparable item.
static
<T> int
binarySearch(Sequence<T> sequence, T item, java.util.Comparator<T> comparator)
          Performs a binary search of a sequence for the given item.
static
<T extends java.lang.Comparable<? super T>>
int
linearSearch(Sequence<T> sequence, T item)
          Performs a linear search of a sequence for the given comparable item.
static
<T> int
linearSearch(Sequence<T> sequence, T item, java.util.Comparator<T> comparator)
          Performs a linear search of a sequence for the given item.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Sequence.Search

public Sequence.Search()
Method Detail

binarySearch

public static <T extends java.lang.Comparable<? super T>> int binarySearch(Sequence<T> sequence,
                                                                           T item)
Performs a binary search of a sequence for the given comparable item. See binarySearch(Sequence, Object, Comparator).


binarySearch

public static <T> int binarySearch(Sequence<T> sequence,
                                   T item,
                                   java.util.Comparator<T> comparator)
Performs a binary search of a sequence for the given item.

Parameters:
sequence - The sequence to search. If the sequence is not sorted, the behavior is undefined.
item - The item to search for.
comparator - Comparator that determines element order.
Returns:
The index of item, if it is contained in the sequence; otherwise, (-(insertion point) - 1). The insertion point is defined as the point at which the item would be inserted into the sequence: the index of the first element greater than item, or sequence.getLength(), if all items in the sequence are less than item. Note that this guarantees that the return value will be greater than 0 if and only if the item is found.

If the sequence contains multiple elements equal to the specified item, there is no guarantee which one will be found.


linearSearch

public static <T extends java.lang.Comparable<? super T>> int linearSearch(Sequence<T> sequence,
                                                                           T item)
Performs a linear search of a sequence for the given comparable item. See linearSearch(Sequence, Object, Comparator).


linearSearch

public static <T> int linearSearch(Sequence<T> sequence,
                                   T item,
                                   java.util.Comparator<T> comparator)
Performs a linear search of a sequence for the given item.

Parameters:
sequence - The sequence to search.
item - The item to search for.
comparator - Comparator that will be used to determine logical equality.
Returns:
The index of item, if it is contained in the sequence; otherwise, -1.

If the sequence contains multiple elements equal to the specified item, this will return the first occurrence.