46 #include <type_traits> 60 #ifndef NVWA_TREE_DEFAULT_STORAGE_POLICY 61 #define NVWA_TREE_DEFAULT_STORAGE_POLICY storage_policy::shared 65 template <
typename _Tp, storage_policy _Policy>
69 template <
typename _Tp>
72 typedef std::unique_ptr<_Tp> type;
76 template <
typename _Tp>
79 typedef std::shared_ptr<_Tp> type;
85 template <
typename _Tp,
90 typedef _Tp value_type;
92 typedef std::vector<tree_ptr> children_type;
93 typedef typename children_type::iterator iterator;
94 typedef typename children_type::const_iterator const_iterator;
96 tree() : _M_value(_Tp()) {}
97 template <
typename _Up>
98 explicit tree(_Up&& value, children_type children = {})
99 : _M_value(std::forward<_Up>(value))
100 , _M_children(std::move(children))
107 const _Tp& value()
const &
113 return std::move(_M_value);
115 tree_ptr& child(
unsigned index)
117 return _M_children[index];
119 const tree_ptr& child(
unsigned index)
const 121 return _M_children[index];
123 void push_back(tree_ptr ptr)
125 _M_children.push_back(std::move(ptr));
129 _M_children.pop_back();
133 return _M_children.begin();
135 const_iterator begin()
const 137 return _M_children.begin();
139 const_iterator cbegin()
const 141 return _M_children.begin();
145 return _M_children.end();
147 const_iterator end()
const 149 return _M_children.end();
151 const_iterator cend()
const 153 return _M_children.end();
155 bool has_child()
const 157 return !_M_children.empty();
159 template <
typename... Args>
160 void set_children(Args&&... args)
164 tree_ptr init[] = {std::forward<Args>(args)...};
165 children_type children(std::make_move_iterator(std::begin(init)),
166 std::make_move_iterator(std::end(init)));
167 _M_children.swap(children);
170 template <
typename... Args>
171 static children_type make_children(Args&&... args)
173 tree_ptr init[] = {std::forward<Args>(args)...};
174 children_type children(std::make_move_iterator(std::begin(init)),
175 std::make_move_iterator(std::end(init)));
179 template <
typename _Up>
180 static tree_ptr create(_Up&& value, children_type children)
182 return tree_ptr(
new tree(std::forward<_Up>(value),
183 std::move(children)));
186 static constexpr tree_ptr null()
193 children_type _M_children;
202 template <
storage_policy _Policy = NVWA_TREE_DEFAULT_STORAGE_POLICY,
208 std::forward<_Tp>(value), {});
218 template <
storage_policy _Policy = NVWA_TREE_DEFAULT_STORAGE_POLICY,
219 typename _Tp,
typename... Args>
224 std::forward<_Tp>(value),
233 template <
typename _Tree>
240 typedef int difference_type;
241 typedef _Tree value_type;
242 typedef _Tree* pointer_type;
243 typedef _Tree& reference;
244 typedef std::forward_iterator_tag iterator_category;
247 iterator(pointer_type root)
248 : _M_this_level({root})
250 _M_current = _M_this_level.begin();
253 reference operator*()
const 258 pointer_type operator->()
const 263 iterator& operator++()
266 for (
auto& child : **_M_current)
268 if (child != _Tree::null())
269 _M_next_level.push_back(&*child);
271 if (++_M_current == _M_this_level.end())
273 _M_this_level = std::move(_M_next_level);
274 _M_current = _M_this_level.begin();
278 iterator operator++(
int)
280 iterator temp(*
this);
286 return _M_current == _M_this_level.end();
288 bool operator==(
const iterator& rhs)
const 290 return (empty() && rhs.empty()) || _M_current == rhs._M_current;
292 bool operator!=(
const iterator& rhs)
const 294 return !operator==(rhs);
298 typename std::vector<pointer_type>::iterator _M_current;
299 std::vector<pointer_type> _M_this_level;
300 std::vector<pointer_type> _M_next_level;
309 return iterator(_M_root);
325 template <
typename _Tree>
332 typedef int difference_type;
333 typedef _Tree value_type;
334 typedef _Tree* pointer_type;
335 typedef _Tree& reference;
336 typedef std::forward_iterator_tag iterator_category;
338 iterator() : _M_current(
nullptr) {}
339 iterator(pointer_type root) : _M_current(root) {}
341 reference operator*()
const 346 pointer_type operator->()
const 351 iterator& operator++()
355 std::make_pair(_M_current->cbegin(), _M_current->cend()));
358 if (_M_stack.empty())
360 _M_current =
nullptr;
363 auto& top = _M_stack.top();
364 auto& next = top.first;
365 auto& end = top.second;
368 _M_current = &**next++;
369 if (_M_current !=
nullptr)
377 iterator operator++(
int)
379 iterator temp(*
this);
385 return _M_current ==
nullptr;
387 bool operator==(
const iterator& rhs)
const 389 return (empty() && rhs.empty()) || _M_current == rhs._M_current;
391 bool operator!=(
const iterator& rhs)
const 393 return !operator==(rhs);
397 pointer_type _M_current;
398 std::stack<std::pair<
typename _Tree::const_iterator,
399 typename _Tree::const_iterator>>
409 return iterator(_M_root);
425 template <
typename _Tree>
432 typedef int difference_type;
433 typedef _Tree value_type;
434 typedef _Tree* pointer_type;
435 typedef _Tree& reference;
436 typedef std::forward_iterator_tag iterator_category;
438 iterator() : _M_current(
nullptr) {}
439 iterator(pointer_type root)
441 _M_current = find_leftmost_child(root);
444 reference operator*()
const 449 pointer_type operator->()
const 454 iterator& operator++()
459 if (_M_stack.empty())
461 _M_current =
nullptr;
464 auto& top = _M_stack.top();
465 auto& node = std::get<0>(top);
466 auto& next = std::get<1>(top);
467 auto& end = std::get<2>(top);
482 _M_current = find_leftmost_child(&**it);
491 iterator operator++(
int)
493 iterator temp(*
this);
499 return _M_current ==
nullptr;
501 bool operator==(
const iterator& rhs)
const 503 return (empty() && rhs.empty()) || _M_current == rhs._M_current;
505 bool operator!=(
const iterator& rhs)
const 507 return !operator==(rhs);
511 pointer_type find_leftmost_child(pointer_type root)
513 using std::make_tuple;
517 if (!curr->has_child())
519 auto left_child = curr->cbegin();
520 auto next_child = left_child;
521 if (next_child != curr->cend())
526 make_tuple(curr, next_child, curr->cend()));
527 curr = &**left_child;
532 make_tuple(
nullptr, next_child, curr->cend()));
539 pointer_type _M_current;
540 std::stack<std::tuple<pointer_type,
541 typename _Tree::const_iterator,
542 typename _Tree::const_iterator>>
552 return iterator(_M_root);
563 template <
typename _Tree>
569 template <
typename _Tree>
575 template <
typename _Tree>
583 #endif // NVWA_TREE_H Members are directly owned.
Declaration of policy class to generate the smart pointer type.
Definition: tree.h:66
Iteration class for breadth-first traversal.
Definition: tree.h:234
Members may be shared and passed around.
tree< typename std::decay< _Tp >::type, _Policy >::tree_ptr create_tree(_Tp &&value, Args &&... args)
Creates a tree with children.
Definition: tree.h:221
Basic tree (node) class template that owns all its children.
Definition: tree.h:87
Iteration class for in-order traversal.
Definition: tree.h:426
storage_policy
_Policy class for how to store members.
Definition: tree.h:54
Iteration class for depth-first traversal.
Definition: tree.h:326
Common definitions for preprocessing.