Index handler: Difference between revisions

From Algowiki
Jump to navigation Jump to search
No edit summary
 
(21 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Abstract view ==
== Representation invariant ==


This abstract data structure is [[Genericity|generic]] and parameterized by some value type <math>\mathcal{V}</math>. An object of an implementation of this abstract data structure is repesented by:
# A positive integral number <math>N</math>.
# A subset <math>I</math> of <math>\{1,\ldots,N\}</math>, the '''currently used indexes'''.
# A mapping <math>I\rightarrow\mathcal{V}</math>.


'''Abstract data structure:'''
'''Remarks:'''
A variation of [[Sets and sequences#Maps|map]], where:
# This abstract data structure may be viewed as a specific, quite restricted type of [[Sets and sequences#Maps|map]].
# The number of elements is bounded by some fixed positive [[Numbers#natural numbers|natural number]] <math>N</math>.
# The returned indexes have to be managed outside this data structure. For example, in [[Dijkstra]] and [[Prim]], it might be a good option to make the index a node attribute,
# 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 alone, 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 basically identical to the corresponding methods of normal maps. A key may be reused by the index handler object after removal.


'''Implementation invariant:'''
== Method ==
# 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 a [[Sets and sequences|set]] <math>U</math> (for unused) of [[Numbers#natural numbers|natural numbers]] of [[Numbers#natural numbers|natural numbers]].
# 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>.


== Methods ==
'''Name:''' reserve index


'''Input:''' A value <math>V\in\mathcal{V}</math>.


# The constructor takes <math>N</math> as an argument, creates <math>A</math> and <math>U</math>, and stores all potential keys <math>1,\ldots,N</math> in <math>U</math>.
'''Precondition:''' <math>|I|<N</math>.
# The method for inserting a new value extracts a key <math>i</math> from <math>U</math>, stores the value at <mah>A[i]</math>and returns <math>i</math>.
 
# The method to look up a value for a given key <math>i</math> returns <math>A[i]</math>.
'''Return value:''' One of the indexes not in <math>I</math>.
# The method to remove a key re-inserts the key in <math>U</math>.
 
'''Postcondition:''' The returned index is inserted in <math>I</math> and associated with <math>V</math>.
 
== Method ==
 
'''Name:''' release index
 
'''Input:''' An integral number <math>i</math>.
 
'''Precondition:''' <math>i\in I</math>.
 
'''Postcondition:''' <math>i</math> is extracted from <math>I</math>, and the associated value is dropped.
 
== Method ==
 
'''Name:''' get value
 
'''Input:''' An integral number <math>i</math>.
 
'''Precondition:''' <math>i\in I</math>.
 
'''Return value:''' The value currently associated with <math>i</math>.
 
== Method ==
 
'''Name:''' change value
 
'''Input:''' An integral number <math>i</math> and a value <math>V\in\mathcal{V}</math>.
 
'''Precondition:''' <math>i\in I</math>.
 
'''Postcondition:''' The value currently associated with <math>i</math> is overwritten by <math>V</math>.
 
== Known implementations ==
 
# [[Index handler with list of unused]]

Latest revision as of 11:02, 7 November 2014

Representation invariant

This abstract data structure is generic and parameterized by some value type [math]\displaystyle{ \mathcal{V} }[/math]. An object of an implementation of this abstract data structure is repesented by:

  1. A positive integral number [math]\displaystyle{ N }[/math].
  2. A subset [math]\displaystyle{ I }[/math] of [math]\displaystyle{ \{1,\ldots,N\} }[/math], the currently used indexes.
  3. A mapping [math]\displaystyle{ I\rightarrow\mathcal{V} }[/math].

Remarks:

  1. This abstract data structure may be viewed as a specific, quite restricted type of map.
  2. The returned indexes have to be managed outside this data structure. For example, in Dijkstra and Prim, it might be a good option to make the index a node attribute,

Method

Name: reserve index

Input: A value [math]\displaystyle{ V\in\mathcal{V} }[/math].

Precondition: [math]\displaystyle{ |I|\lt N }[/math].

Return value: One of the indexes not in [math]\displaystyle{ I }[/math].

Postcondition: The returned index is inserted in [math]\displaystyle{ I }[/math] and associated with [math]\displaystyle{ V }[/math].

Method

Name: release index

Input: An integral number [math]\displaystyle{ i }[/math].

Precondition: [math]\displaystyle{ i\in I }[/math].

Postcondition: [math]\displaystyle{ i }[/math] is extracted from [math]\displaystyle{ I }[/math], and the associated value is dropped.

Method

Name: get value

Input: An integral number [math]\displaystyle{ i }[/math].

Precondition: [math]\displaystyle{ i\in I }[/math].

Return value: The value currently associated with [math]\displaystyle{ i }[/math].

Method

Name: change value

Input: An integral number [math]\displaystyle{ i }[/math] and a value [math]\displaystyle{ V\in\mathcal{V} }[/math].

Precondition: [math]\displaystyle{ i\in I }[/math].

Postcondition: The value currently associated with [math]\displaystyle{ i }[/math] is overwritten by [math]\displaystyle{ V }[/math].

Known implementations

  1. Index handler with list of unused