Ranges
pascal111
judas.the.messiah.111 at gmail.com
Thu Aug 4 22:54:42 UTC 2022
On Thursday, 4 August 2022 at 22:14:26 UTC, Ali Çehreli wrote:
> On 8/4/22 11:05, frame wrote:
>
> > `popFront()`
>
> The function was this:
>
> void popFront() {
> students = students[1 .. $];
> }
>
> > copies all
> > elements except the first one into the variable (and
> overwrites it), so
> > it moves the data forward.
>
> That would be very slow. :) What actually happens is, just the
> two variables that define a slice is adjusted.
>
> Slices consist of two members:
>
> struct __an_int_D_array_behind_the_scenes {
> size_t length;
> int * ptr;
> }
>
> So,
>
> students = students[1..$];
>
> is the same as doing the following:
>
> students.length = (students.length - 1);
> students.ptr = students.ptr + 1;
>
> (ptr's value would change by 4 bytes because 'int'.)
>
> No element is copied or moved. :)
>
> Ali
I didn't notice that all what we needs to pop a range forward is
just a slice, yes, we don't need variable here.
More information about the Digitalmars-d-learn
mailing list