-
Notifications
You must be signed in to change notification settings - Fork 155
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 binary_search and friends #282
Conversation
I would, yeah, at least for
That's an interesting point! I wonder if small enough maps/sets might still win by binary search, but if you're too small then even a linear search might be fastest. But anyway, if someone really wants it they can use
We haven't been great about that, but let's at least add sanity checks to |
I will work on it. I have PackagingCon for then next few days a may not have much time. |
If I had been more awake you would have gone shorter tests. :-P I was not up to determining interesting test cases. So instead you got tests from STD copied in and adapted. They may be more thorough than you asked for. |
Same vibe as "If I had more time, I would have written a shorter letter." :) It's a lot, but that's fine. |
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.
Thanks - the implementation looks good, just need some cleanup in the docs and a few comments.
Actually, there's still a gap there because binary search also finds the insertion point for missing items. So I think it's good to have the doc comment about scalability, but we should still want the map/set methods for the I'll go ahead and add this myself while I'm thinking about it... |
While `IndexMap::binary_search_keys` and `IndexSet::binary_search` don't scale as well as their corresponding `get_index_of`, O(log n) vs. O(1), they do still offer the benefit of returning an `Err` insertion point.
Thanks again! I'll put together a release soon. |
FYI, I did publish this as 2.1.0 the other day. I'd be interested to see where you use this, if that code is open. |
It is for https://github.com/pubgrub-rs/pubgrub, although the particular performance experiments where this would be useful have not yet pulled their weight. Many data structures have to "backtrack" to reset their state as of some previous point in time. For each individual data structure there are many ways this can be achieved.
The particular example of this pattern that inspired the PR was |
This is a start on fixing #281.
I copied the implementation of these methods from std, except for
binary_search_by
where I used std directly. Alternatively we could have all implementations directly forward to.as_slice().foobar
or.entries.foobar
, if you would prefer that.I left off the plane
binary_search{_keys}
on set and map, because they already provide aO(1)
way to look up the index for a key. I left them in onSlice
because they do not.For documentation I adapted the first sentence of std, and provided a link to std.
how would you like to see these tested?