- java.lang.Object
-
- java.util.AbstractCollection<E>
-
- java.util.AbstractList<E>
-
- fr.dufrenoy.util.UnsynchronizedTreeList<E>
-
- Type Parameters:
E- the type of elements maintained by this list
- All Implemented Interfaces:
TreeList<E>,Serializable,Iterable<E>,Collection<E>,List<E>
public class UnsynchronizedTreeList<E> extends AbstractList<E> implements TreeList<E>, Serializable
A non-thread-safe implementation ofTreeListbacked by a red-black tree augmented with subtree sizes (order-statistic tree).The augmentation stores, in each node, the size of the subtree rooted at that node. This allows index-based access (
get(int),remove(int)) to run in O(log n) rather than O(n).All structural operations (
add(Object),remove(int),remove(Object)) run in O(log n). Lookup operations (contains(Object),indexOf(Object)) also run in O(log n) by exploiting the sorted order.This implementation is not thread-safe. For concurrent use, prefer
SynchronizedTreeList.- Version:
- 1.0
- Author:
- Dufrenoy
- See Also:
TreeList,SynchronizedTreeList, Serialized Form
-
-
Field Summary
-
Fields inherited from class java.util.AbstractList
modCount
-
-
Constructor Summary
Constructors Constructor Description UnsynchronizedTreeList()Constructs an emptyUnsynchronizedTreeListordered by the natural ordering of its elements.UnsynchronizedTreeList(Collection<? extends E> c)Constructs aUnsynchronizedTreeListcontaining the elements of the given collection, ordered by their natural ordering.UnsynchronizedTreeList(Comparator<? super E> comparator)Constructs an emptyUnsynchronizedTreeListordered by the given comparator.UnsynchronizedTreeList(Comparator<? super E> comparator, Collection<? extends E> c)Constructs aUnsynchronizedTreeListcontaining the elements of the given collection, ordered by the given comparator.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidadd(int index, E element)Not supported.booleanadd(E e)Inserts the specified element in its sorted position in this list.booleanaddAll(int index, Collection<? extends E> c)booleanaddAll(Collection<? extends E> c)voidclear()Removes all elements from this list.Optional<Comparator<? super E>>comparator()Returns the comparator used to order the elements in this list, or an emptyOptionalif the elements are ordered by their natural ordering.booleancontains(Object o)Returnstrueif this list contains the specified element.Eget(int index)Returns the element at the specified position in this list.intindexOf(Object o)Returns the index of the first (and only) occurrence of the specified element in this list, or-1if it is not present.Iterator<E>iterator()Returns an iterator over the elements in this list in sorted (ascending) order.intlastIndexOf(Object o)Equivalent toindexOf(Object)since this list contains no duplicates.ListIterator<E>listIterator(int index)Returns a list iterator over the elements in this list in sorted order, starting at the specified position.Eremove(int index)Removes the element at the specified position in this list.booleanremove(Object o)Removes the specified element from this list if it is present.booleanremoveAll(Collection<?> c)Removes from this list all elements that are contained in the specified collection.booleanretainAll(Collection<?> c)Retains only the elements in this list that are contained in the specified collection.Eset(int index, E element)Not supported.intsize()Returns the number of elements in this list.TreeList<E>subList(int fromIndex, int toIndex)Returns a live view of the portion of this list whose elements range from the element atfromIndex(inclusive) to the element attoIndex(exclusive).-
Methods inherited from class java.util.AbstractList
equals, hashCode, listIterator, removeRange
-
Methods inherited from class java.util.AbstractCollection
containsAll, isEmpty, toArray, toArray, toString
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Collection
parallelStream, removeIf, stream, toArray
-
Methods inherited from interface java.util.List
containsAll, equals, hashCode, isEmpty, listIterator, replaceAll, sort, spliterator, toArray, toArray
-
-
-
-
Constructor Detail
-
UnsynchronizedTreeList
public UnsynchronizedTreeList()
Constructs an emptyUnsynchronizedTreeListordered by the natural ordering of its elements.All elements inserted must implement
Comparable. Attempting to insert an element that does not will throw aClassCastException.
-
UnsynchronizedTreeList
public UnsynchronizedTreeList(Comparator<? super E> comparator)
Constructs an emptyUnsynchronizedTreeListordered by the given comparator.- Parameters:
comparator- the comparator used to order elements, ornullto use natural ordering
-
UnsynchronizedTreeList
public UnsynchronizedTreeList(Collection<? extends E> c)
Constructs aUnsynchronizedTreeListcontaining the elements of the given collection, ordered by their natural ordering. Duplicate elements (as determined by natural ordering) are silently discarded.- Parameters:
c- the collection whose elements are to be placed into this list- Throws:
ClassCastException- if any element is not mutually comparableNullPointerException- ifcisnull, or if any element ofcisnull
-
UnsynchronizedTreeList
public UnsynchronizedTreeList(Comparator<? super E> comparator, Collection<? extends E> c)
Constructs aUnsynchronizedTreeListcontaining the elements of the given collection, ordered by the given comparator. Duplicate elements (as determined by the comparator) are silently discarded.- Parameters:
comparator- the comparator used to order elements, ornullto use natural orderingc- the collection whose elements are to be placed into this list- Throws:
ClassCastException- if any element is not mutually comparableNullPointerException- ifcisnull, or if any element ofcisnull
-
-
Method Detail
-
comparator
public Optional<Comparator<? super E>> comparator()
Returns the comparator used to order the elements in this list, or an emptyOptionalif the elements are ordered by their natural ordering.- Specified by:
comparatorin interfaceTreeList<E>- Returns:
- the comparator used to order this list, or an empty
Optionalif natural ordering is used
-
size
public int size()
Returns the number of elements in this list.- Specified by:
sizein interfaceCollection<E>- Specified by:
sizein interfaceList<E>- Specified by:
sizein classAbstractCollection<E>- Returns:
- the number of elements in this list
-
get
public E get(int index)
Returns the element at the specified position in this list. Runs in O(log n) using the augmented subtree sizes.- Specified by:
getin interfaceList<E>- Specified by:
getin classAbstractList<E>- Parameters:
index- index of the element to return- Returns:
- the element at the specified position
- Throws:
IndexOutOfBoundsException- ifindex < 0 || index >= size()
-
add
public boolean add(E e)
Inserts the specified element in its sorted position in this list. Returnsfalseif an element equal toe(as determined by the comparator or natural ordering) is already present, leaving the list unchanged.- Specified by:
addin interfaceCollection<E>- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>- Parameters:
e- the element to add- Returns:
trueif this list changed as a result of the call,falseifewas already present- Throws:
ClassCastException- ifeis not mutually comparable with the existing elementsNullPointerException- ifeisnull
-
add
public void add(int index, E element)Not supported. The position of elements in aTreeListis determined by sort order, not by the caller.- Specified by:
addin interfaceList<E>- Overrides:
addin classAbstractList<E>- Throws:
UnsupportedOperationException- always
-
addAll
public boolean addAll(Collection<? extends E> c)
- Specified by:
addAllin interfaceCollection<E>- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractCollection<E>- Throws:
NullPointerException- ifcisnull, or if any element ofcisnull
-
addAll
public boolean addAll(int index, Collection<? extends E> c)- Specified by:
addAllin interfaceList<E>- Overrides:
addAllin classAbstractList<E>- Throws:
UnsupportedOperationException- always (positional insertion is not supported)
-
remove
public E remove(int index)
Removes the element at the specified position in this list. Runs in O(log n).- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractList<E>- Parameters:
index- the index of the element to be removed- Returns:
- the element previously at the specified position
- Throws:
IndexOutOfBoundsException- ifindex < 0 || index >= size()
-
remove
public boolean remove(Object o)
Removes the specified element from this list if it is present. Runs in O(log n) using the sorted structure.- Specified by:
removein interfaceCollection<E>- Specified by:
removein interfaceList<E>- Overrides:
removein classAbstractCollection<E>- Parameters:
o- the element to be removed- Returns:
trueif this list contained the specified element
-
removeAll
public boolean removeAll(Collection<?> c)
Removes from this list all elements that are contained in the specified collection. Runs in O(m log n) where m is the size ofc.- Specified by:
removeAllin interfaceCollection<E>- Specified by:
removeAllin interfaceList<E>- Overrides:
removeAllin classAbstractCollection<E>- Parameters:
c- the collection containing elements to be removed- Returns:
trueif this list changed as a result of the call- Throws:
NullPointerException- ifcisnull
-
retainAll
public boolean retainAll(Collection<?> c)
Retains only the elements in this list that are contained in the specified collection. Runs in O(n log m) where m is the size ofc.- Specified by:
retainAllin interfaceCollection<E>- Specified by:
retainAllin interfaceList<E>- Overrides:
retainAllin classAbstractCollection<E>- Parameters:
c- the collection containing elements to be retained- Returns:
trueif this list changed as a result of the call- Throws:
NullPointerException- ifcisnull
-
clear
public void clear()
Removes all elements from this list. The list will be empty after this call returns.- Specified by:
clearin interfaceCollection<E>- Specified by:
clearin interfaceList<E>- Overrides:
clearin classAbstractList<E>
-
set
public E set(int index, E element)
Not supported. The position of elements in aTreeListis determined by sort order, not by the caller.- Specified by:
setin interfaceList<E>- Overrides:
setin classAbstractList<E>- Throws:
UnsupportedOperationException- always
-
subList
public TreeList<E> subList(int fromIndex, int toIndex)
Returns a live view of the portion of this list whose elements range from the element atfromIndex(inclusive) to the element attoIndex(exclusive). The returned view is itself aTreeList: it is sorted, contains no duplicates, and supports the same operations as this list.The view is bounded by element values, not by indices. After creation, the view contains all elements
ein this list such thatcompare(fromElement, e) <= 0andcompare(e, toElement) < 0, wherefromElementandtoElementare the elements at positionsfromIndexandtoIndexat the time the view was created.Structural modifications through the view (add, remove, clear) are reflected in the parent list, and vice versa. However, if the parent list is structurally modified outside the view, the view becomes invalid and subsequent operations will throw
ConcurrentModificationException(fail-fast).Adding an element that falls outside the view's value range throws
IllegalArgumentException, following the same convention asTreeMap.subMap(Object, Object).Returns a live view bounded by the element values at positions
fromIndexandtoIndexat the time this method is called.- Specified by:
subListin interfaceList<E>- Specified by:
subListin interfaceTreeList<E>- Overrides:
subListin classAbstractList<E>- Parameters:
fromIndex- low endpoint (inclusive) of the subListtoIndex- high endpoint (exclusive) of the subList- Returns:
- a view of the specified range within this list
- Throws:
IndexOutOfBoundsException- iffromIndex < 0ortoIndex > size()IllegalArgumentException- iffromIndex > toIndex
-
contains
public boolean contains(Object o)
Returnstrueif this list contains the specified element. Runs in O(log n) by performing a binary search on the red-black tree.- Specified by:
containsin interfaceCollection<E>- Specified by:
containsin interfaceList<E>- Overrides:
containsin classAbstractCollection<E>- Parameters:
o- the element whose presence is to be tested- Returns:
trueif this list contains the element
-
indexOf
public int indexOf(Object o)
Returns the index of the first (and only) occurrence of the specified element in this list, or-1if it is not present. Runs in O(log n) by performing a binary search on the red-black tree.
-
lastIndexOf
public int lastIndexOf(Object o)
Equivalent toindexOf(Object)since this list contains no duplicates.- Specified by:
lastIndexOfin interfaceList<E>- Overrides:
lastIndexOfin classAbstractList<E>- Parameters:
o- the element to search for- Returns:
- the index of the element, or
-1if not present
-
iterator
public Iterator<E> iterator()
Returns an iterator over the elements in this list in sorted (ascending) order. The iterator performs an in-order traversal of the underlying red-black tree in O(n) total.The iterator does not support
Iterator.remove().
-
listIterator
public ListIterator<E> listIterator(int index)
Returns a list iterator over the elements in this list in sorted order, starting at the specified position.ListIterator.add(Object)andListIterator.set(Object)are not supported and will throwUnsupportedOperationException.- Specified by:
listIteratorin interfaceList<E>- Overrides:
listIteratorin classAbstractList<E>- Parameters:
index- index of the first element to be returned byListIterator.next()- Returns:
- a list iterator over the elements in sorted order
- Throws:
IndexOutOfBoundsException- ifindex < 0 || index > size()
-
-