-
-
Notifications
You must be signed in to change notification settings - Fork 521
feat: add BiMap for maputil #329
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: rc
Are you sure you want to change the base?
Conversation
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.
Thank you Baiy for the nice feature! Some suggestions for more intuitive behaviour of Keys() and Values() in the review!
return m.normal[k] | ||
} | ||
|
||
// Keys returns a slice of query keys |
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.
Hi Bayi,
First of all, thank you for the nice and clean code. Also with the good thought on safety (locks).
Here a kind of reflection on your design and implementation for the Keys() and Values() function.
I wonder that these functions do NOT preserve the order of the returned keys corresponding to the order to which the values are given in the input. Is that true?
I see that the tests for the function for example, only test for the existence of each key in the returned set, but not to their order.
I suggest to have the order of the returned slice corresponding to the order of the input slice. This is much more intuitive and allows the caller to know for each key returned whose value it matches to.
Moreover: I also guess that, for this implementation, if you pass as input some value(s) (or key in the Values func) that are not in the BiMap, the return slice will contain only keys(values) for the found elements. This makes it impossible to know the correspondence between input and output values. That is, for which value(key) in the input, the returned key(value) is associated at the output. This would be true even if the order would be preserved -- because there is no indication of which elements are in the BiMap or not.
That all explained, I have two suggestions:
- Change the implementation, so that the order of the returned elements correspond to the order of the given elements
- Return an error if one of the input elements cannot be found in the BiMap as a value(in the case of the Keys func) or as a key (in the case of the Values func).
assert.Equal(false, biMap.ContainsValue(4)) | ||
|
||
assert.Equal(2, len(biMap.Keys(1, 2))) | ||
assert.Equal(true, slices.Contains(biMap.Keys(1, 2), "one")) |
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.
These tests do not include checking if order of returned slice corresponds to order of input slice.
The test in line 33 (Equal(2, len(biMap.Keys(1,2)) only covers the case when the given elements exist. Tests should also include what should be the behaviour when some of the input parameters are not in the BiMap -- should that issue an error (preferred), or return a subset (less optimal).
feat: add BiMap for maputil