between and among: worth Phobosization?

Ola Fosheim Grøstad" <ola.fosheim.grostad+dlang at gmail.com> Ola Fosheim Grøstad" <ola.fosheim.grostad+dlang at gmail.com>
Mon Dec 16 14:55:41 PST 2013


On Monday, 16 December 2013 at 21:45:40 UTC, Walter Bright wrote:
>> 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.

I would expect one table-lookup for this if vals contains 
strings, not N ifs. If you look at the example, most of them 
could be done with perfect hashing on a single character.

Is it possible for the compiler/template system to turn this into 
a switch/dictionary? Or is there something in the 
language/compiler that makes that impossible?

(I am not trying to be clever, I am curious)


More information about the Digitalmars-d mailing list