Class PatriciaTrie<K,V>
- java.lang.Object
-
- java.util.AbstractMap<K,V>
-
- org.apache.cassandra.index.sasi.utils.trie.PatriciaTrie<K,V>
-
- All Implemented Interfaces:
java.io.Serializable,java.util.Map<K,V>,java.util.SortedMap<K,V>,Trie<K,V>
public class PatriciaTrie<K,V> extends java.util.AbstractMap<K,V> implements java.io.SerializablePATRICIA
Practical Algorithm to Retrieve Information Coded in AlphanumericTrieA PATRICIA
Trieis a compressedTrie. Instead of storing all data at the edges of theTrie(and having empty internal nodes), PATRICIA stores data in every node. This allows for very efficient traversal, insert, delete, predecessor, successor, prefix, range, andTrie.select(Object)operations. All operations are performed at worst in O(K) time, where K is the number of bits in the largest item in the tree. In practice, operations actually take O(A(K)) time, where A(K) is the average number of bits of all items in the tree.Most importantly, PATRICIA requires very few comparisons to keys while doing any operation. While performing a lookup, each comparison (at most K of them, described above) will perform a single bit comparison against the given key, instead of comparing the entire key to another key.
The
Triecan return operations in lexicographical order using theTrie.traverse(Cursor), 'prefix', 'submap', or 'iterator' methods. TheTriecan also scan for items that are 'bitwise' (using an XOR metric) by the 'select' method. Bitwise closeness is determined by theKeyAnalyzerreturning true or false for a bit being set or not in a given key.Any methods here that take an
Objectargument may throw aClassCastExceptionif the method is expecting an instance of K and it isn't K.- Author:
- Roger Kapsi, Sam Berlin
- See Also:
- Radix Tree, PATRICIA, Crit-Bit Tree, Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description protected KeyAnalyzer<? super K>keyAnalyzerTheKeyAnalyzerthat's being used to build the PATRICIATrie
-
Constructor Summary
Constructors Constructor Description PatriciaTrie(KeyAnalyzer<? super K> keyAnalyzer)PatriciaTrie(KeyAnalyzer<? super K> keyAnalyzer, java.util.Map<? extends K,? extends V> m)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()java.util.Comparator<? super K>comparator()booleancontainsKey(java.lang.Object k)java.util.Set<java.util.Map.Entry<K,V>>entrySet()KfirstKey()Vget(java.lang.Object k)java.util.SortedMap<K,V>headMap(K toKey)java.util.Set<K>keySet()KlastKey()java.util.SortedMap<K,V>prefixMap(K prefix)Returns a view of thisTrieof all elements that are prefixed by the given key.Vput(K key, V value)Vremove(java.lang.Object k)java.util.Map.Entry<K,V>select(K key)Returns theMap.Entrywhose key is closest in a bitwise XOR metric to the given key.java.util.Map.Entry<K,V>select(K key, Cursor<? super K,? super V> cursor)Iterates through theTrie, starting with the entry whose bitwise value is closest in an XOR metric to the given key.KselectKey(K key)Returns the key that is closest in a bitwise XOR metric to the provided key.VselectValue(K key)Returns the value whose key is closest in a bitwise XOR metric to the provided key.intsize()java.util.SortedMap<K,V>subMap(K fromKey, K toKey)java.util.SortedMap<K,V>tailMap(K fromKey)java.lang.StringtoString()java.util.Map.Entry<K,V>traverse(Cursor<? super K,? super V> cursor)Traverses theTriein lexicographical order.java.util.Collection<V>values()-
Methods inherited from class java.util.AbstractMap
clone, containsValue, equals, hashCode, isEmpty, putAll
-
-
-
-
Field Detail
-
keyAnalyzer
protected final KeyAnalyzer<? super K> keyAnalyzer
TheKeyAnalyzerthat's being used to build the PATRICIATrie
-
-
Constructor Detail
-
PatriciaTrie
public PatriciaTrie(KeyAnalyzer<? super K> keyAnalyzer)
-
PatriciaTrie
public PatriciaTrie(KeyAnalyzer<? super K> keyAnalyzer, java.util.Map<? extends K,? extends V> m)
-
-
Method Detail
-
comparator
public java.util.Comparator<? super K> comparator()
-
prefixMap
public java.util.SortedMap<K,V> prefixMap(K prefix)
Description copied from interface:TrieReturns a view of thisTrieof all elements that are prefixed by the given key.In a
Triewith fixed size keys, this is essentially aMap.get(Object)operation.For example, if the
Triecontains 'Anna', 'Anael', 'Analu', 'Andreas', 'Andrea', 'Andres', and 'Anatole', then a lookup of 'And' would return 'Andreas', 'Andrea', and 'Andres'.
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Map<K,V>- Overrides:
clearin classjava.util.AbstractMap<K,V>
-
size
public int size()
- Specified by:
sizein interfacejava.util.Map<K,V>- Overrides:
sizein classjava.util.AbstractMap<K,V>
-
put
public V put(K key, V value)- Specified by:
putin interfacejava.util.Map<K,V>- Overrides:
putin classjava.util.AbstractMap<K,V>
-
get
public V get(java.lang.Object k)
- Specified by:
getin interfacejava.util.Map<K,V>- Overrides:
getin classjava.util.AbstractMap<K,V>
-
select
public java.util.Map.Entry<K,V> select(K key)
Description copied from interface:TrieReturns theMap.Entrywhose key is closest in a bitwise XOR metric to the given key. This is NOT lexicographic closeness. For example, given the keys:- D = 1000100
- H = 1001000
- L = 1001100
Triecontained 'H' and 'L', a lookup of 'D' would return 'L', because the XOR distance between D & L is smaller than the XOR distance between D & H.- Returns:
- The
Map.Entrywhose key is closest in a bitwise XOR metric to the provided key.
-
select
public java.util.Map.Entry<K,V> select(K key, Cursor<? super K,? super V> cursor)Description copied from interface:TrieIterates through theTrie, starting with the entry whose bitwise value is closest in an XOR metric to the given key. After the closest entry is found, theTriewill call select on that entry and continue calling select for each entry (traversing in order of XOR closeness, NOT lexicographically) until the cursor returnsCursor.Decision.EXIT.The cursor can return
Cursor.Decision.CONTINUEto continue traversing.Cursor.Decision.REMOVE_AND_EXITis used to remove the current element and stop traversing.Note: The
Cursor.Decision.REMOVEoperation is not supported.- Returns:
- The entry the cursor returned
Cursor.Decision.EXITon, or null if it continued till the end.
-
traverse
public java.util.Map.Entry<K,V> traverse(Cursor<? super K,? super V> cursor)
Description copied from interface:TrieTraverses theTriein lexicographical order.Cursor.select(java.util.Map.Entry)will be called on each entry.The traversal will stop when the cursor returns
Cursor.Decision.EXIT,Cursor.Decision.CONTINUEis used to continue traversing andCursor.Decision.REMOVEis used to remove the element that was selected and continue traversing.Cursor.Decision.REMOVE_AND_EXITis used to remove the current element and stop traversing.- Returns:
- The entry the cursor returned
Cursor.Decision.EXITon, or null if it continued till the end.
-
containsKey
public boolean containsKey(java.lang.Object k)
- Specified by:
containsKeyin interfacejava.util.Map<K,V>- Overrides:
containsKeyin classjava.util.AbstractMap<K,V>
-
entrySet
public java.util.Set<java.util.Map.Entry<K,V>> entrySet()
- Specified by:
entrySetin interfacejava.util.Map<K,V>- Specified by:
entrySetin interfacejava.util.SortedMap<K,V>- Specified by:
entrySetin classjava.util.AbstractMap<K,V>
-
keySet
public java.util.Set<K> keySet()
- Specified by:
keySetin interfacejava.util.Map<K,V>- Specified by:
keySetin interfacejava.util.SortedMap<K,V>- Overrides:
keySetin classjava.util.AbstractMap<K,V>
-
values
public java.util.Collection<V> values()
- Specified by:
valuesin interfacejava.util.Map<K,V>- Specified by:
valuesin interfacejava.util.SortedMap<K,V>- Overrides:
valuesin classjava.util.AbstractMap<K,V>
-
remove
public V remove(java.lang.Object k)
- Specified by:
removein interfacejava.util.Map<K,V>- Overrides:
removein classjava.util.AbstractMap<K,V>- Throws:
java.lang.ClassCastException- if provided key is of an incompatible type
-
selectKey
public K selectKey(K key)
Description copied from interface:TrieReturns the key that is closest in a bitwise XOR metric to the provided key. This is NOT lexicographic closeness! For example, given the keys:- D = 1000100
- H = 1001000
- L = 1001100
Triecontained 'H' and 'L', a lookup of 'D' would return 'L', because the XOR distance between D & L is smaller than the XOR distance between D & H.
-
selectValue
public V selectValue(K key)
Description copied from interface:TrieReturns the value whose key is closest in a bitwise XOR metric to the provided key. This is NOT lexicographic closeness! For example, given the keys:- D = 1000100
- H = 1001000
- L = 1001100
Triecontained 'H' and 'L', a lookup of 'D' would return 'L', because the XOR distance between D & L is smaller than the XOR distance between D & H.- Specified by:
selectValuein interfaceTrie<K,V>- Returns:
- The value whose key is closest in a bitwise XOR metric to the provided key.
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.util.AbstractMap<K,V>
-
-