Class IndexedLinkedList<E>
- All Implemented Interfaces:
Serializable
,Cloneable
,Iterable<E>
,Collection<E>
,Deque<E>
,List<E>
,Queue<E>
This class implements the indexed, heuristic doubly-linked list data
structure that runs all the single-element operations in expected
\(\mathcal{O}(\sqrt{n})\) time. Under the hood, the actual elements are
stored in a doubly-linked list. However, we also maintain a list of so-called
"fingers" stored in a random access array. Each finger F
contains two data fields:
F.node
- the actual element node,F.index
- the appearance index ofF.node
in the actual list.
For the list of size \(n\), we maintain
\(\bigg \lceil \sqrt{n} \bigg \rceil + 1\) fingers. The rightmost finger in
the finger list is a special end-of-list sentinel. It always has
F.node = null
and F.index =
\(n\). The fingers are sorted by
their indices. That arrangement allows simpler and faster code in the method
that accesses a finger via element index; see
IndexedLinkedList.FingerList.getFingerIndexImpl(int)
. Since number of fingers is
\(\sqrt{n}\), and assuming that the fingers are evenly distributed, each
finger "covers" \(n / \sqrt{n} = \sqrt{n}\) elements. In order to access an
element in the actual list, we first consult the finger list for the index
i
of the finger fingerArray[i]
that is closest to the index
of the target element. This runs in
\[
\mathcal{O}(\log \sqrt{n}) = \mathcal{O}(\log n^{1/2}) = \mathcal{O}(\frac{1}{2} \log n) = \mathcal{O}(\log n).
\]
The rest is to "rewind" the closest finger to point to the target
element (which requires \(\mathcal{O}(\sqrt{n})\) on evenly distributed
finger lis).
- Since:
- 1.6 (Sep 1, 2021)
- Version:
- 1.61 (Sep 26, 2021)
- Author:
- Rodion "rodde" Efremov
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal class
This class implements a basic iterator over this list. -
Constructor Summary
ConstructorsConstructorDescriptionConstructs an empty list.IndexedLinkedList
(Collection<? extends E> c) Constructs a new list and copies the data inc
to it. -
Method Summary
Modifier and TypeMethodDescriptionvoid
Inserts the specified element at the specified position in this list.boolean
Appends the specified element to the end of this list.boolean
addAll
(int index, Collection<? extends E> c) Inserts all of the elements in the specified collection into this list, starting at the specified position.boolean
addAll
(Collection<? extends E> c) Appends all of the elements in the specified collection to the end of this list, in the order they are returned by the specified collection's iterator.void
Adds the elemente
before the head of this list.void
Adds the elemente
after the tail of this list.void
Checks the data structure invariant.void
clear()
Completely clears this list.clone()
Returns the clone list with same content as this list.boolean
Returnstrue
only ifo
is present in this list.boolean
containsAll
(Collection<?> c) Returnstrue
only if this list contains all the elements mentioned in thec
.Returns the descending iterator.element()
Returns the first element of this list.boolean
get
(int index) Returnsindex
th element.getFirst()
Returns the first element of this list.getLast()
Returns the last element of this list.int
hashCode()
Returns the hash code of this list.int
Returns the index of the leftmostobj
, or-1
ifobj
does not appear in this list.boolean
isEmpty()
Returnstrue
only if this list is empty.iterator()
Returns the iterator over this list.int
lastIndexOf
(Object obj) Returns the index of the rightmostobj
, or-1
ifobj
does not appear in this list.Returns the list iterator pointing to the head element of this list.listIterator
(int index) Returns the list iterator pointing betweenlist[index - 1]
andlist[index]
.boolean
Addse
after the tail element of this list.boolean
offerFirst
(E e) Addse
before the head element of this list.boolean
Addse
after the tail element of this list.void
optimize()
Moves all the fingers such that they are evenly distributed.peek()
Takes a look at the first element in this list.Takes a look at the first element in this list.peekLast()
Takes a look at the last element in this list.poll()
If this list is empty, does nothing else but returnnull
.If this list is empty, does nothing else but returnnull
.pollLast()
If this list is empty, does nothing else but returnnull
.pop()
Removes the first element and returns it.void
Addse
before the head of this list.remove()
Removes and returns the first element.remove
(int index) Removes the element residing at the given index.boolean
Removes the leftmost occurrence ofo
in this list.boolean
removeAll
(Collection<?> c) Removes from this list all the elements mentioned inc
.Removes the first element from this list.boolean
Removes the leftmost occurrence ofo
.boolean
Removes from this list all the elements that satisfy the given input predicate.Removes and returns the last element of this list.boolean
Removes the rightmost occurrence ofo
.void
replaceAll
(UnaryOperator<E> operator) Replaces all the elements in this list by applying the given input operator to each of the elements.boolean
retainAll
(Collection<?> c) Remove all the elements that do not appear inc
.Sets the element at indexindex
toelement
and returns the old element.int
size()
Returns the number of elements in this list.void
sort
(Comparator<? super E> c) Sorts stably this list into non-descending order.Returns the spliterator over this list.subList
(int fromIndex, int toIndex) Returns a sublist viewlist[fromIndex, fromIndex + 1, ..., toIndex - 1
.Object[]
toArray()
Returns theObject
array containing all the elements in this list, in the same order as they appear in the list.<T> T[]
toArray
(IntFunction<T[]> generator) Generates the array containing all the elements in this list.<T> T[]
toArray
(T[] a) Ifa
is sufficiently large, returns the same array holding all the contents of this list.toString()
Returns the string representation of this list, listing all the elements.Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Collection
parallelStream, stream
-
Constructor Details
-
IndexedLinkedList
public IndexedLinkedList()Constructs an empty list. -
IndexedLinkedList
Constructs a new list and copies the data inc
to it. Runs in \(\mathcal{O}(m + \sqrt{m})\) time, where \(m = |c|\).- Parameters:
c
- the collection to copy.
-
-
Method Details
-
add
Appends the specified element to the end of this list. Runs in amortized constant time.This method is equivalent to
addLast(E)
. -
add
Inserts the specified element at the specified position in this list. The affected finger indices will be incremented by one. A fingerF
is affected, ifF.index >= index
. Runs in \(\mathcal{O}(\sqrt{n})\) time.- Specified by:
add
in interfaceList<E>
- Parameters:
index
- index at which the specified element is to be inserted.element
- element to be inserted.- Throws:
IndexOutOfBoundsException
-
addAll
Appends all of the elements in the specified collection to the end of this list, in the order they are returned by the specified collection's iterator. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress. (Note that this will occur if the specified collection is this list, and it's nonempty.) Runs in \(\mathcal{O}(m + \sqrt{m + n})\), where \(m = |c|\) and \(n\) is the size of this list.- Specified by:
addAll
in interfaceCollection<E>
- Specified by:
addAll
in interfaceDeque<E>
- Specified by:
addAll
in interfaceList<E>
- Parameters:
c
- collection containing elements to be added to this list.- Returns:
true
if this list changed as a result of the call.- Throws:
NullPointerException
- if the specified collection is null.
-
addAll
Inserts all of the elements in the specified collection into this list, starting at the specified position. For each fingerF
withF.index >= index
will incrementF.index
by 1. Runs in \(\mathcal{O}(m + \sqrt{m + n})\), where \(m = |c|\) and \(n\) is the size of this list.- Specified by:
addAll
in interfaceList<E>
- Parameters:
index
- index at which to insert the first element from the specified collection.c
- collection containing elements to be added to this list.- Returns:
true
if this list changed as a result of the call.- Throws:
IndexOutOfBoundsException
NullPointerException
- if the specified collection is null
-
addFirst
Adds the elemente
before the head of this list. Runs in Runs in \(\mathcal{O}(\sqrt{n})\) time. -
addLast
Adds the elemente
after the tail of this list. Runs in constant time. -
checkInvarant
public void checkInvarant()Checks the data structure invariant. ThrowsIllegalStateException
on invalid invariant. The invariant is valid if:- All the fingers in the finger list are sorted by indices.
- There is no duplicate indices.
- The index of the leftmost finger is no less than zero.
- There must be an end-of-list sentinel finger
F
, such thatF.index =
size of linked list andF.node
isnull
. - Each finger
F
points to thei
th linked list node, wherei = F.index
.
-
clear
public void clear()Completely clears this list. -
clone
Returns the clone list with same content as this list. -
contains
Returnstrue
only ifo
is present in this list. Runs in worst-case linear time. -
containsAll
Returnstrue
only if this list contains all the elements mentioned in thec
. Runs in Runs in \(\mathcal{O}(mn)\) time, where \(m = |c|\).- Specified by:
containsAll
in interfaceCollection<E>
- Specified by:
containsAll
in interfaceList<E>
- Parameters:
c
- the query object collection.- Returns:
true
only if this list contains all the elements inc
, orfalse
otherwise.
-
descendingIterator
Returns the descending iterator.- Specified by:
descendingIterator
in interfaceDeque<E>
- Returns:
- the descending iterator pointing to the tail of this list.
-
element
Returns the first element of this list. Runs in constant time. -
equals
Returnstrue
only ifo
is an instance ofList
and has the sane contents as this list. Runs in worst-case linear time. -
get
Returnsindex
th element. Runs in worst-case Runs in worst-case \(\mathcal{O}(\sqrt{n})\) time.- Specified by:
get
in interfaceList<E>
- Returns:
index
th element.- Throws:
IndexOutOfBoundsException
- if the index is out of range0, 1, ..., size - 1
, or if this list is empty.
-
getFirst
Returns the first element of this list. Runs in constant time.- Specified by:
getFirst
in interfaceDeque<E>
- Returns:
- the first element of this list.
- Throws:
NoSuchElementException
- if this list is empty.
-
getLast
Returns the last element of this list. Runs in constant time.- Specified by:
getLast
in interfaceDeque<E>
- Returns:
- the last element of this list.
- Throws:
NoSuchElementException
- if this list is empty.
-
hashCode
public int hashCode()Returns the hash code of this list. Runs in linear time. -
indexOf
Returns the index of the leftmostobj
, or-1
ifobj
does not appear in this list. Runs in worst-case linear time. -
isEmpty
public boolean isEmpty()Returnstrue
only if this list is empty. -
iterator
Returns the iterator over this list. -
lastIndexOf
Returns the index of the rightmostobj
, or-1
ifobj
does not appear in this list. Runs in worst-case linear time.- Specified by:
lastIndexOf
in interfaceList<E>
- Returns:
- the index of the rightmost
obj
, or-1
ifobj
does not appear in this list. - See Also:
-
listIterator
Returns the list iterator pointing to the head element of this list.- Specified by:
listIterator
in interfaceList<E>
- Returns:
- the list iterator.
- See Also:
-
listIterator
Returns the list iterator pointing betweenlist[index - 1]
andlist[index]
.- Specified by:
listIterator
in interfaceList<E>
- Parameters:
index
- the gap index. The value of zero will point before the head element.- Returns:
- the list iterator pointing to the
index
th gap.
-
offer
Addse
after the tail element of this list. Runs in constant time. -
offerFirst
Addse
before the head element of this list. Runs in \(\mathcal{O}(\sqrt{n})\) time.- Specified by:
offerFirst
in interfaceDeque<E>
- Parameters:
e
- the element to add.- Returns:
- always
true
.
-
offerLast
Addse
after the tail element of this list. Runs in constant time. -
optimize
public void optimize()Moves all the fingers such that they are evenly distributed. Runs in linear time. -
peek
Takes a look at the first element in this list. -
peekFirst
Takes a look at the first element in this list. -
peekLast
Takes a look at the last element in this list. -
poll
If this list is empty, does nothing else but returnnull
. Otherwise, removes the first element and returns it. Runs in \(\mathcal{O}(\sqrt{n})\) time. -
pollFirst
If this list is empty, does nothing else but returnnull
. Otherwise, removes the first element and returns it. Runs in \(\mathcal{O}(\sqrt{n})\) time. -
pollLast
If this list is empty, does nothing else but returnnull
. Otherwise, removes the last element and returns it. Runs in constant time. -
pop
Removes the first element and returns it. Runs in \(\mathcal{O}(\sqrt{n})\) time.- Specified by:
pop
in interfaceDeque<E>
- Returns:
- the first element.
- Throws:
NoSuchElementException
- if the list is empty.
-
push
Addse
before the head of this list. Runs in \(\mathcal{O}(\sqrt{n})\) time. -
remove
Removes and returns the first element. Runs in \(\mathcal{O}(\sqrt{n})\) time. -
remove
Removes the leftmost occurrence ofo
in this list. Runs in worst- case Runs in \(\mathcal{O}(n + \sqrt{n})\) time. \(\mathcal{O}(n)\) for iterating the list and \(\mathcal{O}(\sqrt{n})\) time for fixing the fingers. -
remove
Removes the element residing at the given index. Runs in worst-case \(\mathcal{O}(\sqrt{n})\) time. -
removeAll
Removes from this list all the elements mentioned inc
. Runs in \(\mathcal{O}(n\sqrt{n} + fn)\) time, where \(\mathcal{O}(f)\) is the time of checking for element inclusion inc
. -
removeFirst
Removes the first element from this list. Runs in \(\mathcal{O}(\sqrt{n})\) time.- Specified by:
removeFirst
in interfaceDeque<E>
- Returns:
- the first element.
-
removeFirstOccurrence
Removes the leftmost occurrence ofo
. Runs in worst-case \(\mathcal{O}(n)\) time.- Specified by:
removeFirstOccurrence
in interfaceDeque<E>
- Returns:
true
only ifo
was present in the list and was successfully removed.
-
removeIf
Removes from this list all the elements that satisfy the given input predicate. Runs in \(\mathcal{O}(n\sqrt{n})\) time.- Specified by:
removeIf
in interfaceCollection<E>
- Parameters:
filter
- the filtering predicate.- Returns:
true
only if at least one element was removed.
-
removeLast
Removes and returns the last element of this list. Runs in constant time.- Specified by:
removeLast
in interfaceDeque<E>
- Returns:
- the removed head element.
- Throws:
NoSuchElementException
- if this list is empty.
-
removeLastOccurrence
Removes the rightmost occurrence ofo
. Runs in \(\mathcal{O}(n)\) time.- Specified by:
removeLastOccurrence
in interfaceDeque<E>
- Parameters:
o
- the object to remove.- Returns:
true
only if an element was actually removed.
-
replaceAll
Replaces all the elements in this list by applying the given input operator to each of the elements. Runs in linear time.- Specified by:
replaceAll
in interfaceList<E>
- Parameters:
operator
- the operator mapping one element to another.
-
retainAll
Remove all the elements that do not appear inc
. Runs in worst-case \(\mathcal{O}(nf + n\sqrt{n})\) time, where the inclusion check is run in \(\mathcal{O}(f)\) time. -
set
Sets the element at indexindex
toelement
and returns the old element. Runs in worst-case \(\mathcal{O}(\sqrt{n})\) time. -
size
public int size()Returns the number of elements in this list. -
sort
Sorts stably this list into non-descending order. Runs in \(\mathcal{O}(n \log n)\). -
spliterator
Returns the spliterator over this list.- Specified by:
spliterator
in interfaceCollection<E>
- Specified by:
spliterator
in interfaceIterable<E>
- Specified by:
spliterator
in interfaceList<E>
-
subList
Returns a sublist viewlist[fromIndex, fromIndex + 1, ..., toIndex - 1
. -
toArray
Returns theObject
array containing all the elements in this list, in the same order as they appear in the list. -
toArray
Generates the array containing all the elements in this list.- Specified by:
toArray
in interfaceCollection<E>
- Type Parameters:
T
- the array component type.- Parameters:
generator
- the generator function.- Returns:
- the list contents in an array with component type of
T
.
-
toArray
public <T> T[] toArray(T[] a) Ifa
is sufficiently large, returns the same array holding all the contents of this list. Also, ifa
is larger than the input array, setsa[s] = null
, wheres
is the size of this list. However, ifa
is smaller than this list, allocates a new array of the same length, populates it with the list contents and returns it. -
toString
Returns the string representation of this list, listing all the elements.
-