-
Notifications
You must be signed in to change notification settings - Fork 22
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
Add an Update method to update an existing tag #24
Conversation
Pull Request Test Coverage Report for Build 2905847624Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
The detected lint issue is incorrect. The |
6efe903
to
18ace47
Compare
My bad, I always thought Go would panic when looking up a non-existing key. That's not true! |
I'll update the PR shortly with an updated and more generic approach. |
template/tree_v4.go
Outdated
if t.nodes[nodeIndex].TagCount == 0 { | ||
t.nodes[nodeIndex].TagCount = 1 | ||
} else { | ||
ret = false | ||
} | ||
t.tags[(uint64(nodeIndex) << 32)] = tag | ||
t.tags[(uint64(nodeIndex) << 32)] = updateFunc(t.tags[(uint64(nodeIndex) << 32)]) |
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.
This part is incorrect as the existing tag could be random when TagCount was 0. That's one reason I need to work a bit more on this PR.
This is useful to avoid a double lookup: once to find the current value, the second time to update its value. The tag could be a counter we want to update or, with the generics tree, any struct/map. `SetOrUpdate()` is the same as `Set()`, but when there already is a tag, it is passed to the provided update function instead of being replaced by the provided tag value. If no match, the provided tag value is used instead. `AddOrUpdate()` is the same as `Add()` and works like `Set()` except it will append a tag (when no match) or modify the first matching tag (when there is a match). Both functions take a tag value which is used when the existing tag does not exist (no tag for `SetOrUpdate()` or no matching tag for `AddOrUpdate()`). Both functions take an update function which is used when there is an existing tag and the update function is applied on the existing tag to get another tag. The provided tag is therefore ignored in this case.
18ace47
to
8115a48
Compare
Here is the second tentative. The differences:
The |
This is useful to avoid a double lookup: once to find the current
value, the second time to update its value. The tag could be a counter
we want to update or, with the generics tree, any struct/map.
The code change is minimal but
Set()
could be a bit smaller since itreuses the same code path. However, I believe the compiler should be
smart enough to turn the constant function into a single value.