Index handler: Difference between revisions

From Algowiki
Jump to navigation Jump to search
Line 1: Line 1:
== Abstract view ==
== Abstract view ==


'''Representation invariant:'''
 
# There is a positive [[Numbers#natural numbers|natural number]] <math>N</math>.
'''Abstract data structure:'''
# For each object, there is a specific, dynamically changing partition of <math>\{1,\ldots,N\}</math> into '''used''' and '''unused indexes'''.
A variation of [[Sets and sequences|map]], where:
# For each used index, a value is stored.
# The number of elements is bounded by some fixed positive [[Numbers#natural numbers|natural number]] <math>N</math>.
# The key type is the range <math>\{1,\ldots,N\}</math>.
# Instead of a method for inserting (key,value)-pairs, a value can be inserted, and the key for referring to the value is returned. As a precondition, less than <math>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 identical to te correspnding metods of normal maps. A key may be reused by the index handler object after removal.
 




'''Implementation invariant:'''
'''Implementation invariant:'''
 
# There is an array <math>A</math> with index range <math>1,...,N</math>, and the component type is the value type of the map.
# There is an array <math>Indexes</math> with index range <math>1,...,N_\text{max}</math> and integral components from <math>\{1,...,N_\text{max}\}</math>.
# There is a [[Sets and sequences|set]] <math>U</math> (for unused) of [[Numbers#natural numbers|natural numbers]] of [[Numbers#natural numbers|natural numbers]].
# There is a [[Sets and sequences|set]] <math>U</math> (for unused) of [[Numbers#natural numbers|natural numbers]], which has length <math>N_\text{max}-N</math> and stores pairwise different numbers from <math>\{1,...,N_\text{max}\}</math>.
# For each <math>i\in\{1,...,N_\text{max}\}</math> not in <math>Unused</math>, <math>A[i]</math> is the value associated with key <math>i</math>.
# For each <math>i\in\{1,...,N_\text{max}\}</math> not in <math>Unused</math>:
## <math>Positions[i]</math> is the position of one of the <math>N</math> heap items in <math>TheHeap</math>. As long as this heap item is stored, <math>i</math> is permanently associated with this heap item (and can hence be used to locate and access this heap item at any time). The correspondence between the heap items and these numbers <math>i</math> is one-to-one.


== Methods ==
== Methods ==

Revision as of 08:43, 10 October 2014

Abstract view

Abstract data structure: A variation of map, where:

  1. The number of elements is bounded by some fixed positive natural number [math]\displaystyle{ N }[/math].
  2. The key type is the range [math]\displaystyle{ \{1,\ldots,N\} }[/math].
  3. Instead of a method for inserting (key,value)-pairs, a value can be inserted, 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.
  4. The methods to retrieve a value for a key and to remove a key and the associated value from the index handler object are identical to te correspnding metods of normal maps. A key may be reused by the index handler object after removal.


Implementation invariant:

  1. 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.
  2. There is a set [math]\displaystyle{ U }[/math] (for unused) of natural numbers of natural numbers.
  3. For each [math]\displaystyle{ i\in\{1,...,N_\text{max}\} }[/math] not in [math]\displaystyle{ Unused }[/math], [math]\displaystyle{ A[i] }[/math] is the value associated with key [math]\displaystyle{ i }[/math].

Methods

  1. If an index is requested, [math]\displaystyle{ U\neq\emptyset }[/math] is a precondition. Then one index is extracted from [math]\displaystyle{ U }[/math] and returned.
  1. If an index is given back, it is inserted in [math]\displaystyle{ U }[/math].