Skip to content
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 higher/lower search functions in gb_trees and gb_sets #7745

Merged
merged 1 commit into from
Nov 22, 2023

Conversation

zzydxm
Copy link
Contributor

@zzydxm zzydxm commented Oct 11, 2023

Provide some common operations that is available for BSTs.

API follows java TreeSet / TreeMap

@github-actions
Copy link
Contributor

github-actions bot commented Oct 11, 2023

CT Test Results

       2 files       92 suites   35m 17s ⏱️
2 002 tests 1 954 ✔️ 48 💤 0
2 306 runs  2 256 ✔️ 50 💤 0

Results for commit 1595acf.

♻️ This comment has been updated with latest results.

To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.

See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.

Artifacts

// Erlang/OTP Github Action Bot

@zzydxm
Copy link
Contributor Author

zzydxm commented Oct 11, 2023

I will add java tailmap/submap/tailset/subset equivalent later in a separate PR

@zzydxm zzydxm changed the title [Draft] add higher/lower search functions in gb_trees and gb_sets Add higher/lower search functions in gb_trees and gb_sets Oct 12, 2023
@rickard-green rickard-green added the team:VM Assigned to OTP team VM label Oct 16, 2023
@jhogberg jhogberg self-assigned this Oct 16, 2023
@zzydxm
Copy link
Contributor Author

zzydxm commented Oct 18, 2023

Do we want to be able to do gb_sets:prev(element(2, gb_sets:next(Iterator)))? If yes then we will need to change the data structure returned by iterator_from, or deprecate iterator_from and create a new set of functions.

@zzydxm
Copy link
Contributor Author

zzydxm commented Oct 30, 2023

Hi @mikpe, is there any update on this? I cannot proceed with out deciding the behavior of gb_sets:prev(element(2, gb_sets:next(Iterator))). Thanks!

@jhogberg jhogberg added the stalled waiting for input by the Erlang/OTP team label Oct 30, 2023
@jhogberg
Copy link
Contributor

@zzydxm This is stalled until we've discussed things internally, we'll get back to you as soon as we can.

Copy link
Contributor

@jhogberg jhogberg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After discussing this internally, we'd like the following:

To match the maps module, we'd like reverse iteration to be handled by an order argument to iterator/1 and iterator_from/2 instead of having explicit forward and backward functions. next/1 would be used for both directions with the order depending on the iterator.

We would also prefer the names smaller and larger for < and >, to match smallest/1 and largest/1.

>= and =< may have some uses but they can be trivially implemented by composing is_element/2 and smaller/2 or larger/2 in user code. They can always be added later if there is a need.

@zzydxm
Copy link
Contributor Author

zzydxm commented Nov 9, 2023

Sure I will rename these functions and fix the issues.
For iterators, order can only be ordered | reversed, not a function, right?
An iterator that can move in both direction is more powerful without much implementation complexity (iterator can still be list of TreeNodes) but single directional already satisfied our use case.

@zzydxm
Copy link
Contributor Author

zzydxm commented Nov 9, 2023

I just realized that if we only have next but not prev, then reversed iterator need to return some datatype different with iterator/1, like {reverse, iter(Element)}. Otherwise it is either more expensive to create the iterator, or we cannot distinguish whether it is forward or backward iter.

@zzydxm
Copy link
Contributor Author

zzydxm commented Nov 13, 2023

Updated the commit, I kept type spec unchanged because doing suggest way will see this error:

gb_sets.erl:511:15: type variable 'Element1' is only used once (is unbound)
%  511| -spec smaller(Element1, Set) -> none | {found, Element2} when

@zzydxm zzydxm force-pushed the gb_utils branch 6 times, most recently from 703bbf6 to 365bb62 Compare November 13, 2023 23:49
@jhogberg
Copy link
Contributor

For iterators, order can only be ordered | reversed, not a function, right?

Yes.

I just realized that if we only have next but not prev, then reversed iterator need to return some datatype different with iterator/1, like {reverse, iter(Element)}. Otherwise it is either more expensive to create the iterator, or we cannot distinguish whether it is forward or backward iter.

Yep.

Updated the commit, I kept type spec unchanged because doing suggest way will see this error:

How about something like...?

-spec smaller(SmallerThan, Set) -> none | {found, SmallerElement} when
    SmallerThan :: term(),
    SmallerElement :: term(),
    Set :: set().

@zzydxm
Copy link
Contributor Author

zzydxm commented Nov 14, 2023

How about something like...?

Sure I can do that, but it is quite different with other typings in the module. Shall we modify them together in a later commit?

@jhogberg
Copy link
Contributor

Sure I can do that, but it is quite different with other typings in the module. Shall we modify them together in a later commit?

Sure, later commit is fine.

@jhogberg jhogberg added testing currently being tested, tag is used by OTP internal CI and removed stalled waiting for input by the Erlang/OTP team labels Nov 15, 2023
@jhogberg
Copy link
Contributor

Hi! This line needs to be updated (and for trees too?), the property tests fail to compile right now.

@jhogberg jhogberg removed the testing currently being tested, tag is used by OTP internal CI label Nov 16, 2023
@zzydxm
Copy link
Contributor Author

zzydxm commented Nov 16, 2023

Sorry property test had some issue on my laptop so were skipped. It should be fixed now.

@jhogberg jhogberg added the testing currently being tested, tag is used by OTP internal CI label Nov 17, 2023
@jhogberg jhogberg merged commit 728c6a0 into erlang:master Nov 22, 2023
@jhogberg
Copy link
Contributor

Merged, thanks for the PR!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team:VM Assigned to OTP team VM testing currently being tested, tag is used by OTP internal CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants