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

futher simplify remove and shift_back operations in hashmap #491

Merged
merged 3 commits into from
Jun 3, 2024

Conversation

bobzhang
Copy link
Contributor

@bobzhang bobzhang commented Jun 2, 2024

  • simplify the remove operation
  • simplify shift_back
  • tweak

Copy link
Contributor

coderabbitai bot commented Jun 2, 2024

Walkthrough

The recent updates to the HashMap module involve refactoring the remove and shift_back functions to streamline their loop structures using break and continue statements. Additionally, the grow function has been optimized for better capacity adjustment by introducing a new variable for a more concise capacity calculation. These changes aim to enhance the efficiency and readability of the code.

Changes

File Change Summary
hashmap/hashmap.mbt Simplified loop structures in the remove and shift_back functions; Optimized capacity adjustment in the grow function.

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
Loading

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?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@bobzhang bobzhang changed the title hongbo/size enforce futher simplify remove and shift_back operations in hashmap Jun 2, 2024
@bobzhang bobzhang requested a review from yj-qin June 2, 2024 09:40
@@ -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
Copy link
Contributor Author

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,
Copy link
Contributor Author

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

@coveralls
Copy link
Collaborator

Pull Request Test Coverage Report for Build 1568

Details

  • 2 of 3 (66.67%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage remained the same at 89.094%

Changes Missing Coverage Covered Lines Changed/Added Lines %
hashmap/hashmap.mbt 2 3 66.67%
Totals Coverage Status
Change from base Build 1567: 0.0%
Covered Lines: 2557
Relevant Lines: 2870

💛 - Coveralls

@bobzhang
Copy link
Contributor Author

bobzhang commented Jun 2, 2024

@coderabbitai review

Copy link
Contributor

coderabbitai bot commented Jun 2, 2024

Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

Copy link
Contributor

@coderabbitai coderabbitai bot left a 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

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 87eebd3 and e23a200.

Files selected for processing (1)
  • hashmap/hashmap.mbt (2 hunks)
Files not reviewed due to errors (1)
  • hashmap/hashmap.mbt (no review received)

@yj-qin
Copy link
Collaborator

yj-qin commented Jun 2, 2024

The 50% load factor will result in half of the space being wasted, maybe a higher load factor is better?

@bobzhang
Copy link
Contributor Author

bobzhang commented Jun 3, 2024

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:

  1. when people use hashtbl, they want the optimal performance, so it is okay to exchange space for speed
  2. the wasted the space is not that bad, since the array only holds a pointer(not the full content)

@bobzhang bobzhang merged commit efb0b5e into main Jun 3, 2024
8 checks passed
@bobzhang bobzhang deleted the hongbo/size_enforce branch June 3, 2024 01:16
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.

3 participants