between and among: worth Phobosization? (reprise)

Andrei Alexandrescu via Digitalmars-d digitalmars-d at puremagic.com
Thu Mar 26 14:09:15 PDT 2015


On 3/26/15 11:41 AM, Vladimir Panteleev wrote:
> On Sunday, 15 March 2015 at 01:48:53 UTC, Andrei Alexandrescu wrote:
>> On 12/16/13 12:38 PM, Andrei Alexandrescu wrote:
>>> bool between(T, U1, U2)(T v, U1 lo, U2 hi)
>>> {
>>>     return v >= lo && v <= hi;
>>> }
>>>
>>> Add?
>>
>> Looks like among() has proven its worth since we introduced it. Now I
>> somehow forgot between() didn't make it, and reviewed some code at
>> work assuming it exists!
>
> 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


More information about the Digitalmars-d mailing list