Jump to page: 1 2 3
Thread overview
idiomatic D: what to use instead of pointers in constructing a tree data structure?
Jan 07, 2015
Laeeth Isharc
Jan 07, 2015
H. S. Teoh
Jan 07, 2015
Laeeth Isharc
Jan 07, 2015
Paulo Pinto
Jan 07, 2015
Laeeth Isharc
Jan 08, 2015
Baz
Jan 08, 2015
Tobias Pankrath
Jan 08, 2015
Laeeth Isharc
Jan 08, 2015
Mengu
Jan 08, 2015
H. S. Teoh
Jan 08, 2015
Ali Çehreli
Jan 08, 2015
Paulo Pinto
Jan 08, 2015
H. S. Teoh
Jan 10, 2015
Laeeth Isharc
Jan 13, 2015
Laeeth Isharc
Jan 13, 2015
Tobias Pankrath
Jan 14, 2015
Laeeth Isharc
Jan 14, 2015
H. S. Teoh
Jan 14, 2015
H. S. Teoh
Jan 07, 2015
Tobias Pankrath
Jan 07, 2015
Meta
Jan 07, 2015
anonymous
Jan 08, 2015
Meta
January 07, 2015
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
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
 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
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
> ref is a reserved keyword.
doh!

Thanks.
January 07, 2015
 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
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
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
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
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.
« First   ‹ Prev
1 2 3