between and among: worth Phobosization?

Timon Gehr timon.gehr at gmx.ch
Mon Dec 16 13:51:19 PST 2013


On 12/16/2013 10:45 PM, Walter Bright wrote:
> On 12/16/2013 12:38 PM, Andrei Alexandrescu wrote:
>> bool between(T, U1, U2)(T v, U1 lo, U2 hi)
>> {
>>      return v >= lo && v <= hi;
>> }
>
> You'd need 4 such functions, < <, < <=, <= <, <= <=, and it just seems
> like trivia.
>
>>
>> uint among(T, Us...)(T v, Us vals)
>> {
>>      foreach (i, U; Us)
>>      {
>>          if (v == vals[i]) return i + 1;
>>      }
>>      return 0;
>> }
>
> This has O(n) behavior, which might be unexpected for the user.
>

Do you mean O(vals.length)? O(vals.length)=O(1).


More information about the Digitalmars-d mailing list