tree_traits.hpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. #include <boost/tuple/tuple.hpp> //For boost::tie()
  8. namespace boost
  9. {
  10. template < class T > struct tree_traits
  11. {
  12. typedef typename T::node_descriptor node_descriptor;
  13. typedef typename T::children_iterator children_iterator;
  14. };
  15. template < class Tree, class TreeVisitor >
  16. void traverse_tree(typename tree_traits< Tree >::node_descriptor v, Tree& t,
  17. TreeVisitor visitor)
  18. {
  19. visitor.preorder(v, t);
  20. typename tree_traits< Tree >::children_iterator i, end;
  21. boost::tie(i, end) = children(v, t);
  22. if (i != end)
  23. {
  24. traverse_tree(*i++, t, visitor);
  25. visitor.inorder(v, t);
  26. while (i != end)
  27. traverse_tree(*i++, t, visitor);
  28. }
  29. else
  30. visitor.inorder(v, t);
  31. visitor.postorder(v, t);
  32. }
  33. struct null_tree_visitor
  34. {
  35. template < typename Node, typename Tree > void preorder(Node, Tree&) {}
  36. template < typename Node, typename Tree > void inorder(Node, Tree&) {}
  37. template < typename Node, typename Tree > void postorder(Node, Tree&) {}
  38. };
  39. } /* namespace boost */
  40. #endif /* BOOST_TREE_STRUCTURE_HPP */