The analogue of "fill-pointer" in D

Steven Schveighoffer via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 18 10:14:46 PDT 2015


On 5/18/15 6:24 AM, Dennis Ritchie wrote:
> On Monday, 18 May 2015 at 10:14:33 UTC, Kagamin wrote:
>> On Monday, 18 May 2015 at 08:21:38 UTC, Dennis Ritchie wrote:
>>> Hi,
>>>
>>> In Common Lisp, there is such a thing as a fill-pointer (Example 5):
>>> http://www.tutorialspoint.com/lisp/lisp_arrays.htm
>>>
>>> Does D some equivalent?
>>
>> Data stored in the array is indicated by the array length property,
>> use capacity to figure out extra available space:
>> http://dlang.org/phobos/object.html#.capacity
>
> No, afraid not. Function capacity is not an analogue of fill-pointers!

capacity is analogous to the number of elements in the vector (as 
returned by array-dimension according to 
https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node162.html).

arr.length is analogous to the fill pointer.

example:

int[] arr = new int[](5);
assert(arr.capacity > 5);
assert(arr.length == 5);

arr.reserve(100); // expand arr memory block to be able to hold *at 
least* 100 ints

assert(arr.capacity >= 100);
assert(arr.length == 5);

auto ptr = arr.ptr; // for later assert

arr ~= 1; // increment length by 1, 'fill in' tail of array with '1'

// this should demonstrate how it works
assert(arr.length == 6); // new fill pointer
assert(arr.capacity >= 100); // capacity unchanged
assert(arr.ptr is ptr); // array still lives in same memory block

Apologies for not translating to lisp, I don't know it.

-Steve


More information about the Digitalmars-d-learn mailing list