CS 70

Phase 6: Rehashing

To meet our complexity guarantees when people don't know the size of their hash table ahead of time, we need to be able to rehash the hash table, creating a new, different-sized, underlying array of buckets and moving our items into that array.


Handy reference material:

Let's Go!

  • RHS Cow speaking

    There are some good tips in the Hints and Tips section; check them out before you start!

Following our usual development process, implement the following functions:

  • loadFactor
  • maxLoadFactor
  • rehash
  • reallocations

Also: Adjust the code for insert so that if an insert would cause the load factor to exceed the maximum, the rehash function is called automatically.

Planning Image

Add a planning image, Phase_6.jpg, to the Planning folder.

Hints and Tips

Writing loadFactor

This function should return the current load factor of the table (i.e., the number of keys divided by the number of buckets).

Note that in this definition, the numerator in the division is the number of keys, NOT the size of the HashMultiset! The specifications say that size returns the total count, but a specific key having a count of 1,000,000 versus a count of 1 has no impact on the load factor, because either way the table only contains one KeyCount item for that key. If you get stuck on this part, remember that there is no cost to adding more private member variables (as long as you remember to keep them updated) 😉

The load factor of an empty table should be 0.

Careful! The load factor is a double value but dividing one integer by another will result in an integer value!

Writing rehash

Probably the easiest way to write rehash is to

  • Create a new (empty) HashMultiset with the desired properties.
  • Go through each bucket in the original hash table, taking each KeyCount in the bucket and directly inserting it into the new table using a special private helper function. If you went through the Helpful Hints in the previous parts, perhaps you already have such a helper function!
  • Finish up by swapping the new hash set and the old one.
  • The old HashMultiset will get cleaned up automatically when the variable goes away.

Testing

Because you can explicitly call rehash and explicitly set the maximum load factor, either in the constructor or via maxLoadFactor, you can write tests that will work on any correct HashMultiset implementation.

To Complete This Part of the Assignment…

You'll know you're done with this part of the assignment when you've done all of the following:

(When logged in, completion status appears here.)