Jump to page: 1 2
Thread overview
The analogue of "fill-pointer" in D
May 18, 2015
Dennis Ritchie
May 18, 2015
John Colvin
May 18, 2015
Kagamin
May 18, 2015
Dennis Ritchie
May 18, 2015
John Colvin
May 18, 2015
thedeemon
May 18, 2015
John Colvin
May 18, 2015
Ali Çehreli
May 18, 2015
Ali Çehreli
May 18, 2015
John Colvin
May 18, 2015
Ali Çehreli
May 18, 2015
Ali Çehreli
May 18, 2015
Kagamin
May 18, 2015
Dennis Ritchie
May 19, 2015
Kagamin
May 18, 2015
Dennis Ritchie
May 18, 2015
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?
May 18, 2015
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?

Fill pointers, combined with the various helper functions (e.g. vector-push) and vector-extend, perform tasks that D uses slices for.

e.g. vector-push-extend is roughly equivalent to the ~= operator, given a non-aliased array.

There are important differences, but the functionality overlaps a lot.
May 18, 2015
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
May 18, 2015
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."
May 18, 2015
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.
May 18, 2015
On Monday, 18 May 2015 at 10:24:25 UTC, Dennis Ritchie wrote:

> No, afraid not. Function capacity is not an analogue of fill-pointers!

It's exactly the same.

> Lisp-programmer explains the usefulness of fill-pointers as follows:
>
> "Fill pointer "cuts" the tail of the vector.

In D: .length "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.

In D: vector elements 100, but if you set the .length equal to
 3, the length of the array (returned by length) will be equal
 to 3. The remaining elements are not visible.

.capacity tells you "real" size of the buffer while .length is like that fill pointer.
May 18, 2015
On Monday, 18 May 2015 at 11:40:13 UTC, thedeemon wrote:
> On Monday, 18 May 2015 at 10:24:25 UTC, Dennis Ritchie wrote:
>
>> No, afraid not. Function capacity is not an analogue of fill-pointers!
>
> It's exactly the same.

But in D capacity is affected by other things.

auto a = new int[20];
auto b = arr[0..10];
//can't now append to b without re-allocating or using assumeSafeAppend.
May 18, 2015
On Monday, 18 May 2015 at 10:24:25 UTC, Dennis Ritchie wrote:
> 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."

Filling a buffer is usually done this way: http://dlang.org/phobos/std_stdio.html#.File.rawRead
May 18, 2015
On Monday, 18 May 2015 at 12:49:56 UTC, Kagamin wrote:
> Filling a buffer is usually done this way: http://dlang.org/phobos/std_stdio.html#.File.rawRead

Here such example, the task. There is a flow stream, associated, for example, with any socket. It wrote several bytes at a time. To once again not to pull the socket, we start buffer as an array with Phill-pointer. Adding byte array - using the vector-push. When the buffer is stuffed, dump it into the stream and moves pointer to zero. How to do it with the help of readRaw or there writeRaw?
May 18, 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
« First   ‹ Prev
1 2