com.netflix.nfgraph.util
Class OrdinalMap<T>

java.lang.Object
  extended by com.netflix.nfgraph.util.OrdinalMap<T>
All Implemented Interfaces:
java.lang.Iterable<T>

public class OrdinalMap<T>
extends java.lang.Object
implements java.lang.Iterable<T>

An OrdinalMap will generate and maintain a mapping between objects added and an integer value between 0 and n, where n is the number of objects in the map.

The values mapped to the objects will be the order in which the objects are inserted.

The OrdinalMap is memory-efficient and can retrieve an object given an ordinal, or an ordinal given an object, both in O(1) time.

If, for example, some application refers to graph nodes as Strings, the OrdinalMap can be used as follows:

 OrdinalMap<String> ordinalMap = new OrdinalMap<String>();
 
 int ord0 = ordinalMap.add("node0");  // returns 0
 int ord1 = ordinalMap.add("node1");  // returns 1
 int ord2 = ordinalMap.add("node2");  // returns 2
 int ord3 = ordinalMap.add("node1");  // returns 1
 
 ordinalMap.get("node2"); // returns 2
 ordinalMap.get(ord2);    // returns "node2"
 
 
 


Constructor Summary
OrdinalMap()
           
OrdinalMap(int expectedSize)
           
 
Method Summary
 int add(T obj)
          Add an object into this OrdinalMap.
 T get(int ordinal)
           
 int get(T obj)
           
 java.util.Iterator<T> iterator()
           
 int size()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

OrdinalMap

public OrdinalMap()

OrdinalMap

public OrdinalMap(int expectedSize)
Method Detail

add

public int add(T obj)
Add an object into this OrdinalMap. If the same object (or an Object.equals(Object) object) is already in the map, then no changes will be made.

Returns:
the ordinal of obj

get

public int get(T obj)
Returns:
the ordinal of an object previously added to the map. If the object has not been added to the map, returns -1 instead.

get

public T get(int ordinal)
Returns:
the object for a given ordinal. If the ordinal does not yet exist, returns null.

size

public int size()
Returns:
the number of objects in this map.

iterator

public java.util.Iterator<T> iterator()
Specified by:
iterator in interface java.lang.Iterable<T>
Returns:
an Iterator over the objects in this mapping.