between and among: worth Phobosization? (reprise)
Vladimir Panteleev via Digitalmars-d
digitalmars-d at puremagic.com
Thu Mar 26 14:51:52 PDT 2015
On Thursday, 26 March 2015 at 21:09:16 UTC, Andrei Alexandrescu
wrote:
> On 3/26/15 11:41 AM, Vladimir Panteleev wrote:
>> I don't know if it's been mentioned yet, but there exists an
>> optimization for between with integer arguments:
>>
>> bool between(T, U1, U2)(T v, U1 lo, U2 hi)
>> if (is(T:long) && is(U1:long) && is(U2:long))
>> {
>> return cast(Unsigned!T )v - cast(Unsigned!U1)lo
>> <= cast(Unsigned!U2)hi - cast(Unsigned!U1)lo;
>> }
>>
>> For this reason, I think this makes "between" non-trivial, so
>> it is
>> worth adding.
>
> Wait, that doesn't work. 5.between(4, 3) returns true, should
> return false. -- Andrei
Oh yeah, this assumes hi <= lo. I thought this was part of the
function contract.
More information about the Digitalmars-d
mailing list