Jump to page: 1 2
Thread overview
Straight Forward Arrays
Oct 01, 2023
dhs
Oct 01, 2023
Imperatorn
Oct 01, 2023
dhs
Oct 01, 2023
bachmeier
Oct 01, 2023
bachmeier
Oct 01, 2023
dhs
Oct 01, 2023
dhs
Oct 01, 2023
Imperatorn
Oct 01, 2023
dhs
Oct 01, 2023
dhs
Oct 01, 2023
Adam D Ruppe
Oct 01, 2023
dhs
Oct 02, 2023
Jonathan M Davis
Oct 04, 2023
dhs
Oct 05, 2023
Jesse Phillips
Oct 06, 2023
dhs
October 01, 2023

Hi,

Is there a straight forward Array type in D similar to C++'s vector class? Something along the lines of the tuple: (pointer to elements, length, capacity).

I tried two implementations: D's dynamic array and std.container.array.

When D creates a dynamic array, it returns a slice. Functions that add or remove elements begin by asking the memory manager for the dynamic array that the slice belongs to. Only then can they go on and add elements.

I switched to std.container.array. It is more straight forward, but is also reference counted. Array is basically a pointer to the tuple (pointer to elements, length, capacity, refCount). Functions that add or remove elements begin by checking that the Array is not null, follow the pointer to the actual tuple, then follow the "pointer to elements" to access the actual elements. In C++, this is similar to shared_ptr<vector>. The documentation does not mention nor explain why. Can someone explain? Is there a simpler, more efficient alternative?

Thanks in Advance,
dhs

October 01, 2023

On Sunday, 1 October 2023 at 09:01:53 UTC, dhs wrote:

>

Hi,

Is there a straight forward Array type in D similar to C++'s vector class? Something along the lines of the tuple: (pointer to elements, length, capacity).

[...]

https://dlang.org/spec/simd.html
https://dlang.org/phobos/core_simd.html

Or if you want to use it, you can check out core.stdcpp.vector.

October 01, 2023

On Sunday, 1 October 2023 at 09:21:37 UTC, Imperatorn wrote:

>

On Sunday, 1 October 2023 at 09:01:53 UTC, dhs wrote:

>

Hi,

Is there a straight forward Array type in D similar to C++'s vector class? Something along the lines of the tuple: (pointer to elements, length, capacity).

[...]

https://dlang.org/spec/simd.html
https://dlang.org/phobos/core_simd.html

Or if you want to use it, you can check out core.stdcpp.vector.

core.stdcpp.vector does look the dynamic array implementation I was looking for.

Thank you,
dhs

October 01, 2023

On Sunday, 1 October 2023 at 09:01:53 UTC, dhs wrote:

>

Hi,

Is there a straight forward Array type in D similar to C++'s vector class? Something along the lines of the tuple: (pointer to elements, length, capacity).

I tried two implementations: D's dynamic array and std.container.array.

When D creates a dynamic array, it returns a slice. Functions that add or remove elements begin by asking the memory manager for the dynamic array that the slice belongs to. Only then can they go on and add elements.

Have you read this article? I'm not sure what you mean with your reference to the memory manager, but consider this program:

import std;

void main() {
    int[] x;
    x.length = 100;
    foreach(ii; 0..100) {
        x.ptr[ii] = ii;
    }
    x.length = 100;
    writeln(x);
}
October 01, 2023

On Sunday, 1 October 2023 at 11:39:11 UTC, bachmeier wrote:

>

On Sunday, 1 October 2023 at 09:01:53 UTC, dhs wrote:

>

Hi,

Is there a straight forward Array type in D similar to C++'s vector class? Something along the lines of the tuple: (pointer to elements, length, capacity).

I tried two implementations: D's dynamic array and std.container.array.

When D creates a dynamic array, it returns a slice. Functions that add or remove elements begin by asking the memory manager for the dynamic array that the slice belongs to. Only then can they go on and add elements.

Have you read this article? I'm not sure what you mean with your reference to the memory manager, but consider this program:

import std;

void main() {
    int[] x;
    x.length = 100;
    foreach(ii; 0..100) {
        x.ptr[ii] = ii;
    }
    x.length = 100;
    writeln(x);
}

Or if you want a safer version:

import std;

void main() {
    int[] x;
    x.length = 100;
    foreach(ii; 0..150) {
        if (ii < x.length) {
        	x.ptr[ii] = ii;
        }
    }
    writeln(x);
}
October 01, 2023

On Sunday, 1 October 2023 at 11:43:17 UTC, bachmeier wrote:

>

On Sunday, 1 October 2023 at 11:39:11 UTC, bachmeier wrote:

>

On Sunday, 1 October 2023 at 09:01:53 UTC, dhs wrote:

>

Hi,

Is there a straight forward Array type in D similar to C++'s vector class? Something along the lines of the tuple: (pointer to elements, length, capacity).

I tried two implementations: D's dynamic array and std.container.array.

When D creates a dynamic array, it returns a slice. Functions that add or remove elements begin by asking the memory manager for the dynamic array that the slice belongs to. Only then can they go on and add elements.

Have you read this article? I'm not sure what you mean with your reference to the memory manager, but consider this program:

import std;

void main() {
    int[] x;
    x.length = 100;
    foreach(ii; 0..100) {
        x.ptr[ii] = ii;
    }
    x.length = 100;
    writeln(x);
}

Or if you want a safer version:

import std;

void main() {
    int[] x;
    x.length = 100;
    foreach(ii; 0..150) {
        if (ii < x.length) {
        	x.ptr[ii] = ii;
        }
    }
    writeln(x);
}

Thanks for the article.

When you write x.length = 100, the code looks at x.capacity to see if the allocated memory is big enough for 100 elements. Since 'capacity' is not part of x, the code calculates it by asking the Garbage Collected how much memory was allocated. This is my understanding of the code and what I meant by "asking the memory manager".

std.container.array uses a 'capacity' field, but is reference counted. This is not documented and means an additional indirection, which in my case is unnecessary.

User 'Imperatorn' suggested using core.stdcpp.vector. Unfortunately, it compile for me (neither using DMD nor LDC). In addition, it looks like it depends on the C++ runtime for 'new' and 'delete'.

So I am still in search of a straightforward dynamic array similar to C++ 'vector'.

October 01, 2023

On Sunday, 1 October 2023 at 09:01:53 UTC, dhs wrote:

>

Hi,

Is there a straight forward Array type in D similar to C++'s vector class? Something along the lines of the tuple: (pointer to elements, length, capacity).

[...]

Std::vector uses value semantics. D does not have anything like that. It could be done someone just has to do it.

-Steve

October 01, 2023

On Sunday, 1 October 2023 at 13:05:12 UTC, Steven Schveighoffer wrote:

>

On Sunday, 1 October 2023 at 09:01:53 UTC, dhs wrote:

>

Hi,

Is there a straight forward Array type in D similar to C++'s vector class? Something along the lines of the tuple: (pointer to elements, length, capacity).

[...]

Std::vector uses value semantics. D does not have anything like that. It could be done someone just has to do it.

-Steve

Yes, and therein lies the problem: writing a dynamic array is not a very difficult task for an old developer like me. I looked at the D runtime and at the Phobos implementation for reference. The code is so extremely difficult to understand and uses so many advanced D features, that I doubt that I am up to the task. For me, the point of switching to D was to use a language that is simpler to read and maintain.

October 01, 2023
On Sunday, 1 October 2023 at 09:01:53 UTC, dhs wrote:
> When D creates a dynamic array, it returns a slice. Functions that add or remove elements begin by asking the memory manager for the dynamic array that the slice belongs to. Only then can they go on and add elements.

Why is this a problem? It is convenient and usually works fine.

I use the built in arrays very very often for a lot of things.
October 01, 2023

On Sunday, 1 October 2023 at 13:24:27 UTC, dhs wrote:

>

On Sunday, 1 October 2023 at 13:05:12 UTC, Steven Schveighoffer wrote:

>

On Sunday, 1 October 2023 at 09:01:53 UTC, dhs wrote:

>

Hi,

Is there a straight forward Array type in D similar to C++'s vector class? Something along the lines of the tuple: (pointer to elements, length, capacity).

[...]

Std::vector uses value semantics. D does not have anything like that. It could be done someone just has to do it.

-Steve

Yes, and therein lies the problem: writing a dynamic array is not a very difficult task for an old developer like me. I looked at the D runtime and at the Phobos implementation for reference. The code is so extremely difficult to understand and uses so many advanced D features, that I doubt that I am up to the task. For me, the point of switching to D was to use a language that is simpler to read and maintain.

D can be very readable and maintainable, but since all the advanced features exist, we are tempted to use them, which can cause otherwise normal code to become a bit obfuscated.

« First   ‹ Prev
1 2