"in" everywhere

Michel Fortin michel.fortin at michelf.com
Thu Oct 7 14:23:31 PDT 2010


On 2010-10-07 14:38:50 -0400, Andrei Alexandrescu 
<SeeWebsiteForEmail at erdani.org> said:

> At no point. "Linear" means "linear in the input size". I don't think 
> such arguments are valid.

It is linear in regard to the array length, the static array being the 
input. That the length is known at compile time doesn't make it less of 
an input for the "in" operator, even though it's not an input of the 
program at runtime.

Let's say for instance that you have a big static array of keywords and 
you want to check if a string contains one of those keywords, would you 
really do this?

	string[200] keywords = ["for", "foreach", "auto", .... 200 keywords .... ];

	foreach (word; listOfWords)
		if (word in keywords)
			writeln("Found a keyword! ", word);

That's so easy to read and write! but so stupid at the same time if 
"in" performs a linear search.

In the example above, if "keywords" was immutable then the compiler 
could optimize things by using a good search strategy, but otherwise 
it's just ridiculous to allow it to compile.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/



More information about the Digitalmars-d mailing list