awelgorithms

Handle an empty hash map

Updated

Empty is size 0, no keys, get and delete-of-a-key miss, contains-key is false. Put onto empty is legal and produces a one-entry map.

What to focus on

Before get, ask: is the map empty or is this key absent? Empty means the table has no keys, not that some reserved key stands for empty. Do not return 0 for a miss unless that is your chosen default.

Worked examples

no get

a = []
  1. size 0
  2. do not invent a value

Result: get any key misses

put onto empty

a = [], put 4→2
  1. the new map is {4:2}

Result: {4:2}

zero is a value

a = [], put 4→0
  1. 0 is data, not empty

Result: {4:0} · size 1

Try it

Write get(m, k) and put(m, k, v). m is the hash map. k is the key. v is the value. On {} get misses and contains-key is false. Put onto {} is legal. 0 is a stored value, not empty. a = [] → get any key misses. a = [], put 4→2 → {4:2}. a = [], put 4→0 → {4:0} · size 1.

keys and values are integers. The map starts as {}. Put onto empty is in scope.