Ranges

Ali Çehreli acehreli at yahoo.com
Thu Aug 4 22:14:26 UTC 2022


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



More information about the Digitalmars-d-learn mailing list