Repository navigation
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change
data_structures/hashing/hash_map.py::HashMapmishandles deleted slots in two ways._add_itemstored the value in the first free-looking slot on the probe path, and a deleted slot counts as free. If a colliding key had been deleted earlier, setting a key that still sits further along the path inserted a second copy instead of updating it. Onmaster:hm[0] = 1; hm[8] = 2; del hm[0]; hm[8] = 3giveslen(hm) == 2, and afterdel hm[8]the key is stillin hm._add_itemnow looks for the key along the path first (stopping at a never-used slot) and only then takes the first free slot.__delitem__raisedKeyErroronly when it reached a never-used slot. After enough inserts and deletes every bucket is either an item or a deleted marker, so the loop ended without finding the key,del hm[missing]silently succeeded, and the map could resize down. The loop now raisesKeyErrorwhen it has visited every bucket without a match.I added doctests for both; they fail on
master, and I kept them in this PR as the regression evidence for these two fixes. I compared the class with adictover random sets and deletes of keys 0..15 (600 sequences of up to 60 operations per seed, checking return values,lenand the items after every step): after the change there are no mismatches over four seeds, whilemastershowed wronglenand wrong delete results.ruff check,ruff format --checkandpython -m doctestpass.Written with the help of Claude Code (Claude Sonnet 5.5).
Checklist