-
Notifications
You must be signed in to change notification settings - Fork 121
Add set operations to @immut/hash{map, set}
and @internal/sparse_array
Summary
#2145
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
Open
Asterless
wants to merge
23
commits into
moonbitlang:main
Choose a base branch
from
Asterless:feature/20250522_HAMT
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+382
−0
Conversation
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
…ence methods, now they can handle branchs
Potential performance issue in hashset intersection implementationCategory Branch(sa1), Branch(sa2)) => {
let res = sa1.intersection(sa2, fn(m1, m2) { m1.intersection(m2) })
match res.size() {
0 => Empty
_ => Branch(res)
}
}
**Reasoning**
The current implementation creates a Branch node and then checks if it's empty, potentially wasting memory. By checking size first, we can return Empty directly without allocating unnecessary Branch node.
</details>
<details>
<summary> Missing documentation for important parameter in union_with </summary>
**Category**
Maintainability
**Code Snippet**
pub fn[K : Eq + Hash, V] union_with(
self : T[K, V],
other : T[K, V],
f : (K, V, V) -> V
) -> T[K, V]
**Recommendation**
Add detailed documentation for the union function parameter:
```moonbit
///|
/// Union two hashmaps with a function
/// @param f Function to resolve conflicts, receives (key, value1, value2) and returns merged value
/// @return New hashmap containing all keys with values combined using f for duplicates
pub fn[K : Eq + Hash, V] union_with(...)
**Reasoning**
The function parameter f is critical for understanding how value conflicts are resolved, but lacks documentation about its expected behavior and contract. Clear documentation helps users implement correct combining functions.
</details>
<details>
<summary> Inefficient handling of empty cases in difference operation </summary>
**Category**
Performance
**Code Snippet**
pub fn[K : Eq + Hash] difference(self : T[K], other : T[K]) -> T[K] {
match (self, other) {
(Empty, _) => Empty
(_, Empty) => self
**Recommendation**
Add more specific pattern matching for efficiency:
```moonbit
match (self, other) {
(Empty, _) => Empty
(s, Empty) => s.clone()
(Leaf(k), Leaf(k2)) when k == k2 => Empty
(Leaf(k), Leaf(_)) => self
**Reasoning**
Adding more specific pattern matching for common cases like identical single elements can avoid unnecessary iteration and improve performance for simple cases. The clone() is explicit about creating a new copy.
</details> |
Pull Request Test Coverage Report for Build 6913Details
💛 - Coveralls |
…ence methods, now they can handle branchs
44c0a79
to
eb6ef82
Compare
Add set operations to @immut/hash{map, set} and @internal/sparse_array Summary and re-fmt
Closed
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.
Related Issue
Fixes #1830: Efficient set operations for @immut/hash{map, set}
Changes
@immut/hashmap
union_with(f: (K, V, V) => V)
Merges two hashmaps, resolving key conflicts with a custom function
f
.intersection()
Returns a new hashmap containing keys present in both input maps, with values from the first map.
intersection_with(f: (K, V, V) => V)
Computes intersection, resolving overlapping keys' values with function
f
.difference()
Returns entries present in the first map but not in the second.
@immut/hashset
intersection()
Returns a new set containing elements common to both input sets.
difference()
Returns elements present in the first set but not in the second.
@internal/sparse_array
intersection()
Computes index-wise intersection of two sparse arrays.
difference()
Computes index-wise difference between two sparse arrays.
Motivation
These changes provide a more complete and consistent set of set operations for immutable collections, making it easier to perform common set algebra tasks and improving API parity across collection types.
Tests
Added and updated unit tests for all new and modified methods to ensure correctness and expected behavior.
Checklist
All new and existing tests pass
Code is formatted and documented where appropriate