Index handler with list of unused
Revision as of 13:16, 20 October 2014 by Weihe (talk | contribs) (Weihe moved page Index handler with unused list to Index handler with list of unused)
Abstract view
Abstract data structure: A variation of map, where:
- The number of elements is bounded by some fixed positive natural number [math]\displaystyle{ N }[/math].
- The key type is the range [math]\displaystyle{ \{1,\ldots,N\} }[/math].
- Instead of a method for inserting (key,value)-pairs, a value can be inserted alone, and the key for referring to the value is returned. As a precondition, less than [math]\displaystyle{ N }[/math] values must be currently stored in that index handler object.
- The methods to retrieve a value for a key and to remove a key and the associated value from the index handler object are basically identical to the corresponding methods of normal maps. A key may be reused by the index handler object after removal.
Implementation invariant:
- There is an array [math]\displaystyle{ A }[/math] with index range [math]\displaystyle{ 1,...,N }[/math], and the component type is the value type of the map.
- There is a set [math]\displaystyle{ U }[/math] (for unused) of natural numbers.
- For each [math]\displaystyle{ i\in\{1,...,N_\text{max}\} }[/math] not in [math]\displaystyle{ U }[/math], [math]\displaystyle{ A[i] }[/math] is the value associated with key [math]\displaystyle{ i }[/math].
Methods
- The constructor takes [math]\displaystyle{ N }[/math] as an argument, creates [math]\displaystyle{ A }[/math] and [math]\displaystyle{ U }[/math], and stores all potential keys [math]\displaystyle{ 1,\ldots,N }[/math] in [math]\displaystyle{ U }[/math].
- The method for inserting a new value extracts a key [math]\displaystyle{ i }[/math] from [math]\displaystyle{ U }[/math], stores the value at <mah>A[i]</math>and returns [math]\displaystyle{ i }[/math].
- The method to look up a value for a given key [math]\displaystyle{ i }[/math] returns [math]\displaystyle{ A[i] }[/math].
- The method to remove a key re-inserts the key in [math]\displaystyle{ U }[/math].