tree.hh icon indicating copy to clipboard operation
tree.hh copied to clipboard

Example to fixed_depth_iterator / missing end-iterator for fixed_depth_iterator

Open etiwal opened this issue 4 years ago • 3 comments

When i try to use the fixed_depth iteration i am running into issues.

First of all this assertion always blocks my code from running: assert(1==0); // FIXME: not correct yet: use is_valid() as a temporary workaround

Also i have issues when using the begin_fixed() and end_fixed() to define the range of my iteration.

could you maybe provide an example on how to properly iterate over all the nodes at a certain depth and within this iteration create a child for every node?

Any help is greatly appreciated!

etiwal avatar Oct 28 '20 07:10 etiwal

The fixed-depth iterators lack proper STL behaviour in the sense that there is no end-iterator. But you can still iterate over elements at a fixed depth by getting the begin-iterator using auto it = begin_fixed(...) and then iterating until it.is_valid() no longer returns true. So something like

auto it = tr.begin_fixed(tr.begin(), 3);
while(it.is_valid()) {
    tr.append_child(it, ...);
    ++it;
}

Hope this helps; if not, ask again.

kpeeters avatar Nov 07 '20 14:11 kpeeters

auto it = tr.begin_fixed(tr.begin(), 3);
while(it.is_valid()) {
    tr.append_child(it, ...);
    ++it;
}

Sorry to revive an old question, but (unless I'm missing something) it looks like a fixed_depth_iterator doesn't have a is_valid() method; should this be using something like:

auto it = tr.begin_fixed(tr.begin(), 3);
while (tr.is_valid(it)) {
    tr.append_child(it, ...);
    ++it;
}

jmccabe avatar Oct 09 '23 12:10 jmccabe

@jmccabe Yes, sorry.

kpeeters avatar Oct 09 '23 18:10 kpeeters