-
Notifications
You must be signed in to change notification settings - Fork 72
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
futher simplify remove and shift_back operations in hashmap #491
Conversation
bobzhang
commented
Jun 2, 2024
- simplify the remove operation
- simplify shift_back
- tweak
WalkthroughThe recent updates to the Changes
Sequence Diagram(s) (Beta)sequenceDiagram
participant Client
participant HashMap
Client->>HashMap: remove(key)
loop Find the key
HashMap->>HashMap: Check each slot
alt Key found
HashMap->>HashMap: Remove key-value pair
HashMap->>HashMap: shift_back(start_index)
else Key not found
HashMap->>Client: Return
end
end
Client->>HashMap: grow()
HashMap->>HashMap: Calculate new_capacity
HashMap->>HashMap: Adjust capacity
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (invoked as PR comments)
Additionally, you can add CodeRabbit Configration File (
|
@@ -135,9 +135,7 @@ pub fn contains[K : Hash + Eq, V](self : HashMap[K, V], key : K) -> Bool { | |||
/// Remove a key-value pair from hash map. | |||
pub fn remove[K : Hash + Eq, V](self : HashMap[K, V], key : K) -> Unit { | |||
let hash = key.hash() | |||
for i = 0, idx = hash.land(self.capacity - 1) | |||
i < self.capacity |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i < self.capacity
check is not needed?
} | ||
} | ||
} | ||
|
||
fn shift_back[K : Hash, V](self : HashMap[K, V], start_index : Int) -> Unit { | ||
for i = 0, prev = start_index, curr = (start_index + 1).land( | ||
self.capacity - 1, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the loop state variable i
is not needed
Pull Request Test Coverage Report for Build 1568Details
💛 - Coveralls |
@coderabbitai review |
Actions performedReview triggered.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
The 50% load factor will result in half of the space being wasted, maybe a higher load factor is better? |
I am open to change the load factor later, rationale why I prefer 0.5 instead of higher values:
|