|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectmessif.utility.SortedArrayData<K,T>
K
- the type of keys this array is ordered byT
- the type of objects stored in this arraypublic abstract class SortedArrayData<K,T>
Abstract implementation of a basic sorted array data.
The order is maintained by the compare(K, T)
method.
Methods for binary search with O(log n) complexity
as well as some basic collection operations are provided.
Constructor Summary | |
---|---|
SortedArrayData()
|
Method Summary | ||
---|---|---|
protected int |
binarySearch(K key,
int low,
int high,
boolean indicateFailure)
Searches a range in this collection of objects for the specified value using the binary search algorithm. |
|
protected abstract int |
compare(K key,
T object)
Compares its two arguments for order. |
|
T |
first()
Returns the last element of this collection according to the order specified by the comparator. |
|
protected
|
fullSearch(IndexComparator<C,T> comparator,
C key,
int low,
int high)
Searches a range in this collection for objects that are equal to the specified key. |
|
protected abstract T |
get(int index)
Returns the element at the specified position in this collection. |
|
protected int |
indexOf(java.lang.Object o)
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. |
|
T |
last()
Returns the last element of this collection according to the order specified by the comparator. |
|
static
|
mergeSort(T[] items1,
int from1,
int to1,
T[] items2,
int from2,
int to2,
java.util.Comparator<T> comparator,
T[] destination,
int fromDest,
int toDest)
Merges two sorted arrays into the destination array using the specified comparator. |
|
abstract int |
size()
Returns the number of elements in this collection. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public SortedArrayData()
Method Detail |
---|
public abstract int size()
protected abstract T get(int index) throws java.lang.IndexOutOfBoundsException, java.lang.IllegalStateException
index
- index of the element to return
java.lang.IndexOutOfBoundsException
- if the index is out of range
(index < 0 || index >= size())
java.lang.IllegalStateException
- if the object at position index
cannot be accessedprotected abstract int compare(K key, T object) throws java.lang.ClassCastException
key
- the key to indexCompareobject
- the object to be compared
java.lang.ClassCastException
- if the arguments' types prevent them from
being compared by this comparator.protected int binarySearch(K key, int low, int high, boolean indicateFailure) throws java.lang.IndexOutOfBoundsException, java.lang.IllegalStateException
If the range contains multiple elements with the specified value, there is no guarantee which one will be found.
low
- the index of the first element (inclusive) to be searchedhigh
- the index of the last element (inclusive) to be searchedkey
- the key value to be searched forindicateFailure
- flag that controls the return value when the key is not found;
if indicateFailure == true
, a negative number -(insertionPoint + 1)
is returned, otherwise, the index of the nearest bigger key is returned
indicateFailure
java.lang.IndexOutOfBoundsException
- if the specified boundaries are outside range
java.lang.IllegalStateException
- if there was an error accessing object via get(int)
protected <C> int fullSearch(IndexComparator<C,T> comparator, C key, int low, int high) throws java.lang.IndexOutOfBoundsException, java.lang.IllegalStateException
Object.equals(java.lang.Object)
is used.
C
- type of keys for the comparatorcomparator
- the comparator used to check for equality of objects (can be null)low
- the index of the first element (inclusive) to be searchedhigh
- the index of the last element (inclusive) to be searchedkey
- the key value to be searched for
java.lang.IndexOutOfBoundsException
- if the specified boundaries are outside range
java.lang.IllegalStateException
- if there was an error accessing object via get(int)
protected int indexOf(java.lang.Object o)
o
- element to search for
public T first() throws java.util.NoSuchElementException, java.lang.IllegalStateException
java.util.NoSuchElementException
- if the collection is empty
java.lang.IllegalStateException
- if there was an error accessing object via get(int)
public T last() throws java.util.NoSuchElementException, java.lang.IllegalStateException
java.util.NoSuchElementException
- if the collection is empty
java.lang.IllegalStateException
- if there was an error accessing object via get(int)
public static <T> void mergeSort(T[] items1, int from1, int to1, T[] items2, int from2, int to2, java.util.Comparator<T> comparator, T[] destination, int fromDest, int toDest)
T
- type of array elementsitems1
- the first array to mergefrom1
- the starting index in the first arrayto1
- the last index (exclusive) in the first arrayitems2
- the second array to mergefrom2
- the starting index in the second arrayto2
- the last index (exclusive) in the second arraycomparator
- the comparator used to compare objects from the collections;
its type check is supressed, the array items are expected to be compatibledestination
- the destination arrayfromDest
- the starting index in the destination arraytoDest
- the last index (exclusive) in the destination array
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |