Sets again

Craig Black cblack at ara.com
Tue Feb 21 09:06:05 PST 2006


>> How about in for arrays for consistency with AA's:
>>
>> void foo(char[] )
>> {
>> char[] arr = "abcdef";
>> //...
>> if('d' in arr) { ... }
>> }
>>
>
> NO! Please don't do this. The operation is O(n). If will be useful perhaps 
> for "if (day in Weekdays)", but the system is extremely unscalable, and 
> using "in" on a larger array will harm performance. I don't think it's a 
> good idea to hide such a potential performance hit behind a small word 
> like "in".
>
> (In fact, I generally think the costly operations should get long, 
> complicated names, so they don't get used too often)
>
> Now if the array were sorted, one could do a binary search, which is not 
> all too bad. But this still needs special syntax, since the compiler 
> should enforce the sorting.

I disagree.  The linear search would be adequate for most problems. 
Typically "in" is merely used instead of an "if(x == a || x == y || x == z 
...)", which is also a linear search.  If you need a binary search then you 
can use a different syntax for that.

-Craig 





More information about the Digitalmars-d mailing list