Index mapping: Difference between revisions

Content deleted Content added
 
See also: and many other things
 
(36 intermediate revisions by 22 users not shown)
Line 1:
{{distinguish|text=[[Index map]], a finding aid in cartography}}
'''Index mapping''' is a [[computer science]] term used to describe the mapping of [[raw data]], used directly as in [[array index]], on an [[array data structure|array]].
'''Index mapping''' (or '''direct addressing''', or a '''trivial [[hash function]]''') in [[computer science]] describes using an [[array data structure|array]], in which each position corresponds to a key in the [[Universe (mathematics)|universe]] of possible values.<ref name=cormen>{{cite book|last1=Cormen|first1=Thomas H.|title=Introduction to algorithms|date=2009|publisher=MIT Press|___location=Cambridge, Mass.|isbn=9780262033848|pages=253–255|edition=3rd|url=https://mitpress.mit.edu/books/introduction-algorithms|accessdate=26 November 2015}}</ref>
The technique is most effective when the universe of keys is reasonably small, such that [[memory allocation|allocating]] an array with one position for every possible key is affordable.
Its effectiveness comes from the fact that an arbitrary position in an array can be examined in [[time complexity#Constant time|constant time]].
 
==Applicable arrays==
There are many practical examples of data whose valid values are restricted within a small range. A trivial hash function is a suitable choice when such data needs to act as a lookup key. Some examples include:
* [[month]] in the year (1–12)
* [[day]] in the month (1–31)
* [[day of the week]] (1–7)
* human age (0–130) – e.g. lifecover actuary tables, fixed-term mortgage
* [[ASCII]] characters (0–127), encompassing common mathematical operator symbols, digits, punctuation marks, and English language alphabet
 
==Examples==
Using a trivial hash function, in a non-iterative table lookup, can eliminate conditional testing and branching completely, reducing the [[instruction path length]] of a computer program.
The technique can be most effective for mapping data with a small range. If the array encompasses all combinations of input, a range check is not required.
 
===Avoid branching===
Roger Sayle gives an example<ref>{{cite journal|last1=Sayle|first1=Roger Anthony|title=A Superoptimizer Analysis of Multiway Branch Code Generation|journal=Proceedings of the GCC Developers' Summit|date=June 17, 2008|pages=103–116|url=https://www.nextmovesoftware.com/technology/SwitchOptimization.pdf|accessdate=26 November 2015}}</ref> of eliminating a [[multiway branch]] caused by a [[switch statement]]:
 
<syntaxhighlight lang="c++">
inline bool HasOnly30Days(int m)
{
switch (m) {
case 4: // April
case 6: // June
case 9: // September
case 11: // November
return true;
default:
return false;
}
}
</syntaxhighlight>
 
Which can be replaced with a table lookup:
 
<syntaxhighlight lang="c++">
===C example===
inline bool HasOnly30Days(int m)
This example<ref>[http://ols.fedoraproject.org/GCC/Reprints-2008/sayle-reprint.pdf "A Superoptimizer Analysis of Multiway Branch Code Generation"] by Roger Anthony Sayle</ref> of a C function - returning TRUE if a month (x) contains 30 days (otherwise FALSE), illustrates the concept succinctly
{
if ((unsigned)x > 11) return 0; /*x>12?*/
static const intbool T[12] = { 0, 0, 0,0, 1, 0, 1, 0, 0, 1, 0,1}; /* 0-based table 'if 30 days =1,else 0' */};
return T[m-1];
return T[x]; /* return with boolean 1 = true, 0=false */
}
</syntaxhighlight>
 
==See also==
*[[Multiway branch]]
*[[lookup table]]
==References==
{{Reflist}}
==External links==
*[http://ols.fedoraproject.org/GCC/Reprints-2008/sayle-reprint.pdf A Superoptimizer Analysis of Multiway Branch Code Generation] by Roger Anthony Sayle
 
[[Category:Arrays]]
[[Category:Associative arrays]]
[[Category:Articles with example C code]]
[[Category:Hashing]]
[[Category:Search algorithms]]