tree_traits.hpp 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // (C) Copyright Jeremy Siek 1999.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef BOOST_TREE_STRUCTURE_HPP
  6. #define BOOST_TREE_STRUCTURE_HPP
  7. namespace boost {
  8. template <class T>
  9. struct tree_traits {
  10. typedef typename T::node_descriptor node_descriptor;
  11. typedef typename T::children_iterator children_iterator;
  12. };
  13. template <class Tree, class TreeVisitor>
  14. void traverse_tree(typename tree_traits<Tree>::node_descriptor v,
  15. Tree& t, TreeVisitor visitor)
  16. {
  17. visitor.preorder(v, t);
  18. typename tree_traits<Tree>::children_iterator i, end;
  19. boost::tie(i, end) = children(v, t);
  20. if (i != end) {
  21. traverse_tree(*i++, t, visitor);
  22. visitor.inorder(v, t);
  23. while (i != end)
  24. traverse_tree(*i++, t, visitor);
  25. } else
  26. visitor.inorder(v, t);
  27. visitor.postorder(v, t);
  28. }
  29. struct null_tree_visitor {
  30. template <typename Node, typename Tree> void preorder(Node, Tree&) { }
  31. template <typename Node, typename Tree> void inorder(Node, Tree&) { }
  32. template <typename Node, typename Tree> void postorder(Node, Tree&) { }
  33. };
  34. } /* namespace boost */
  35. #endif /* BOOST_TREE_STRUCTURE_HPP */