Class ZFastTrie<T>
- All Implemented Interfaces:
it.unimi.dsi.fastutil.objects.ObjectBidirectionalIterable<T>, it.unimi.dsi.fastutil.objects.ObjectCollection<T>, it.unimi.dsi.fastutil.objects.ObjectIterable<T>, it.unimi.dsi.fastutil.objects.ObjectSet<T>, it.unimi.dsi.fastutil.objects.ObjectSortedSet<T>, Serializable, Cloneable, Iterable<T>, Collection<T>, SequencedCollection<T>, SequencedSet<T>, Set<T>, SortedSet<T>
In rough terms, the z-fast trie uses time |x|/w (which is optimal) to actually look at the string content,
and log(max{|x|, |x-|, |x+|}) to perform the search. This is known to be (essentially) optimal.
String lengths are up to Integer.MAX_VALUE, and not limited to be a constant multiple of w for the bounds to hold.
The linear overhead of a z-fast trie is very low. For n keys we allocate 2n − 1 nodes containing six references and two longs, plus a dictionary containing n − 1 nodes (thus using around 2n references and 2n longs).
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprotected static final classprotected static final classA linear-probing hash map that compares keys using signatures as a first try.protected static final classA internal node.protected static final classAn external node, a.k.a. leaf.protected static classA node of the trie.protected static final class -
Field Summary
FieldsModifier and TypeFieldDescriptionA dictionary mapping handles to the corresponding internal nodes.static final long -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new z-fast trie using the given transformation strategy.ZFastTrie(Iterable<? extends T> elements, it.unimi.dsi.bits.TransformationStrategy<? super T> transform) Creates a new z-fast trie using the given elements and transformation strategy.ZFastTrie(Iterator<? extends T> elements, it.unimi.dsi.bits.TransformationStrategy<? super T> transform) Creates a new z-fast trie using the given elements and transformation strategy. -
Method Summary
Modifier and TypeMethodDescriptionbooleanReturns the first element in the trie that is greater than or equal to the provided bound.static final longcheckMask(long b) Returns the mask used for check for 2-fattest numbers when the left extreme of the interval is −1.static final longcheckMask(long a, long b) Returns the mask used for check for 2-fattest numbers when the left extreme of the interval is not -1.Comparator<? super T> protected voidcompleteFatBinarySearchStack(it.unimi.dsi.bits.LongArrayBitVector v, long[] state, it.unimi.dsi.fastutil.objects.ObjectArrayList<ZFastTrie.InternalNode<T>> stack, long a, long b) Completes the stack of a previous successful fat binary search.booleanprotected ZFastTrie.InternalNode<T> fatBinarySearch(it.unimi.dsi.bits.LongArrayBitVector v, long[] state, long b) Performs a non-exact fat binary search.protected ZFastTrie.InternalNode<T> fatBinarySearchExact(it.unimi.dsi.bits.LongArrayBitVector v, long[] state, long b) Performs an exact fat binary search.protected voidfatBinarySearchStack(it.unimi.dsi.bits.LongArrayBitVector v, long[] state, it.unimi.dsi.fastutil.objects.ObjectArrayList<ZFastTrie.InternalNode<T>> stack, long b) Performs a non-exact fat binary search with stack.protected voidfatBinarySearchStackExact(it.unimi.dsi.bits.LongArrayBitVector v, long[] state, it.unimi.dsi.fastutil.objects.ObjectArrayList<ZFastTrie.InternalNode<T>> stack, long b) Performs an exact fat binary search with stack.first()Returns the first element in the trie that is smaller than or equal to the provided bound.voidgetGrandParentExitNode(it.unimi.dsi.bits.LongArrayBitVector v, long[] state, it.unimi.dsi.fastutil.objects.ObjectArrayList<ZFastTrie.InternalNode<T>> stack) Returns the grandparent of the exit node of a given bit vector.getParentExitNode(it.unimi.dsi.bits.LongArrayBitVector v, long[] state, it.unimi.dsi.fastutil.objects.ObjectArrayList<ZFastTrie.InternalNode<T>> stack) Returns the parent of the exit node of a given bit vector.it.unimi.dsi.fastutil.objects.ObjectSortedSet<T> Returns the first element in the trie that is greater than the provided bound.booleanisNonempty(T lowerBound, T upperBound) Returns whether there is an element between the given bounds.it.unimi.dsi.fastutil.objects.ObjectBidirectionalIterator<T> iterator()it.unimi.dsi.fastutil.objects.ObjectBidirectionalIterator<T> last()Returns the first element in the trie that is smaller than the provided bound.static voidpredecessor(T upperBound) Returns the first element in the trie that is smaller than the provided bound.booleanintsize()strictSuccessor(T lowerBound) Returns the first element in the trie that is greater than the provided bound.it.unimi.dsi.fastutil.objects.ObjectSortedSet<T> Returns the first element in the trie that is greater than or equal to the provided bound.it.unimi.dsi.fastutil.objects.ObjectSortedSet<T> static final longtwoFattest(long a, long b) Returns the 2-fattest number in an interval.weakPredecessor(T upperBound) Returns the first element in the trie that is smaller than or equal to the provided bound.Methods inherited from class it.unimi.dsi.fastutil.objects.AbstractObjectSet
equals, hashCodeMethods inherited from class it.unimi.dsi.fastutil.objects.AbstractObjectCollection
toStringMethods inherited from class AbstractCollection
addAll, clear, containsAll, isEmpty, removeAll, retainAll, toArray, toArrayMethods inherited from interface Collection
parallelStream, removeIf, stream, toArrayMethods inherited from interface it.unimi.dsi.fastutil.objects.ObjectSortedSet
spliteratorMethods inherited from interface Set
addAll, clear, containsAll, equals, hashCode, isEmpty, removeAll, retainAll, toArray, toArrayMethods inherited from interface SortedSet
addFirst, addLast, getFirst, getLast, removeFirst, removeLast, reversed
-
Field Details
-
serialVersionUID
public static final long serialVersionUID- See Also:
-
handle2Node
A dictionary mapping handles to the corresponding internal nodes.
-
-
Constructor Details
-
ZFastTrie
Creates a new z-fast trie using the given transformation strategy.- Parameters:
transform- a transformation strategy that must turn distinct elements into distinct, prefix-free bit vectors.
-
ZFastTrie
public ZFastTrie(Iterator<? extends T> elements, it.unimi.dsi.bits.TransformationStrategy<? super T> transform) Creates a new z-fast trie using the given elements and transformation strategy.- Parameters:
elements- an iterator returning the elements to be inserted in the trie.transform- a transformation strategy that must turn distinct elements into distinct, prefix-free bit vectors.
-
ZFastTrie
public ZFastTrie(Iterable<? extends T> elements, it.unimi.dsi.bits.TransformationStrategy<? super T> transform) Creates a new z-fast trie using the given elements and transformation strategy.- Parameters:
elements- an iterator returning the elements to be inserted in the trie.transform- a transformation strategy that must turn distinct elements into distinct, prefix-free bit vectors.
-
-
Method Details
-
size
public int size()- Specified by:
sizein interfaceCollection<T>- Specified by:
sizein interfaceSet<T>- Specified by:
sizein classAbstractCollection<T>
-
twoFattest
public static final long twoFattest(long a, long b) Returns the 2-fattest number in an interval.Note that to get the length of the handle of a node you must call this function passing the length of the extent of the parent (one less than the node name) and the length of the extent of the node.
- Parameters:
a- left extreme, ≥-1 (excluded).b- right extreme, ≥ 0 (included).- Returns:
- the 2-fattest number in (
a..b].
-
checkMask
public static final long checkMask(long a, long b) Returns the mask used for check for 2-fattest numbers when the left extreme of the interval is not -1.Note that to get the length of the handle of a node you must call this function passing the length of the extent of the parent (one less than the node name) and the length of the extent of the node.
- Parameters:
a- left extreme, ≥-1 (excluded).b- right extreme, ≥ 0 (included).- Returns:
- −1 ≪ λ(
a⊕b), the initial mask for fat binary search in(a..b].
-
checkMask
public static final long checkMask(long b) Returns the mask used for check for 2-fattest numbers when the left extreme of the interval is −1.- Parameters:
b- right extreme, ≥ 0 (included).- Returns:
- −1 ≪ λ
b+ 1, the initial mask for fat binary search in(-1..b].
-
add
- Specified by:
addin interfaceCollection<T>- Specified by:
addin interfaceSet<T>- Overrides:
addin classAbstractCollection<T>
-
remove
- Specified by:
removein interfaceCollection<T>- Specified by:
removein interfaceSet<T>- Overrides:
removein classAbstractCollection<T>
-
getParentExitNode
public ZFastTrie.ParexData<T> getParentExitNode(it.unimi.dsi.bits.LongArrayBitVector v, long[] state, it.unimi.dsi.fastutil.objects.ObjectArrayList<ZFastTrie.InternalNode<T>> stack) Returns the parent of the exit node of a given bit vector.- Parameters:
v- a bit vector.state- the hash state ofvprecomputed byHashes.preprocessMurmur(BitVector, long).stack- a stack that will be filled with the 2-fat ancestors.- Returns:
- the parent of the exit node of
v, ornullif the exit node is the root.
-
getGrandParentExitNode
public void getGrandParentExitNode(it.unimi.dsi.bits.LongArrayBitVector v, long[] state, it.unimi.dsi.fastutil.objects.ObjectArrayList<ZFastTrie.InternalNode<T>> stack) Returns the grandparent of the exit node of a given bit vector.- Parameters:
v- a bit vector.state- the hash state ofvprecomputed byHashes.preprocessMurmur(BitVector, long).stack- a nonempty stack as filled bygetParentExitNode(LongArrayBitVector, long[], ObjectArrayList); the top of the stack must not be the root.
-
fatBinarySearchStack
protected void fatBinarySearchStack(it.unimi.dsi.bits.LongArrayBitVector v, long[] state, it.unimi.dsi.fastutil.objects.ObjectArrayList<ZFastTrie.InternalNode<T>> stack, long b) Performs a non-exact fat binary search with stack.- Parameters:
v- the bit vector on which to perform the search.state- preprocessed MurmurHash state forv.stack- a stack where the results of the search will be cumulated.b- the right extreme of the search interval, ≥ −1 (included).
-
fatBinarySearchStackExact
protected void fatBinarySearchStackExact(it.unimi.dsi.bits.LongArrayBitVector v, long[] state, it.unimi.dsi.fastutil.objects.ObjectArrayList<ZFastTrie.InternalNode<T>> stack, long b) Performs an exact fat binary search with stack.- Parameters:
v- the bit vector on which to perform the search.state- preprocessed MurmurHash state forv.stack- a stack where the results of the search will be cumulated.b- the right extreme of the search interval, ≥ −1 (included).
-
completeFatBinarySearchStack
protected void completeFatBinarySearchStack(it.unimi.dsi.bits.LongArrayBitVector v, long[] state, it.unimi.dsi.fastutil.objects.ObjectArrayList<ZFastTrie.InternalNode<T>> stack, long a, long b) Completes the stack of a previous successful fat binary search.- Parameters:
v- the bit vector on which to perform the search.state- preprocessed MurmurHash state forv.stack- a stack where the results of the completion will be cumulated.a- the left extreme of the completion interval, ≥ −1 (excluded)b- the right extreme of the completion interval, ≥a(included).
-
fatBinarySearch
protected ZFastTrie.InternalNode<T> fatBinarySearch(it.unimi.dsi.bits.LongArrayBitVector v, long[] state, long b) Performs a non-exact fat binary search.- Parameters:
v- the bit vector on which to perform the search.state- preprocessed MurmurHash state forv.b- the right extreme of the search interval, ≥ −1 (included).- Returns:
- the parent of the exit node or the exit node, in case of success; an arbitrary node otherwise.
-
fatBinarySearchExact
protected ZFastTrie.InternalNode<T> fatBinarySearchExact(it.unimi.dsi.bits.LongArrayBitVector v, long[] state, long b) Performs an exact fat binary search.- Parameters:
v- the bit vector on which to perform the search.state- preprocessed MurmurHash state forv.b- the right extreme of the search interval, ≥ −1 (included).- Returns:
- the parent of the exit node.
-
contains
- Specified by:
containsin interfaceCollection<T>- Specified by:
containsin interfaceSet<T>- Overrides:
containsin classAbstractCollection<T>
-
successor
Returns the first element in the trie that is greater than or equal to the provided bound.- Parameters:
lowerBound- a lower bound on the returned value.- Returns:
- the first element in the trie that is greater than or equal to
lowerBound, ornullif no such element exists.
-
ceiling
Returns the first element in the trie that is greater than or equal to the provided bound.- Parameters:
lowerBound- a lower bound on the returned value.- Returns:
- the first element in the trie that is greater than or equal to
lowerBound, ornullif no such element exists. - Implementation Specification:
- This method just delegates to
successor(Object).
-
strictSuccessor
-
higher
Returns the first element in the trie that is greater than the provided bound.- Parameters:
lowerBound- a strict lower bound on the returned value.- Returns:
- the first element in the trie that is greater than
lowerBound, ortailif no such element exists. - Implementation Specification:
- This method just delegates to
strictSuccessor(Object).
-
predecessor
-
lower
Returns the first element in the trie that is smaller than the provided bound.- Parameters:
upperBound- a strict upper bound on the returned value.- Returns:
- the first element in the trie that is smaller than
upperBound, orheadif no such element exists. - Implementation Specification:
- This method just delegates to
predecessor(Object).
-
weakPredecessor
Returns the first element in the trie that is smaller than or equal to the provided bound.- Parameters:
upperBound- an upper bound on the returned value.- Returns:
- the first element in the trie that is smaller than or equal to
upperBound, orheadif no such element exists.
-
floor
Returns the first element in the trie that is smaller than or equal to the provided bound.- Parameters:
upperBound- an upper bound on the returned value.- Returns:
- the first element in the trie that is smaller than or equal to
upperBound, orheadif no such element exists. - Implementation Specification:
- This method just delegates to
weakPredecessor(Object).
-
isNonempty
-
iterator
- Specified by:
iteratorin interfaceCollection<T>- Specified by:
iteratorin interfaceIterable<T>- Specified by:
iteratorin interfaceit.unimi.dsi.fastutil.objects.ObjectBidirectionalIterable<T>- Specified by:
iteratorin interfaceit.unimi.dsi.fastutil.objects.ObjectCollection<T>- Specified by:
iteratorin interfaceit.unimi.dsi.fastutil.objects.ObjectIterable<T>- Specified by:
iteratorin interfaceit.unimi.dsi.fastutil.objects.ObjectSet<T>- Specified by:
iteratorin interfaceit.unimi.dsi.fastutil.objects.ObjectSortedSet<T>- Specified by:
iteratorin interfaceSet<T>- Specified by:
iteratorin classit.unimi.dsi.fastutil.objects.AbstractObjectSortedSet<T>
-
iterator
-
comparator
- Specified by:
comparatorin interfaceSortedSet<T>
-
first
-
last
-
headSet
-
subSet
-
tailSet
-
main
public static void main(String[] arg) throws NoSuchMethodException, IOException, com.martiansoftware.jsap.JSAPException - Throws:
NoSuchMethodExceptionIOExceptioncom.martiansoftware.jsap.JSAPException
-