- Type Parameters:
E- the element type
- All Implemented Interfaces:
Serializable,Iterable<E>,ImmutableCollection<E>,ImmutableSet<E>,ReadOnlyCollection<E>,ReadOnlySet<E>
ImmutableSet interface using a Compressed Hash-Array
Mapped Prefix-tree (CHAMP).
Features:
- supports up to 231 - 1 elements
- allows null elements
- is immutable
- is thread-safe
- does not guarantee a specific iteration order
Performance characteristics:
- add: O(log₃₂ N)
- remove: O(log₃₂ N
- contains: O(log₃₂ N)
- toMutable: O(1) + O(log₃₂ N) distributed across subsequent updates in the mutable copy
- clone: O(1)
- iterator.next(): O(1)
Implementation details:
This set performs read and write operations of single elements in O(log₃₂ N) time, and in O(log₃₂ N) space.
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).
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.
All operations on this set can be performed concurrently, without a need for synchronisation.
References:
Portions of the code in this class has been derived from 'The Capsule Hash Trie Collections Library'.
- Michael J. Steindorfer (2017). Efficient Immutable Collections.
- michael.steindorfer.name
- The Capsule Hash Trie Collections Library.
Copyright (c) Michael Steindorfer. BSD-2-Clause License - github.com
- See Also:
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedChampSet(@NonNull 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.booleanReturnstrueif this collection contains the specified object.Returns an immutable set that contains the provided elements.empty()Returns an empty set instance that has the specified element type.booleanCompares the specified object with this set for equality.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 holdnewInstance(@NonNull PrivateData privateData) Creates a new instance with the provided privateData object as its internal data structure.of()Returns an empty immutable set.Returns an immutable set that contains the provided elements.static <T> ChampSet<T> ofIterator(Iterator<T> iterator) 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 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()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.readonly.ReadOnlyCollection
asCollection, containsAll, isEmpty, stream, toArray, toArrayMethods inherited from interface org.jhotdraw8.icollection.readonly.ReadOnlySet
asSet, characteristics
-
Constructor Details
-
ChampSet
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
-
ofIterator
-
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 interfaceImmutableSet<E>- Parameters:
element- 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 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
-
empty
Returns an empty set instance that has the specified element type.- Specified by:
emptyin interfaceImmutableCollection<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
-
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
-
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 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 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
-
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 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 interfaceImmutableSet<E>- Returns:
- a mutable copy.
-
toString
-