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.

Hmmm... so we have two subtractions and one comparisons vs. two comparisons and a jump in between. I think you're right! -- Andrei

Reply via email to