kpeeters / tree.hh

An STL-like C++ header-only tree library
GNU General Public License v3.0
132 stars 31 forks source link

Example to fixed_depth_iterator / missing end-iterator for fixed_depth_iterator #5

Open etiwal opened 4 years ago

etiwal commented 4 years ago

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!

kpeeters commented 4 years ago

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.

jmccabe commented 1 year ago
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;
}
kpeeters commented 1 year ago

@jmccabe Yes, sorry.