public class TreeBidiMap extends java.lang.Object implements OrderedBidiMap
Comparable
interface.
This class guarantees that the map will be in both ascending key order and ascending value order, sorted according to the natural order for the key's and value's classes.
This Map is intended for applications that need to be able to look up a key-value pairing by either key or value, and need to do so with equal efficiency.
While that goal could be accomplished by taking a pair of TreeMaps
and redirecting requests to the appropriate TreeMap (e.g.,
containsKey would be directed to the TreeMap that maps values to
keys, containsValue would be directed to the TreeMap that maps keys
to values), there are problems with that implementation.
If the data contained in the TreeMaps is large, the cost of redundant
storage becomes significant. The DualTreeBidiMap
and
DualHashBidiMap
implementations use this approach.
This solution keeps minimizes the data storage by holding data only once. The red-black algorithm is based on java util TreeMap, but has been modified to simultaneously map a tree node by key and by value. This doubles the cost of put operations (but so does using two TreeMaps), and nearly doubles the cost of remove operations (there is a savings in that the lookup of the node to be removed only has to be performed once). And since only one node contains the key and value, storage is significantly less than that required by two TreeMaps.
The Map.Entry instances returned by the appropriate methods will not allow setValue() and will throw an UnsupportedOperationException on attempts to call that method.
Modifier and Type | Class and Description |
---|---|
(package private) static class |
TreeBidiMap.EntryView
A view of this map.
|
(package private) static class |
TreeBidiMap.Inverse
A node used to store the data.
|
(package private) static class |
TreeBidiMap.Node
A node used to store the data.
|
(package private) static class |
TreeBidiMap.View
A view of this map.
|
(package private) static class |
TreeBidiMap.ViewIterator
An iterator over the map.
|
(package private) static class |
TreeBidiMap.ViewMapIterator
An iterator over the map.
|
Modifier and Type | Field and Description |
---|---|
private static java.lang.String[] |
dataName |
private java.util.Set |
entrySet |
private static int |
FIRST_INDEX |
private TreeBidiMap.Inverse |
inverse |
private static int |
INVERSEMAPENTRY |
private static int |
KEY |
private java.util.Set |
keySet |
private static int |
MAPENTRY |
private int |
modifications |
private int |
nodeCount |
private static int |
NUMBER_OF_INDICES |
private TreeBidiMap.Node[] |
rootNode |
private static int |
SUM_OF_INDICES |
private static int |
VALUE |
private java.util.Set |
valuesSet |
Constructor and Description |
---|
TreeBidiMap()
Constructs a new empty TreeBidiMap.
|
TreeBidiMap(java.util.Map map)
Constructs a new TreeBidiMap by copying an existing Map.
|
Modifier and Type | Method and Description |
---|---|
private static void |
checkKey(java.lang.Object key)
check a key for validity (non-null and implements Comparable)
|
private static void |
checkKeyAndValue(java.lang.Object key,
java.lang.Object value)
check a key and a value for validity (non-null and implements
Comparable)
|
private static void |
checkNonNullComparable(java.lang.Object o,
int index)
check if an object is fit to be proper input ...
|
private static void |
checkValue(java.lang.Object value)
check a value for validity (non-null and implements Comparable)
|
void |
clear()
Removes all mappings from this map.
|
private static int |
compare(java.lang.Comparable o1,
java.lang.Comparable o2)
Compare two objects
|
boolean |
containsKey(java.lang.Object key)
Checks whether this map contains the a mapping for the specified key.
|
boolean |
containsValue(java.lang.Object value)
Checks whether this map contains the a mapping for the specified value.
|
private static void |
copyColor(TreeBidiMap.Node from,
TreeBidiMap.Node to,
int index)
copy the color from one node to another, dealing with the fact
that one or both nodes may, in fact, be null
|
private boolean |
doEquals(java.lang.Object obj,
int type)
Compares for equals as per the API.
|
private java.lang.Object |
doGet(java.lang.Comparable obj,
int index)
Common get logic, used to get by key or get by value
|
private int |
doHashCode(int type)
Gets the hash code value for this map as per the API.
|
private java.lang.Object |
doPut(java.lang.Comparable key,
java.lang.Comparable value,
int index)
Common put logic, differing only in the return value.
|
private void |
doRedBlackDelete(TreeBidiMap.Node deletedNode)
complicated red-black delete stuff.
|
private void |
doRedBlackDeleteFixup(TreeBidiMap.Node replacementNode,
int index)
complicated red-black delete stuff.
|
private void |
doRedBlackInsert(TreeBidiMap.Node insertedNode,
int index)
complicated red-black insert stuff.
|
private java.lang.Object |
doRemove(java.lang.Comparable o,
int index)
Remove by object (remove by key or remove by value)
|
private java.lang.String |
doToString(int type)
Gets the string form of this map as per AbstractMap.
|
java.util.Set |
entrySet()
Returns a set view of the entries contained in this map in key order.
|
boolean |
equals(java.lang.Object obj)
Compares for equals as per the API.
|
java.lang.Object |
firstKey()
Gets the first (lowest) key currently in this map.
|
java.lang.Object |
get(java.lang.Object key)
Gets the value to which this map maps the specified key.
|
private static TreeBidiMap.Node |
getGrandParent(TreeBidiMap.Node node,
int index)
get a node's grandparent.
|
java.lang.Object |
getKey(java.lang.Object value)
Returns the key to which this map maps the specified value.
|
private static TreeBidiMap.Node |
getLeftChild(TreeBidiMap.Node node,
int index)
get a node's left child.
|
private static TreeBidiMap.Node |
getParent(TreeBidiMap.Node node,
int index)
get a node's parent.
|
private static TreeBidiMap.Node |
getRightChild(TreeBidiMap.Node node,
int index)
get a node's right child.
|
private static TreeBidiMap.Node |
greatestNode(TreeBidiMap.Node node,
int index)
Find the greatest node from a given node.
|
private void |
grow()
bump up the size and note that the map has changed
|
int |
hashCode()
Gets the hash code value for this map as per the API.
|
private void |
insertValue(TreeBidiMap.Node newNode)
insert a node by its value
|
BidiMap |
inverseBidiMap()
Gets the inverse map for comparison.
|
OrderedBidiMap |
inverseOrderedBidiMap()
Gets the inverse map for comparison.
|
private static boolean |
isBlack(TreeBidiMap.Node node,
int index)
is the specified black red? if the node does not exist, sure,
it's black, thank you
|
boolean |
isEmpty()
Checks whether the map is empty or not.
|
private static boolean |
isLeftChild(TreeBidiMap.Node node,
int index)
is this node its parent's left child? mind you, the node, or
its parent, may not exist.
|
private static boolean |
isRed(TreeBidiMap.Node node,
int index)
is the specified node red? if the node does not exist, no, it's
black, thank you
|
private static boolean |
isRightChild(TreeBidiMap.Node node,
int index)
is this node its parent's right child? mind you, the node, or
its parent, may not exist.
|
java.util.Set |
keySet()
Returns a set view of the keys contained in this map in key order.
|
java.lang.Object |
lastKey()
Gets the last (highest) key currently in this map.
|
private static TreeBidiMap.Node |
leastNode(TreeBidiMap.Node node,
int index)
Find the least node from a given node.
|
private TreeBidiMap.Node |
lookup(java.lang.Comparable data,
int index)
do the actual lookup of a piece of data
|
private static void |
makeBlack(TreeBidiMap.Node node,
int index)
force a node (if it exists) black
|
private static void |
makeRed(TreeBidiMap.Node node,
int index)
force a node (if it exists) red
|
MapIterator |
mapIterator()
Gets an iterator over the map entries.
|
private void |
modify()
increment the modification count -- used to check for
concurrent modification of the map through the map and through
an Iterator from one of its Set or Collection views
|
private TreeBidiMap.Node |
nextGreater(TreeBidiMap.Node node,
int index)
get the next larger node from the specified node
|
java.lang.Object |
nextKey(java.lang.Object key)
Gets the next key after the one specified.
|
private TreeBidiMap.Node |
nextSmaller(TreeBidiMap.Node node,
int index)
get the next larger node from the specified node
|
private static int |
oppositeIndex(int index)
Get the opposite index of the specified index
|
OrderedMapIterator |
orderedMapIterator()
Gets an ordered iterator over the map entries.
|
java.lang.Object |
previousKey(java.lang.Object key)
Gets the previous key before the one specified.
|
java.lang.Object |
put(java.lang.Object key,
java.lang.Object value)
Puts the key-value pair into the map, replacing any previous pair.
|
void |
putAll(java.util.Map map)
Puts all the mappings from the specified map into this map.
|
java.lang.Object |
remove(java.lang.Object key)
Removes the mapping for this key from this map if present.
|
java.lang.Object |
removeValue(java.lang.Object value)
Removes the mapping for this value from this map if present.
|
private void |
rotateLeft(TreeBidiMap.Node node,
int index)
do a rotate left.
|
private void |
rotateRight(TreeBidiMap.Node node,
int index)
do a rotate right.
|
private void |
shrink()
decrement the size and note that the map has changed
|
int |
size()
Returns the number of key-value mappings in this map.
|
private void |
swapPosition(TreeBidiMap.Node x,
TreeBidiMap.Node y,
int index)
swap two nodes (except for their content), taking care of
special cases where one is the other's parent ...
|
java.lang.String |
toString()
Returns a string version of this Map in standard format.
|
java.util.Collection |
values()
Returns a set view of the values contained in this map in key order.
|
private static final int KEY
private static final int VALUE
private static final int MAPENTRY
private static final int INVERSEMAPENTRY
private static final int SUM_OF_INDICES
private static final int FIRST_INDEX
private static final int NUMBER_OF_INDICES
private static final java.lang.String[] dataName
private TreeBidiMap.Node[] rootNode
private int nodeCount
private int modifications
private java.util.Set keySet
private java.util.Set valuesSet
private java.util.Set entrySet
private TreeBidiMap.Inverse inverse
public TreeBidiMap()
public TreeBidiMap(java.util.Map map)
map
- the map to copyjava.lang.ClassCastException
- if the keys/values in the map are
not Comparable or are not mutually comparablejava.lang.NullPointerException
- if any key or value in the map is nullpublic int size()
size
in interface java.util.Map
public boolean isEmpty()
isEmpty
in interface java.util.Map
public boolean containsKey(java.lang.Object key)
The key must implement Comparable
.
containsKey
in interface java.util.Map
key
- key whose presence in this map is to be testedjava.lang.ClassCastException
- if the key is of an inappropriate typejava.lang.NullPointerException
- if the key is nullpublic boolean containsValue(java.lang.Object value)
The value must implement Comparable
.
containsValue
in interface java.util.Map
value
- value whose presence in this map is to be testedjava.lang.ClassCastException
- if the value is of an inappropriate typejava.lang.NullPointerException
- if the value is nullpublic java.lang.Object get(java.lang.Object key)
The key must implement Comparable
.
get
in interface java.util.Map
key
- key whose associated value is to be returnedjava.lang.ClassCastException
- if the key is of an inappropriate typejava.lang.NullPointerException
- if the key is nullpublic java.lang.Object put(java.lang.Object key, java.lang.Object value)
When adding a key-value pair, the value may already exist in the map against a different key. That mapping is removed, to ensure that the value only occurs once in the inverse map.
BidiMap map1 = new TreeBidiMap(); map.put("A","B"); // contains A mapped to B, as per Map map.put("A","C"); // contains A mapped to C, as per Map BidiMap map2 = new TreeBidiMap(); map.put("A","B"); // contains A mapped to B, as per Map map.put("C","B"); // contains C mapped to B, key A is removed
Both key and value must implement Comparable
.
put
in interface java.util.Map
put
in interface BidiMap
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified keyjava.lang.ClassCastException
- if the key is of an inappropriate typejava.lang.NullPointerException
- if the key is nullpublic void putAll(java.util.Map map)
All keys and values must implement Comparable
.
putAll
in interface java.util.Map
map
- the map to copy frompublic java.lang.Object remove(java.lang.Object key)
The key must implement Comparable
.
remove
in interface java.util.Map
key
- key whose mapping is to be removed from the map.java.lang.ClassCastException
- if the key is of an inappropriate typejava.lang.NullPointerException
- if the key is nullpublic void clear()
clear
in interface java.util.Map
public java.lang.Object getKey(java.lang.Object value)
The value must implement Comparable
.
getKey
in interface BidiMap
value
- value whose associated key is to be returned.java.lang.ClassCastException
- if the value is of an inappropriate typejava.lang.NullPointerException
- if the value is nullpublic java.lang.Object removeValue(java.lang.Object value)
The value must implement Comparable
.
removeValue
in interface BidiMap
value
- value whose mapping is to be removed from the mapjava.lang.ClassCastException
- if the value is of an inappropriate typejava.lang.NullPointerException
- if the value is nullpublic java.lang.Object firstKey()
firstKey
in interface OrderedMap
java.util.NoSuchElementException
- if this map is emptypublic java.lang.Object lastKey()
lastKey
in interface OrderedMap
java.util.NoSuchElementException
- if this map is emptypublic java.lang.Object nextKey(java.lang.Object key)
The key must implement Comparable
.
nextKey
in interface OrderedMap
key
- the key to search for next frompublic java.lang.Object previousKey(java.lang.Object key)
The key must implement Comparable
.
previousKey
in interface OrderedMap
key
- the key to search for previous frompublic java.util.Set keySet()
The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined.
The set supports element removal, which removes the corresponding mapping from the map. It does not support the add or addAll operations.
keySet
in interface java.util.Map
public java.util.Collection values()
The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined.
The set supports element removal, which removes the corresponding mapping from the map. It does not support the add or addAll operations.
values
in interface java.util.Map
public java.util.Set entrySet()
The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. If the map is modified while an iteration over the set is in progress, the results of the iteration are undefined.
The set supports element removal, which removes the corresponding mapping from the map. It does not support the add or addAll operations. The returned MapEntry objects do not support setValue.
entrySet
in interface java.util.Map
public MapIterator mapIterator()
For this map, this iterator is the fastest way to iterate over the entries.
mapIterator
in interface BidiMap
mapIterator
in interface IterableMap
public OrderedMapIterator orderedMapIterator()
This iterator allows both forward and reverse iteration over the entries.
orderedMapIterator
in interface OrderedMap
public BidiMap inverseBidiMap()
inverseBidiMap
in interface BidiMap
inverseBidiMap
in interface OrderedBidiMap
public OrderedBidiMap inverseOrderedBidiMap()
inverseOrderedBidiMap
in interface OrderedBidiMap
public boolean equals(java.lang.Object obj)
equals
in interface java.util.Map
equals
in class java.lang.Object
obj
- the object to compare topublic int hashCode()
hashCode
in interface java.util.Map
hashCode
in class java.lang.Object
public java.lang.String toString()
toString
in class java.lang.Object
private java.lang.Object doGet(java.lang.Comparable obj, int index)
obj
- the key or value that we're looking forindex
- the KEY or VALUE intprivate java.lang.Object doPut(java.lang.Comparable key, java.lang.Comparable value, int index)
key
- the key, always the main map keyvalue
- the value, always the main map valueindex
- the KEY or VALUE int, for the return value onlyprivate java.lang.Object doRemove(java.lang.Comparable o, int index)
o
- the key, or value, that we're looking forindex
- the KEY or VALUE intprivate TreeBidiMap.Node lookup(java.lang.Comparable data, int index)
data
- the key or value to be looked upindex
- the KEY or VALUE intprivate TreeBidiMap.Node nextGreater(TreeBidiMap.Node node, int index)
node
- the node to be searched fromindex
- the KEY or VALUE intprivate TreeBidiMap.Node nextSmaller(TreeBidiMap.Node node, int index)
node
- the node to be searched fromindex
- the KEY or VALUE intprivate static int oppositeIndex(int index)
index
- the KEY or VALUE intprivate static int compare(java.lang.Comparable o1, java.lang.Comparable o2)
o1
- the first objecto2
- the second objectprivate static TreeBidiMap.Node leastNode(TreeBidiMap.Node node, int index)
node
- the node from which we will start searchingindex
- the KEY or VALUE intprivate static TreeBidiMap.Node greatestNode(TreeBidiMap.Node node, int index)
node
- the node from which we will start searchingindex
- the KEY or VALUE intprivate static void copyColor(TreeBidiMap.Node from, TreeBidiMap.Node to, int index)
from
- the node whose color we're copying; may be nullto
- the node whose color we're changing; may be nullindex
- the KEY or VALUE intprivate static boolean isRed(TreeBidiMap.Node node, int index)
node
- the node (may be null) in questionindex
- the KEY or VALUE intprivate static boolean isBlack(TreeBidiMap.Node node, int index)
node
- the node (may be null) in questionindex
- the KEY or VALUE intprivate static void makeRed(TreeBidiMap.Node node, int index)
node
- the node (may be null) in questionindex
- the KEY or VALUE intprivate static void makeBlack(TreeBidiMap.Node node, int index)
node
- the node (may be null) in questionindex
- the KEY or VALUE intprivate static TreeBidiMap.Node getGrandParent(TreeBidiMap.Node node, int index)
node
- the node (may be null) in questionindex
- the KEY or VALUE intprivate static TreeBidiMap.Node getParent(TreeBidiMap.Node node, int index)
node
- the node (may be null) in questionindex
- the KEY or VALUE intprivate static TreeBidiMap.Node getRightChild(TreeBidiMap.Node node, int index)
node
- the node (may be null) in questionindex
- the KEY or VALUE intprivate static TreeBidiMap.Node getLeftChild(TreeBidiMap.Node node, int index)
node
- the node (may be null) in questionindex
- the KEY or VALUE intprivate static boolean isLeftChild(TreeBidiMap.Node node, int index)
node
- the node (may be null) in questionindex
- the KEY or VALUE intprivate static boolean isRightChild(TreeBidiMap.Node node, int index)
node
- the node (may be null) in questionindex
- the KEY or VALUE intprivate void rotateLeft(TreeBidiMap.Node node, int index)
node
- the node to be rotatedindex
- the KEY or VALUE intprivate void rotateRight(TreeBidiMap.Node node, int index)
node
- the node to be rotatedindex
- the KEY or VALUE intprivate void doRedBlackInsert(TreeBidiMap.Node insertedNode, int index)
insertedNode
- the node to be insertedindex
- the KEY or VALUE intprivate void doRedBlackDelete(TreeBidiMap.Node deletedNode)
deletedNode
- the node to be deletedprivate void doRedBlackDeleteFixup(TreeBidiMap.Node replacementNode, int index)
replacementNode
- the node being replacedindex
- the KEY or VALUE intprivate void swapPosition(TreeBidiMap.Node x, TreeBidiMap.Node y, int index)
x
- one nodey
- another nodeindex
- the KEY or VALUE intprivate static void checkNonNullComparable(java.lang.Object o, int index)
o
- the object being checkedindex
- the KEY or VALUE int (used to put the right word in the
exception message)java.lang.NullPointerException
- if o is nulljava.lang.ClassCastException
- if o is not Comparableprivate static void checkKey(java.lang.Object key)
key
- the key to be checkedjava.lang.NullPointerException
- if key is nulljava.lang.ClassCastException
- if key is not Comparableprivate static void checkValue(java.lang.Object value)
value
- the value to be checkedjava.lang.NullPointerException
- if value is nulljava.lang.ClassCastException
- if value is not Comparableprivate static void checkKeyAndValue(java.lang.Object key, java.lang.Object value)
key
- the key to be checkedvalue
- the value to be checkedjava.lang.NullPointerException
- if key or value is nulljava.lang.ClassCastException
- if key or value is not Comparableprivate void modify()
private void grow()
private void shrink()
private void insertValue(TreeBidiMap.Node newNode) throws java.lang.IllegalArgumentException
newNode
- the node to be insertedjava.lang.IllegalArgumentException
- if the node already exists
in the value mappingprivate boolean doEquals(java.lang.Object obj, int type)
obj
- the object to compare totype
- the KEY or VALUE intprivate int doHashCode(int type)
type
- the KEY or VALUE intprivate java.lang.String doToString(int type)
type
- the KEY or VALUE int