algorithm API design question

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Sun Sep 29 19:35:01 PDT 2013


I need a function that finds a run of length k of elements equal to x in 
a range r, and I presume such a simple yet nontrivial algorithm (a 
dozen-liner) should be part of std.algorithm.

This raises an interesting question - what form should the API have. I 
see three options:

1. The existing find(r1, r2) figures out a way to dynamically check that 
r2 is a run of identical elements and tailor the argument accordingly. 
For example, during Boyer-Moore initialization that test comes cheap.

2. We should statically specialize find(r1, r2) for the case r2 is an 
instance of Repeat. The specialization runs the tailored algorithm. The 
user is supposed to call e.g. find(r, repeat(x, k)) to benefit of the 
specialized algorithm.

3. We should introduce a new function called e.g. findRun(r, x, k).

Each option has advantages and disadvantages. What do you all think is 
the best API?


Andrei


More information about the Digitalmars-d mailing list