Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved HashMap.hashCode() implementation inspired by Clojure #67

Merged

Conversation

fmw
Copy link
Contributor

@fmw fmw commented Nov 2, 2022

There are two important changes:

  • use bitwise XOR instead of plus operator to combine the hashcode of the key and value, which intertwines them and prevents collisions on hashmaps with values swapped between keys,
  • cache the hash code internally, to avoid unnecessary recalculations.

I've also added a test that verifies the hashcode doesn't take the order in which the kv-pairs are supplied into account.

@fmw
Copy link
Contributor Author

fmw commented Nov 2, 2022

(the force push was to fix a typo in the test it description).

Copy link
Owner

@emmanueltouzery emmanueltouzery left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just tiny things, but when we clear this up, no problem to merge this!

src/HashMap.ts Outdated Show resolved Hide resolved
src/HashMap.ts Outdated Show resolved Hide resolved
src/HashMap.ts Outdated Show resolved Hide resolved
@fmw fmw force-pushed the prevent-hashcode-collision branch from 6767361 to 0396608 Compare November 4, 2022 11:24
@emmanueltouzery emmanueltouzery merged commit af4e55d into emmanueltouzery:master Nov 4, 2022
@emmanueltouzery
Copy link
Owner

thanks a lot! i'll try to release this ASAP

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants