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 range searches to TreeMap/TreeSet #4604

Closed
thestinger opened this issue Jan 24, 2013 · 6 comments
Closed

Add range searches to TreeMap/TreeSet #4604

thestinger opened this issue Jan 24, 2013 · 6 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.

Comments

@thestinger
Copy link
Contributor

TreeMap (and TreeSet) should provide a variant of find that returns an iterator instead of the value, for finding the start (or end) of a range of values in O(log n). It would probably make sense to expose lower_bound and upper_bound like C++ does in std::map too.

The basic functionality is pretty simple, it's just the same process as find but each node along the way is pushed onto an iterator's stack, which is then returned. However, it would be nice to get values in the reverse direction too, especially without needing to walk down the tree twice. In C++, the lazy iterators are bidirectional - but that's because they have pointers to walk (which really wouldn't work for an owned tree in Rust, and is likely slower).

@ghost ghost assigned thestinger Mar 9, 2013
@lucab
Copy link
Contributor

lucab commented May 28, 2013

That would be really appreciated!

@dim-an
Copy link
Contributor

dim-an commented Aug 1, 2013

@thestinger hi, I have some progress on this issue and I would like to try to resolve it, if you don't mind.

However I need some advice:

  1. iterators returned from find_iter/lower_bound/upper_bound can not return exact size_hint, am I right? We'll need to return some estimation, won't we?
  2. Didn't you think about another approach to iteration? To keep in the iterator stack all ancestors of current node, using this approach we can get parents of the node and iterate in both directions using the same iterator.

@thestinger
Copy link
Contributor Author

It can return an upper bound but it won't be able to return a lower bound, so it would return (0, Some(tree.len())).

Iterating in both directions from the same iterator isn't a pattern represented by iterators in Rust, because a pair of iterators is more useful. A double-ended iterator is possible for some of the iterators, but it would be slower so it can't just replace the normal iterators.

@dim-an
Copy link
Contributor

dim-an commented Aug 2, 2013

@thestinger I implemented lower_bound/upper_bound, please review: #8227

@dim-an
Copy link
Contributor

dim-an commented Aug 3, 2013

If pull request (#8227) is accepted and merged (as I hope ^_^), it will be easy to implement find_iter. Though it's not clear to me what this method should return. Option<TreeMapIterator> ? TreeMapIterator that might point to empty sequence? Both options look little bit ugly for me.

Also I can't find a good example of situation when I would want use find_iter, but not lower_bound_iter or upper_bound_iter.

What do you think about it?

@thestinger
Copy link
Contributor Author

I'm going to call this done. There's the potential for a find_iter convenience function but it's unnecessary, and can be added once there's a use case.

@thestinger thestinger removed their assignment Jun 16, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one.
Projects
None yet
Development

No branches or pull requests

3 participants