Skip to content

Commit 42531ad

Browse files
committed
Add missing include <string>.
Add sample program for iterators <-> paths, and documentation for it. Remove compiler warnings.
1 parent 45760ce commit 42531ad

File tree

6 files changed

+61
-6
lines changed

6 files changed

+61
-6
lines changed

ChangeLog

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
2022-02-13 Kasper Peeters <[email protected]>
2+
3+
* Include <string> to avoid compilation errors.
4+
15
2020-11-07 Kasper Peeters <[email protected]>
26

37
* Release 3.17

doc/main.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,26 @@ keep effect for a single increment or decrement of the
108108
iterator. Finally, whether or not an iterator is actually pointing at
109109
a node (i.e.~is not an "end" iterator) can be tested using the
110110
``is_valid(iterator)`` member of the tree class.
111-
111+
112+
- **Paths**:
113+
114+
Tree iterators, like those for all STL containers, are tied to the
115+
container, and cannot simply be used to "access an element at the
116+
same location in a different tree" (they are, in essence, wrappers
117+
for pointers to memory locations). If you want a representation of
118+
the iterator which is independent of the particular location where
119+
your tree is located in memory, use the following two methods:
120+
121+
``path_from_iterator(iterator, top)``: obtain a ``path_t`` object
122+
from an iterator, relative to the iterator ``top``.
123+
124+
``iterator_from_path(path, top)``: the inverse of the above, that
125+
is, obtain an iterator from a ``path_t`` object.
126+
127+
Paths are simply vectors of integers, indicating which child branch
128+
to follow at every depth of the tree. See ``src/sample_path.cc`` for
129+
an example.
130+
112131

113132
Basic operations
114133
----------------

src/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ test1
44
test2
55
test3
66
test4
7+
sample_path
8+
test_tree

src/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ test2: test2.o
1313
test_tree: test_tree.o
1414
g++ -o test_tree test_tree.o
1515

16+
sample_path: sample_path.o
17+
g++ -o sample_path sample_path.o
18+
1619
run_tests: test1 test1.req
1720
./test1 > test1.res
1821
@diff test1.res test1.req

src/sample_path.cc

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
#include "tree.hh"
3+
#include <iostream>
4+
5+
// Sample to demonstrate how to turn a tree iterator into a
6+
// path_t, which is a vector of integers indicating which
7+
// child is taken at any depth.
8+
9+
int main(int argc, char **argv)
10+
{
11+
tree<std::string> tr;
12+
13+
tr.set_head("head");
14+
auto i1 = tr.append_child(tr.begin(), "one");
15+
auto i2 = tr.append_child(tr.begin(), "two");
16+
auto i3 = tr.append_child(tr.begin(), "three");
17+
auto i4 = tr.append_child(i2, "four");
18+
auto i5 = tr.append_child(i2, "five");
19+
20+
auto path = tr.path_from_iterator(i5, tr.begin());
21+
for(auto& p: path)
22+
std::cerr << p << "/";
23+
std::cerr << std::endl; // prints '0/1/1/'
24+
25+
auto fnd = tr.iterator_from_path(path, tr.begin());
26+
std::cerr << *fnd << std::endl; // prints 'five'
27+
}

src/tree.hh

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99

1010
/** \mainpage tree.hh
1111
\author Kasper Peeters
12-
\version 3.17
13-
\date 07-Nov-2020
12+
\version 3.18
13+
\date 13-Feb-2021
1414
\see http://tree.phi-sci.com/
1515
\see http://github.com/kpeeters/tree.hh/
1616
@@ -34,7 +34,7 @@
3434
#include <queue>
3535
#include <algorithm>
3636
#include <cstddef>
37-
37+
#include <string>
3838

3939
/// A node in the tree, combining links to other nodes as well as the actual data.
4040
template<class T>
@@ -86,7 +86,7 @@ class navigation_error : public std::logic_error {
8686
// std::cerr << boost::stacktrace::stacktrace() << std::endl;
8787
// str << boost::stacktrace::stacktrace();
8888
// stacktrace=str.str();
89-
};
89+
}
9090

9191
// virtual const char *what() const noexcept override
9292
// {
@@ -503,7 +503,7 @@ class tree {
503503
template<class StrictWeakOrdering>
504504
class compare_nodes {
505505
public:
506-
compare_nodes(StrictWeakOrdering comp) : comp_(comp) {};
506+
compare_nodes(StrictWeakOrdering comp) : comp_(comp) {}
507507

508508
bool operator()(const tree_node *a, const tree_node *b)
509509
{

0 commit comments

Comments
 (0)