Class TernarySearchTree<V>
- java.lang.Object
-
- net.sf.eBus.util.TernarySearchTree<V>
-
- Type Parameters:
V- Value type.
- All Implemented Interfaces:
Serializable,Map<String,V>
public final class TernarySearchTree<V> extends Object implements Map<String,V>, Serializable
Ternary search tree (TST)-based implementation of theMapinterface. This implementation provides all optional interface methods, allowsnullvalues but restricts keys to non-null java.lang.Stringobjects.It must be pointed out that this implementation of
entrySet(),keySet()andvalues()does not return sets backed by the map. Changes to the map are not reflected in the returned set nor are changes to the returned set reflected in the map.Note: this implementation is not synchronized. If multiple threads currently access this map and at least one thread modifies the map by adding or removing an entry, then this map must be externally synchronized. External synchronization is accomplished in two ways:
-
Placing the map inside a
synchronizedblock:synchronized (tstMap) { tstMap.put("abcd", obj); } -
Constructing a
TernarySearchTreewithin ajava.util.Collections.synchronizedMap():Map m = Collections.synchronizedMap( new TernarySearchTree(...));
For more information on ternary search trees, see Bentley, J., and Sedgewick, R. Fast algorithms for sorting and searching strings. In Eighth Annual ACM-SIAM Symposium on Discrete Algorithms (1997), SIAM Press.
- Author:
- Charles Rapp
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description TernarySearchTree()Constructs an empty ternary search tree map.TernarySearchTree(Map<String,? extends V> m)Construct a newTernarySearchTreewith the same mappings as the specifiedMap.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears out all stored values, leaving an empty map.booleancontainsKey(Object key)Returnstrueif the key is in the ternary search tree andfalseotherwise.booleancontainsValue(Object value)Returnstrueifvalueis stored in the ternary search tree andfalseotherwise.Set<Map.Entry<String,V>>entrySet()Returns the set of all key-value mappings.Set<Map.Entry<String,V>>entrySet(Pattern query)Returns the set of all key-value mappings whose keys match the given query.Set<Map.Entry<String,V>>entrySet(Pattern query, int maxMatches)Returns the set of at mostmaxMatcheskey-value mappings whose keys match the given query.Vget(Object key)Returns the value associated withkey.booleanisEmpty()Returnstrueif this map contains no key-value mappings andfalseotherwise.Set<String>keySet()Returns all keys currently stored in the tree.Set<String>keySet(Pattern query)Returns the words matching the query.Set<String>keySet(Pattern query, int maxMatches)Returns at mostmaxMatcheswords matching the query.Collection<String>nearSearch(String s, int distance)Returns the keys which are within a specified Hamming distance of character sequences.longnodeCount()Returns the number of nodes used in this map.Vput(String key, V value)Enters a value into the ternary search tree using the text key.voidputAll(Map<? extends String,? extends V> map)Copies all the mappings from the specified map to this tree.Vremove(Object key)Removes the key-value mapping from the tree and returns the now removed value.intsize()Returns the number of key-value mappings in this tree.Collection<V>values()Returns a collection of all the trees values.Collection<V>values(Pattern query)Returns a non-nullcollection of all trees values with keys matching the given pattern.Collection<V>values(Pattern query, int maxMatches)Returns a collection of at mostmaxMatchesvalues whose keys match the given pattern.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Method Detail
-
isEmpty
public boolean isEmpty()
Returnstrueif this map contains no key-value mappings andfalseotherwise.
-
size
public int size()
Returns the number of key-value mappings in this tree.
-
nodeCount
public long nodeCount()
Returns the number of nodes used in this map.- Returns:
- the number of nodes used in this map.
-
clear
public void clear()
Clears out all stored values, leaving an empty map.
-
containsKey
public boolean containsKey(Object key)
Returnstrueif the key is in the ternary search tree andfalseotherwise.- Specified by:
containsKeyin interfaceMap<String,V>- Parameters:
key- Search for this key in the tree.- Returns:
trueif the key is in the ternary search tree andfalseotherwise.
-
containsValue
public boolean containsValue(Object value)
Returnstrueifvalueis stored in the ternary search tree andfalseotherwise.- Specified by:
containsValuein interfaceMap<String,V>- Parameters:
value- Searches for this value.- Returns:
trueifvalueis stored in the ternary search tree andfalseotherwise.
-
get
public V get(Object key)
Returns the value associated withkey. If the ternary search tree does not contain the key, then returnsnull.
-
put
public V put(String key, V value)
Enters a value into the ternary search tree using the text key. If the key is already in the tree, then replaces the existing value with the new value and returns the replaced value. If the key is not in the tree, thennullis returned.
-
putAll
public void putAll(Map<? extends String,? extends V> map)
Copies all the mappings from the specified map to this tree. This method does nothing more than iterate over the specified map, callingput(String, Object)successively.
-
remove
public V remove(Object key)
Removes the key-value mapping from the tree and returns the now removed value.
-
keySet
public Set<String> keySet()
Returns all keys currently stored in the tree. If the tree is empty, then an empty set is returned. This set is not backed by the tree. Changes to the returned set are not reflected in the tree and changes to the tree are not reflected in the set.
-
keySet
public Set<String> keySet(Pattern query)
Returns the words matching the query. This set is not backed by the tree. Changes to the returned set are not reflected in the tree and changes to the tree are not reflected in the set.- Parameters:
query- Match against this query.- Returns:
- the words matching the query.
-
keySet
public Set<String> keySet(Pattern query, int maxMatches)
Returns at mostmaxMatcheswords matching the query. This set is not backed by the tree. Changes to the returned set are not reflected in the tree and changes to the tree are not reflected in the set.- Parameters:
query- Match against this query.maxMatches- Match at most this many keys.- Returns:
- the words matching the query.
- Throws:
IllegalArgumentException- ifmaxMatchesis <= zero.IllegalStateException- ifmaxMatchesis exceeded.
-
values
public Collection<V> values()
Returns a collection of all the trees values. This collection is not backed by the tree. Changes to the returned collection are not reflected in the tree and changes to the tree are not reflected in the collection.
-
values
public Collection<V> values(Pattern query)
Returns a non-nullcollection of all trees values with keys matching the given pattern. This collection is not backed by the tree. Changes to the returned collection are not reflected in the tree and changes to the tree are not reflected in the collection.- Parameters:
query- match against this query.- Returns:
- a collection of all the trees values matching
query.
-
values
public Collection<V> values(Pattern query, int maxMatches)
Returns a collection of at mostmaxMatchesvalues whose keys match the given pattern. This collection is not backed by the tree. Changes to the returned collection are not reflected in the tree and changes to the tree are not reflected in the collection.- Parameters:
query- Match against this query.maxMatches- Match at most this many keys.- Returns:
- a collection of matching keys' values.
- Throws:
IllegalArgumentException- ifmaxMatchesis <= zero.IllegalStateException- ifmaxMatchesis exceeded.
-
entrySet
public Set<Map.Entry<String,V>> entrySet()
Returns the set of all key-value mappings. If this tree is empty, then an empty set is returned. The returned set is not backed by the tree. Changes to the returned set or to this tree are not reflected in the other.
-
entrySet
public Set<Map.Entry<String,V>> entrySet(Pattern query)
Returns the set of all key-value mappings whose keys match the given query. If this tree is empty, then an empty set is returned. The returned set is not backed by the tree. Changes to the returned set or to this tree are not reflected in the other.- Parameters:
query- Match against this query.- Returns:
- the set of all key-value mappings.
-
entrySet
public Set<Map.Entry<String,V>> entrySet(Pattern query, int maxMatches)
Returns the set of at mostmaxMatcheskey-value mappings whose keys match the given query. If this tree is empty, then an empty set is returned. The returned set is not backed by the tree. Changes to the returned set or to this tree are not reflected in the other.- Parameters:
query- Match against this query.maxMatches- Match at most this many keys.- Returns:
- the set of at most
maxMatcheskey-value mappings. Throws IllegalArgumentException ifmaxMatchesis <= zero.
-
nearSearch
public Collection<String> nearSearch(String s, int distance)
Returns the keys which are within a specified Hamming distance of character sequences. The Hamming distance between two strings of equal length is the number of positions at which corresponding characters are different. One string may be transformed into the other by changing the characters at these positions to the other strings values. The Hamming distance may be thought of as the number of errors in one string.If this ternary search tree contains a dictionary, then this method may be used to find possible correct spellings for a misspelled word.
- Parameters:
s- find the keys within the specified Hamming distance to this character sequence.distance- the desired Hamming distance.- Returns:
- the keys which are within a specified Hamming
distance of character sequence
s. If no such keys are found, then returns an empty collection.
-
-