liveBook · Manning

Direct-access table

As explained in Grokking Data Structures

A direct-access table is an array-based data structure that stores each element at an array position determined directly by its key or associated index.

Imagine storing baseball cards in an array slot named by each card’s key. A direct-access table places an element with key k directly at array index k, rather than packing elements in insertion order. Empty positions contain None or another marker, so the table can distinguish unused cells from stored elements. section 12.10 section 12.22

The ideal operation sequence is simple:

  • Extract or compute an element’s key, use that key as the array index, and read, insert, or delete the element in constant time once the index is known. section 12.10 section 12.11
  • Treat indexing cost separately: converting a string identifier to an integer may take O(k) time, though bounded key lengths can make this effectively constant. section 12.11
  • Use direct access only when the key range is constrained; otherwise, most array cells remain empty and memory usage becomes impractical. section 12.12

Joe Di Maggio’s card

Suppose an indexing function assigns Joe Di Maggio’s card the index 3. Store the card in the array’s fourth cell, at position 3, and retrieve it later from that same position. A string identifier may first be converted into an integer by processing its characters, such as with a base-256 calculation. section 12.10 section 12.11

Hash tables address the main limitation by mapping a large key range into a smaller array, accepting collisions that require techniques such as chaining or open addressing. Direct-access tables avoid those collisions but may require an array as large as the entire key space. section 12.22 section 12.13

How Grokking Data Structures covers it

How deep it goes
Explains storage, empty-cell markers, key extraction, indexing cost, and memory limitations, then contrasts hash tables.
Examples
Uses baseball cards, Joe Di Maggio’s card, sparse arrays, and base-256 conversion of string identifiers.
What you can do after
Evaluate when direct indexing provides constant-time operations and when its key-space memory cost makes hashing preferable.
How it connects
Connects direct-access tables to dictionaries, arrays, indexing functions, hash tables, collisions, chaining, and open addressing.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Data Structures. The section links point to the parts of the book it draws on.