RedBlackTree.lowerBound
Ellery Newcomer
ellery-newcomer at utulsa.edu
Sun Feb 19 13:44:05 PST 2012
Is it just me or are lowerBound and upperBound really unintuitively
named? From DDOC:
c.lowerBound(v) Returns a range of all elements strictly less than v
c.upperBound(v) Returns a range of all elements strictly greater than v.
So c.lowerBound(v) will return a range for which v is the .. upper bound
noninclusive. And c.upperBound(v) the lower bound noninclusive.
Over in the STL, we have set::lower_bound and set::upper_bound, but
these are different in two ways
1) they return iterators
1.a) lower_bound is kinda meant to be a starting iterator, upper_bound
is kinda meant to be a ending iterator. kinda. Anyways:
c.lower_bound(v) points to the first element less than or equal to v
c.upper_bound(v) points to the first element strictly greater than v
So iterators are like these half-ranges, they aren't really useful by
themselves but you can pair them up and stuff.
Consider the ordered set C = {1,2,3,4,5}.
if you want the range [3,inf) intersect C, over in the STL you'd use the
pair (C.lower_bound(3), C.end()). Here, I guess you'd use
C.upperBound(2). Similarly,
(-inf, 3) -> (C.begin(), C.lower_bound(3)) vs C.lowerBound(3)
(-inf, 3] -> (C.begin(), C.upper_bound(3)) vs C.lowerBound(4)
(3, inf) -> (C.upper_bound(3), C.end()) vs C.upperBound(3)
[3, inf) -> (C.lower_bound(3), C.end()) vs C.upperBound(4) //
yeah, well I couldn't find it in the text above quickly enough
[2, 4) -> (C.lower_bound(2), C.lower_bound(4)) vs ???
(2, 4) -> (C.upper_bound(2), C.lower_bound(4)) vs ???
[2, 4] -> (C.lower_bound(2), C.upper_bound(4)) vs ???
(2, 4] -> (C.upper_bound(2), C.upper_bound(4)) vs ???
So now two things emerge that I wasn't exactly going for when I started
this rumination, but hey:
1) lowerBound and upperBound are actually not especially useful as they
capture only a small subset of the functionality of lower_bound and
upper_bound
2) lower_bound and upper_bound are versatile, but rather unreadable
3) D can do better than this.
Suppose instead of having lowerBound and upperBound we have
bound!(string boundary = "[")(one_end) // possible: "[","(","]",")"
bounds!(string boundaries = "[]")(lower, upper) // kinda like
std.random.uniform
Anyways, back to the topic at hand.
I don't see much if any correlation between STL's lower bound and upper
bound and std.container's, and I don't see much use of the current
names/semantics except for confusing people, so I think at the very
least the names should be changed to something like
belowBound
aboveBound
Am I missing anything?
More information about the Digitalmars-d-learn
mailing list