-
Notifications
You must be signed in to change notification settings - Fork 115
implement Map::update #2041
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
base: main
Are you sure you want to change the base?
implement Map::update #2041
Conversation
Iter::group_by implementation could be simplified furtherCategory let _ = result.update(key, fn(arr) {
arr.unwrap_or([]) + [element]
}) Reasoning Unnecessary assignment to discard value in Map::update testsCategory map.update("a", fn(v) {...}) Reasoning Inefficient array copying in Map::update test with different value typesCategory Some(arr) => arr + [1] Reasoning |
Pull Request Test Coverage Report for Build 6545Details
💛 - Coveralls |
pub fn Map::update[K : Hash + Eq, V]( | ||
self : Map[K, V], | ||
key : K, | ||
f : (V?) -> V | ||
) -> V { | ||
let value = f(self.get(key)) | ||
self.set(key, value) | ||
value | ||
} | ||
|
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 goal was to be efficient, which is not the case with this implementation.
I think you can copy the code of set
and modify the line curr_entry.value = value
to curr_entry.value = f(Some(value))
and the let entry = { .., value, ..}
to let entry = { .., value : f(None), .. }
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.
Something like this:
if self.size >= self.growAt {
self.grow()
}
let hash = key.hash()
let (idx, psl) = for psl = 0, idx = hash & self.capacity_mask {
match self.entries[idx] {
None => break (idx, psl)
Some(curr_entry) => {
if curr_entry.hash == hash && curr_entry.key == key {
let new_value = f(Some(value))
curr_entry.value = new_value
return new_value
}
if psl > curr_entry.psl {
self.push_away(idx, curr_entry)
break (idx, psl)
}
continue psl + 1, (idx + 1) & self.capacity_mask
}
}
}
let new_value = f(None)
let entry = { prev: self.tail, next: None, psl, key, value: new_value, hash }
self.add_entry_to_tail(idx, entry)
return new_value
I also have some doubt about the function name and function signature. In Rust it might corresponds to However the return value of As of the name, I'm not quite sure whether it should be called |
|
Thank you @hackwaly . But our operation is more complex than that, so maybe we need a name like |
Fixes #1965
And improves Iter::group_by