Slicing upward

Paul Backus snarwin at
Sat Sep 14 18:08:56 UTC 2019

On Saturday, 14 September 2019 at 11:34:35 UTC, Brett wrote:
> I have an algorithm that is most efficiently implement by 
> taking an array and slicing it upward, meaning removing the 
> leading elements.
> Because the algorithm is complex(deterministic but chaotic) and 
> deals with multiple arrays it is difficult to efficiently use 
> slicing.
> Is there some easy way to take an array and slice it in a way 
> that as the array grows any slices will "shift".

No, there is no easy way to do this. You will have to implement 
your own "slice" type that keeps a reference to the original 
array it was taken from. For example:

struct UpwardSlice(T)
     private T[]* source;
     private size_t offset;

     this(ref T[] source, size_t offset = 0)
         this.source = &source;
         this.offset = offset;

     T opIndex(size_t i)
         return (*source)[offset + i];

     T[] opIndex()
         return (*source)[offset .. $];

     @property size_t length()
         return (*source).length - offset;

More information about the Digitalmars-d-learn mailing list