Jump to content

Index mapping

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by ChrisGualtieri (talk | contribs) at 01:46, 8 August 2012 (Applicable arrays: Typo fixing, typos fixed: eg. → e.g. using AWB). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Index mapping is a computer science term (also known as a "trivial hash function") that is used to describe the mapping[clarification needed] of raw data, used directly as in array index, for an array. The technique can be most effective for mapping data with a small range[clarification needed]. If the array encompasses all combinations of input, a range check is not required.

Applicable arrays

In practice there are many examples of data exhibiting a small range of valid values all of which are suitable for processing[clarification needed] using a trivial hash function including:

Examples

The following two examples demonstrate how a simple non-iterative table lookup, using a trivial hash function, can eliminate conditional testing & branching completely thereby reducing instruction path length significantly. Although both examples are shown here as functions, the required code would be better inlined to avoid function call overhead in view of their obvious simplicity.

C example 1

This example[1] 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 int 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[x];                                       /* return with boolean 1 = true, 0=false */

C example 2

Example of another C function – incrementing a month number (x) by 1 and automatically resetting if greater than 12

                                            
 static const int M[12] ={2,3,4,5,6,7,8,9,10,11,12,1}; /* 1-based table to increment x          */
 return M[x];                                          /* return with new month number          */

See also

References