Theory

Six titles, two tables

There is no code here. What you have here is what a hash table does — step by step, on the same six titles:

Dune   Emma   Ulysses   Hamlet   Beloved   Lolita

You write nothing in this lesson yet. Read the tables top to bottom and check at least one row with a pencil.

How we count

Every insertion (Put) counts comparisons: how many keys you looked at while walking a chain. In code that is c.Hit(). An empty bucket costs zero.

The bucket number comes from two parts: the hash function turns the title into a big number, and the remainder of that number divided by the bucket count is the bucket:

hashKey("Dune") = 5153105828920738339
5153105828920738339 % 4 = 3     →  "Dune" goes into bucket 3

We are not looking inside the function yet — all that matters is that the same title always gives the same number, and different titles almost always give different ones.

The same six titles in tables of different sizes:

              4 buckets   2 buckets   8 buckets
Dune              3           1           3
Emma              1           1           5
Ulysses           1           1           1
Hamlet            2           0           2
Beloved           0           0           4
Lolita            0           0           4

Notice: the bucket number depends on the size of the table. Emma is 1 in a 4-bucket table and 5 in an 8-bucket one. Remember that when the table grows.

First table: 4 buckets, no growing

Seven Puts — one of them repeats a title (Dune a second time). Only buckets with something in them are shown; a chain is written front first.

                    bucket contents                             cmps
start               (all empty)
put Dune            [3] Dune                                      0
put Emma            [1] Emma  [3] Dune                            0
put Ulysses         [1] Ulysses Emma  [3] Dune                    1
put Hamlet          [1] Ulysses Emma  [2] Hamlet  [3] Dune        0
put Dune (again)    nothing changed                               1
put Beloved         [0] Beloved  + the earlier ones               0
put Lolita          [0] Lolita Beloved  [1] Ulysses Emma
                    [2] Hamlet  [3] Dune                          1
                                                        total:    3

And the lookups in this table — one Get per title:

Get Dune        1 comparison      (bucket 3: Dune)
Get Emma        2                 (bucket 1: Ulysses → Emma)
Get Ulysses     1
Get Hamlet      1
Get Beloved     2                 (bucket 0: Lolita → Beloved)
Get Lolita      1
                         total:   8   (1.33 on average)

Second table: start at 2 buckets, the table may grow

The same seven Puts, but now a table that has reached full load (items = buckets) doubles before it stores a new key:

                buckets   bucket contents                         cmps
put Dune           2      [1] Dune                                  0
put Emma           2      [1] Emma Dune                             1
put Ulysses        2→4    full (2 of 2) — GROW, then insert
                          [1] Ulysses Emma  [3] Dune                2
put Hamlet         4      [1] Ulysses Emma  [2] Hamlet  [3] Dune    0
put Dune (again)   4      nothing changed                           1
put Beloved        4→8    full (4 of 4) — GROW, then insert
                          [1] Ulysses  [2] Hamlet  [3] Dune
                          [4] Beloved  [5] Emma                     0
put Lolita         8      [4] Lolita Beloved  + the earlier ones    1

At the end: 6 keys, 8 buckets, 2 resizes, load 6 ÷ 8 = 0.75. The longest chain is 2, and 3 buckets are empty.

The same six lookups cost 7 comparisons (8 in the first table): only Beloved still stands second in its chain, and Emma is alone now.

Three observations

Do not skip them — the following steps are built from nothing else.

1. A collision breaks nothing — it costs one comparison. Ulysses and Emma landed in the same bucket. Nobody made a mistake: the chain is simply longer, and Emma costs two comparisons instead of one. That is why the counter in this lesson measures chain length — it is the entire price of "O(1)".

2. A repeated key is not a collision. Dune the second time spent one comparison (it found itself) but added nothing: 6 keys remain, no chain got longer, and the table did not grow because of it. The entry was replaced.

3. Growing regroups the keys. Ulysses and Emma shared a bucket in the 4-bucket table. In the 8-bucket one they are in different buckets (1 and 5). That is not luck: the divisor changes, so the remainder changes. That is why growing must work out every key's bucket again — the old bucket number means nothing in the new table.

What next

Now you know what Put and Get do, and what growing does. In the next step you write Put — the most important method — as Go code: the loop is given, two places are missing.