Skip to content

Conversation

manuelrojas19
Copy link
Contributor

@manuelrojas19 manuelrojas19 commented Jul 10, 2024

As I was going through the current implementation of HashMap in Go, I noticed that the following improvements could be made:

Enhanced Collision Handling:

  • Modified the Put method to handle collisions using linked lists (chaining).
  • Ensured that new key-value pairs are correctly inserted even when collisions occur.

Resizing Logic

  • Refactored the resize method to correctly rehash and relocate all nodes.
  • The hash map now doubles its capacity when the load factor exceeds 0.75, ensuring efficient operations.

Refactored Node Retrieval:

  • Implemented getNodeByKey to traverse the linked list at a given hash index to find the node with the matching key.
  • Updated the Get method to use getNodeByKey for retrieving values.

Code Cleanup:

  • Removed redundant functions (newNode and newNodeWithNext).
  • Added comments.

Miscellaneous:

  • Corrected the Make function to return a pointer to the HashMap.
  • Improved hashing function for better distribution of keys.

This PR ensures that the HashMap correctly handles collisions, dynamically resizes based on the load factor, and maintains efficient access and modification operations.

@codecov-commenter
Copy link

codecov-commenter commented Jul 17, 2024

Codecov Report

Attention: Patch coverage is 94.11765% with 2 lines in your changes missing coverage. Please review.

Project coverage is 87.50%. Comparing base (483431b) to head (195428a).

Files Patch % Lines
structure/hashmap/hashmap.go 94.11% 2 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##           master     #729   +/-   ##
=======================================
  Coverage   87.49%   87.50%           
=======================================
  Files         205      205           
  Lines        5439     5434    -5     
=======================================
- Hits         4759     4755    -4     
  Misses        539      539           
+ Partials      141      140    -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@raklaptudirm raklaptudirm merged commit ee6fef2 into TheAlgorithms:master Jul 18, 2024
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.

4 participants