public class ThriftyList<T> extends AbstractList<T> implements List<T>, Deque<T>, Serializable, Cloneable
Deque and List interfaces. Internally, this data structure
maintains multiple sublists each having size O(sqrt(this.size())), giving a
reasonable upper bound on the size of any one contiguous memory block
consumed by an instance. Furthermore, the resizing approach taken by this
data structure guarantees a memory overhead of O(sqrt(this.size())) instead
of the O(this.size()) overhead that is typical of resizable array
implementations (including ArrayList) while maintaining O(1) amortized time
add and remove operations; the data structure is compact in the
sense that this is theoretically optimal [ResizableArraysTR]. In addition,
this data structure achieves O(sqrt(this.size())) time middle
insertion/deletion by maintaining circular lists throughout.
@techreport{ResizableArraysTR, author = {Andrej Brodnik and Svante Carlsson
and Erik D. Demaine and J. Ian Munro and
Robert Sedgewick}, title = {Resizable Arrays
in Optimal Time and Space}, institution =
{Department of Computer Science, University of
Waterloo}, institutionurl =
{http://www.cs.uwaterloo.ca/}, number =
{CS-99-09}, numberurl =
{http://www.cs.uwaterloo
.ca/research/tr/1999/09/CS-99-09.pdf}, year =
{1999}}
@misc{Goodrich_tieredvectors:, author = {Michael T. Goodrich and John G.
Kloss and II}, title = {Tiered Vectors:
Efficient Dynamic Arrays for Rank-Based
Sequences}, year = {} }
ArrayList,
ArrayDeque,
Serialized Form| Modifier and Type | Class and Description |
|---|---|
protected static class |
ThriftyList.CircularListInternal<T>
A fixed capacity circular list.
|
protected static class |
ThriftyList.FixedListInternal<T>
A fixed capacity list.
|
protected class |
ThriftyList.Iter |
protected class |
ThriftyList.IterBase |
protected static interface |
ThriftyList.ListInternal<T>
Interface for internal helper lists to
ThriftyList as instances
are not required to perform bounds checking (all such responsibilities
are delegated to ThriftyList itself). |
protected class |
ThriftyList.ReverseIter |
modCount| Constructor and Description |
|---|
ThriftyList()
Construct an empty instance of
ThriftyList with the default
capacity. |
| Modifier and Type | Method and Description |
|---|---|
void |
add(int index,
T item) |
boolean |
add(T item) |
void |
addFirst(T item) |
protected void |
addImpl(int index,
int sublistIndex,
int sublistOffset,
T item)
Internal add method requiring the sublist index and offset be
pre-calculated.
|
void |
addLast(T item) |
protected int |
calculateFreeCapacityHead()
Helper method to calculate the free capacity at the head end of the
sublists.
|
protected int |
calculateSublistsUsed()
Helper method to fetch the count of used sublists.
|
protected void |
checkCapacity()
Manipulate the state of this instance appropriately if its capacity has
fallen outside of either capacity limit.
|
void |
clear() |
Object |
clone()
Clone this list.
|
boolean |
contains(Object o) |
static <T,E extends ThriftyList.ListInternal<T>> |
copyTo(E source,
E destination)
Helper function to copy all data from the source to the destination and
return the destination.
|
Iterator<T> |
descendingIterator() |
T |
element() |
T |
get(int index) |
T |
getFirst() |
T |
getLast() |
protected boolean |
growHead()
Grow the head of this list by adding capacity of
smallSublistSizeExp to the beginning. |
protected boolean |
growTail()
Grow the tail of this list.
|
int |
indexOf(Object o) |
boolean |
isEmpty() |
Iterator<T> |
iterator() |
int |
lastIndexOf(Object o) |
ListIterator<T> |
listIterator() |
ListIterator<T> |
listIterator(int index) |
static <T,E extends ThriftyList.ListInternal<T>> |
merge(E l1,
E l2,
E target)
Helper function to merge two
ThriftyList.ListInternal instances into the
target ThriftyList.ListInternal instance. |
protected void |
mergeNextSmallSublists()
Merge two small sublists to a single large sublist if two small sublists
exist, or double the capacity of the single small sublist to that of a
large sublist if only a single small sublist exists.
|
boolean |
offer(T e) |
boolean |
offerFirst(T e) |
boolean |
offerLast(T e) |
T |
peek() |
T |
peekFirst() |
T |
peekLast() |
T |
poll() |
T |
pollFirst() |
T |
pollLast() |
T |
pop() |
void |
push(T e) |
T |
remove() |
T |
remove(int index) |
T |
removeFirst() |
boolean |
removeFirstOccurrence(Object o) |
protected T |
removeImpl(int sublistIndex,
int sublistOffset)
Internal remove method requiring the sublist index and offset be
pre-calculated.
|
T |
removeLast() |
boolean |
removeLastOccurrence(Object o) |
T |
set(int index,
T item) |
protected T |
setImpl(int sublistIndex,
int sublistOffset,
T item)
Internal set method requiring the sublist index and offset be
pre-calculated.
|
protected void |
shrinkHead()
Shrink the head of this list.
|
protected void |
shrinkTail()
Shrink the tail of this list.
|
int |
size() |
static <T> void |
split(ThriftyList.ListInternal<T> src,
ThriftyList.ListInternal<T> dst1,
ThriftyList.ListInternal<T> dst2,
boolean alignRight)
Helper function to split the data of the source list between two
destination lists.
|
protected void |
splitNextLargeSublist()
Splits a large sublist into two small sublists.
|
T[] |
toArray() |
<U> U[] |
toArray(U[] target) |
String |
toString() |
addAll, equals, hashCode, removeRange, subListaddAll, containsAll, remove, removeAll, retainAllfinalize, getClass, notify, notifyAll, wait, wait, waitaddAll, addAll, containsAll, equals, hashCode, remove, removeAll, replaceAll, retainAll, sort, spliterator, subListparallelStream, removeIf, streampublic ThriftyList()
ThriftyList with the default
capacity.public static <T,E extends ThriftyList.ListInternal<T>> E copyTo(E source, E destination)
source - the source list.destination - the destination list.public static <T,E extends ThriftyList.ListInternal<T>> E merge(E l1, E l2, E target)
ThriftyList.ListInternal instances into the
target ThriftyList.ListInternal instance. The target capacity should be equal
to the sum of the capacities of its constituent lists.l1 - the first constituent list.l2 - the second constituent list.ThriftyList.ListInternal instance containing the data of
l1 followed by the data of l2.public static <T> void split(ThriftyList.ListInternal<T> src, ThriftyList.ListInternal<T> dst1, ThriftyList.ListInternal<T> dst2, boolean alignRight)
alignRight - true for right alignment of the data in the src to the two
destinations, false for left alignment.public void add(int index,
T item)
public boolean add(T item)
protected void addImpl(int index,
int sublistIndex,
int sublistOffset,
T item)
protected int calculateFreeCapacityHead()
protected int calculateSublistsUsed()
protected void checkCapacity()
public void clear()
clear in interface Collection<T>clear in interface List<T>clear in class AbstractList<T>public boolean contains(Object o)
public Iterator<T> descendingIterator()
descendingIterator in interface Deque<T>public T element()
public T get(int index)
protected boolean growHead()
smallSublistSizeExp to the beginning. Note that each time
capacity is added to this ThriftyList, small lists will be merged
to large lists.protected boolean growTail()
ThriftyList, small lists will be merged to large lists.public int indexOf(Object o)
public boolean isEmpty()
isEmpty in interface Collection<T>isEmpty in interface List<T>isEmpty in class AbstractCollection<T>public int lastIndexOf(Object o)
lastIndexOf in interface List<T>lastIndexOf in class AbstractList<T>public ListIterator<T> listIterator()
listIterator in interface List<T>listIterator in class AbstractList<T>public ListIterator<T> listIterator(int index)
listIterator in interface List<T>listIterator in class AbstractList<T>protected void mergeNextSmallSublists()
public boolean offer(T e)
public boolean offerFirst(T e)
offerFirst in interface Deque<T>public T peek()
public T poll()
public T remove()
public T remove(int index)
public T removeFirst()
removeFirst in interface Deque<T>public boolean removeFirstOccurrence(Object o)
removeFirstOccurrence in interface Deque<T>protected T removeImpl(int sublistIndex, int sublistOffset)
public T removeLast()
removeLast in interface Deque<T>public boolean removeLastOccurrence(Object o)
removeLastOccurrence in interface Deque<T>protected T setImpl(int sublistIndex, int sublistOffset, T item)
protected void shrinkHead()
ThriftyList, large lists will be split to small lists.protected void shrinkTail()
ThriftyList, large lists will be split to small lists.public int size()
protected void splitNextLargeSublist()
public T[] toArray()
toArray in interface Collection<T>toArray in interface List<T>toArray in class AbstractCollection<T>public <U> U[] toArray(U[] target)
toArray in interface Collection<T>toArray in interface List<T>toArray in class AbstractCollection<T>public String toString()
toString in class AbstractCollection<T>Copyright © 1994–2024 Peter Murray-Rust. All rights reserved.