Now lets use our open addressing scheme in the setter for our HashMap
.
Instructions
Now that we have a hash function that uses the number of collisions to determine the hash code, we can update where we set a key in the event of a collision.
When we notice that the key we’re trying to set is different from the key at our hash code’s address, create a new variable called number_collisions
, set that equal to 1
.
After defining number_collisions
, create a new while
loop that checks if current_array_value[0] != key
.
In the while loop, you want to replicate our setting logic while incrementing the number of collisions.
Call .hash()
with both the key
and number_collisions
. Save that result into new_hash_code
.
Plug new_hash_code
into .compressor()
. Save that result into new_array_index
.
Check self.array
at new_array_index
and save the result as current_array_value
. Check against the three possibilities:
- If it’s
None
, save the[key, value]
atself.array[new_array_index]
and thenreturn
. - If it has a value, but the same key as
key
, overwrite the array at that address with[key, value]
and thenreturn
. - If it has a value, but a different key, increment
number_collisions
.