Class MapWithOrdinal<K,V>
java.lang.Object
org.apache.drill.common.collections.MapWithOrdinal<K,V>
- Type Parameters:
K
- key typeV
- value type
- All Implemented Interfaces:
Map<K,
V>
An implementation of map that supports constant time look-up by a generic key or an ordinal.
This class extends the functionality a regular
Map
with ordinal lookup support.
Upon insertion an unused ordinal is assigned to the inserted (key, value) tuple.
Upon update the same ordinal id is re-used while value is replaced.
Upon deletion of an existing item, its corresponding ordinal is recycled and could be used by another item.
For any instance with N items, this implementation guarantees that ordinals are in the range of [0, N). However,
the ordinal assignment is dynamic and may change after an insertion or deletion. Consumers of this class are
responsible for explicitly checking the ordinal corresponding to a key via
getOrdinal(Object)
before attempting to execute a lookup
with an ordinal.-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
boolean
containsKey
(Object key) boolean
containsValue
(Object value) entrySet()
getByOrdinal
(int id) Returns the value corresponding to the given ordinalint
getOrdinal
(K key) Returns the ordinal corresponding to the given key.boolean
isEmpty()
keySet()
Inserts the tuple (key, value) into the map extending the semantics ofMap.put(K, V)
with automatic ordinal assignment.void
Removes the element corresponding to the key if exists extending the semantics ofMap.remove(java.lang.Object)
with ordinal re-cycling.int
size()
values()
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
-
Constructor Details
-
MapWithOrdinal
public MapWithOrdinal()
-
-
Method Details
-
getByOrdinal
Returns the value corresponding to the given ordinal- Parameters:
id
- ordinal value for lookup- Returns:
- an instance of V
-
getOrdinal
Returns the ordinal corresponding to the given key.- Parameters:
key
- key for ordinal lookup- Returns:
- ordinal value corresponding to key if it exists or -1
-
size
public int size() -
isEmpty
public boolean isEmpty() -
get
-
put
Inserts the tuple (key, value) into the map extending the semantics ofMap.put(K, V)
with automatic ordinal assignment. A new ordinal is assigned if key does not exists. Otherwise the same ordinal is re-used but the value is replaced. -
values
-
containsKey
- Specified by:
containsKey
in interfaceMap<K,
V>
-
containsValue
- Specified by:
containsValue
in interfaceMap<K,
V>
-
remove
Removes the element corresponding to the key if exists extending the semantics ofMap.remove(java.lang.Object)
with ordinal re-cycling. The ordinal corresponding to the given key may be re-assigned to another tuple. It is important that consumer checks the ordinal value viagetOrdinal(Object)
before attempting to look-up by ordinal. -
putAll
-
clear
public void clear() -
keySet
-
entrySet
-