Class powersoft.jcm.util.SortableVector
All Packages Class Hierarchy This Package Previous Next Index
Class powersoft.jcm.util.SortableVector
java.lang.Object
|
+----powersoft.jcm.util.SortableVector
- public class SortableVector
- extends Object
- implements Cloneable
A general-purpose vector class. The size of the vector
is dynamic and increased as necessary. The elements of the vector may or
may not be sorted. Each element of the vector is a (null or non-null)
reference to an Object instance. There may be duplicates in the vector.
Notes:
The following methods invoke the equals method on the elements to determine
equality:
- int index( Object );
- Object remove( Object );
- boolean equals( Object );
- boolean equals( SortableVector );
Note that if null values are stored in the vector, the equals method of any
object should return false when a null value is supplied.
The following methods require a user-defined comparison function (compare
routine) to determine the order among objects. The compare routine must have
been specifie before these methods can be invoked. Otherwise, a runtime
exception will be thrown.
- int binarySearch( Object );
- int insert( Object );
- void sort();
The compare routine must be an object derived from the interface
CompareRoutine.
- See Also:
- CompareRoutine
-
_count
- The number of objects stored in the vector.
-
_growBy
- The default amount to grow the array used in #grow.
-
_objs
- The internal array storing the objects.
-
_quickSortThreshold
- If the size of an array is less than the threshold, insertion sort
will be used instead of quick sort.
-
_routine
- The compare routine.
-
SortableVector()
- Constructs an empty vector.
-
SortableVector(CompareRoutine)
- Constructs an empty vector and sets the compare routine as specified.
-
SortableVector(int)
- Constructs a vector initially containing the specified number
of null values.
-
SortableVector(Object[])
- Constructs a vector from the elements of the specified array.
-
SortableVector(Vector)
- Constructs a vector from the elements of the specified vector.
-
append(Object)
- Append an object to the end of the vector.
-
binarySearch(Object)
- Performs a binary search for the specified object.
-
clear()
- Clears the vector by setting all elements to
null
.
-
clone()
- Creates a clone of the vector.
-
equals(Object)
- Determines whether this vector is equal to the specified object.
-
equals(Object, Object)
-
-
equals(SortableVector)
- Determines whether this vector is equal to the specified sortable
vector.
-
getArray()
- Gets a shallow copy of the internal array
-
getCapacity()
- Determines the capacity of the vector.
-
getCompareRoutine()
- Gets the comparison routine.
-
getCount()
- Determines the number of objects currently stored in the vector.
-
getElement(int)
- Retrieves the object stored at the specified index.
-
getElements()
- Gets an enumeration of the elements of the vector.
-
getLowerBound(Object)
-
-
getMedian(Object, Object, Object)
-
-
grow()
- Increases the capacity of the vector by the default
amount (10 elements).
-
grow(int)
- Increases the capacity by the specified amount.
-
index(Object)
- Finds the index of the specified object.
-
insert(int, Object)
- Inserts an object at the specified position.
-
insert(int, Object, boolean)
- Inserts an object at the specified position.
-
insert(Object)
- Inserts the specified object in sorted order.
-
insert(Object, boolean)
- Inserts the specified object in sorted order.
-
partition(int, int, Object)
-
-
quickSort(int, int)
-
-
remove(Object)
- Removes the specifed object from the vector.
-
removeAt(int)
- Removes the object at the specified index.
-
removeAtIndex(int)
-
-
setArray(Object[])
- Makes a shallow copy of the given array and stores it in the vector.
-
setCompareRoutine(CompareRoutine)
- Specifies the comparison routine.
-
setElement(int, Object)
- Stores an object in the specified element.
-
sort()
- Sorts the vector using the comparison routine specified by the
CompareRoutine property.
-
sortTwo(int)
-
-
toString()
- Generates a string representation of the contents of the vector.
_growBy
protected final static int _growBy
- The default amount to grow the array used in #grow.
_quickSortThreshold
protected final static int _quickSortThreshold
- If the size of an array is less than the threshold, insertion sort
will be used instead of quick sort.
_count
protected int _count
- The number of objects stored in the vector.
_objs
protected Object _objs[]
- The internal array storing the objects.
_routine
protected CompareRoutine _routine
- The compare routine.
SortableVector
public SortableVector()
- Constructs an empty vector.
SortableVector
public SortableVector(int initialSize)
- Constructs a vector initially containing the specified number
of null values.
- Parameters:
- initialSize - Specifies the initial size of the vector.
SortableVector
public SortableVector(Object array[])
- Constructs a vector from the elements of the specified array.
- Parameters:
- array - Specifies an array of Objects.
SortableVector
public SortableVector(Vector vec)
- Constructs a vector from the elements of the specified vector.
- Parameters:
- vec - Specifies the vector whose elements are copies to
the new vector.
SortableVector
public SortableVector(CompareRoutine routine)
- Constructs an empty vector and sets the compare routine as specified.
- Parameters:
- routine - Specifies an initial compare routine.
append
public synchronized int append(Object object)
- Append an object to the end of the vector.
- Parameters:
- object - Specifies the object to be appended to the vector.
- Returns:
- The index of the new item. If
object
is null,
-1 is returned.
binarySearch
public synchronized int binarySearch(Object object)
- Performs a binary search for the specified object.
Notes:
The vector must be sorted.
- Parameters:
- object - The object to search for.
- Returns:
- The index of the object or -1 if the object is not found.
- Throws: InvalidStateException
- if the CompareRoutine property has
not be set for the vector.
- See Also:
- setCompareRoutine
clear
public synchronized void clear()
- Clears the vector by setting all elements to
null
.
Notes:
The Count property to set to 0. The Capacity property is not changed.
- See Also:
- getCount, getCapacity
grow
public void grow()
- Increases the capacity of the vector by the default
amount (10 elements).
grow
public synchronized void grow(int growBy)
- Increases the capacity by the specified amount.
- Parameters:
- growBy - Specifies the number of elements by which to increase
the capacity.
- Throws: IllegalArgumentException
- if (code>growBy < 0.
index
public synchronized int index(Object object)
- Finds the index of the specified object.
- Parameters:
- object - Specifies the object whose index is be determined.
- Returns:
- The index of the object, or -1 if the object
cannot be found.
- See Also:
- binarySearch
insert
public synchronized int insert(Object object)
- Inserts the specified object in sorted order.
Notes:
The vector must be sorted. Duplicate objects will be inserted.
- Parameters:
- object - The object to be inserted.
- Returns:
- The index of the inserted object.
- See Also:
- append
insert
public synchronized int insert(Object object,
boolean allowsDuplicates)
- Inserts the specified object in sorted order.
Notes:
The vector must be sorted. Duplicate objects will be inserted if
allowsDuplicates
is true
.
- Parameters:
- object - The object to be inserted.
- allowsDuplicates - If
true
, duplicate objects are permitted. If
a copy of object
already exists, another copy will be inserted.
If false
, duplicate objects are not permitted. If the object
already exists, another copy is not inserted.
- Returns:
- The index of the inserted object, or -1 if the object could
not be inserted.
- Throws: InvalidStateException
- is thrown if the CompareRoutine
property has not been set.
- See Also:
- setCompareRoutine
insert
public synchronized int insert(int index,
Object object)
- Inserts an object at the specified position.
Notes:
If the vector already contains the object, a duplicate copy will
be inserted.
- Parameters:
- index - Specifies the zero-base index of the object.
- object - The object to insert.
- Returns:
- The index of the inserted object.
insert
public synchronized int insert(int index,
Object object,
boolean allowsDuplicates)
- Inserts an object at the specified position.
Notes:
If the vector already contains the specified object, a duplicate
copy will be added if allowsDuplicates
is
true
.
- Parameters:
- index - Specifies the zero-based index of the object.
- object - The object to insert.
- allowsDuplicates - If
true
, duplicate objects are permitted. If
a copy of object
already exists, another copy will be inserted.
If false
, duplicate objects are not permitted. If the object
already exists, another copy is not inserted.
- Returns:
- The index is the inserted object.
- Throws: ArrayIndexOutOfBoundsException
- if
index
is not between
0 and getCount() inclusive.
remove
public synchronized Object remove(Object object)
- Removes the specifed object from the vector.
- Parameters:
- object - The object to be removed.
- Returns:
- The object that was removed from the vector,
or
null
if the object cannot be found.
removeAt
public synchronized Object removeAt(int index)
- Removes the object at the specified index.
- Parameters:
- index - The index of the object to be removed.
- Returns:
- The object that was removed from the vector,
or
null
if the object cannot be found.
- Throws: ArrayIndexOutOfBoundsException
- if index is not valid.
sort
public synchronized void sort()
- Sorts the vector using the comparison routine specified by the
CompareRoutine property.
- Throws: InvalidStateException
- if the CompareRoutine property has
not been set.
- See Also:
- setCompareRoutine
clone
public synchronized Object clone()
- Creates a clone of the vector. The elements of the original vector
are copied to the clone.
- Returns:
- The cloned vector.
- Overrides:
- clone in class Object
equals
public synchronized boolean equals(Object obj)
- Determines whether this vector is equal to the specified object.
The object must be an instance of a sortable vector.
Notes:
The vectors are equal if their sizes are equal and all pairs of
corresponding elements are equal.
- Parameters:
- obj - Specifies the object to be compared with the
current vector.
- Returns:
-
true
if the vectors are equal; false
otherwise.
- Overrides:
- equals in class Object
equals
public synchronized boolean equals(SortableVector vec)
- Determines whether this vector is equal to the specified sortable
vector.
Notes:
The vectors are equal if their sizes are equal and all pairs of
corresponding elements are equal.
- Parameters:
- vec - Specified the sortable vector to be compared with the
current vector.
- Returns:
-
true
if the vectors are equal; false
otherwise.
toString
public synchronized String toString()
- Generates a string representation of the contents of the vector.
Notes:
The string representation is generated by appending the string
representation of each element. Each element is separated by a
comma and the entire string is enclose in square brackets '[]'.
- Returns:
- A string representation of the vector (see Notes).
- Overrides:
- toString in class Object
getArray
public synchronized Object[] getArray()
- Gets a shallow copy of the internal array
- Returns:
- The copied array if the vector is non-empty, or
null if the vector is empty.
setArray
public synchronized void setArray(Object array[])
- Makes a shallow copy of the given array and stores it in the vector.
The elements originally in the vector are cleared first.
- Parameters:
- array - The array to be stored; null to set the vector empty.
getCapacity
public int getCapacity()
- Determines the capacity of the vector.
- Returns:
- The number of objects that can be stored in the vector.
getCompareRoutine
public CompareRoutine getCompareRoutine()
- Gets the comparison routine.
- Returns:
- The comparison routine.
setCompareRoutine
public synchronized void setCompareRoutine(CompareRoutine routine)
- Specifies the comparison routine.
- Parameters:
- routine - The comparison routine.
getCount
public int getCount()
- Determines the number of objects currently stored in the vector.
- Returns:
- The number objects in the vector.
- See Also:
- getCapacity
getElement
public synchronized Object getElement(int index)
- Retrieves the object stored at the specified index.
- Parameters:
- index - The index of the element to be retrieved.
- Returns:
- The object contained in the specified element.
- Throws: ArrayIndexOutOfBoundsException
- if
index
is out of range.
setElement
public synchronized void setElement(int index,
Object obj)
- Stores an object in the specified element.
- Parameters:
- index - The index of the element in which to store the object.
- obj - The object to store in the specified element.
- Throws: ArrayIndexOutOfBoundsException
- if
index
is out of range.
getElements
public Enumeration getElements()
- Gets an enumeration of the elements of the vector.
equals
protected boolean equals(Object obj1,
Object obj2)
removeAtIndex
protected Object removeAtIndex(int index)
getLowerBound
protected int getLowerBound(Object object)
partition
protected int partition(int first,
int last,
Object pivot)
getMedian
protected Object getMedian(Object a,
Object b,
Object c)
sortTwo
protected void sortTwo(int first)
quickSort
protected void quickSort(int first,
int last)
All Packages Class Hierarchy This Package Previous Next Index