Skip to content

Commit

Permalink
Fix fixed-depth iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
kpeeters committed Apr 25, 2018
1 parent f2996d2 commit c6d1eec
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
7 changes: 7 additions & 0 deletions ChangeLog
@@ -1,3 +1,10 @@
2018-04-23 Kasper Peeters <kasper.peeters@phi-sci.com>

* Release 3.8.

* Make fixed-depth iterators start at the first node in a range of
siblings.

2018-01-30 Kasper Peeters <kasper.peeters@phi-sci.com>

* Release 3.6.
Expand Down
22 changes: 22 additions & 0 deletions examples/iterate_head.cc
@@ -0,0 +1,22 @@
#include "tree.hh"
#include <iostream>

// This example shows how to iterate over the head nodes
// of a tree only.

int main(int, char **)
{
tree<std::string> t;

t.set_head("one");
auto i2 = t.insert(t.end(), "two");
auto i3 = t.insert(t.end(), "three");
t.append_child(i2, "apple");
t.append_child(i2, "pear");
t.append_child(i3, "banana");
t.append_child(i3, "kiwi");

tree<std::string>::sibling_iterator it=t.begin();
while(it!=t.end())
std::cerr << (*it++) << std::endl;
}
33 changes: 33 additions & 0 deletions examples/move_subtree.cc
@@ -0,0 +1,33 @@
#include <iostream>
#include "tree.hh"
#include "tree_util.hh"

void print_tree(const tree<std::string>& tr)
{
tree<std::string>::pre_order_iterator it = tr.begin();
tree<std::string>::pre_order_iterator end = tr.end();
if(!tr.is_valid(it)) return;
int rootdepth=tr.depth(it);
std::cout << "-----" << std::endl;
while(it!=end) {
for(int i=0; i<tr.depth(it)-rootdepth; ++i)
std::cout << " ";
std::cout << (*it) << std::endl << std::flush;
++it;
}
std::cout << "-----" << std::endl;
}

int main(int, char **)
{
tree<std::string> my_tree;

tree<std::string>::iterator iA = my_tree.insert(my_tree.end(), "A");
tree<std::string>::iterator iB = my_tree.insert(my_tree.end(), "B");
tree<std::string>::iterator iB1 = my_tree.append_child(iB, "B1");
tree<std::string>::iterator iC = my_tree.insert(my_tree.end(), "C");
print_tree(my_tree);

my_tree.move_after(iB1, iC);
print_tree(my_tree);
}
16 changes: 16 additions & 0 deletions src/test_tree.cc
Expand Up @@ -365,6 +365,22 @@ int main(int argc, char **argv)
++li;
}

// Fixed-depth.
tree<std::string> fdt;
fdt.set_head("beings");
auto mammals = fdt.append_child(fdt.begin(), "mammals");
fdt.append_child(fdt.begin(), "birds");
fdt.append_child(mammals, "human");
auto felins = fdt.append_child(mammals, "felins");
fdt.append_child(mammals, "canids");
fdt.append_child(felins, "cats");

auto fi = tr.begin_fixed(felins, 0);
while(fdt.is_valid(fi)) {
std::cout << *fi << std::endl;
++fi;
}

// test_move_constructor();

}
Expand Down
8 changes: 6 additions & 2 deletions src/tree.hh
Expand Up @@ -9,8 +9,8 @@

/** \mainpage tree.hh
\author Kasper Peeters
\version 3.7
\date 30-Jan-2018
\version 3.8
\date 23-Apr-2018
\see http://tree.phi-sci.com/
\see http://tree.phi-sci.com/ChangeLog
Expand Down Expand Up @@ -792,6 +792,10 @@ typename tree<T, tree_node_allocator>::fixed_depth_iterator tree<T, tree_node_al
++curdepth;
}

// Now walk back to the first sibling in this range.
while(tmp->prev_sibling!=0)
tmp=tmp->prev_sibling;

ret.node=tmp;
return ret;
}
Expand Down

0 comments on commit c6d1eec

Please sign in to comment.