The rfind challenge
FG
home at fgda.pl
Tue Jan 15 03:53:45 PST 2013
On 2013-01-15 06:51, Andrei Alexandrescu wrote:
> If r is a forward range but not better, this is rather simple:
>
> R rfind(R, E)(R range, E element)
> {
> for (;;)
> {
> auto ahead = range.save.find(element);
> if (ahead.empty) return range;
> range = ahead;
> }
> }
That example creates an infinite loop. Needs a popFront:
R rfind(R, E)(R range, E element)
{
if (range.empty) return range;
for (;;)
{
auto ahead = range.save;
ahead.popFront();
ahead = ahead.find(element);
if (ahead.empty) return range;
range = ahead;
}
}
Another thing: if the element is not found, I think an empty range should be
returned and not the whole range.
More information about the Digitalmars-d
mailing list