fold_tree.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. ///////////////////////////////////////////////////////////////////////////////
  2. /// \file fold_tree.hpp
  3. /// Contains definition of the fold_tree<> and reverse_fold_tree<> transforms.
  4. //
  5. // Copyright 2008 Eric Niebler. Distributed under the Boost
  6. // Software License, Version 1.0. (See accompanying file
  7. // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  8. #ifndef BOOST_PROTO_TRANSFORM_FOLD_TREE_HPP_EAN_11_05_2007
  9. #define BOOST_PROTO_TRANSFORM_FOLD_TREE_HPP_EAN_11_05_2007
  10. #include <boost/type_traits/is_same.hpp>
  11. #include <boost/proto/proto_fwd.hpp>
  12. #include <boost/proto/traits.hpp>
  13. #include <boost/proto/matches.hpp>
  14. #include <boost/proto/transform/fold.hpp>
  15. #include <boost/proto/transform/impl.hpp>
  16. namespace boost { namespace proto
  17. {
  18. namespace detail
  19. {
  20. template<typename Tag>
  21. struct has_tag
  22. {
  23. template<typename Expr, typename State, typename Data, typename EnableIf = Tag>
  24. struct impl
  25. {
  26. typedef mpl::false_ result_type;
  27. };
  28. template<typename Expr, typename State, typename Data>
  29. struct impl<Expr, State, Data, typename Expr::proto_tag>
  30. {
  31. typedef mpl::true_ result_type;
  32. };
  33. template<typename Expr, typename State, typename Data>
  34. struct impl<Expr &, State, Data, typename Expr::proto_tag>
  35. {
  36. typedef mpl::true_ result_type;
  37. };
  38. };
  39. template<typename Tag, typename Fun>
  40. struct fold_tree_
  41. : if_<has_tag<Tag>, fold<_, _state, fold_tree_<Tag, Fun> >, Fun>
  42. {};
  43. template<typename Tag, typename Fun>
  44. struct reverse_fold_tree_
  45. : if_<has_tag<Tag>, reverse_fold<_, _state, reverse_fold_tree_<Tag, Fun> >, Fun>
  46. {};
  47. }
  48. /// \brief A PrimitiveTransform that recursively applies the
  49. /// <tt>fold\<\></tt> transform to sub-trees that all share a common
  50. /// tag type.
  51. ///
  52. /// <tt>fold_tree\<\></tt> is useful for flattening trees into lists;
  53. /// for example, you might use <tt>fold_tree\<\></tt> to flatten an
  54. /// expression tree like <tt>a | b | c</tt> into a Fusion list like
  55. /// <tt>cons(c, cons(b, cons(a)))</tt>.
  56. ///
  57. /// <tt>fold_tree\<\></tt> is easily understood in terms of a
  58. /// <tt>recurse_if_\<\></tt> helper, defined as follows:
  59. ///
  60. /// \code
  61. /// template<typename Tag, typename Fun>
  62. /// struct recurse_if_
  63. /// : if_<
  64. /// // If the current node has type "Tag" ...
  65. /// is_same<tag_of<_>, Tag>()
  66. /// // ... recurse, otherwise ...
  67. /// , fold<_, _state, recurse_if_<Tag, Fun> >
  68. /// // ... apply the Fun transform.
  69. /// , Fun
  70. /// >
  71. /// {};
  72. /// \endcode
  73. ///
  74. /// With <tt>recurse_if_\<\></tt> as defined above,
  75. /// <tt>fold_tree\<Sequence, State0, Fun\>()(e, s, d)</tt> is
  76. /// equivalent to
  77. /// <tt>fold<Sequence, State0, recurse_if_<Expr::proto_tag, Fun> >()(e, s, d).</tt>
  78. /// It has the effect of folding a tree front-to-back, recursing into
  79. /// child nodes that share a tag type with the parent node.
  80. template<typename Sequence, typename State0, typename Fun>
  81. struct fold_tree
  82. : transform<fold_tree<Sequence, State0, Fun> >
  83. {
  84. template<typename Expr, typename State, typename Data>
  85. struct impl
  86. : fold<
  87. Sequence
  88. , State0
  89. , detail::fold_tree_<typename Expr::proto_tag, Fun>
  90. >::template impl<Expr, State, Data>
  91. {};
  92. template<typename Expr, typename State, typename Data>
  93. struct impl<Expr &, State, Data>
  94. : fold<
  95. Sequence
  96. , State0
  97. , detail::fold_tree_<typename Expr::proto_tag, Fun>
  98. >::template impl<Expr &, State, Data>
  99. {};
  100. };
  101. /// \brief A PrimitiveTransform that recursively applies the
  102. /// <tt>reverse_fold\<\></tt> transform to sub-trees that all share
  103. /// a common tag type.
  104. ///
  105. /// <tt>reverse_fold_tree\<\></tt> is useful for flattening trees into
  106. /// lists; for example, you might use <tt>reverse_fold_tree\<\></tt> to
  107. /// flatten an expression tree like <tt>a | b | c</tt> into a Fusion list
  108. /// like <tt>cons(a, cons(b, cons(c)))</tt>.
  109. ///
  110. /// <tt>reverse_fold_tree\<\></tt> is easily understood in terms of a
  111. /// <tt>recurse_if_\<\></tt> helper, defined as follows:
  112. ///
  113. /// \code
  114. /// template<typename Tag, typename Fun>
  115. /// struct recurse_if_
  116. /// : if_<
  117. /// // If the current node has type "Tag" ...
  118. /// is_same<tag_of<_>, Tag>()
  119. /// // ... recurse, otherwise ...
  120. /// , reverse_fold<_, _state, recurse_if_<Tag, Fun> >
  121. /// // ... apply the Fun transform.
  122. /// , Fun
  123. /// >
  124. /// {};
  125. /// \endcode
  126. ///
  127. /// With <tt>recurse_if_\<\></tt> as defined above,
  128. /// <tt>reverse_fold_tree\<Sequence, State0, Fun\>()(e, s, d)</tt> is
  129. /// equivalent to
  130. /// <tt>reverse_fold<Sequence, State0, recurse_if_<Expr::proto_tag, Fun> >()(e, s, d).</tt>
  131. /// It has the effect of folding a tree back-to-front, recursing into
  132. /// child nodes that share a tag type with the parent node.
  133. template<typename Sequence, typename State0, typename Fun>
  134. struct reverse_fold_tree
  135. : transform<reverse_fold_tree<Sequence, State0, Fun> >
  136. {
  137. template<typename Expr, typename State, typename Data>
  138. struct impl
  139. : reverse_fold<
  140. Sequence
  141. , State0
  142. , detail::reverse_fold_tree_<typename Expr::proto_tag, Fun>
  143. >::template impl<Expr, State, Data>
  144. {};
  145. template<typename Expr, typename State, typename Data>
  146. struct impl<Expr &, State, Data>
  147. : reverse_fold<
  148. Sequence
  149. , State0
  150. , detail::reverse_fold_tree_<typename Expr::proto_tag, Fun>
  151. >::template impl<Expr &, State, Data>
  152. {};
  153. };
  154. /// INTERNAL ONLY
  155. ///
  156. template<typename Sequence, typename State0, typename Fun>
  157. struct is_callable<fold_tree<Sequence, State0, Fun> >
  158. : mpl::true_
  159. {};
  160. /// INTERNAL ONLY
  161. ///
  162. template<typename Sequence, typename State0, typename Fun>
  163. struct is_callable<reverse_fold_tree<Sequence, State0, Fun> >
  164. : mpl::true_
  165. {};
  166. }}
  167. #endif