awelgorithms

Pair keys need a composite

Updated

A hash map has one key slot. Two facts (row, col) or (u, v) cannot be the key unless you wrap them: a nested map, a pair or tuple object, or a single encoded integer. Passing two arguments to get is not a pair key. That is two lookups.

What to focus on

Pick one composite and use it everywhere. Mixing nested maps for some pairs and tuples for others is two structures.

Worked examples

nested

a = [], store (4,1)→2 as outer 4 then inner 1
  1. two gets

Result: {4:{1:2}}

tuple key

a = [], store (4,1)→2 as one key
  1. one get of the pair

Result: {(4,1):2}

two ints fail

a = [], put 4 then put 1 hoping that means (4,1)
  1. those are two keys, values unrelated

Result: {4:?, 1:?} — not a pair

Try it

Write storePair(u, v, w). u and v are the pair components. w is the payload. Store (u,v)→w two legal ways: nested maps, and one tuple key. Do not store u and v as two unrelated keys. a = [], store (4,1)→2 as outer 4 then inner 1 → {4:{1:2}}. a = [], store (4,1)→2 as one key → {(4,1):2}. a = [], put 4 then put 1 hoping that means (4,1) → {4:?, 1:?} — not a pair.

pair components are integers. You store one payload per pair.