|
J avolution v5.2 (J2SE 1.5+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavolution.util.FastCollection<E>
javolution.util.FastTable<E>
public class FastTable<E>
This class represents a random access collection with real-time behavior (smooth capacity increase).
This class has the following advantages over the widely used
java.util.ArrayList
:
javolution.util
discussion). Iterations over the FastTable
values are faster when
performed using the get(int)
method rather than using collection
records or iterators:
for (int i = 0, n = table.size(); i < n; i++) {
table.get(i);
}
FastTable
supports sorting
in place (quick sort)
using the value comparator
for the table (no object or array allocation when sorting).
The size of a FastTable
can be set
directly
and populated concurrently through the set(int, Object)
method (e.g. table shared by multiple threads each working on
different index ranges).
Nested Class Summary |
---|
Nested classes/interfaces inherited from class javolution.util.FastCollection |
---|
FastCollection.Record |
Constructor Summary | |
---|---|
FastTable()
Creates a table of small initial capacity. |
|
FastTable(java.util.Collection<? extends E> values)
Creates a table containing the specified values, in the order they are returned by the collection's iterator. |
|
FastTable(int capacity)
Creates a table of specified initial capacity; unless the table size reaches the specified capacity, operations on this table will not allocate memory (no lazy object creation). |
|
FastTable(java.lang.String id)
Creates a persistent table associated to the specified unique identifier (convenience method). |
Method Summary | ||
---|---|---|
boolean |
add(E value)
Appends the specified value to the end of this table. |
|
void |
add(int index,
E value)
Inserts the specified value at the specified position in this table. |
|
boolean |
addAll(int index,
java.util.Collection<? extends E> values)
Inserts all of the values in the specified collection into this table at the specified position. |
|
void |
addLast(E value)
Appends the specified value to the end of this table (fast). |
|
void |
clear()
Removes all of the values from this collection (optional operation). |
|
boolean |
contains(java.lang.Object value)
Indicates if this collection contains the specified value. |
|
void |
delete(FastCollection.Record record)
Deletes the specified record from this collection. |
|
E |
get(int index)
Returns the element at the specified index. |
|
protected int |
getCapacity()
Returns the current capacity of this table. |
|
E |
getFirst()
Returns the first value of this table. |
|
E |
getLast()
Returns the last value of this table. |
|
FastComparator<? super E> |
getValueComparator()
Returns the value comparator for this collection (default FastComparator.DEFAULT ). |
|
FastCollection.Record |
head()
Returns the head record of this collection; it is the record such as head().getNext() holds the first collection value. |
|
int |
indexOf(java.lang.Object value)
Returns the index in this table of the first occurrence of the specified value, or -1 if this table does not contain this value. |
|
java.util.Iterator<E> |
iterator()
Returns an iterator over the elements in this list (allocated on the stack when executed in a StackContext ). |
|
int |
lastIndexOf(java.lang.Object value)
Returns the index in this table of the last occurrence of the specified value, or -1 if this table does not contain this value. |
|
java.util.ListIterator<E> |
listIterator()
Returns a list iterator over the elements in this list (allocated on the stack when executed in a StackContext ). |
|
java.util.ListIterator<E> |
listIterator(int index)
Returns a list iterator from the specified position (allocated on the stack when executed in a StackContext ). |
|
static
|
newInstance()
Returns a new, preallocated or recycled table instance
(on the stack when executing in a StackContext ). |
|
static void |
recycle(FastTable instance)
Recycles a table instance immediately
(on the stack when executing in a StackContext ). |
|
E |
remove(int index)
Removes the value at the specified position from this table. |
|
E |
removeLast()
Removes and returns the last value of this table (fast). |
|
void |
removeRange(int fromIndex,
int toIndex)
Removes the values between [fromIndex..toIndex[ |
|
void |
reset()
Resets the internal state of this object to its default values. |
|
E |
set(int index,
E value)
Replaces the value at the specified position in this table with the specified value. |
|
void |
setSize(int size)
Sets the size of this table. |
|
FastTable<E> |
setValueComparator(FastComparator<? super E> comparator)
Sets the comparator to use for value equality or comparison if the collection is ordered (see sort() ). |
|
int |
size()
Returns the number of values in this collection. |
|
FastTable<E> |
sort()
Sorts this table in place (quick sort) using this table value comparator
(smallest first). |
|
java.util.List<E> |
subList(int fromIndex,
int toIndex)
Returns a view of the portion of this list between the specified indexes (instance of FastList allocated from the "stack" when
executing in a StackContext ). |
|
FastCollection.Record |
tail()
Returns the tail record of this collection; it is the record such as tail().getPrevious() holds the last collection value. |
|
void |
trimToSize()
Reduces the capacity of this table to the current size (minimize storage space). |
|
java.util.List<E> |
unmodifiable()
Returns the unmodifiable view associated to this collection. |
|
E |
valueOf(FastCollection.Record record)
Returns the collection value for the specified record. |
Methods inherited from class javolution.util.FastCollection |
---|
addAll, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, toArray, toArray, toString, toText |
Methods inherited from class java.lang.Object |
---|
clone, finalize, getClass, notify, notifyAll, wait, wait, wait |
Methods inherited from interface java.util.List |
---|
addAll, containsAll, equals, hashCode, isEmpty, remove, removeAll, retainAll, toArray, toArray |
Constructor Detail |
---|
public FastTable()
public FastTable(java.lang.String id)
id
- the unique identifier for this map.
java.lang.IllegalArgumentException
- if the identifier is not unique.PersistentContext.Reference
public FastTable(int capacity)
capacity
- the initial capacity.public FastTable(java.util.Collection<? extends E> values)
values
- the values to be placed into this table.Method Detail |
---|
public static <E> FastTable<E> newInstance()
recycled
table instance
(on the stack when executing in a StackContext
).
public static void recycle(FastTable instance)
instance
immediately
(on the stack when executing in a StackContext
).
public void setSize(int size)
null
elements are added; otherwise
the last elements are removed until the desired size is reached.
size
- the new size.public final E get(int index)
get
in interface java.util.List<E>
index
- index of value to return.
java.lang.IndexOutOfBoundsException
- if (index < 0) ||
(index >= size())
public final E set(int index, E value)
set
in interface java.util.List<E>
index
- index of value to replace.value
- value to be stored at the specified position.
java.lang.IndexOutOfBoundsException
- if (index < 0) ||
(index >= size())
public final boolean add(E value)
add
in interface java.util.Collection<E>
add
in interface java.util.List<E>
add
in class FastCollection<E>
value
- the value to be appended to this table.
true
(as per the general contract of the
Collection.add
method).public final E getFirst()
java.util.NoSuchElementException
- if this table is empty.public final E getLast()
java.util.NoSuchElementException
- if this table is empty.public final void addLast(E value)
value
- the value to be added.public final E removeLast()
java.util.NoSuchElementException
- if this table is empty.public final void clear()
FastCollection
clear
in interface java.util.Collection<E>
clear
in interface java.util.List<E>
clear
in class FastCollection<E>
public void reset()
Reusable
reset
in interface Reusable
public final boolean addAll(int index, java.util.Collection<? extends E> values)
Note: If this method is used concurrent access must be synchronized (the table is no more thread-safe).
addAll
in interface java.util.List<E>
index
- the index at which to insert first value from the specified
collection.values
- the values to be inserted into this list.
true
if this list changed as a result of the call;
false
otherwise.
java.lang.IndexOutOfBoundsException
- if (index < 0) ||
(index > size())
public final void add(int index, E value)
Note: If this method is used concurrent access must be synchronized (the table is no more thread-safe).
add
in interface java.util.List<E>
index
- the index at which the specified value is to be inserted.value
- the value to be inserted.
java.lang.IndexOutOfBoundsException
- if (index < 0) ||
(index > size())
public final E remove(int index)
Note: If this method is used concurrent access must be synchronized (the table is no more thread-safe).
remove
in interface java.util.List<E>
index
- the index of the value to removed.
java.lang.IndexOutOfBoundsException
- if (index < 0) ||
(index >= size())
public final void removeRange(int fromIndex, int toIndex)
[fromIndex..toIndex[ from
this table.
Note: If this method is used concurrent access must be synchronized
(the table is no more thread-safe).
- Parameters:
fromIndex
- the beginning index, inclusive.toIndex
- the ending index, exclusive.
- Throws:
java.lang.IndexOutOfBoundsException
- if (fromIndex < 0) || (toIndex < 0)
|| (fromIndex > toIndex) || (toIndex > this.size())
public final int indexOf(java.lang.Object value)
indexOf
in interface java.util.List<E>
value
- the value to search for.
public final int lastIndexOf(java.lang.Object value)
lastIndexOf
in interface java.util.List<E>
value
- the value to search for.
public java.util.Iterator<E> iterator()
StackContext
).
iterator
in interface java.lang.Iterable<E>
iterator
in interface java.util.Collection<E>
iterator
in interface java.util.List<E>
iterator
in class FastCollection<E>
public java.util.ListIterator<E> listIterator()
StackContext
).
listIterator
in interface java.util.List<E>
public java.util.ListIterator<E> listIterator(int index)
StackContext
).
The list iterator being returned does not support insertion/deletion.
listIterator
in interface java.util.List<E>
index
- the index of first value to be returned from the
list iterator (by a call to the next
method).
java.lang.IndexOutOfBoundsException
- if the index is out of range
(index < 0 || index > size())
public final java.util.List<E> subList(int fromIndex, int toIndex)
FastList
allocated from the "stack" when
executing in a StackContext
).
If the specified indexes are equal, the returned list is empty.
The returned list is backed by this list, so non-structural changes in
the returned list are reflected in this list, and vice-versa.
This method eliminates the need for explicit range operations (of
the sort that commonly exist for arrays). Any operation that expects
a list can be used as a range operation by passing a subList view
instead of a whole list. For example, the following idiom
removes a range of values from a list:
list.subList(from, to).clear();
Similar idioms may be constructed for indexOf
and
lastIndexOf
, and all of the algorithms in the
Collections
class can be applied to a subList.
The semantics of the list returned by this method become undefined if
the backing list (i.e., this list) is structurally modified in
any way other than via the returned list (structural modifications are
those that change the size of this list, or otherwise perturb it in such
a fashion that iterations in progress may yield incorrect results).
subList
in interface java.util.List<E>
fromIndex
- low endpoint (inclusive) of the subList.toIndex
- high endpoint (exclusive) of the subList.
java.lang.IndexOutOfBoundsException
- if (fromIndex < 0 ||
toIndex > size || fromIndex > toIndex)
public final void trimToSize()
public final FastTable<E> sort()
value comparator
(smallest first).
this
public FastTable<E> setValueComparator(FastComparator<? super E> comparator)
sort()
).
comparator
- the value comparator.
this
public FastComparator<? super E> getValueComparator()
FastCollection
FastComparator.DEFAULT
).
getValueComparator
in class FastCollection<E>
public final int size()
FastCollection
size
in interface java.util.Collection<E>
size
in interface java.util.List<E>
size
in class FastCollection<E>
public final FastCollection.Record head()
FastCollection
head().getNext()
holds the first collection value.
head
in class FastCollection<E>
public final FastCollection.Record tail()
FastCollection
tail().getPrevious()
holds the last collection value.
tail
in class FastCollection<E>
public final E valueOf(FastCollection.Record record)
FastCollection
valueOf
in class FastCollection<E>
record
- the record whose current value is returned.
public final void delete(FastCollection.Record record)
FastCollection
Implementation must ensure that removing a record from the collection does not affect in any way the records preceding the record being removed (it might affect the next records though, e.g. in a list collection, the indices of the subsequent records will change).
delete
in class FastCollection<E>
record
- the record to be removed.public java.util.List<E> unmodifiable()
FastCollection
UnsupportedOperationException
being thrown. The view is
typically part of the collection itself (created only once)
and also an instance of FastCollection
supporting direct
iterations.
unmodifiable
in class FastCollection<E>
public final boolean contains(java.lang.Object value)
FastCollection
contains
in interface java.util.Collection<E>
contains
in interface java.util.List<E>
contains
in class FastCollection<E>
value
- the value whose presence in this collection
is to be tested.
true
if this collection contains the specified
value;false
otherwise.protected final int getCapacity()
|
J avolution v5.2 (J2SE 1.5+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |