Code

hash.go — the skeleton

Create hash.go. As since lesson 7 you get a skeleton: package, imports, types, signatures and contracts in the comments. The bodies are yours.

Worth noting before you start:

  • Put with a key that is already present must REPLACE the item, not add a second one. The test checks it; step 6 explains why it matters.
  • Every key comparison goes through c.Hit(). That count is the chain you walked — the one quantity the whole "average O(1)" rests on.
  • Grow BEFORE inserting, once the load factor has reached 1.0. Grow after and the factor briefly exceeds the bound — the test will notice.
  • grow recomputes hashKey % new size. The old bucket number is meaningless after doubling; you cannot move a chain across wholesale.
  • NoGrow exists only for the demonstration (-nogrow): it lets you see what happens without resizing.

ChainStats averages over the non-empty buckets only. Including the empty ones would make the mean equal the load factor and tell you nothing new; without them it tells you how long a chain actually is when a bucket holds anything at all.