-
Notifications
You must be signed in to change notification settings - Fork 141
add get_by_view method to Map
#2082
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
Conversation
Missing hash code collision handling in get_by_viewCategory Duplicated probing logic between get and get_by_viewCategory Test coverage could be improved for hash collisionsCategory |
Pull Request Test Coverage Report for Build 6660Details
💛 - Coveralls |
8b22d17 to
0a0400a
Compare
|
This is one of the case which show generic params of traits is useful. |
0a0400a to
c0a033e
Compare
|
this may be somthing easier to understand : Then we can build something safe on top of it, is it a premature optimization? |
Is this a user-facing API, or just an internal helper? In terms of implementation, |
c0a033e to
b733ea7
Compare
|
How about: pub fn[K, View : Hash, Value] Map::get_by(
self : Map[K, Value],
key : View,
equal : (K, View) -> Bool
) -> Value? {
...
} |
peter-jerry-ye
left a comment
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.
How about a
pub fn[K, Value] Map::get_by(self : Map[K, Value], hash : Int, is_ : (K) -> Bool) -> Value? {
...
}
/// The `view` function and the `Hash` implementation of `View` should satisfy:
/// ```
/// view(key).hash() == key.hash()
/// ```
fn get_by_view[K, View : Hash + Eq, Value](
self : Self[K, Value],
key : View,
view~ : (K) -> View
) -> Value?It is hard to reason about if the variant is met or not. for String, we added an optimization that |
This PR adds a new method
get_by_viewtoMap. The motivation is the need for querying aMap[String, V]with@string.View. Converting the view toStringfirst requires a copy, storing@string.Viewdirectly in the map may result in memory leak. So a new API is necessary for this case.The signature of the new API is as follows:
The map will be searched with the hash code of
key. When comparing equality, existing keys in the map will be first converted toViewusingview.Note that for the
@string.Viewcase, a specialized method may be adequate. But there are other cases such as@bytes.View. So a generic implementation is used here. Theviewfunction will only be called when comparing keys with identical hash code, so it will not be invoked a lot. Therefore even ifget_by_viewis not inlined, the indirect call cost ofviewshould be acceptable (since usingget_by_viewimplies that the key is probably a large type with non-trivial comparison, such asString)