-
Notifications
You must be signed in to change notification settings - Fork 106
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
Make Set.choose return the fastest available element #751
Conversation
The specification says that |
we need a new function for what rixed asked. I think it could be called any, and we should say |
Would it be interesting to also return "the rest" of the tree, or have a function for that? (If I want to recursively traverse the elements in no particular order, I would first start on the root, then look at the left and right subtrees.) Or can we convince ourselves that calling |
Your suggestion of also returning the rest of the tree makes for a more generic functionality. |
Indeed. The root would not be equal (empty + 0 + 1 will have 0 at its root while empty + 1 + 0 will have 1):
Adding a test for that, and implemented |
Agreed with @gasche , a function |
Updated the commit to implement the |
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.
There should be a test for any.
But we have some pop_ functions already, they have a different semantic: an (element, set) pair are returned... |
The documentation should be clearer than "non-deterministic". You should have a full sentence pointing out that sets that have the same elements could still return different values. (This does not mean that calling the same function several time on the same value may give different result, which is what the current wording suggests.). As a minor detail, I would rather use I would also be interested in having a similar function implemented in Map, returning a key/value pair: there is no reason not to have the same interface for both. Finally, I think there remain the question of whether we want to return an |
I personally would like both. |
Updated the doc and rebased.
Why isn't |
Map.choose already is O(1) but adding any as an alias for the sake of completeness. |
Thanks for noticing this! If I understand correctly, the current definition of |
Indeed, ocaml's stdlib uses |
It may look like this is never-ending, but I think we are almost done!
|
Choose must return equal elements for equal sets/maps/splay-maps. Therefore we need another function to return (in constant time) any element from a container with no such constraint.
Did all this, added a couple more tests and a Changelog entry. |
Merged, thanks! Thanks to @UnixJunkie and @c-cube for their feedback as well. |
I've always assumed Set.choose was returning the quickest element available and
that it had complexity O(1). Instead, I just discovered that it's actually
looking for the smallest element, for no good reason that I can see. A comment
from @thelema suggests that he also expected that: "I'd rather this chose the
root, but okay".
Maybe there were some good reasons at the time not to return the root? In case
those reasons do not apply any more this patch makes it return the root.