Thread overview | ||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
January 07, 2015 idiomatic D: what to use instead of pointers in constructing a tree data structure? | ||||
---|---|---|---|---|
| ||||
Another schoolboy question. Suppose I am constructing a tree (in this case it is an AST). In C I would have a pointer for the child to find the parent, and an array or linked list of pointers to find the children from the parent. Obviously, I could still use pointers, but that would not be idiomatic. I also could just use integer array index references. A slice seems overkill to refer to just one object, but is that the best way ? |
January 07, 2015 Re: idiomatic D: what to use instead of pointers in constructing a tree data structure? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laeeth Isharc | On Wed, Jan 07, 2015 at 02:52:51PM +0000, Laeeth Isharc via Digitalmars-d-learn wrote:
> Another schoolboy question.
>
> Suppose I am constructing a tree (in this case it is an AST). In C I would have a pointer for the child to find the parent, and an array or linked list of pointers to find the children from the parent.
>
> Obviously, I could still use pointers, but that would not be idiomatic.
Not true. If you're using a tree structure, you *should* use pointers. Unless you're using classes, which are by-reference, in which case you can just use the class as-is. :-)
--T
|
January 07, 2015 Re: idiomatic D: what to use instead of pointers in constructing a tree data structure? | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | Not true. If you're using a tree structure, you *should* use
> pointers.
> Unless you're using classes, which are by-reference, in which case you
> can just use the class as-is. :-)
Thanks v much.
I just came to that realization also when I stepped away.
class node
{
string name;
node ref;
}
what's wrong with the code above ? i get an error no identifier for declarator node. (I have not used classes much, since structs often seem to be enough for what I need to do mostly).
|
January 07, 2015 Re: idiomatic D: what to use instead of pointers in constructing a tree data structure? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laeeth Isharc | On Wednesday, 7 January 2015 at 15:02:34 UTC, Laeeth Isharc wrote:
> Not true. If you're using a tree structure, you *should* use
>> pointers.
>> Unless you're using classes, which are by-reference, in which case you
>> can just use the class as-is. :-)
>
> Thanks v much.
>
> I just came to that realization also when I stepped away.
>
> class node
> {
> string name;
> node ref;
> }
>
> what's wrong with the code above ? i get an error no identifier for declarator node. (I have not used classes much, since structs often seem to be enough for what I need to do mostly).
ref is a reserved keyword.
--
Paulo
|
January 07, 2015 Re: idiomatic D: what to use instead of pointers in constructing a tree data structure? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paulo Pinto | > ref is a reserved keyword.
doh!
Thanks.
|
January 07, 2015 Re: idiomatic D: what to use instead of pointers in constructing a tree data structure? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Laeeth Isharc | A slice seems overkill to refer to just one object, but is that
> the best way ?
struct Tree
{
Tree[] children;
}
Is one way to do it.
|
January 07, 2015 Re: idiomatic D: what to use instead of pointers in constructing a tree data structure? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Tobias Pankrath | On Wednesday, 7 January 2015 at 16:17:47 UTC, Tobias Pankrath wrote: > A slice seems overkill to refer to just one object, but is that >> the best way ? > > struct Tree > { > Tree[] children; > } > > Is one way to do it. You can add some nice sugar for this as well: struct Tree { this(string data, Tree[] children...) { this.data = data; this.children = children; } string data; Tree[] children; } http://dpaste.dzfl.pl/76a8a4c44345 |
January 07, 2015 Re: idiomatic D: what to use instead of pointers in constructing a tree data structure? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Meta | On Wednesday, 7 January 2015 at 20:25:10 UTC, Meta wrote: > struct Tree > { > this(string data, Tree[] children...) > { > this.data = data; > this.children = children; Don't do this without `dup`ing. Quoting the documentation: > An implementation may construct the object or array instance on the stack. Therefore, it is an error to refer to that instance after the variadic function has returned: [...] > int[] test(int[] a ...) > { > return a; // error, array contents invalid after return > return a[0..1]; // error, array contents invalid after return > return a.dup; // ok, since copy is made >} http://dlang.org/function#variadic > } > > string data; > Tree[] children; > } |
January 08, 2015 Re: idiomatic D: what to use instead of pointers in constructing a tree data structure? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paulo Pinto | On Wednesday, 7 January 2015 at 15:04:24 UTC, Paulo Pinto wrote:
> On Wednesday, 7 January 2015 at 15:02:34 UTC, Laeeth Isharc wrote:
>> Not true. If you're using a tree structure, you *should* use
>>> pointers.
>>> Unless you're using classes, which are by-reference, in which case you
>>> can just use the class as-is. :-)
>>
>> Thanks v much.
>>
>> I just came to that realization also when I stepped away.
>>
>> class node
>> {
>> string name;
>> node ref;
>> }
>>
>> what's wrong with the code above ? i get an error no identifier for declarator node. (I have not used classes much, since structs often seem to be enough for what I need to do mostly).
>
> ref is a reserved keyword.
>
> --
> Paulo
this conversation is so funny: well what's wrong with this . It's a keyword...
Aa Ha ha ha ha , rol.
Seriously, is it so complicated to use a D editor ? I mean with syntax color...
|
January 08, 2015 Re: idiomatic D: what to use instead of pointers in constructing a tree data structure? | ||||
---|---|---|---|---|
| ||||
Posted in reply to anonymous | On Wednesday, 7 January 2015 at 23:27:19 UTC, anonymous wrote:
> Don't do this without `dup`ing. Quoting the documentation:
Oh, whoops. I thought those special variadic args were always allocated on the heap.
|
Copyright © 1999-2021 by the D Language Foundation