Put — fill in two holes
In the last step you watched what Put does: it works out the bucket, walks its
chain, and if the key is not there, stores a new entry. Now the same thing is
written down as an algorithm — and you fill in two missing pieces.
The hash function — given
Before Put, one function you do not have to write but should read. FNV-1a is
one of the simplest good ones: one xor and one multiply per byte.
hashKey(s):
h ← 14695981039346656037 // offset basis
for each byte b in s:
h ← h XOR b
h ← h * 1099511628211 // prime
return h
The bucket number is hashKey(title) % len(buckets), as in the last step.
It works on BYTES, and for a hash table that is correct. Two identical strings have identical bytes whatever alphabet they are written in; different ones almost certainly have different bytes. The function does not need to know what a letter is. In step 8 you will meet a structure for which bytes are not enough — and why.
Put in pseudocode
Put(item):
i ← hashKey(item.Title) % len(buckets)
for each e from buckets[i] along the chain:
c.Hit()
if e.key == item.Title:
e.item ← item
return // replaced, added nothing
buckets[i] ← new entry { key: item.Title, item: item, next: buckets[i] }
n ← n + 1
That is exactly what you saw in the table: walking the chain costs one
comparison per entry, finding the same key replaces the entry, and a new key
is linked in at the front — which is why Ulysses sits before Emma in the
table. The front, because then there is no need to look for the end of the chain.
An equal key is not a collision. Easy to confuse, so straight away:
- collision — different keys land in the same bucket. Unavoidable, handled by the chain;
- equal key — the same title stored a second time. That is not a collision;
Putmust replace the existing entry.
In lesson 1 you wrote gen -dup-rate. It produces equal keys, not
collisions — and step 7 shows what it actually does to the index.
There is another way to handle collisions — open addressing: instead of keeping a chain, look for another free bucket. You will meet it in drill 3; here we build chains.
Your job
Create hash.go and hash_test.go (see the panel).
hash.go is complete and compiles, but it does not work: the types, the
constructor, hashKey and Get are written, and Put has the bucket index and
the walk along the chain — but two places are missing. They are marked HOLE 1
and HOLE 2:
- HOLE 1 — the condition "this entry already has the same key", and the
replacement itself. In the pseudocode that is
if e.key == item.Title: e.item ← item, return. Replacefalsewith a real condition and add the replacement before thereturn. - HOLE 2 — linking the new entry in at the front of the chain, and counting
it (
n ← n + 1).
Nothing else needs changing. Get is already there — it walks a chain exactly
the way your Put will, and it is your model.
hash_test.go is the verdict — you do not write it, you pass it. For now it
checks only what exists: storing and finding.
The test also checks the first table from the last step: 4 buckets, no growing,
seven Puts. It demands the same comparisons (0 0 1 0 1 0 1), the same chains
(Lolita Beloved, Ulysses Emma, Hamlet, Dune) and the same lookup costs.
If your code counts differently, something is off even if everything is found.
Run the test before you fill anything in
Run it as it stands first:
$ go test ./...
--- FAIL: TestHashIndexStoresAndFinds (0.00s)
hash_test.go:17: stored 0, want 1000
--- FAIL: TestDuplicateKeysReplaceRatherThanAccumulate (0.00s)
hash_test.go:39: stored 0 entries for one key, want 1
hash_test.go:43: got ID 0, want 3 — the last Put should win
--- FAIL: TestFixedTableReproducesTheLessonTrace (0.00s)
hash_test.go:68: Put 3 ("Ulysses"): 0 comparisons, want 1
hash_test.go:68: Put 5 ("Dune"): 0 comparisons, want 1
hash_test.go:68: Put 7 ("Lolita"): 0 comparisons, want 1
hash_test.go:72: stored 0 keys, want 6 (one of the seven puts was a duplicate)
hash_test.go:84: bucket 0 holds [], want [Lolita Beloved]
FAIL
That is not an error — it is the starting point. The skeleton stores nothing:
stored 0, the chains are empty, so there is nothing to compare.
Fill in both holes and run it again — you should see ok.
When it does not pass
got ID 1, want 3 — the last Put should win— you found the same key and stopped, but did not replace the item. Thereturnneedse.item = itbefore it.stored 3 entries for one key, want 1(andbucket 3 holds [Dune Dune]) — the first hole was left asfalse: a duplicate was treated as a new key.stored 0, want 1000— you linked the entry but did not count it. After linking you needh.n++.Get("00000"): ok=falseandbucket 0 holds [Lolita], want [Lolita Beloved]— you made the new entry but left itsnextempty. The new entry must point at the chain's previous head (next: h.buckets[i]), otherwise you lose everything that was already in the bucket.