- Type Parameters:
E- the element type
- All Implemented Interfaces:
Serializable,Iterable<E>,ImmutableCollection<E>,ImmutableSequencedCollection<E>,ImmutableSequencedSet<E>,ImmutableSet<E>,ReadOnlyCollection<E>,ReadOnlySequencedCollection<E>,ReadOnlySequencedSet<E>,ReadOnlySet<E>
ImmutableSequencedSet interface using a Compressed
Hash-Array Mapped Prefix-tree (CHAMP) and a bit-mapped trie (Vector).
Features:
- supports up to 230 elements
- allows null elements
- is immutable
- is thread-safe
- iterates in the order, in which elements were inserted
Performance characteristics:
- add: O(log₃₂ N) in an amortized sense, because we sometimes have to renumber the elements.
- remove: O(log₃₂ N) in an amortized sense, because we sometimes have to renumber the elements.
- contains: O(log₃₂ N)
- toMutable: O(1) + O(log₃₂ N) distributed across subsequent updates in the mutable copy
- clone: O(1)
- iterator creation: O(log₃₂ N)
- iterator.next: O(1)
- getFirst(), getLast(): O(log₃₂ N)
Implementation details:
This set performs read and write operations of single elements in O(log N) time, and in O(log N) space, where N is the number of elements in the set.
The CHAMP trie contains nodes that may be shared with other sets.
If a write operation is performed on a node, then this set creates a copy of the node and of all parent nodes up to the root (copy-path-on-write). Since the CHAMP trie has a fixed maximal height, the cost is O(1).
This set can create a mutable copy of itself in O(1) time and O(1) space
using method toMutable(). The mutable copy shares its nodes
with this set, until it has gradually replaced the nodes with exclusively
owned nodes.
Insertion Order:
This set uses a counter to keep track of the insertion order. It stores the current value of the counter in the sequence number field of each data entry. If the counter wraps around, it must renumber all sequence numbers.
The renumbering is why the add and remove methods are O(1)
only in an amortized sense.
To support iteration, we use a Vector. The Vector has the same contents as the CHAMP trie. However, its elements are stored in insertion order.
If an element is removed from the CHAMP trie that is not the first or the last element of the Vector, we replace its corresponding element in the Vector by a tombstone. If the element is at the start or end of the Vector, we remove the element and all its neighboring tombstones from the Vector.
A tombstone can store the number of neighboring tombstones in ascending and in descending direction. We use these numbers to skip tombstones when we iterate over the vector. Since we only allow iteration in ascending or descending order from one of the ends of the vector, we do not need to keep the number of neighbors in all tombstones up to date. It is sufficient, if we update the neighbor with the lowest index and the one with the highest index.
If the number of tombstones exceeds half of the size of the collection, we renumber all sequence numbers, and we create a new Vector.
The immutable version of this set extends from the non-public class
ChampBitmapIndexNode. This design safes 16 bytes for every instance,
and reduces the number of redirections for finding an element in the
collection by 1.
References:
For a similar design, see 'SimpleImmutableSequencedMap.scala'. Note, that this code is not a derivative of that code.
- The Scala library. SimpleImmutableSequencedMap.scala. Copyright EPFL and Lightbend, Inc. Apache License 2.0.
- github.com
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedChampVectorSet(PrivateData privateData) Creates a new instance with the provided privateData data object. -
Method Summary
Modifier and TypeMethodDescriptionReturns a copy of this set that contains all elements of this set and also the specified element.Returns a copy of this set that contains all elements of this set and also all elements of the specified collection.Returns a copy of this collection that contains all elements of this collection and also the specified element as the first element in the iteration order.Returns a copy of this collection that contains all elements of this collection and also the specified element as the last element in the iteration order.booleanReturnstrueif this collection contains the specified object.static <E> ChampVectorSet<E> Returns an immutable set that contains the provided elements.<T> ChampVectorSet<T> empty()Returns an empty set instance that has the specified element type.booleanCompares the specified object with this set for equality.getFirst()Gets the first element.getLast()Gets the last element.inthashCode()Returns the hash code value for this set.iterator()Returns an iterator over the elements in this collection.intmaxSize()Returns the maximal number of elements that this collection type can holdprotected ChampVectorSet<E> newInstance(PrivateData privateData) Creates a new instance with the provided privateData object as its internal data structure.static <E> ChampVectorSet<E> of()Returns an empty immutable set.static <E> ChampVectorSet<E> of(E @Nullable ... elements) Returns an immutable set that contains the provided elements.Returns a reversed-order view of this set.Returns a copy of this set that contains all elements of this set except the specified element.Returns a copy of this set that contains all elements of this set except the elements of the specified collection.Returns a copy of this set that contains all elements of this set except the first.Returns a copy of this set that contains all elements of this set except the last.Returns a copy of this set that contains only elements that are in this set and in the specified collection.intsize()Returns the size of the collection.Returns a mutable copy of this set.toString()Returns a string representation of this set.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, waitMethods inherited from interface org.jhotdraw8.icollection.immutable.ImmutableCollection
filterMethods inherited from interface org.jhotdraw8.icollection.immutable.ImmutableSequencedSet
reverseMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlyCollection
containsAll, isEmpty, stream, toArray, toArrayMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlySequencedCollection
asCollectionMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlySequencedSet
asSetMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlySet
characteristics
-
Constructor Details
-
ChampVectorSet
Creates a new instance with the provided privateData data object.This constructor is intended to be called from a constructor of the subclass, that is called from method
newInstance(PrivateData).- Parameters:
privateData- an privateData data object
-
-
Method Details
-
newInstance
Creates a new instance with the provided privateData object as its internal data structure.Subclasses must override this method, and return a new instance of their subclass!
- Parameters:
privateData- the internal data structure needed by this class for creating the instance.- Returns:
- a new instance of the subclass
-
copyOf
Returns an immutable set that contains the provided elements.- Type Parameters:
E- the element type- Parameters:
c- an iterable- Returns:
- an immutable set of the provided elements
-
of
Returns an empty immutable set.- Type Parameters:
E- the element type- Returns:
- an empty immutable set
-
of
Returns an immutable set that contains the provided elements.- Type Parameters:
E- the element type- Parameters:
elements- elements- Returns:
- an immutable set of the provided elements
-
add
Description copied from interface:ImmutableSetReturns a copy of this set that contains all elements of this set and also the specified element.- Specified by:
addin interfaceImmutableCollection<E>- Specified by:
addin interfaceImmutableSequencedCollection<E>- Specified by:
addin interfaceImmutableSequencedSet<E>- Specified by:
addin interfaceImmutableSet<E>- Parameters:
key- an element- Returns:
- this set instance if it already contains the element, or a different set instance with the element added
-
addAll
Description copied from interface:ImmutableSetReturns a copy of this set that contains all elements of this set and also all elements of the specified collection.- Specified by:
addAllin interfaceImmutableCollection<E>- Specified by:
addAllin interfaceImmutableSequencedCollection<E>- Specified by:
addAllin interfaceImmutableSequencedSet<E>- Specified by:
addAllin interfaceImmutableSet<E>- Parameters:
c- a collection to be added to this set- Returns:
- this set instance if it already contains the elements, or a different set instance with the elements added
-
addFirst
Description copied from interface:ImmutableSequencedCollectionReturns a copy of this collection that contains all elements of this collection and also the specified element as the first element in the iteration order.A collection may prevent that the same element can be added more than once.
If the iteration order is based on an ordering relation of the elements, then the element is only the first in a sequence of elements with the same ordering relation; which is not necessarily the first in the total iteration order.
- Specified by:
addFirstin interfaceImmutableSequencedCollection<E>- Specified by:
addFirstin interfaceImmutableSequencedSet<E>- Parameters:
element- an element- Returns:
- this collection instance if it already contains the element as the first in the iteration order, or a different collection instance with the element added as the first in the iteration order
-
addLast
Description copied from interface:ImmutableSequencedCollectionReturns a copy of this collection that contains all elements of this collection and also the specified element as the last element in the iteration order.A collection may prevent that the same element can be added more than once.
If the iteration order is based on an ordering relation of the elements, then the element is only the last in a sequence of elements with the same ordering relation; which is not necessarily the last in the total iteration order.
- Specified by:
addLastin interfaceImmutableSequencedCollection<E>- Specified by:
addLastin interfaceImmutableSequencedSet<E>- Parameters:
element- an element- Returns:
- this collection instance if it already contains the element as the last in the iteration order, or a different collection instance with the element added as the last in the iteration order
-
empty
Returns an empty set instance that has the specified element type.- Specified by:
emptyin interfaceImmutableCollection<E>- Specified by:
emptyin interfaceImmutableSequencedCollection<E>- Specified by:
emptyin interfaceImmutableSequencedSet<E>- Specified by:
emptyin interfaceImmutableSet<E>- Type Parameters:
T- the element type of the returned set- Returns:
- an empty set of the specified element type.
-
contains
Description copied from interface:ReadOnlyCollectionReturnstrueif this collection contains the specified object.- Specified by:
containsin interfaceReadOnlyCollection<E>- Parameters:
o- an object- Returns:
trueif this collection contains the specified object
-
equals
Description copied from interface:ReadOnlySetCompares the specified object with this set for equality.Returns
trueif the given object is also a read-only set and the two sets contain the same elements, ignoring the sequence of the elements.Implementations of this method should use
ReadOnlySet.setEquals(org.jhotdraw8.icollection.readonly.ReadOnlySet<E>, java.lang.Object).- Specified by:
equalsin interfaceReadOnlySet<E>- Overrides:
equalsin classObject- Parameters:
other- an object- Returns:
trueif the object is equal to this map
-
getFirst
Description copied from interface:ReadOnlySequencedCollectionGets the first element.- Specified by:
getFirstin interfaceReadOnlySequencedCollection<E>- Returns:
- an element
-
getLast
Description copied from interface:ReadOnlySequencedCollectionGets the last element.- Specified by:
getLastin interfaceReadOnlySequencedCollection<E>- Returns:
- an element
-
hashCode
public int hashCode()Description copied from interface:ReadOnlySetReturns the hash code value for this set. The hash code is the sum of the hash code of its elements.Implementations of this method should use
ReadOnlySet.iteratorToHashCode(java.util.Iterator<E>).- Specified by:
hashCodein interfaceReadOnlySet<E>- Overrides:
hashCodein classObject- Returns:
- the hash code value for this set
- See Also:
-
iterator
Description copied from interface:ReadOnlyCollectionReturns an iterator over the elements in this collection. -
maxSize
public int maxSize()Description copied from interface:ImmutableCollectionReturns the maximal number of elements that this collection type can hold- Specified by:
maxSizein interfaceImmutableCollection<E>- Returns:
- the maximal size
-
readOnlyReversed
Description copied from interface:ReadOnlySequencedSetReturns a reversed-order view of this set. Changes to the underlying set are visible in the reversed view.- Specified by:
readOnlyReversedin interfaceReadOnlySequencedCollection<E>- Specified by:
readOnlyReversedin interfaceReadOnlySequencedSet<E>- Returns:
- a reversed-order view of this set
-
remove
Description copied from interface:ImmutableSetReturns a copy of this set that contains all elements of this set except the specified element.- Specified by:
removein interfaceImmutableCollection<E>- Specified by:
removein interfaceImmutableSequencedCollection<E>- Specified by:
removein interfaceImmutableSequencedSet<E>- Specified by:
removein interfaceImmutableSet<E>- Parameters:
key- an element- Returns:
- this set instance if it already does not contain the element, or a different set instance with the element removed
-
removeAll
Description copied from interface:ImmutableSetReturns a copy of this set that contains all elements of this set except the elements of the specified collection.- Specified by:
removeAllin interfaceImmutableCollection<E>- Specified by:
removeAllin interfaceImmutableSequencedCollection<E>- Specified by:
removeAllin interfaceImmutableSequencedSet<E>- Specified by:
removeAllin interfaceImmutableSet<E>- Parameters:
c- a collection with elements to be removed from this set- Returns:
- this set instance if it already does not contain the elements, or a different set instance with the elements removed
-
removeFirst
Description copied from interface:ImmutableSequencedSetReturns a copy of this set that contains all elements of this set except the first.- Specified by:
removeFirstin interfaceImmutableSequencedCollection<E>- Specified by:
removeFirstin interfaceImmutableSequencedSet<E>- Returns:
- a new set instance with the first element removed
-
removeLast
Description copied from interface:ImmutableSequencedSetReturns a copy of this set that contains all elements of this set except the last.- Specified by:
removeLastin interfaceImmutableSequencedCollection<E>- Specified by:
removeLastin interfaceImmutableSequencedSet<E>- Returns:
- a new set instance with the last element removed
-
retainAll
Description copied from interface:ImmutableSetReturns a copy of this set that contains only elements that are in this set and in the specified collection.- Specified by:
retainAllin interfaceImmutableCollection<E>- Specified by:
retainAllin interfaceImmutableSequencedCollection<E>- Specified by:
retainAllin interfaceImmutableSequencedSet<E>- Specified by:
retainAllin interfaceImmutableSet<E>- Parameters:
c- a collection with elements to be retained in this set- Returns:
- this set instance if it has not changed, or a different set instance with elements removed
-
size
public int size()Description copied from interface:ReadOnlyCollectionReturns the size of the collection.- Specified by:
sizein interfaceReadOnlyCollection<E>- Returns:
- the size
-
spliterator
- Specified by:
spliteratorin interfaceIterable<E>
-
toMutable
Description copied from interface:ImmutableSetReturns a mutable copy of this set.- Specified by:
toMutablein interfaceImmutableCollection<E>- Specified by:
toMutablein interfaceImmutableSequencedSet<E>- Specified by:
toMutablein interfaceImmutableSet<E>- Returns:
- a mutable copy.
-
toString
Returns a string representation of this set.The string representation is consistent with the one produced by
AbstractCollection.toString().
-