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 class
SplayMap.AscendingSubMap<V>
protected static class
SplayMap.DescendingSubMap<V>
static class
SplayMap.ImmutableSplayMapEntry<E>
protected static class
SplayMap.NavigableSubMapKeySet
protected static class
SplayMap.SplayedEntry<E>
protected class
SplayMap.SplayMapKeySet
-
Field Summary
Fields Modifier and Type Field Description protected static Comparator<UnsignedInteger>
COMPARATOR
protected int
DEFAULT_ENTRY_POOL_SIZE
protected NavigableMap<UnsignedInteger,E>
descendingMapView
protected int
entriesInExistence
protected RingQueue<SplayMap.SplayedEntry<E>>
entryPool
protected Set<Map.Entry<UnsignedInteger,E>>
entrySet
protected NavigableSet<UnsignedInteger>
keySet
protected int
modCount
protected static Comparator<UnsignedInteger>
REVERSE_COMPARATOR
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 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)
UnsignedInteger
ceilingKey(UnsignedInteger key)
void
clear()
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(Object key)
boolean
containsValue(Object value)
protected void
delete(SplayMap.SplayedEntry<E> node)
NavigableSet<UnsignedInteger>
descendingKeySet()
NavigableMap<UnsignedInteger,E>
descendingMap()
protected void
entryAdded(SplayMap.SplayedEntry<E> newEntry)
protected void
entryDeleted(SplayMap.SplayedEntry<E> deletedEntry)
Set<Map.Entry<UnsignedInteger,E>>
entrySet()
boolean
equals(Object o)
protected static <V> SplayMap.ImmutableSplayMapEntry<V>
export(SplayMap.SplayedEntry<V> entry)
SplayMap.ImmutableSplayMapEntry<E>
firstEntry()
UnsignedInteger
firstKey()
SplayMap.ImmutableSplayMapEntry<E>
floorEntry(UnsignedInteger key)
UnsignedInteger
floorKey(UnsignedInteger key)
void
forEach(BiConsumer<? super UnsignedInteger,? super E> action)
void
forEach(Consumer<? super E> action)
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(Object key)
E
getOrDefault(int key, E defaultValue)
Gets the value of the element stored in theMap
with the key (treated as an unsigned integer for comparison.E
getOrDefault(Object key, E defaultValue)
int
hashCode()
SortedMap<UnsignedInteger,E>
headMap(UnsignedInteger toKey)
NavigableMap<UnsignedInteger,E>
headMap(UnsignedInteger toKey, boolean inclusive)
SplayMap.ImmutableSplayMapEntry<E>
higherEntry(UnsignedInteger key)
UnsignedInteger
higherKey(UnsignedInteger key)
boolean
isEmpty()
Set<UnsignedInteger>
keySet()
SplayMap.ImmutableSplayMapEntry<E>
lastEntry()
UnsignedInteger
lastKey()
SplayMap.ImmutableSplayMapEntry<E>
lowerEntry(UnsignedInteger key)
UnsignedInteger
lowerKey(UnsignedInteger key)
NavigableSet<UnsignedInteger>
navigableKeySet()
SplayMap.ImmutableSplayMapEntry<E>
pollFirstEntry()
SplayMap.ImmutableSplayMapEntry<E>
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(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, 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(Object key)
boolean
remove(Object key, 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(BiFunction<? super UnsignedInteger,? super E,? extends E> function)
protected static Comparator<? super UnsignedInteger>
reverseComparator()
int
size()
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:
size
in interfaceMap<UnsignedInteger,E>
-
isEmpty
public boolean isEmpty()
- Specified by:
isEmpty
in interfaceMap<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
.
-
getOrDefault
public E getOrDefault(int key, E defaultValue)
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
.
-
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
.
-
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 interfaceMap<UnsignedInteger,E>
-
putIfAbsent
public E putIfAbsent(UnsignedInteger key, E value)
- Specified by:
putIfAbsent
in interfaceMap<UnsignedInteger,E>
-
getOrDefault
public E getOrDefault(Object key, E defaultValue)
- Specified by:
getOrDefault
in interfaceMap<UnsignedInteger,E>
-
containsKey
public boolean containsKey(Object key)
- Specified by:
containsKey
in interfaceMap<UnsignedInteger,E>
-
clear
public void clear()
- Specified by:
clear
in interfaceMap<UnsignedInteger,E>
-
putAll
public void putAll(Map<? extends UnsignedInteger,? extends E> source)
- Specified by:
putAll
in interfaceMap<UnsignedInteger,E>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValue
in interfaceMap<UnsignedInteger,E>
-
hashCode
public int hashCode()
-
equals
public boolean equals(Object o)
-
keySet
public Set<UnsignedInteger> keySet()
- Specified by:
keySet
in interfaceMap<UnsignedInteger,E>
- Specified by:
keySet
in interfaceSortedMap<UnsignedInteger,E>
-
values
public Collection<E> values()
- Specified by:
values
in interfaceMap<UnsignedInteger,E>
- Specified by:
values
in interfaceSortedMap<UnsignedInteger,E>
-
entrySet
public Set<Map.Entry<UnsignedInteger,E>> entrySet()
- Specified by:
entrySet
in interfaceMap<UnsignedInteger,E>
- Specified by:
entrySet
in interfaceSortedMap<UnsignedInteger,E>
-
forEach
public void forEach(BiConsumer<? super UnsignedInteger,? super E> action)
- Specified by:
forEach
in interfaceMap<UnsignedInteger,E>
-
forEach
public void forEach(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(BiFunction<? super UnsignedInteger,? super E,? extends E> function)
- Specified by:
replaceAll
in interfaceMap<UnsignedInteger,E>
-
remove
public boolean remove(Object key, Object value)
- Specified by:
remove
in 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:
true
if an entry was removed from theMap
-
replace
public boolean replace(UnsignedInteger key, E oldValue, E newValue)
- Specified by:
replace
in 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 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 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 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 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:
comparator
in interfaceSortedMap<UnsignedInteger,E>
-
firstKey
public UnsignedInteger firstKey()
- Specified by:
firstKey
in interfaceSortedMap<UnsignedInteger,E>
-
lastKey
public UnsignedInteger lastKey()
- Specified by:
lastKey
in interfaceSortedMap<UnsignedInteger,E>
-
firstEntry
public SplayMap.ImmutableSplayMapEntry<E> firstEntry()
- Specified by:
firstEntry
in interfaceNavigableMap<UnsignedInteger,E>
-
lastEntry
public SplayMap.ImmutableSplayMapEntry<E> lastEntry()
- Specified by:
lastEntry
in interfaceNavigableMap<UnsignedInteger,E>
-
pollFirstEntry
public SplayMap.ImmutableSplayMapEntry<E> pollFirstEntry()
- Specified by:
pollFirstEntry
in interfaceNavigableMap<UnsignedInteger,E>
-
pollLastEntry
public SplayMap.ImmutableSplayMapEntry<E> pollLastEntry()
- Specified by:
pollLastEntry
in interfaceNavigableMap<UnsignedInteger,E>
-
lowerEntry
public SplayMap.ImmutableSplayMapEntry<E> lowerEntry(UnsignedInteger key)
- Specified by:
lowerEntry
in interfaceNavigableMap<UnsignedInteger,E>
-
lowerKey
public UnsignedInteger lowerKey(UnsignedInteger key)
- Specified by:
lowerKey
in interfaceNavigableMap<UnsignedInteger,E>
-
higherEntry
public SplayMap.ImmutableSplayMapEntry<E> higherEntry(UnsignedInteger key)
- Specified by:
higherEntry
in interfaceNavigableMap<UnsignedInteger,E>
-
higherKey
public UnsignedInteger higherKey(UnsignedInteger key)
- Specified by:
higherKey
in interfaceNavigableMap<UnsignedInteger,E>
-
floorEntry
public SplayMap.ImmutableSplayMapEntry<E> floorEntry(UnsignedInteger key)
- Specified by:
floorEntry
in interfaceNavigableMap<UnsignedInteger,E>
-
floorKey
public UnsignedInteger floorKey(UnsignedInteger key)
- Specified by:
floorKey
in interfaceNavigableMap<UnsignedInteger,E>
-
ceilingEntry
public SplayMap.ImmutableSplayMapEntry<E> ceilingEntry(UnsignedInteger key)
- Specified by:
ceilingEntry
in interfaceNavigableMap<UnsignedInteger,E>
-
ceilingKey
public UnsignedInteger ceilingKey(UnsignedInteger key)
- Specified by:
ceilingKey
in interfaceNavigableMap<UnsignedInteger,E>
-
descendingMap
public NavigableMap<UnsignedInteger,E> descendingMap()
- Specified by:
descendingMap
in interfaceNavigableMap<UnsignedInteger,E>
-
navigableKeySet
public NavigableSet<UnsignedInteger> navigableKeySet()
- Specified by:
navigableKeySet
in interfaceNavigableMap<UnsignedInteger,E>
-
descendingKeySet
public NavigableSet<UnsignedInteger> descendingKeySet()
- Specified by:
descendingKeySet
in interfaceNavigableMap<UnsignedInteger,E>
-
subMap
public NavigableMap<UnsignedInteger,E> subMap(UnsignedInteger fromKey, boolean fromInclusive, UnsignedInteger toKey, boolean toInclusive)
- Specified by:
subMap
in interfaceNavigableMap<UnsignedInteger,E>
-
headMap
public NavigableMap<UnsignedInteger,E> headMap(UnsignedInteger toKey, boolean inclusive)
- Specified by:
headMap
in interfaceNavigableMap<UnsignedInteger,E>
-
tailMap
public NavigableMap<UnsignedInteger,E> tailMap(UnsignedInteger fromKey, boolean inclusive)
- Specified by:
tailMap
in interfaceNavigableMap<UnsignedInteger,E>
-
subMap
public SortedMap<UnsignedInteger,E> subMap(UnsignedInteger fromKey, UnsignedInteger toKey)
- Specified by:
subMap
in interfaceNavigableMap<UnsignedInteger,E>
- Specified by:
subMap
in interfaceSortedMap<UnsignedInteger,E>
-
headMap
public SortedMap<UnsignedInteger,E> headMap(UnsignedInteger toKey)
- Specified by:
headMap
in interfaceNavigableMap<UnsignedInteger,E>
- Specified by:
headMap
in interfaceSortedMap<UnsignedInteger,E>
-
tailMap
public SortedMap<UnsignedInteger,E> tailMap(UnsignedInteger fromKey)
- Specified by:
tailMap
in interfaceNavigableMap<UnsignedInteger,E>
- Specified by:
tailMap
in interfaceSortedMap<UnsignedInteger,E>
-
-