When trying to use these functions I got a behavior not consistent with the documentation. Here is an example:
import algorithm
let a = [2, 4, 6, 8]
echo a.lowerBound(4) # display 1
echo a.upperBound(4) # display 2
The documentation for`lowerbound` in version 1.4.8 says: “Returns a position to the first element in the a that is greater than key, or last if no such element is found.” That means that, if all elements are distinct, the value at the returned position cannot be equal to the key. If documentation was right, we would expect to get 2 instead on 1.
Similarly, the documentation for`upperbound` in version 1.4.8 says: “Returns a position to the first element in the a that is not less (i.e. greater or equal to) than key, or last if no such element is found.” If documentation was right, in the example we would expect to get 1 instead on 2.
Obviously, the descriptions have been swapped as we expect lowerBound to return an index less or equal to the one returned by upperBound, not the reverse.
Now, looking at the issues on github, it appears that the documentations have indeed been swapped, but I don’t know if it was a mistake or the correction of a mistake. In the documentation of development version, the descriptions seem to be right.
I didn’t want to create a new issue before understanding what is the current situation. Documentation in version 1.4.8 is wrong, but can we expect that it will be right in next version?
Both statements seem to be true, but now i've even less of an intuition about how these work.
The idea is very sensitive to allowing duplicates:
import algorithm
# |- insert here
echo lowerBound([1, 2, 2, 3], 2) # 1
echo upperBound([1, 2, 2, 3], 2) # 3
# |- insert here
So, sort order is preserved after the insert in both cases, but for lowerBound it inserts at the front of a duplicate series and upperBound at the back of the dups.You are right. I checked and this is the reason why I asked the question here. Release 1.4.8 is recent, so I was surprised that it exists a difference with the development version.
To better understand, I checked and found that the commit has been done on January 2. So, I suppose that the documentation will be updated in next major version, but is not updated in minor ones.