annotate DEPENDENCIES/generic/include/boost/proto/transform/fold_tree.hpp @ 125:34e428693f5d vext

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