Improving std.algorithm.find

bearophile bearophileHUGS at lycos.com
Sun Jul 18 05:36:04 PDT 2010


Andrei:

>Any ideas - please chime in.<

As with most software it's better to first ask yourself what your software will mostly be used for, and then to write it. Otherwise you are wasting your time and the user time.

The most common usages (I call them micropatterns) for find-like functions (every one of the following seven operations takes an optional transform function like schwartzSort):


1) boolean predicate, is the item/iterable x inside the iterable foo? ("in" operator, or if not possible a "isIn" function).
2) int function, tell me how many items/arrays x are inside the iterable foo ("count" function).
3) int function, tell me the index of the first item/iterable x in the iterable foo ("index" function).
4 extra) find lazily all the indexes of an item/iterable x inside the iterable foo ("indexes" struct or class).

5) find the first max/min item in a iterable ("max", "min" functions, they have overloads for 2 and 3 items too).
6) find the index of the max/min item in a iterable ("maxPos", "minPos" functions).
7) find lazily all the max/min items in a iterable ("maxs", "mins" structs or classes).


An iterable is a Range (like a string, array, AA, etc) or a struct/class that defines just opApply.

In my dlibs1 in all those functions if the sequence is an associative arrays, the search is done on the keys. D2 AAs have .byKey() that reduces this need a bit.

In the first 4 micropatterns it's good to have a specialization for strings, to increase performance with some string-specific tricks .

Bye,
bearophile


More information about the Digitalmars-d mailing list