The rfind challenge

Phil Lavoie maidenphil at hotmail.com
Tue Jan 15 11:07:26 PST 2013


Continuing with reversible ranges:

struct ForwardRange {
  _start;
  _end;

  BackwardRange reverse() { return BackwardRange( _end, _start ); }
}

struct BackwardRange {
  _start;
  _end;

  //guess what reverse is.
}

That would give the power to mimick bidirectional ranges (see 
post on popBack before)

Now, we still need a way to move one of those pointers to a given 
place to fulfill rfind:

auto rfind( R, E )( R r, E e ) if( isReversibleRange!R ) {
  auto original = r.save; //For the end purpose;
  auto found = find( r.reverse, e ); //found is reversed and 
starts on e.
  //What we want now is a range of type typeof( original ) 
starting on found but ending on original. Therefore, we need an 
additional primitive. Suggestion: jumpTo.
  return original.jumpTo( found ); //Keeps the ordering, only move 
the start.
}

struct ForwardRange {
...
void jumpTo( ForwardRange r )( _start = r._start; }
void jumpTo( BackwardRange r )( _start = r._start; }

}


More information about the Digitalmars-d mailing list