Index Mapping

Index mapping (or direct addressing, or a trivial hash function) in computer science describes using an array, in which each position corresponds to a key in the universe of possible values.

The technique is most effective when the universe of keys is reasonably small, such that 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 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.

Avoid branching

Roger Sayle gives an example of eliminating a multiway branch caused by a switch statement:

inline bool HasOnly30Days(int m) { switch (m) { case 4:  // April case 6:  // June case 9:  // September case 11: // November return true; default: return false; } } 

Which can be replaced with a table lookup:

inline bool HasOnly30Days(int m) { static const bool T[] = { 0, 0, 0, 1, 0, 1, 0, 0, 1, 0, 1, 0 }; return T[m-1]; } 

See also

  • Associative array
  • Hash table
  • Lookup table

References

Tags:

Index Mapping Applicable arraysIndex Mapping ExamplesIndex MappingArray data structureComputer scienceHash functionMemory allocationTime complexityUniverse (mathematics)

🔥 Trending searches on Wiki English:

Franklin D. RooseveltLeBron JamesWarwick DavisRodrygoList of Indian Premier League seasons and resultsSarita ChoudhuryAssyrian peopleWikipediaGrimesVictoria BeckhamBarack ObamaPolyphemusRudolf HössOlivia MunnLeslie UggamsCaroline CelicoGeorge IIIGene TierneyDomantas SabonisMia KhalifaList of countries by GDP (nominal) per capitaGoogle TranslateAtlético MadridSandeep WarrierThe BeatlesTaylor SwiftTed BundyMegan FoxStephen CurryAlex PereiraEuropeRyan ReynoldsWWEIvanka TrumpArab al-AramsheAavesham (2024 film)Manchester United F.C.Fallout 32024 United States presidential electionChris KyleIsidor StrausJack GrealishCraig Conway (actor)Lockheed Martin F-35 Lightning IIFallout 2Alexander VolkanovskiChinaNelson MandelaAna de ArmasMarcel SabitzerHeartbreak HighBen CousinsAlia BhattJackie RobinsonReal Madrid CFHamasMillie Bobby BrownMillennialsChaturbate2023–24 NBA seasonOusmane DembéléWolfgang Amadeus MozartSalman RushdiePremaluSagrada FamíliaDune (2021 film)Barbara O'NeillGolden State WarriorsSkibidi ToiletKylian MbappéLawrence WongAbigail (2024 film)Poor Things (film)Battle of GettysburgSplit (2016 American film)Opinion polling for the 2024 Indian general electionThe SympathizerKilling of Lacey Fletcher🡆 More