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.
  • Ranges pascal111 via Digitalmars-d-learn
    • Re: Ranges Salih Dincer via Digitalmars-d-learn
    • Re: Ranges frame via Digitalmars-d-learn
      • Re: Ranges Ali Çehreli via Digitalmars-d-learn
        • Re: Ranges pascal111 via Digitalmars-d-learn

Reply via email to