www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Generating a tree structure

reply "Ricky C" <kf6kjg+dlang gmail.com> writes:
I'm trying to make a tree data structure in part of my first 
non-trivial D-based program.  Turns out that DMD likes to re-use 
PODs - sounds good, but that trapped me, and I'm not sure how to 
clear myself out of the problem nicely.

My simplified, but not oversimplified, example is 
http://dpaste.dzfl.pl/67b022ca029e

You'll note in the output of the program several duplicated 
values for "self" and the parent value goes to places other than 
the parent.  This is all wrong, and shows my ignorance, but is as 
far as I've been able to work it.

In my operational code I bypassed the problem by storing every 
leaf and branch in an array and changing the self, parent, and 
children properties to be indices into said array.  But this 
feels hackish: I'd rather have the tree itself hold everything it 
needs.

Note: I'm not stuck with a struct. I could class the thing, but I 
figured that a struct holds what I need simply...

Thoughts, solutions?
Aug 21 2014
next sibling parent "anonymous" <anonymous example.com> writes:
On Thursday, 21 August 2014 at 14:09:24 UTC, Ricky C wrote:
 I'm trying to make a tree data structure in part of my first 
 non-trivial D-based program.  Turns out that DMD likes to 
 re-use PODs - sounds good, but that trapped me, and I'm not 
 sure how to clear myself out of the problem nicely.

 My simplified, but not oversimplified, example is 
 http://dpaste.dzfl.pl/67b022ca029e

 You'll note in the output of the program several duplicated 
 values for "self" and the parent value goes to places other 
 than the parent.  This is all wrong, and shows my ignorance, 
 but is as far as I've been able to work it.

 In my operational code I bypassed the problem by storing every 
 leaf and branch in an array and changing the self, parent, and 
 children properties to be indices into said array.  But this 
 feels hackish: I'd rather have the tree itself hold everything 
 it needs.

 Note: I'm not stuck with a struct. I could class the thing, but 
 I figured that a struct holds what I need simply...

 Thoughts, solutions?
As far as I can see, you're overwriting old trees with new ones. You store everything in tile_trees, and use pointers to its data. When you then "Copy the trees into the branches and clear the forest for the next pass", the pointers still point to the now cleared old locations in tile_trees. You fill tile_trees again, reusing the same locations and pointers. Maybe try TileTree*[string] tile_trees; and auto new_tree = new TileTree(...); or make TileTree a class.
Aug 21 2014
prev sibling parent "monarch_dodra" <monarchdodra gmail.com> writes:
On Thursday, 21 August 2014 at 14:09:24 UTC, Ricky C wrote:
 TileTree* self;
Don't do that. D has banned internal pointers to implement its move semantics.
 branches[index] = tile_trees[index];
This will make a *value-copy* of your TileTree nodes. There are good chances it'll break your tree. As a rule of thumbn when managing an Node-like structure, try to avoid storing them by value in a container. Unless you are *exceptionally* about not copying the nodes from one container to another, you'll usually end up shooting yourself in the foot. This is particularly relevant what when you have an internal pointer, as the copy will point to the old node, not itself. Try to rewrite your code to use TileTree* objects instead: http://dpaste.dzfl.pl/13194a966c07
Aug 21 2014