On Thursday, 17 October 2013 at 19:05:32 UTC, Jonathan M Davis
wrote:
before incrementing dSlice's length, it'll print out 0. dSlice
can't grow in
place, because if it did, its new elements would overlap with
those of dArr;
dArr -> [10, 11, 12]
dSlice -> [10, 11]
Well this is only one part that makes no sense in my eyes. I
thought the slices should be overlaping.
Here, step by step:
void main() {
int* arrPtr1;
int* arrPtr2;
int[] arr = [1, 2, 3];
arrPtr1 = arr.ptr;
arr.reserve(5); // reserve capacity
arrPtr2 = arr.ptr;
assert(arrPtr1 != arrPtr2); // ok, this makes sense
assert(arr.capacity==7); // ok, this makes not so
// much sense, but it's bigger than 5,
// I guess it's ok
// I reserved extra capacity. I got more
// than I needed, but ok.
arr ~= 4; // appending an element
assert(arr[3]==4);
arrPtr1 = arr.ptr;
assert(arrPtr1==arrPtr2); // no reallocation,
assert(arr.capacity==7); // good
// I have enough capacity to append an
// element; everything went fine
arr.length++;
assert(arr[4]==0 && arr.length==5);
arrPtr1 = arr.ptr;
assert(arrPtr1==arrPtr2); // still no reallocation,
assert(arr.capacity==7); // very good
// Also the direct manipulation of
// the length works, as long as a
// value is assigned that is bigger
// then the length.
arr.length--;
arrPtr1 = arr.ptr;
assert(arrPtr1==arrPtr2); // good, but..
assert(arr.capacity==0); // <- WHY ??
// after the length is reduced the
// capacity is set to ZERO, this will
// cause the array to be reallocated when
// the length is increased by next time,
// but what is the purpose of this?
arr.length++;
arrPtr1 = arr.ptr;
assert(arrPtr1!=arrPtr2); // different arrays now!
assert(arr.capacity==7); // yes, as expected
}