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 class
SplayMap.ImmutableSplayMapEntry
An immutableMap
entry that can be used when exposing raw entry mappings via theMap
API.protected static class
SplayMap.SplayedEntry<E>
-
Field Summary
Fields Modifier and Type Field Description protected RingQueue<SplayMap.SplayedEntry<E>>
entryPool
protected java.util.Set<java.util.Map.Entry<UnsignedInteger,E>>
entrySet
protected java.util.Set<UnsignedInteger>
keySet
protected int
modCount
protected SplayMap.SplayedEntry<E>
root
Root node which can be null if the tree has no elements (size == 0)protected int
size
Current 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.ImmutableSplayMapEntry
ceilingEntry(UnsignedInteger key)
UnsignedInteger
ceilingKey(UnsignedInteger key)
void
clear()
java.util.Comparator<? super UnsignedInteger>
comparator()
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.boolean
containsKey(java.lang.Object key)
boolean
containsValue(java.lang.Object value)
protected void
delete(SplayMap.SplayedEntry<E> node)
java.util.NavigableSet<UnsignedInteger>
descendingKeySet()
java.util.NavigableMap<UnsignedInteger,E>
descendingMap()
protected void
entryAdded(SplayMap.SplayedEntry<E> newEntry)
protected void
entryDeleted(SplayMap.SplayedEntry<E> deletedEntry)
java.util.Set<java.util.Map.Entry<UnsignedInteger,E>>
entrySet()
protected SplayMap.ImmutableSplayMapEntry
export(SplayMap.SplayedEntry<E> entry)
SplayMap.ImmutableSplayMapEntry
firstEntry()
UnsignedInteger
firstKey()
SplayMap.ImmutableSplayMapEntry
floorEntry(UnsignedInteger key)
UnsignedInteger
floorKey(UnsignedInteger key)
void
forEach(java.util.function.BiConsumer<? super UnsignedInteger,? super E> action)
void
forEach(java.util.function.Consumer<? super E> action)
A specialized forEach implementation that accepts aConsumer
function that will be called for each value in theSplayMap
.E
get(int key)
Gets the value of the element stored in theMap
with the key (treated as an unsigned integer for comparison.E
get(java.lang.Object key)
java.util.SortedMap<UnsignedInteger,E>
headMap(UnsignedInteger toKey)
java.util.NavigableMap<UnsignedInteger,E>
headMap(UnsignedInteger toKey, boolean inclusive)
SplayMap.ImmutableSplayMapEntry
higherEntry(UnsignedInteger key)
UnsignedInteger
higherKey(UnsignedInteger key)
boolean
isEmpty()
java.util.Set<UnsignedInteger>
keySet()
SplayMap.ImmutableSplayMapEntry
lastEntry()
UnsignedInteger
lastKey()
SplayMap.ImmutableSplayMapEntry
lowerEntry(UnsignedInteger key)
UnsignedInteger
lowerKey(UnsignedInteger key)
java.util.NavigableSet<UnsignedInteger>
navigableKeySet()
SplayMap.ImmutableSplayMapEntry
pollFirstEntry()
SplayMap.ImmutableSplayMapEntry
pollLastEntry()
E
put(int key, E value)
Puts the value into the in theMap
at the entry specified by the given key (treated as an unsigned integer for comparison.E
put(UnsignedInteger key, E value)
void
putAll(java.util.Map<? extends UnsignedInteger,? extends E> source)
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.E
putIfAbsent(UnsignedInteger key, E value)
E
remove(int key)
Removes the mapping for the primitiveint
key 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.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
.E
remove(java.lang.Object key)
boolean
remove(java.lang.Object key, java.lang.Object value)
E
remove(UnsignedInteger key)
Removes the mapping for theUnsignedInteger
key 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.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 theMap
with the new value provided.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 theMap
with the new value provided.E
replace(UnsignedInteger key, E value)
boolean
replace(UnsignedInteger key, E oldValue, E newValue)
void
replaceAll(java.util.function.BiFunction<? super UnsignedInteger,? super E,? extends E> function)
int
size()
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:
size
in interfacejava.util.Map<UnsignedInteger,E>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfacejava.util.Map<UnsignedInteger,E>
-
get
public E get(int key)
Gets the value of the element stored in theMap
with 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 theMap
at 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:
putIfAbsent
in 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 theUnsignedInteger
key 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
- TheUnsignedInteger
key 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 primitiveint
key 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
- TheUnsignedInteger
key 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:
true
if the key mapping is found within thisMap
.
-
put
public E put(UnsignedInteger key, E value)
- Specified by:
put
in interfacejava.util.Map<UnsignedInteger,E>
-
get
public E get(java.lang.Object key)
- Specified by:
get
in interfacejava.util.Map<UnsignedInteger,E>
-
remove
public E remove(java.lang.Object key)
- Specified by:
remove
in interfacejava.util.Map<UnsignedInteger,E>
-
containsKey
public boolean containsKey(java.lang.Object key)
- Specified by:
containsKey
in interfacejava.util.Map<UnsignedInteger,E>
-
clear
public void clear()
- Specified by:
clear
in interfacejava.util.Map<UnsignedInteger,E>
-
putAll
public void putAll(java.util.Map<? extends UnsignedInteger,? extends E> source)
- Specified by:
putAll
in interfacejava.util.Map<UnsignedInteger,E>
-
containsValue
public boolean containsValue(java.lang.Object value)
- Specified by:
containsValue
in interfacejava.util.Map<UnsignedInteger,E>
-
keySet
public java.util.Set<UnsignedInteger> keySet()
- Specified by:
keySet
in interfacejava.util.Map<UnsignedInteger,E>
- Specified by:
keySet
in interfacejava.util.SortedMap<UnsignedInteger,E>
-
values
public java.util.Collection<E> values()
- Specified by:
values
in interfacejava.util.Map<UnsignedInteger,E>
- Specified by:
values
in interfacejava.util.SortedMap<UnsignedInteger,E>
-
entrySet
public java.util.Set<java.util.Map.Entry<UnsignedInteger,E>> entrySet()
- Specified by:
entrySet
in interfacejava.util.Map<UnsignedInteger,E>
- Specified by:
entrySet
in interfacejava.util.SortedMap<UnsignedInteger,E>
-
forEach
public void forEach(java.util.function.BiConsumer<? super UnsignedInteger,? super E> action)
- Specified by:
forEach
in interfacejava.util.Map<UnsignedInteger,E>
-
forEach
public void forEach(java.util.function.Consumer<? super E> action)
A specialized forEach implementation that accepts aConsumer
function 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:
replaceAll
in interfacejava.util.Map<UnsignedInteger,E>
-
remove
public boolean remove(java.lang.Object key, java.lang.Object value)
- Specified by:
remove
in 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:
true
if an entry was removed from theMap
-
replace
public boolean replace(UnsignedInteger key, E oldValue, E newValue)
- Specified by:
replace
in 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 theMap
with 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:
true
if an entry was replaced in theMap
-
replace
public E replace(UnsignedInteger key, E value)
- Specified by:
replace
in 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 theMap
with 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:
true
if 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:
comparator
in interfacejava.util.SortedMap<UnsignedInteger,E>
-
firstKey
public UnsignedInteger firstKey()
- Specified by:
firstKey
in interfacejava.util.SortedMap<UnsignedInteger,E>
-
lastKey
public UnsignedInteger lastKey()
- Specified by:
lastKey
in interfacejava.util.SortedMap<UnsignedInteger,E>
-
firstEntry
public SplayMap.ImmutableSplayMapEntry firstEntry()
- Specified by:
firstEntry
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
lastEntry
public SplayMap.ImmutableSplayMapEntry lastEntry()
- Specified by:
lastEntry
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
pollFirstEntry
public SplayMap.ImmutableSplayMapEntry pollFirstEntry()
- Specified by:
pollFirstEntry
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
pollLastEntry
public SplayMap.ImmutableSplayMapEntry pollLastEntry()
- Specified by:
pollLastEntry
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
lowerEntry
public SplayMap.ImmutableSplayMapEntry lowerEntry(UnsignedInteger key)
- Specified by:
lowerEntry
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
lowerKey
public UnsignedInteger lowerKey(UnsignedInteger key)
- Specified by:
lowerKey
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
higherEntry
public SplayMap.ImmutableSplayMapEntry higherEntry(UnsignedInteger key)
- Specified by:
higherEntry
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
higherKey
public UnsignedInteger higherKey(UnsignedInteger key)
- Specified by:
higherKey
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
floorEntry
public SplayMap.ImmutableSplayMapEntry floorEntry(UnsignedInteger key)
- Specified by:
floorEntry
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
floorKey
public UnsignedInteger floorKey(UnsignedInteger key)
- Specified by:
floorKey
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
ceilingEntry
public SplayMap.ImmutableSplayMapEntry ceilingEntry(UnsignedInteger key)
- Specified by:
ceilingEntry
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
ceilingKey
public UnsignedInteger ceilingKey(UnsignedInteger key)
- Specified by:
ceilingKey
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
descendingMap
public java.util.NavigableMap<UnsignedInteger,E> descendingMap()
- Specified by:
descendingMap
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
navigableKeySet
public java.util.NavigableSet<UnsignedInteger> navigableKeySet()
- Specified by:
navigableKeySet
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
descendingKeySet
public java.util.NavigableSet<UnsignedInteger> descendingKeySet()
- Specified by:
descendingKeySet
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
subMap
public java.util.NavigableMap<UnsignedInteger,E> subMap(UnsignedInteger fromKey, boolean fromInclusive, UnsignedInteger toKey, boolean toInclusive)
- Specified by:
subMap
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
headMap
public java.util.NavigableMap<UnsignedInteger,E> headMap(UnsignedInteger toKey, boolean inclusive)
- Specified by:
headMap
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
tailMap
public java.util.NavigableMap<UnsignedInteger,E> tailMap(UnsignedInteger fromKey, boolean inclusive)
- Specified by:
tailMap
in interfacejava.util.NavigableMap<UnsignedInteger,E>
-
subMap
public java.util.SortedMap<UnsignedInteger,E> subMap(UnsignedInteger fromKey, UnsignedInteger toKey)
- Specified by:
subMap
in interfacejava.util.NavigableMap<UnsignedInteger,E>
- Specified by:
subMap
in interfacejava.util.SortedMap<UnsignedInteger,E>
-
headMap
public java.util.SortedMap<UnsignedInteger,E> headMap(UnsignedInteger toKey)
- Specified by:
headMap
in interfacejava.util.NavigableMap<UnsignedInteger,E>
- Specified by:
headMap
in interfacejava.util.SortedMap<UnsignedInteger,E>
-
tailMap
public java.util.SortedMap<UnsignedInteger,E> tailMap(UnsignedInteger fromKey)
- Specified by:
tailMap
in interfacejava.util.NavigableMap<UnsignedInteger,E>
- Specified by:
tailMap
in interfacejava.util.SortedMap<UnsignedInteger,E>
-
-