Module le :: Class NameToIndex
[frames] | no frames]

Class NameToIndex

Construct a unique mapping of names to indices. Every time a new name is inserted, it is assigned a new index. Indices start at 0 and increment by 1. For example:

   >>> n2n = nameToIndex()
   >>> n2n.insert('n1')
   >>> n2n.insert('n2')
   >>> n2n.insert('n1')   # has no effect since it is a duplicate
   >>> n2n.lookup('n1')
   0
   >>> n2n.names()
   ['n1', 'n2']
Instance Methods
 
__init__(self)
 
insert(self, name)
If name has been inserted before, do nothing.
 
lookup(self, name)
Returns the index associated with name.
 
names(self)
Returns list of names that have been inserted so far, in the order they were inserted.
Instance Variables
  nextIndex
The next index to be allocated.
  namesToNums
Dictionary mapping names to their assigned indices
  namesList
List of names in order of insertion
Method Details

insert(self, name)

 

If name has been inserted before, do nothing. Otherwise, assign it the next index.

lookup(self, name)

 

Returns the index associated with name. Generates an error if it name has not previously been inserted.