Index handler with list of unused: Difference between revisions

From Algowiki
Jump to navigation Jump to search
No edit summary
Line 5: Line 5:


'''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>A</math> with index range <math>1,...,N</math> and component type <math>\mathcal{V}</math>.
# There is a [[Sets and sequences|set]] <math>U</math> (for unused) of [[Numbers#natural numbers|natural numbers]].
# There is a [[Sets and sequences|set]] <math>U</math> (for unused) of [[Numbers#natural numbers|natural numbers]].
# For each <math>i\in\{1,...,N_\text{max}\}</math> not in <math>U</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>U</math>, <math>A[i]</math> is the value associated with key <math>i</math>.

Revision as of 13:59, 20 October 2014

Abstract view

Abstract data structure: Index handler

Implementation invariant:

  1. There is an array [math]\displaystyle{ A }[/math] with index range [math]\displaystyle{ 1,...,N }[/math] and component type [math]\displaystyle{ \mathcal{V} }[/math].
  2. There is a set [math]\displaystyle{ U }[/math] (for unused) of natural numbers.
  3. 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

  1. 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].
  2. 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].
  3. The method to look up a value for a given key [math]\displaystyle{ i }[/math] returns [math]\displaystyle{ A[i] }[/math].
  4. The method to remove a key re-inserts the key in [math]\displaystyle{ U }[/math].