On Monday, 18 May 2015 at 10:24:25 UTC, 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!
Lisp-programmer explains the usefulness of fill-pointers as
follows:
"Fill pointer "cuts" the tail of the vector. For example,
vector elements 100, but if you set the fill pointer equal to
3, the length of the array (returned by length) will be equal
to 3. The remaining elements are not visible.
It seems to be nonsense. But this is nonsense, ideal for
buffers. If the buffer is implemented as an array, then fill
pointer just marks the boundary of the filled part of the
buffer, and adding a buffer (moving away from the fill
pointer-a) is carried out using the vector-push. Or a buffer
can be filled with the format-a. If you work with the same
buffer C, fill pointer simulates a pointer to the last
completed item."
There are a lot of ways of doing this in D.
std.array.appender makes a good imitation of this, just missing
the vector-push, which can be implemented like this, roughly:
ptrdiff_t putNoAlloc(App, T)(App app, T el)
{
if (app.capacity)
{
app.put(el);
return app.data.length - 1;
}
else
return -1;
}
It would also be trivial to hand-make a type that has whatever
behaviour you want with regards to array appending, lengths etc.