"in" everywhere

Don nospam at nospam.com
Thu Oct 7 13:32:22 PDT 2010


Andrei Alexandrescu wrote:
> On 10/7/10 12:11 CDT, Michel Fortin wrote:
>> On 2010-10-07 11:47:21 -0400, Andrei Alexandrescu
>> <SeeWebsiteForEmail at erdani.org> said:
>>
>>> On 10/7/10 10:39 CDT, Daniel Gibson wrote:
>>>> Andrei Alexandrescu schrieb:
>>>>> On 10/7/10 9:59 CDT, Daniel Gibson wrote:
>>>>>> Andrei Alexandrescu schrieb:
>>>>>>> On 10/7/10 6:54 CDT, atommixz wrote:
>>>>>>>> It would be nice if it were possible to use the "in" expression
>>>>>>>> wherever
>>>>>>>> possible. Now it is only implemented for associative. arrays.
>>>>>>>> (Weird).
>>>>>>>> Examples of how this could be used:
>>>>>>>> - Find string in string
>>>>>>>> - Search for a character in a string
>>>>>>>> - Search for an item in the array, array of characters, array of
>>>>>>>> strings,
>>>>>>>> tuples, enum, structure
>>>>>>>> - what else?
>>>>>>>>
>>>>>>>> In Python done something like this.
>>>>>>>>
>>>>>>>> Here it would be useful to me
>>>>>>>> http://code.google.com/p/atommixz/source/browse/analyze-x86/analyze-x86.py 
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>> http://code.google.com/p/atommixz/source/browse/analyze-x86/analyzex86.d 
>>>>>>>>
>>>>>>>>
>>>>>>>>
>>>>>>>
>>>>>>> I'm a bit leary of adopting this feature (it has been discussed). To
>>>>>>> me "in" implies a fast operation and substring searching isn't quite
>>>>>>> it.
>>>>>>>
>>>>>>> One thing that could be done is to allow "in" with literal arrays to
>>>>>>> their right:
>>>>>>>
>>>>>>> if (x in ["abcde", "asd"]) { ... }
>>>>>>>
>>>>>>> The size of the operand is constant, known, and visible.
>>>>>>>
>>>>>>>
>>>>>>> Andrei
>>>>>>
>>>>>> That feels inconsistent.. to be able to use it with "literal 
>>>>>> arrays to
>>>>>> their right" (and what about fixed size arrays?) but not with actual
>>>>>> arrays and dynamic arrays seems weird.
>>>>>
>>>>> It's not. It's all about constant size in the size of the input vs.
>>>>> arbitrary size. Makes perfect sense to me.
>>>>>
>>>>
>>>> So what about static arrays?
>>>
>>> Same deal - same as literal arrays: they can be searched. The expected
>>> run time is known during compilation and independent of the input size.
>>
>> What about a static array of 200000 elements? At which point does it
>> become linear?
> 
> At no point. "Linear" means "linear in the input size". I don't think 
> such arguments are valid.


I actually see a very significant difference between 'in' on a 
compile-time array literal, vs 'in' on a static array. When all values 
are known, the compiler can make a perfect hash. So potentially it could 
be O(1).

But 'find' on a static array will run the same function which would be 
used for a dynamic array. The difference is theoretical ONLY -- the 
exact same asm instructions will be executed!

In fact,   x in ["abc", "def"] could be supported by allowing implicit 
conversion from array literals to AA literals.
["abc", "def"] -----> ["abc" : 0, "def" : 1]


More information about the Digitalmars-d mailing list