|
Boost Users : |
Subject: [Boost-users] [Proto] proto::reverse_fold_tree
From: Kim Kuen Tang (kuentang_at_[hidden])
Date: 2009-02-01 06:31:06
Hi all,
i want to use the function proto::reverse_fold_tree to build a
heterogeneous list from the expression "((a*b+c)*d+e)*f". The resulting
sequence only contains the two terminals 'a' and b'. So i am a little
bit confused.
But using the function proto::fold_tree it produces correctly the
sequence [f,e,d,c,b,a]. (Btw the two functions proto::fold_tree and
proto::fold produce the same sequence [f,e,d,c,b,a], what is their
difference? )
Here is the code:
#include <iostream>
#include <boost/proto/proto.hpp>
#include <boost/proto/proto_typeof.hpp>
#include <boost/fusion/container.hpp>
#include <boost/fusion/algorithm/iteration.hpp>
#include <boost/fusion/include/iteration.hpp>
#include <boost/typeof/typeof.hpp>
namespace mpl = boost::mpl;
namespace proto = boost::proto;
namespace fusion = boost::fusion;
using proto::_;
proto::terminal<char>::type const a = {'a'};
proto::terminal<char>::type const b = {'b'};
proto::terminal<char>::type const c = {'c'};
proto::terminal<char>::type const d = {'d'};
proto::terminal<char>::type const e = {'e'};
proto::terminal<char>::type const f = {'f'};
struct FoldToList
: proto::or_<
proto::when<proto::terminal<_>,fusion::cons<proto::_value,
proto::_state>(proto::_value, proto::_state) >
,proto::when<proto::nary_expr<_,proto::vararg<_> >,
proto::fold_tree<proto::_,fusion::nil(),FoldToList > >
>
{};
struct FoldToListReverse
: proto::or_<
proto::when<proto::terminal<_>,fusion::cons<proto::_value,
proto::_state>(proto::_value, proto::_state) >
,proto::when<proto::nary_expr<_,proto::vararg<_> >,
proto::reverse_fold_tree<proto::_,fusion::nil(),FoldToListReverse > >
>
{};
struct print
{
typedef void result_type;
template<class T>
result_type
operator()(T const& t) const
{
std::cout<<t<<"\t";
}
};
int main()
{
BOOST_PROTO_AUTO(expr, ((a*b+c)*d+e)*f );
proto::display_expr(expr);
BOOST_MPL_ASSERT(( proto::matches<BOOST_TYPEOF(expr), FoldToList>));
BOOST_MPL_ASSERT(( proto::matches<BOOST_TYPEOF(expr),
FoldToListReverse>));
BOOST_AUTO(sequence1,FoldToList()(expr));
fusion::for_each(sequence1,print());
std::cout << "\n--------------------\n";
BOOST_AUTO(sequence2,FoldToListReverse()(expr));
fusion::for_each(sequence2,print());
std::cout << "\n--------------------\n";
}
Cheers
Kim Tang
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net