How to use lowerBound and upperBound effectively?

A. Bressan invalid at nospam.donotuse
Tue Jul 2 21:56:01 UTC 2019


On Tuesday, 2 July 2019 at 17:07:25 UTC, Ali Çehreli wrote:
> On 07/02/2019 02:27 AM, A. Bressan wrote:
>
> > contrary to C++, lowerBound and
> > upperBound give the same piece of information because they
> return
> > complementary sub-ranges.
>
> I don't understand the specific problem but can 'trisect' be 
> useful?
>
>   https://dlang.org/library/std/range/sorted_range.trisect.html
>
> Ali

I try to clarify the problem, sorry for not being clearer.

Given a value 'v', 'std::lower_bound' returns an iterator 
pointing to the first element 'a' for which 'a<v' is false. 
'std::upper_bound' returns an iterator to the first element 'a' 
between a pair of iterators for which 'a<=v' is false. Thus the 
two C++ functions find a different point.

The D function 'lowerBound' returns a range containing all 
elements '<v'. It is possible to recover the position of the 
first element '>=v' by using length.
'upperBound' returns a range containing all elements '>=v'. Again 
it is possible to recover the position of the first element '>=v' 
by subtracting the length of the returned range from the original 
length.
Thus 'lowerBound' and 'upperBound' provide the same piece of 
information: the position of the first element '>=v'. I need the 
position of the first element '>v'.

v=5
data              [1,2,3,4,5,5,6,7,8,9]
std::lower_bound           ^
std::upper_bound               ^
lowerBound         _______
upperBound                 ___________


My current solution is to reverse the range and the ordering so 
that I can pin-point the correct location.

v=5
data              [9,8,7,6,5,5,4,3,2,1]
std::lower_bound           ^
std::upper_bound               ^
lowerBound         _______
upperBound                 ___________

My question is motivated by the fact that the search policies 
(except the linear scan)  are variants of the bisection method 
https://en.wikipedia.org/wiki/Bisection_method
and can find both the first element '>=v' or the first element 
'>v' by changing the predicate '>=' for '>'. It seems to me that 
there must be a simpler way and that I am overlooking something.

The 'trisect' method, provides both the position of first element 
'>=v' and that of  the first element '>v'. So it provides more 
than what I need, but it is nicer to read and maybe faster than 
my solution.

Thanks

Andrea


More information about the Digitalmars-d-learn mailing list