[phobos] Slicer

David Simcha dsimcha at gmail.com
Thu Aug 12 20:24:03 PDT 2010


A whole bunch of stuff in Phobos (including std.range.Radial and the FFT 
algorithm I just checked in) requires that ranges provided to it have 
slicing.  This limitation is a PITA.  Should we add a Slicer meta-range 
that takes a finite random-access range and bolts slicing on in the 
obvious but relatively inefficient way if it's not already supported and 
simply returns the range if it already supports slicing?  This would be 
used under the hood when slicing is required.  For example:

struct Slicer(R) if(isRandomAccessRange!R && !hasSlicing!R) {
     R range;
     size_t lowerLim, upperLim;

     this(R r) {
          this.range = range;
          this.upperLim = range.length;
     }

     // Range primitives:  front, popFront, etc.

     typeof(this) opSlice(size_t lower, size_t upper) {
         // Error checking
         auto ret = this;
         ret.upperLim -= this.length - upper;
         ret.lowerLim += lower;
         return ret;
     }
}

auto slicer(R)(R range) {
     static if(hasSlicing!R) {
         return range;
     } else {
          return Slicer!(R)(range);
     }
}


More information about the phobos mailing list