Class SplayMap<E>
- java.lang.Object
-
- org.apache.qpid.protonj2.engine.util.SplayMap<E>
-
- Type Parameters:
E- The type stored in the map entries
- All Implemented Interfaces:
java.util.Map<UnsignedInteger,E>,java.util.NavigableMap<UnsignedInteger,E>,java.util.SortedMap<UnsignedInteger,E>
- Direct Known Subclasses:
LinkedSplayMap
public class SplayMap<E> extends java.lang.Object implements java.util.NavigableMap<UnsignedInteger,E>
Map class that is implemented using a Splay Tree and uses primitive integers as the keys for the specified value type. The splay tree is a specialized form of a binary search tree that is self balancing and provides faster access in general to frequently used items. The splay tree serves well as an LRU cache of sorts where 80 percent of the accessed elements comes from 20 percent of the overall load in theMap. The best case access time is generally O(long n) however it can be Theta(n) in a very worst case scenario.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description classSplayMap.ImmutableSplayMapEntryAn immutableMapentry that can be used when exposing raw entry mappings via theMapAPI.protected static classSplayMap.SplayedEntry<E>
-
Field Summary
Fields Modifier and Type Field Description protected RingQueue<SplayMap.SplayedEntry<E>>entryPoolprotected java.util.Set<java.util.Map.Entry<UnsignedInteger,E>>entrySetprotected java.util.Set<UnsignedInteger>keySetprotected intmodCountprotected SplayMap.SplayedEntry<E>rootRoot node which can be null if the tree has no elements (size == 0)protected intsizeCurrent size of the splayed map tree.protected java.util.Collection<E>values
-
Constructor Summary
Constructors Constructor Description SplayMap()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description SplayMap.ImmutableSplayMapEntryceilingEntry(UnsignedInteger key)UnsignedIntegerceilingKey(UnsignedInteger key)voidclear()java.util.Comparator<? super UnsignedInteger>comparator()booleancontainsKey(int key)Searches the map using the given primitive integer key value which will be treated internally as an unsigned value when comparing against keys in the mapping.booleancontainsKey(java.lang.Object key)booleancontainsValue(java.lang.Object value)protected voiddelete(SplayMap.SplayedEntry<E> node)java.util.NavigableSet<UnsignedInteger>descendingKeySet()java.util.NavigableMap<UnsignedInteger,E>descendingMap()protected voidentryAdded(SplayMap.SplayedEntry<E> newEntry)protected voidentryDeleted(SplayMap.SplayedEntry<E> deletedEntry)java.util.Set<java.util.Map.Entry<UnsignedInteger,E>>entrySet()protected SplayMap.ImmutableSplayMapEntryexport(SplayMap.SplayedEntry<E> entry)SplayMap.ImmutableSplayMapEntryfirstEntry()UnsignedIntegerfirstKey()SplayMap.ImmutableSplayMapEntryfloorEntry(UnsignedInteger key)UnsignedIntegerfloorKey(UnsignedInteger key)voidforEach(java.util.function.BiConsumer<? super UnsignedInteger,? super E> action)voidforEach(java.util.function.Consumer<? super E> action)A specialized forEach implementation that accepts aConsumerfunction that will be called for each value in theSplayMap.Eget(int key)Gets the value of the element stored in theMapwith the key (treated as an unsigned integer for comparison.Eget(java.lang.Object key)java.util.SortedMap<UnsignedInteger,E>headMap(UnsignedInteger toKey)java.util.NavigableMap<UnsignedInteger,E>headMap(UnsignedInteger toKey, boolean inclusive)SplayMap.ImmutableSplayMapEntryhigherEntry(UnsignedInteger key)UnsignedIntegerhigherKey(UnsignedInteger key)booleanisEmpty()java.util.Set<UnsignedInteger>keySet()SplayMap.ImmutableSplayMapEntrylastEntry()UnsignedIntegerlastKey()SplayMap.ImmutableSplayMapEntrylowerEntry(UnsignedInteger key)UnsignedIntegerlowerKey(UnsignedInteger key)java.util.NavigableSet<UnsignedInteger>navigableKeySet()SplayMap.ImmutableSplayMapEntrypollFirstEntry()SplayMap.ImmutableSplayMapEntrypollLastEntry()Eput(int key, E value)Puts the value into the in theMapat the entry specified by the given key (treated as an unsigned integer for comparison.Eput(UnsignedInteger key, E value)voidputAll(java.util.Map<? extends UnsignedInteger,? extends E> source)EputIfAbsent(int key, E value)If the specified key is not already associated with a value associates it with the given value and returns null, otherwise returns the current value.EputIfAbsent(UnsignedInteger key, E value)Eremove(int key)Removes the mapping for the primitiveintkey from this map if it is present and returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.booleanremove(int key, java.lang.Object value)Removes the entry for the specified primitive int (treated as unsigned) key only if it is currently mapped to the specified value in theMap.Eremove(java.lang.Object key)booleanremove(java.lang.Object key, java.lang.Object value)Eremove(UnsignedInteger key)Removes the mapping for theUnsignedIntegerkey from this map if it is present and returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.Ereplace(int key, E value)Replaces the entry for the specified primitive int (treated as unsigned) key only if it is currently mapped to the a value in theMapwith the new value provided.booleanreplace(int key, E oldValue, E newValue)Replaces the entry for the specified primitive int (treated as unsigned) key only if it is currently mapped to the specified value in theMapwith the new value provided.Ereplace(UnsignedInteger key, E value)booleanreplace(UnsignedInteger key, E oldValue, E newValue)voidreplaceAll(java.util.function.BiFunction<? super UnsignedInteger,? super E,? extends E> function)intsize()java.util.NavigableMap<UnsignedInteger,E>subMap(UnsignedInteger fromKey, boolean fromInclusive, UnsignedInteger toKey, boolean toInclusive)java.util.SortedMap<UnsignedInteger,E>subMap(UnsignedInteger fromKey, UnsignedInteger toKey)java.util.SortedMap<UnsignedInteger,E>tailMap(UnsignedInteger fromKey)java.util.NavigableMap<UnsignedInteger,E>tailMap(UnsignedInteger fromKey, boolean inclusive)java.util.Collection<E>values()
-
-
-
Field Detail
-
entryPool
protected final RingQueue<SplayMap.SplayedEntry<E>> entryPool
-
root
protected SplayMap.SplayedEntry<E> root
Root node which can be null if the tree has no elements (size == 0)
-
size
protected int size
Current size of the splayed map tree.
-
modCount
protected int modCount
-
keySet
protected java.util.Set<UnsignedInteger> keySet
-
values
protected java.util.Collection<E> values
-
entrySet
protected java.util.Set<java.util.Map.Entry<UnsignedInteger,E>> entrySet
-
-
Method Detail
-
size
public int size()
- Specified by:
sizein interfacejava.util.Map<UnsignedInteger,E>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfacejava.util.Map<UnsignedInteger,E>
-
get
public E get(int key)
Gets the value of the element stored in theMapwith the key (treated as an unsigned integer for comparison. As a side effect of calling this method the tree that comprises the Map can be modified to bring up the found key or the last accessed key if the key given is not in theMap. For entries at the root of the tree that match the given search key the method returns immediately without modifying theMap.- Parameters:
key- the integer key value to search for in theSplayMap.- Returns:
- the value stored for the given key if found or null if not in the
Map.
-
put
public E put(int key, E value)
Puts the value into the in theMapat the entry specified by the given key (treated as an unsigned integer for comparison. As a side effect of calling this method the tree that comprises the Map can be modified to bring up the found key or the last accessed key if the key given is not in theMap. For entries at the root of the tree that match the given search key the method returns immediately without modifying theMap.- Parameters:
key- the integer key value to search for and or insert in theSplayMap.value- the value to assign to the entry accessed via the given key.- Returns:
- the previous value stored for the given key if found or null if not in the
Map.
-
putIfAbsent
public E putIfAbsent(UnsignedInteger key, E value)
- Specified by:
putIfAbsentin interfacejava.util.Map<UnsignedInteger,E>
-
putIfAbsent
public E putIfAbsent(int key, E value)
If the specified key is not already associated with a value associates it with the given value and returns null, otherwise returns the current value. As a side effect of calling this method the tree that comprises the Map can be modified to bring up the found key or the last accessed key if the key given is not in theMap. For entries at the root of the tree that match the given search key the method returns immediately without modifying theMap.- Parameters:
key- the integer key value to search for and or insert in theSplayMap.value- the value to assign to the entry accessed via the given key.- Returns:
- the previous value associated with the given key or null if none was present.
-
remove
public E remove(UnsignedInteger key)
Removes the mapping for theUnsignedIntegerkey from this map if it is present and returns the value to which this map previously associated the key, or null if the map contained no mapping for the key.- Parameters:
key- TheUnsignedIntegerkey whose value will be removed from theSplayMap.- Returns:
- the value that was removed if one was present in the
Map.
-
remove
public E remove(int key)
Removes the mapping for the primitiveintkey from this map if it is present and returns the value to which this map previously associated the key, or null if the map contained no mapping for the key. The integer value is treated as an unsigned int internally.- Parameters:
key- TheUnsignedIntegerkey whose value will be removed from theSplayMap.- Returns:
- the value that was removed if one was present in the
Map.
-
containsKey
public boolean containsKey(int key)
Searches the map using the given primitive integer key value which will be treated internally as an unsigned value when comparing against keys in the mapping.- Parameters:
key- The key which will be searched for in this mapping.- Returns:
trueif the key mapping is found within thisMap.
-
put
public E put(UnsignedInteger key, E value)
- Specified by:
putin interfacejava.util.Map<UnsignedInteger,E>
-
get
public E get(java.lang.Object key)
- Specified by:
getin interfacejava.util.Map<UnsignedInteger,E>
-
remove
public E remove(java.lang.Object key)
- Specified by:
removein interfacejava.util.Map<UnsignedInteger,E>
-
containsKey
public boolean containsKey(java.lang.Object key)
- Specified by:
containsKeyin interfacejava.util.Map<UnsignedInteger,E>
-
clear
public void clear()
- Specified by:
clearin interfacejava.util.Map<UnsignedInteger,E>
-
putAll
public void putAll(java.util.Map<? extends UnsignedInteger,? extends E> source)
- Specified by:
putAllin interfacejava.util.Map<UnsignedInteger,E>
-
containsValue
public boolean containsValue(java.lang.Object value)
- Specified by:
containsValuein interfacejava.util.Map<UnsignedInteger,E>
-
keySet
public java.util.Set<UnsignedInteger> keySet()
- Specified by:
keySetin interfacejava.util.Map<UnsignedInteger,E>- Specified by:
keySetin interfacejava.util.SortedMap<UnsignedInteger,E>
-
values
public java.util.Collection<E> values()
- Specified by:
valuesin interfacejava.util.Map<UnsignedInteger,E>- Specified by:
valuesin interfacejava.util.SortedMap<UnsignedInteger,E>
-
entrySet
public java.util.Set<java.util.Map.Entry<UnsignedInteger,E>> entrySet()
- Specified by:
entrySetin interfacejava.util.Map<UnsignedInteger,E>- Specified by:
entrySetin interfacejava.util.SortedMap<UnsignedInteger,E>
-
forEach
public void forEach(java.util.function.BiConsumer<? super UnsignedInteger,? super E> action)
- Specified by:
forEachin interfacejava.util.Map<UnsignedInteger,E>
-
forEach
public void forEach(java.util.function.Consumer<? super E> action)
A specialized forEach implementation that accepts aConsumerfunction that will be called for each value in theSplayMap. This method can save overhead as it does not need to box the primitive key values into an object for the call to the provided function. Unless otherwise specified by the implementing class, actions are performed in the order of entry set iteration (if an iteration order is specified.)- Parameters:
action- The action to be performed for each of the values in theSplayMap.
-
replaceAll
public void replaceAll(java.util.function.BiFunction<? super UnsignedInteger,? super E,? extends E> function)
- Specified by:
replaceAllin interfacejava.util.Map<UnsignedInteger,E>
-
remove
public boolean remove(java.lang.Object key, java.lang.Object value)- Specified by:
removein interfacejava.util.Map<UnsignedInteger,E>
-
remove
public boolean remove(int key, java.lang.Object value)Removes the entry for the specified primitive int (treated as unsigned) key only if it is currently mapped to the specified value in theMap.- Parameters:
key- The key whose value will be removed if matched.value- The value that must be contained in the mapping for the remove to be performed.- Returns:
trueif an entry was removed from theMap
-
replace
public boolean replace(UnsignedInteger key, E oldValue, E newValue)
- Specified by:
replacein interfacejava.util.Map<UnsignedInteger,E>
-
replace
public boolean replace(int key, E oldValue, E newValue)Replaces the entry for the specified primitive int (treated as unsigned) key only if it is currently mapped to the specified value in theMapwith the new value provided.- Parameters:
key- The key whose value will be removed if matched.oldValue- The old value that must be contained in the mapping for the replace to be performed.newValue- The value that will replace the old value mapped to the given key if one existed..- Returns:
trueif an entry was replaced in theMap
-
replace
public E replace(UnsignedInteger key, E value)
- Specified by:
replacein interfacejava.util.Map<UnsignedInteger,E>
-
replace
public E replace(int key, E value)
Replaces the entry for the specified primitive int (treated as unsigned) key only if it is currently mapped to the a value in theMapwith the new value provided.- Parameters:
key- The key whose value will be removed if matched.value- The value that will replace the old value mapped to the given key if one existed..- Returns:
trueif an entry was replaced in theMap
-
entryAdded
protected void entryAdded(SplayMap.SplayedEntry<E> newEntry)
-
entryDeleted
protected void entryDeleted(SplayMap.SplayedEntry<E> deletedEntry)
-
delete
protected void delete(SplayMap.SplayedEntry<E> node)
-
export
protected SplayMap.ImmutableSplayMapEntry export(SplayMap.SplayedEntry<E> entry)
-
comparator
public java.util.Comparator<? super UnsignedInteger> comparator()
- Specified by:
comparatorin interfacejava.util.SortedMap<UnsignedInteger,E>
-
firstKey
public UnsignedInteger firstKey()
- Specified by:
firstKeyin interfacejava.util.SortedMap<UnsignedInteger,E>
-
lastKey
public UnsignedInteger lastKey()
- Specified by:
lastKeyin interfacejava.util.SortedMap<UnsignedInteger,E>
-
firstEntry
public SplayMap.ImmutableSplayMapEntry firstEntry()
- Specified by:
firstEntryin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
lastEntry
public SplayMap.ImmutableSplayMapEntry lastEntry()
- Specified by:
lastEntryin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
pollFirstEntry
public SplayMap.ImmutableSplayMapEntry pollFirstEntry()
- Specified by:
pollFirstEntryin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
pollLastEntry
public SplayMap.ImmutableSplayMapEntry pollLastEntry()
- Specified by:
pollLastEntryin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
lowerEntry
public SplayMap.ImmutableSplayMapEntry lowerEntry(UnsignedInteger key)
- Specified by:
lowerEntryin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
lowerKey
public UnsignedInteger lowerKey(UnsignedInteger key)
- Specified by:
lowerKeyin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
higherEntry
public SplayMap.ImmutableSplayMapEntry higherEntry(UnsignedInteger key)
- Specified by:
higherEntryin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
higherKey
public UnsignedInteger higherKey(UnsignedInteger key)
- Specified by:
higherKeyin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
floorEntry
public SplayMap.ImmutableSplayMapEntry floorEntry(UnsignedInteger key)
- Specified by:
floorEntryin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
floorKey
public UnsignedInteger floorKey(UnsignedInteger key)
- Specified by:
floorKeyin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
ceilingEntry
public SplayMap.ImmutableSplayMapEntry ceilingEntry(UnsignedInteger key)
- Specified by:
ceilingEntryin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
ceilingKey
public UnsignedInteger ceilingKey(UnsignedInteger key)
- Specified by:
ceilingKeyin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
descendingMap
public java.util.NavigableMap<UnsignedInteger,E> descendingMap()
- Specified by:
descendingMapin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
navigableKeySet
public java.util.NavigableSet<UnsignedInteger> navigableKeySet()
- Specified by:
navigableKeySetin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
descendingKeySet
public java.util.NavigableSet<UnsignedInteger> descendingKeySet()
- Specified by:
descendingKeySetin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
subMap
public java.util.NavigableMap<UnsignedInteger,E> subMap(UnsignedInteger fromKey, boolean fromInclusive, UnsignedInteger toKey, boolean toInclusive)
- Specified by:
subMapin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
headMap
public java.util.NavigableMap<UnsignedInteger,E> headMap(UnsignedInteger toKey, boolean inclusive)
- Specified by:
headMapin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
tailMap
public java.util.NavigableMap<UnsignedInteger,E> tailMap(UnsignedInteger fromKey, boolean inclusive)
- Specified by:
tailMapin interfacejava.util.NavigableMap<UnsignedInteger,E>
-
subMap
public java.util.SortedMap<UnsignedInteger,E> subMap(UnsignedInteger fromKey, UnsignedInteger toKey)
- Specified by:
subMapin interfacejava.util.NavigableMap<UnsignedInteger,E>- Specified by:
subMapin interfacejava.util.SortedMap<UnsignedInteger,E>
-
headMap
public java.util.SortedMap<UnsignedInteger,E> headMap(UnsignedInteger toKey)
- Specified by:
headMapin interfacejava.util.NavigableMap<UnsignedInteger,E>- Specified by:
headMapin interfacejava.util.SortedMap<UnsignedInteger,E>
-
tailMap
public java.util.SortedMap<UnsignedInteger,E> tailMap(UnsignedInteger fromKey)
- Specified by:
tailMapin interfacejava.util.NavigableMap<UnsignedInteger,E>- Specified by:
tailMapin interfacejava.util.SortedMap<UnsignedInteger,E>
-
-