org.knowceans.map
Class BijectiveTreeMap<X,Y>

java.lang.Object
  extended by java.util.AbstractMap<K,V>
      extended by java.util.TreeMap<X,Y>
          extended by org.knowceans.map.BijectiveTreeMap<X,Y>
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable, java.util.Map<X,Y>, java.util.NavigableMap<X,Y>, java.util.SortedMap<X,Y>, IBijectiveMap<X,Y>

public class BijectiveTreeMap<X,Y>
extends java.util.TreeMap<X,Y>
implements IBijectiveMap<X,Y>

BijectiveHashMap is a TreeMap that bijectively assigns unique keys to unique values and vice versa. The inverse mapping is done by a TreeMap that maps values to keys and sorts in the natural order of keys. With getInverse(), a key can be found from the value without the search overhead of a value search. The bijective property constrains all input to obey unique keys AND unique values, while permitting the null element.

In relational terms, this class implements a 1:1 relation.

As the underlying TreeMap has O(log(n)) get() and put() complexity, it can be used to identify numerical dimensions, such as the rows and columns of a matrix, much more conveniently than by array lookups (which would require array search for the inverse), however with some additive overhead over array index lookups.

Author:
heinrich
See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class java.util.AbstractMap
java.util.AbstractMap.SimpleEntry<K,V>, java.util.AbstractMap.SimpleImmutableEntry<K,V>
 
Nested classes/interfaces inherited from interface java.util.Map
java.util.Map.Entry<K,V>
 
Constructor Summary
BijectiveTreeMap()
           
BijectiveTreeMap(java.util.Comparator<? super X> forward)
          initialise with a comparator on the forward relation
BijectiveTreeMap(java.util.Comparator<? super X> forward, java.util.Comparator<? super Y> backward)
          initialise with comparators on the forward and backward relations
BijectiveTreeMap(IBijectiveMap<? extends X,? extends Y> t)
           
BijectiveTreeMap(java.util.Map<? extends X,? extends Y> t)
           
 
Method Summary
 java.util.TreeMap<Y,X> getInverse()
           
 X getInverse(Y val)
          gets key for a value.
 java.util.Set<Y> getValues()
          returns the keys of the inverse map.
static void main(java.lang.String[] args)
           
 Y put(X key, Y val)
          put key-value pair into the map.
 Y remove(java.lang.Object key)
          removes the key and its value from the map.
 java.util.Collection<Y> values()
           
 
Methods inherited from class java.util.TreeMap
ceilingEntry, ceilingKey, clear, clone, comparator, containsKey, containsValue, descendingKeySet, descendingMap, entrySet, firstEntry, firstKey, floorEntry, floorKey, get, headMap, headMap, higherEntry, higherKey, keySet, lastEntry, lastKey, lowerEntry, lowerKey, navigableKeySet, pollFirstEntry, pollLastEntry, putAll, size, subMap, subMap, tailMap, tailMap
 
Methods inherited from class java.util.AbstractMap
equals, hashCode, isEmpty, toString
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.util.Map
clear, containsKey, containsValue, entrySet, equals, get, hashCode, isEmpty, keySet, putAll, size
 

Constructor Detail

BijectiveTreeMap

public BijectiveTreeMap()

BijectiveTreeMap

public BijectiveTreeMap(java.util.Map<? extends X,? extends Y> t)

BijectiveTreeMap

public BijectiveTreeMap(IBijectiveMap<? extends X,? extends Y> t)

BijectiveTreeMap

public BijectiveTreeMap(java.util.Comparator<? super X> forward)
initialise with a comparator on the forward relation

Parameters:
forward -

BijectiveTreeMap

public BijectiveTreeMap(java.util.Comparator<? super X> forward,
                        java.util.Comparator<? super Y> backward)
initialise with comparators on the forward and backward relations

Parameters:
forward -
Method Detail

main

public static void main(java.lang.String[] args)

put

public Y put(X key,
             Y val)
put key-value pair into the map. If the key exists, it is replaced, along with the value it points to. If the value exists, it is replaced, along with the key it pointed to. The method returns the old value that the key pointed to. This is somewhat asymmetric because if a key gets overwritten, there is no reaction. Thus, check before calling the method.

Specified by:
put in interface java.util.Map<X,Y>
Overrides:
put in class java.util.TreeMap<X,Y>

remove

public Y remove(java.lang.Object key)
removes the key and its value from the map. In the inverse map, the value is removed. Returns the value

Specified by:
remove in interface java.util.Map<X,Y>
Overrides:
remove in class java.util.TreeMap<X,Y>

getInverse

public X getInverse(Y val)
gets key for a value.

Specified by:
getInverse in interface IBijectiveMap<X,Y>
Parameters:
val -
Returns:

getInverse

public java.util.TreeMap<Y,X> getInverse()
Specified by:
getInverse in interface IBijectiveMap<X,Y>
Returns:
the complete inverse HashMap

getValues

public java.util.Set<Y> getValues()
returns the keys of the inverse map. Use this preferably over values().

Returns:

values

public java.util.Collection<Y> values()
Specified by:
values in interface java.util.Map<X,Y>
Specified by:
values in interface java.util.SortedMap<X,Y>
Overrides:
values in class java.util.TreeMap<X,Y>