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:
Map<UnsignedInteger,E>,NavigableMap<UnsignedInteger,E>,SortedMap<UnsignedInteger,E>
- Direct Known Subclasses:
LinkedSplayMap
public class SplayMap<E> extends Object implements 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 protected static classSplayMap.AscendingSubMap<V>protected static classSplayMap.DescendingSubMap<V>static classSplayMap.ImmutableSplayMapEntry<E>protected static classSplayMap.NavigableSubMapKeySetprotected static classSplayMap.SplayedEntry<E>protected classSplayMap.SplayMapKeySet
-
Field Summary
Fields Modifier and Type Field Description protected static Comparator<UnsignedInteger>COMPARATORprotected intDEFAULT_ENTRY_POOL_SIZEprotected NavigableMap<UnsignedInteger,E>descendingMapViewprotected intentriesInExistenceprotected RingQueue<SplayMap.SplayedEntry<E>>entryPoolprotected Set<Map.Entry<UnsignedInteger,E>>entrySetprotected NavigableSet<UnsignedInteger>keySetprotected intmodCountprotected static Comparator<UnsignedInteger>REVERSE_COMPARATORprotected 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 Collection<E>values
-
Constructor Summary
Constructors Constructor Description SplayMap()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description SplayMap.ImmutableSplayMapEntry<E>ceilingEntry(UnsignedInteger key)UnsignedIntegerceilingKey(UnsignedInteger key)voidclear()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(Object key)booleancontainsValue(Object value)protected voiddelete(SplayMap.SplayedEntry<E> node)NavigableSet<UnsignedInteger>descendingKeySet()NavigableMap<UnsignedInteger,E>descendingMap()protected voidentryAdded(SplayMap.SplayedEntry<E> newEntry)protected voidentryDeleted(SplayMap.SplayedEntry<E> deletedEntry)Set<Map.Entry<UnsignedInteger,E>>entrySet()booleanequals(Object o)protected static <V> SplayMap.ImmutableSplayMapEntry<V>export(SplayMap.SplayedEntry<V> entry)SplayMap.ImmutableSplayMapEntry<E>firstEntry()UnsignedIntegerfirstKey()SplayMap.ImmutableSplayMapEntry<E>floorEntry(UnsignedInteger key)UnsignedIntegerfloorKey(UnsignedInteger key)voidforEach(BiConsumer<? super UnsignedInteger,? super E> action)voidforEach(Consumer<? super E> action)Eget(int key)Gets the value of the element stored in theMapwith the key (treated as an unsigned integer for comparison.Eget(Object key)EgetOrDefault(int key, E defaultValue)Gets the value of the element stored in theMapwith the key (treated as an unsigned integer for comparison.EgetOrDefault(Object key, E defaultValue)inthashCode()SortedMap<UnsignedInteger,E>headMap(UnsignedInteger toKey)NavigableMap<UnsignedInteger,E>headMap(UnsignedInteger toKey, boolean inclusive)SplayMap.ImmutableSplayMapEntry<E>higherEntry(UnsignedInteger key)UnsignedIntegerhigherKey(UnsignedInteger key)booleanisEmpty()Set<UnsignedInteger>keySet()SplayMap.ImmutableSplayMapEntry<E>lastEntry()UnsignedIntegerlastKey()SplayMap.ImmutableSplayMapEntry<E>lowerEntry(UnsignedInteger key)UnsignedIntegerlowerKey(UnsignedInteger key)NavigableSet<UnsignedInteger>navigableKeySet()SplayMap.ImmutableSplayMapEntry<E>pollFirstEntry()SplayMap.ImmutableSplayMapEntry<E>pollLastEntry()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(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, 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(Object key)booleanremove(Object key, 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(BiFunction<? super UnsignedInteger,? super E,? extends E> function)protected static Comparator<? super UnsignedInteger>reverseComparator()intsize()NavigableMap<UnsignedInteger,E>subMap(UnsignedInteger fromKey, boolean fromInclusive, UnsignedInteger toKey, boolean toInclusive)SortedMap<UnsignedInteger,E>subMap(UnsignedInteger fromKey, UnsignedInteger toKey)SortedMap<UnsignedInteger,E>tailMap(UnsignedInteger fromKey)NavigableMap<UnsignedInteger,E>tailMap(UnsignedInteger fromKey, boolean inclusive)Collection<E>values()-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, merge
-
-
-
-
Field Detail
-
COMPARATOR
protected static final Comparator<UnsignedInteger> COMPARATOR
-
REVERSE_COMPARATOR
protected static final Comparator<UnsignedInteger> REVERSE_COMPARATOR
-
DEFAULT_ENTRY_POOL_SIZE
protected final int DEFAULT_ENTRY_POOL_SIZE
- See Also:
- Constant Field Values
-
entryPool
protected final RingQueue<SplayMap.SplayedEntry<E>> entryPool
-
entriesInExistence
protected int entriesInExistence
-
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 NavigableSet<UnsignedInteger> keySet
-
values
protected Collection<E> values
-
entrySet
protected Set<Map.Entry<UnsignedInteger,E>> entrySet
-
descendingMapView
protected NavigableMap<UnsignedInteger,E> descendingMapView
-
-
Method Detail
-
size
public int size()
- Specified by:
sizein interfaceMap<UnsignedInteger,E>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmptyin interfaceMap<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.
-
getOrDefault
public E getOrDefault(int key, E defaultValue)
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.
-
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.
-
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 interfaceMap<UnsignedInteger,E>
-
putIfAbsent
public E putIfAbsent(UnsignedInteger key, E value)
- Specified by:
putIfAbsentin interfaceMap<UnsignedInteger,E>
-
getOrDefault
public E getOrDefault(Object key, E defaultValue)
- Specified by:
getOrDefaultin interfaceMap<UnsignedInteger,E>
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKeyin interfaceMap<UnsignedInteger,E>
-
clear
public void clear()
- Specified by:
clearin interfaceMap<UnsignedInteger,E>
-
putAll
public void putAll(Map<? extends UnsignedInteger,? extends E> source)
- Specified by:
putAllin interfaceMap<UnsignedInteger,E>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValuein interfaceMap<UnsignedInteger,E>
-
hashCode
public int hashCode()
-
equals
public boolean equals(Object o)
-
keySet
public Set<UnsignedInteger> keySet()
- Specified by:
keySetin interfaceMap<UnsignedInteger,E>- Specified by:
keySetin interfaceSortedMap<UnsignedInteger,E>
-
values
public Collection<E> values()
- Specified by:
valuesin interfaceMap<UnsignedInteger,E>- Specified by:
valuesin interfaceSortedMap<UnsignedInteger,E>
-
entrySet
public Set<Map.Entry<UnsignedInteger,E>> entrySet()
- Specified by:
entrySetin interfaceMap<UnsignedInteger,E>- Specified by:
entrySetin interfaceSortedMap<UnsignedInteger,E>
-
forEach
public void forEach(BiConsumer<? super UnsignedInteger,? super E> action)
- Specified by:
forEachin interfaceMap<UnsignedInteger,E>
-
forEach
public void forEach(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(BiFunction<? super UnsignedInteger,? super E,? extends E> function)
- Specified by:
replaceAllin interfaceMap<UnsignedInteger,E>
-
remove
public boolean remove(Object key, Object value)
- Specified by:
removein interfaceMap<UnsignedInteger,E>
-
remove
public boolean remove(int key, 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 interfaceMap<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 interfaceMap<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 static <V> SplayMap.ImmutableSplayMapEntry<V> export(SplayMap.SplayedEntry<V> entry)
-
reverseComparator
protected static Comparator<? super UnsignedInteger> reverseComparator()
-
comparator
public Comparator<? super UnsignedInteger> comparator()
- Specified by:
comparatorin interfaceSortedMap<UnsignedInteger,E>
-
firstKey
public UnsignedInteger firstKey()
- Specified by:
firstKeyin interfaceSortedMap<UnsignedInteger,E>
-
lastKey
public UnsignedInteger lastKey()
- Specified by:
lastKeyin interfaceSortedMap<UnsignedInteger,E>
-
firstEntry
public SplayMap.ImmutableSplayMapEntry<E> firstEntry()
- Specified by:
firstEntryin interfaceNavigableMap<UnsignedInteger,E>
-
lastEntry
public SplayMap.ImmutableSplayMapEntry<E> lastEntry()
- Specified by:
lastEntryin interfaceNavigableMap<UnsignedInteger,E>
-
pollFirstEntry
public SplayMap.ImmutableSplayMapEntry<E> pollFirstEntry()
- Specified by:
pollFirstEntryin interfaceNavigableMap<UnsignedInteger,E>
-
pollLastEntry
public SplayMap.ImmutableSplayMapEntry<E> pollLastEntry()
- Specified by:
pollLastEntryin interfaceNavigableMap<UnsignedInteger,E>
-
lowerEntry
public SplayMap.ImmutableSplayMapEntry<E> lowerEntry(UnsignedInteger key)
- Specified by:
lowerEntryin interfaceNavigableMap<UnsignedInteger,E>
-
lowerKey
public UnsignedInteger lowerKey(UnsignedInteger key)
- Specified by:
lowerKeyin interfaceNavigableMap<UnsignedInteger,E>
-
higherEntry
public SplayMap.ImmutableSplayMapEntry<E> higherEntry(UnsignedInteger key)
- Specified by:
higherEntryin interfaceNavigableMap<UnsignedInteger,E>
-
higherKey
public UnsignedInteger higherKey(UnsignedInteger key)
- Specified by:
higherKeyin interfaceNavigableMap<UnsignedInteger,E>
-
floorEntry
public SplayMap.ImmutableSplayMapEntry<E> floorEntry(UnsignedInteger key)
- Specified by:
floorEntryin interfaceNavigableMap<UnsignedInteger,E>
-
floorKey
public UnsignedInteger floorKey(UnsignedInteger key)
- Specified by:
floorKeyin interfaceNavigableMap<UnsignedInteger,E>
-
ceilingEntry
public SplayMap.ImmutableSplayMapEntry<E> ceilingEntry(UnsignedInteger key)
- Specified by:
ceilingEntryin interfaceNavigableMap<UnsignedInteger,E>
-
ceilingKey
public UnsignedInteger ceilingKey(UnsignedInteger key)
- Specified by:
ceilingKeyin interfaceNavigableMap<UnsignedInteger,E>
-
descendingMap
public NavigableMap<UnsignedInteger,E> descendingMap()
- Specified by:
descendingMapin interfaceNavigableMap<UnsignedInteger,E>
-
navigableKeySet
public NavigableSet<UnsignedInteger> navigableKeySet()
- Specified by:
navigableKeySetin interfaceNavigableMap<UnsignedInteger,E>
-
descendingKeySet
public NavigableSet<UnsignedInteger> descendingKeySet()
- Specified by:
descendingKeySetin interfaceNavigableMap<UnsignedInteger,E>
-
subMap
public NavigableMap<UnsignedInteger,E> subMap(UnsignedInteger fromKey, boolean fromInclusive, UnsignedInteger toKey, boolean toInclusive)
- Specified by:
subMapin interfaceNavigableMap<UnsignedInteger,E>
-
headMap
public NavigableMap<UnsignedInteger,E> headMap(UnsignedInteger toKey, boolean inclusive)
- Specified by:
headMapin interfaceNavigableMap<UnsignedInteger,E>
-
tailMap
public NavigableMap<UnsignedInteger,E> tailMap(UnsignedInteger fromKey, boolean inclusive)
- Specified by:
tailMapin interfaceNavigableMap<UnsignedInteger,E>
-
subMap
public SortedMap<UnsignedInteger,E> subMap(UnsignedInteger fromKey, UnsignedInteger toKey)
- Specified by:
subMapin interfaceNavigableMap<UnsignedInteger,E>- Specified by:
subMapin interfaceSortedMap<UnsignedInteger,E>
-
headMap
public SortedMap<UnsignedInteger,E> headMap(UnsignedInteger toKey)
- Specified by:
headMapin interfaceNavigableMap<UnsignedInteger,E>- Specified by:
headMapin interfaceSortedMap<UnsignedInteger,E>
-
tailMap
public SortedMap<UnsignedInteger,E> tailMap(UnsignedInteger fromKey)
- Specified by:
tailMapin interfaceNavigableMap<UnsignedInteger,E>- Specified by:
tailMapin interfaceSortedMap<UnsignedInteger,E>
-
-