Reading the BTreeSet
documentation, I can't seem to figure out how to get the least value greater than, or greatest value less than an element from a BTreeSet
in logarithmic time.
I see there is a range
method that can give the values in an arbitrary (min, max) range, but what if I don't know the range and I just want the previous and/or the next element in logarithmic time?
This would be similar to lower_bound
and upper_bound
in std::set
in C++.
Then use an unbounded range:
BTreeSet::range
returns a double-ended iterator, so you can pull from either side of it.Note that we are using the very explicit
Bound
operator so that we do not include the value we are looking around.There have been discussions about enhancing
BTreeMap
/BTreeSet
to have a "cursor" API that might allow you to find an element and then "move around" inside the tree. This would allow you to avoid searching through the tree to find the start node twice, but it has not been implemented.A pull request was opened to do so, but it was closed because it was deemed that there should be more discussion about how such an API should look and work.
Well... if you don't mind modifying the current collection and taking a performance hit... it appears that you can use
split_off
creatively.Will print
{1}
and{3, 5}
:Once you are done, you can reassemble the tree using
tree.append(other)
.And yes, it's really less than ideal...
If you can change your data structure, you can use intrusive collections.
You have the desired methods:
RBTree::lower_bound
RBTree::upper_bound