added reverse_fold/iter_fold/reverse_iter_fold (1)

[SVN r63066]
This commit is contained in:
Christopher Schmidt
2010-06-17 21:21:37 +00:00
parent 7d14098b21
commit ea89db72cb
18 changed files with 782 additions and 304 deletions

View File

@ -8,11 +8,12 @@
#if !defined(BOOST_FUSION_ADAPTED_30122005_1420)
#define BOOST_FUSION_ADAPTED_30122005_1420
#include <boost/fusion/adapted/boost_tuple.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/fusion/adapted/array.hpp>
#include <boost/fusion/adapted/mpl.hpp>
#include <boost/fusion/adapted/struct.hpp>
#include <boost/fusion/adapted/boost_array.hpp>
#include <boost/fusion/adapted/boost_tuple.hpp>
#include <boost/fusion/adapted/class.hpp>
#include <boost/fusion/adapted/mpl.hpp>
#include <boost/fusion/adapted/std_pair.hpp>
#include <boost/fusion/adapted/struct.hpp>
#endif

View File

@ -10,5 +10,8 @@
#include <boost/fusion/algorithm/iteration/accumulate.hpp>
#include <boost/fusion/algorithm/iteration/fold.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
#include <boost/fusion/algorithm/iteration/iter_fold.hpp>
#include <boost/fusion/algorithm/iteration/reverse_fold.hpp>
#include <boost/fusion/algorithm/iteration/reverse_iter_fold.hpp>
#endif

View File

@ -1,278 +1,434 @@
/*=============================================================================
Copyright (c) 2001-2006 Joel de Guzman
Copyright (c) 2006 Dan Marsden
Copyright (c) 2009-2010 Christopher Schmidt
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#if !defined(BOOST_FUSION_FOLD_HPP_20070528_1253)
#define BOOST_FUSION_FOLD_HPP_20070528_1253
#include <boost/mpl/bool.hpp>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/fusion/iterator/equal_to.hpp>
#ifndef BOOST_FUSION_ALGORITHM_ITERATION_DETAIL_FOLD_HPP
#define BOOST_FUSION_ALGORITHM_ITERATION_DETAIL_FOLD_HPP
#include <boost/config.hpp>
#include <boost/fusion/sequence/intrinsic/begin.hpp>
#include <boost/fusion/sequence/intrinsic/end.hpp>
#include <boost/fusion/sequence/intrinsic/empty.hpp>
#include <boost/fusion/sequence/intrinsic/size.hpp>
#include <boost/fusion/iterator/equal_to.hpp>
#include <boost/fusion/iterator/deref.hpp>
#include <boost/fusion/iterator/value_of.hpp>
#include <boost/fusion/iterator/prior.hpp>
#include <boost/fusion/iterator/next.hpp>
#include <boost/fusion/iterator/distance.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/bool.hpp>
#include <boost/utility/result_of.hpp>
#include <boost/type_traits/add_const.hpp>
#include <boost/type_traits/add_reference.hpp>
namespace boost { namespace fusion {
namespace result_of
{
template <typename Sequence, typename State, typename F>
struct fold;
}
namespace detail
{
template <typename F>
struct apply_fold_result
{
template <typename State, typename Value>
struct apply
: boost::result_of<F(State, Value)>
{};
};
template <typename State, typename Iterator, typename F>
struct fold_apply
{
typedef typename result_of::deref<Iterator>::type dereferenced;
typedef typename add_reference<typename add_const<State>::type>::type lvalue_state;
typedef typename boost::result_of<F(lvalue_state, dereferenced)>::type type;
};
template <typename First, typename Last, typename State, typename F>
struct static_fold;
template <typename First, typename Last, typename State, typename F>
struct next_result_of_fold
{
typedef typename
static_fold<
typename result_of::next<First>::type
, Last
, typename fold_apply<State, First, F>::type
, F
>::type
type;
};
template <typename First, typename Last, typename State, typename F>
struct static_fold
{
typedef typename
mpl::if_<
result_of::equal_to<First, Last>
, mpl::identity<State>
, next_result_of_fold<First, Last, State, F>
>::type
result;
typedef typename result::type type;
};
template<typename State, typename I0, typename F, int N>
struct result_of_unrolled_fold;
template<int N>
struct unrolled_fold
{
template<typename State, typename I0, typename F>
static typename result_of_unrolled_fold<State, I0, F, N>::type
call(State const& state, I0 const& i0, F f)
{
typedef typename result_of::next<I0>::type I1;
I1 i1 = fusion::next(i0);
typedef typename result_of::next<I1>::type I2;
I2 i2 = fusion::next(i1);
typedef typename result_of::next<I2>::type I3;
I3 i3 = fusion::next(i2);
typedef typename result_of::next<I3>::type I4;
I4 i4 = fusion::next(i3);
return unrolled_fold<N-4>::call(f(f(f(f(state, *i0), *i1), *i2), *i3), i4, f);
}
};
template<>
struct unrolled_fold<3>
{
template<typename State, typename I0, typename F>
static typename result_of_unrolled_fold<State, I0, F, 3>::type
call(State const& state, I0 const& i0, F f)
{
typedef typename result_of::next<I0>::type I1;
I1 i1 = fusion::next(i0);
typedef typename result_of::next<I1>::type I2;
I2 i2 = fusion::next(i1);
return f(f(f(state, *i0), *i1), *i2);
}
};
template<>
struct unrolled_fold<2>
{
template<typename State, typename I0, typename F>
static typename result_of_unrolled_fold<State, I0, F, 2>::type
call(State const& state, I0 const& i0, F f)
{
typedef typename result_of::next<I0>::type I1;
I1 i1 = fusion::next(i0);
return f(f(state, *i0), *i1);
}
};
template<>
struct unrolled_fold<1>
{
template<typename State, typename I0, typename F>
static typename result_of_unrolled_fold<State, I0, F, 1>::type
call(State const& state, I0 const& i0, F f)
{
return f(state, *i0);
}
};
template<>
struct unrolled_fold<0>
{
template<typename State, typename I0, typename F>
static State call(State const& state, I0 const&, F)
{
return state;
}
};
// terminal case
template <typename First, typename Last, typename State, typename F>
inline State const&
linear_fold(First const&, Last const&, State const& state, F, mpl::true_)
{
return state;
}
// non-terminal case
template <typename First, typename Last, typename State, typename F>
inline typename static_fold<First, Last, State, F>::type
linear_fold(
First const& first
, Last const& last
, State const& state
, F f
, mpl::false_)
{
return detail::linear_fold(
fusion::next(first)
, last
, f(state, *first)
, f
, result_of::equal_to<typename result_of::next<First>::type, Last>()
);
}
template<typename State, typename I0, typename F, int N>
struct result_of_unrolled_fold
{
typedef typename result_of::next<I0>::type I1;
typedef typename result_of::next<I1>::type I2;
typedef typename result_of::next<I2>::type I3;
typedef typename result_of::next<I3>::type I4;
typedef typename fold_apply<State, I0, F>::type Rest1;
typedef typename fold_apply<Rest1, I1, F>::type Rest2;
typedef typename fold_apply<Rest2, I2, F>::type Rest3;
typedef typename fold_apply<Rest3, I3, F>::type Rest4;
typedef typename result_of_unrolled_fold<Rest4, I4, F, N-4>::type type;
};
template<typename State, typename I0, typename F>
struct result_of_unrolled_fold<State, I0, F, 3>
{
typedef typename result_of::next<I0>::type I1;
typedef typename result_of::next<I1>::type I2;
typedef typename fold_apply<State, I0, F>::type Rest;
typedef typename fold_apply<Rest, I1, F>::type Rest2;
typedef typename fold_apply<Rest2, I2, F>::type type;
};
template<typename State, typename I0, typename F>
struct result_of_unrolled_fold<State, I0, F, 2>
{
typedef typename result_of::next<I0>::type I1;
typedef typename fold_apply<State, I0, F>::type Rest;
typedef typename fold_apply<Rest, I1, F>::type type;
};
template<typename State, typename I0, typename F>
struct result_of_unrolled_fold<State, I0, F, 1>
{
typedef typename fold_apply<State, I0, F>::type type;
};
template<typename State, typename I0, typename F>
struct result_of_unrolled_fold<State, I0, F, 0>
{
typedef State type;
};
template<typename Sequence, typename State, typename F, bool>
struct choose_fold;
template<typename Sequence, typename State, typename F>
struct choose_fold<Sequence, State, F, true>
{
typedef typename result_of::begin<Sequence>::type begin;
typedef typename result_of::end<Sequence>::type end;
typedef typename result_of_unrolled_fold<
State, begin, F, result_of::distance<begin, end>::type::value>::type type;
};
template<typename Sequence, typename State, typename F>
struct choose_fold<Sequence, State, F, false>
{
typedef typename
detail::static_fold<
typename result_of::begin<Sequence>::type
, typename result_of::end<Sequence>::type
, State
, F
>::type
type;
};
template<typename Sequence, typename State, typename F, typename Tag>
typename result_of::fold<Sequence, State, F>::type
fold(Sequence& seq, State const& state, F f, Tag)
{
return linear_fold(
fusion::begin(seq)
, fusion::end(seq)
, state
, f
, result_of::equal_to<
typename result_of::begin<Sequence>::type
, typename result_of::end<Sequence>::type>()
);
}
template<typename Sequence, typename State, typename F>
typename result_of::fold<Sequence, State, F>::type
fold(Sequence& seq, State const& state, F f, random_access_traversal_tag)
{
typedef typename result_of::begin<Sequence>::type begin;
typedef typename result_of::end<Sequence>::type end;
return unrolled_fold<result_of::distance<begin, end>::type::value>::call(
state
, fusion::begin(seq)
, f);
}
}}}
#endif
#ifdef BOOST_FUSION_REVERSE_FOLD
# ifdef BOOST_FUSION_ITER_FOLD
# define BOOST_FUSION_FOLD_NAME reverse_iter_fold
# else
# define BOOST_FUSION_FOLD_NAME reverse_fold
# endif
# define BOOST_FUSION_FOLD_IMPL_FIRST_IT_FUNCTION end
# define BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION prior
# define BOOST_FUSION_FOLD_IMPL_FIRST_IT_META_TRANSFORM(IT) \
typename fusion::result_of::prior<IT>::type
# define BOOST_FUSION_FOLD_IMPL_FIRST_IT_TRANSFORM(IT) fusion::prior(IT)
#else
# ifdef BOOST_FUSION_ITER_FOLD
# define BOOST_FUSION_FOLD_NAME iter_fold
# else
# define BOOST_FUSION_FOLD_NAME fold
# endif
# define BOOST_FUSION_FOLD_IMPL_FIRST_IT_FUNCTION begin
# define BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION next
# define BOOST_FUSION_FOLD_IMPL_FIRST_IT_META_TRANSFORM(IT) IT
# define BOOST_FUSION_FOLD_IMPL_FIRST_IT_TRANSFORM(IT) IT
#endif
#ifdef BOOST_FUSION_ITER_FOLD
# define BOOST_FUSION_FOLD_IMPL_INVOKE_IT_META_TRANSFORM(IT) IT&
# define BOOST_FUSION_FOLD_IMPL_INVOKE_IT_TRANSFORM(IT) IT
#else
# define BOOST_FUSION_FOLD_IMPL_INVOKE_IT_META_TRANSFORM(IT) \
typename fusion::result_of::deref<IT>::type
# define BOOST_FUSION_FOLD_IMPL_INVOKE_IT_TRANSFORM(IT) fusion::deref(IT)
#endif
namespace boost { namespace fusion
{
namespace detail
{
template<typename State, typename It, typename F>
struct BOOST_PP_CAT(BOOST_FUSION_FOLD_NAME, _rvalue_state)
: boost::result_of<
F(
typename add_reference<typename add_const<State>::type>::type,
BOOST_FUSION_FOLD_IMPL_INVOKE_IT_META_TRANSFORM(It))
>
{};
template<typename Result,int N>
struct BOOST_PP_CAT(unrolled_,BOOST_FUSION_FOLD_NAME)
{
template<typename State, typename It0, typename F>
static Result
call(State const& state,It0 const& it0,F f)
{
typedef typename
result_of::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION<
It0 const
>::type
It1;
It1 it1 = fusion::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION(it0);
typedef typename
result_of::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION<
It1
>::type
It2;
It2 it2 = fusion::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION(it1);
typedef typename
result_of::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION<
It2
>::type
It3;
It3 it3 = fusion::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION(it2);
return BOOST_PP_CAT(unrolled_,BOOST_FUSION_FOLD_NAME)<
Result
, N-4
>::call(
f(
f(
f(
f(
state,
BOOST_FUSION_FOLD_IMPL_INVOKE_IT_TRANSFORM(
it0)
),
BOOST_FUSION_FOLD_IMPL_INVOKE_IT_TRANSFORM(it1)
),
BOOST_FUSION_FOLD_IMPL_INVOKE_IT_TRANSFORM(it2)
),
BOOST_FUSION_FOLD_IMPL_INVOKE_IT_TRANSFORM(it3)
),
fusion::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION(it3),
f);
}
};
template<typename Result>
struct BOOST_PP_CAT(unrolled_,BOOST_FUSION_FOLD_NAME)<Result,3>
{
template<typename State, typename It0, typename F>
static Result
call(State const& state,It0 const& it0,F f)
{
typedef typename
result_of::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION<
It0 const
>::type
It1;
It1 it1 = fusion::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION(it0);
typedef typename
result_of::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION<
It1
>::type
It2;
It2 it2 = fusion::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION(it1);
return f(
f(
f(
state,
BOOST_FUSION_FOLD_IMPL_INVOKE_IT_TRANSFORM(it0)
),
BOOST_FUSION_FOLD_IMPL_INVOKE_IT_TRANSFORM(it1)
),
BOOST_FUSION_FOLD_IMPL_INVOKE_IT_TRANSFORM(
fusion::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION(it1)
));
}
};
template<typename Result>
struct BOOST_PP_CAT(unrolled_,BOOST_FUSION_FOLD_NAME)<Result,2>
{
template<typename State, typename It0, typename F>
static Result
call(State const& state,It0 const& it0,F f)
{
return f(
f(state,
BOOST_FUSION_FOLD_IMPL_INVOKE_IT_TRANSFORM(it0)),
BOOST_FUSION_FOLD_IMPL_INVOKE_IT_TRANSFORM(
fusion::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION(it0)));
}
};
template<typename Result>
struct BOOST_PP_CAT(unrolled_,BOOST_FUSION_FOLD_NAME)<Result,1>
{
template<typename State, typename It0, typename F>
static Result
call(State const& state,It0 const& it0,F f)
{
return f(state,
BOOST_FUSION_FOLD_IMPL_INVOKE_IT_TRANSFORM(it0));
}
};
template<typename Result>
struct BOOST_PP_CAT(unrolled_,BOOST_FUSION_FOLD_NAME)<Result,0>
{
template<typename State, typename It0, typename F>
static Result
call(State const& state,It0 const& it0,F f)
{
return static_cast<Result>(state);
}
};
template<typename StateRef, typename It0, typename F, int N>
struct BOOST_PP_CAT(result_of_unrolled_,BOOST_FUSION_FOLD_NAME)
{
typedef typename
BOOST_PP_CAT(BOOST_FUSION_FOLD_NAME, _rvalue_state)<
StateRef
, It0 const
, F
>::type
rest1;
typedef typename
result_of::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION<
It0 const
>::type
it1;
typedef typename
BOOST_PP_CAT(BOOST_FUSION_FOLD_NAME, _rvalue_state)<
rest1
, it1
, F
>::type
rest2;
typedef typename
result_of::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION<it1>::type
it2;
typedef typename
BOOST_PP_CAT(BOOST_FUSION_FOLD_NAME, _rvalue_state)<
rest2
, it2
, F
>::type
rest3;
typedef typename
result_of::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION<it2>::type
it3;
typedef typename
BOOST_PP_CAT(result_of_unrolled_,BOOST_FUSION_FOLD_NAME)<
typename BOOST_PP_CAT(
BOOST_FUSION_FOLD_NAME, _rvalue_state)<
rest3
, it3
, F
>::type
, typename result_of::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION<
it3
>::type
, F
, N-4
>::type
type;
};
template<typename StateRef, typename It0, typename F>
struct BOOST_PP_CAT(result_of_unrolled_,BOOST_FUSION_FOLD_NAME)<
StateRef
, It0
, F
, 3
>
{
typedef typename
BOOST_PP_CAT(BOOST_FUSION_FOLD_NAME, _rvalue_state)<
StateRef
, It0 const
, F
>::type
rest1;
typedef typename
result_of::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION<
It0 const
>::type
it1;
typedef typename
BOOST_PP_CAT(BOOST_FUSION_FOLD_NAME, _rvalue_state)<
typename BOOST_PP_CAT(
BOOST_FUSION_FOLD_NAME, _rvalue_state)<
rest1
, it1
, F
>::type
, typename result_of::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION<
it1 const
>::type const
, F
>::type
type;
};
template<typename StateRef, typename It0, typename F>
struct BOOST_PP_CAT(result_of_unrolled_,BOOST_FUSION_FOLD_NAME)<
StateRef
, It0
, F
, 2
>
: BOOST_PP_CAT(BOOST_FUSION_FOLD_NAME, _rvalue_state)<
typename BOOST_PP_CAT(BOOST_FUSION_FOLD_NAME, _rvalue_state)<
StateRef
, It0 const
, F
>::type
, typename result_of::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION<
It0 const
>::type const
, F
>
{};
template<typename StateRef, typename It0, typename F>
struct BOOST_PP_CAT(result_of_unrolled_,BOOST_FUSION_FOLD_NAME)<
StateRef
, It0
, F
, 1
>
: BOOST_PP_CAT(BOOST_FUSION_FOLD_NAME, _rvalue_state)<
StateRef
, It0 const
, F
>
{};
template<typename StateRef, typename It0, typename F>
struct BOOST_PP_CAT(result_of_unrolled_,BOOST_FUSION_FOLD_NAME)<
StateRef
, It0
, F
, 0
>
{
typedef StateRef type;
};
template<typename StateRef, typename It0, typename F, int SeqSize>
struct BOOST_PP_CAT(result_of_first_unrolled,BOOST_FUSION_FOLD_NAME)
{
typedef typename
BOOST_PP_CAT(result_of_unrolled_,BOOST_FUSION_FOLD_NAME)<
typename boost::result_of<
F(
StateRef,
BOOST_FUSION_FOLD_IMPL_INVOKE_IT_META_TRANSFORM(
It0 const)
)
>::type
, typename result_of::BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION<
It0 const
>::type
, F
, SeqSize-1
>::type
type;
};
template<int SeqSize, typename StateRef, typename It0, typename F>
struct BOOST_PP_CAT(BOOST_FUSION_FOLD_NAME,_impl)
{
typedef typename
BOOST_PP_CAT(
result_of_first_unrolled,BOOST_FUSION_FOLD_NAME)<
StateRef
, BOOST_FUSION_FOLD_IMPL_FIRST_IT_META_TRANSFORM(It0)
, F
, SeqSize
>::type
type;
static type
call(StateRef state, It0 const& it0, F f)
{
return BOOST_PP_CAT(unrolled_,BOOST_FUSION_FOLD_NAME)<
type
, SeqSize
>::call(state,BOOST_FUSION_FOLD_IMPL_FIRST_IT_TRANSFORM(it0),f);
}
};
template<typename StateRef, typename It0, typename F>
struct BOOST_PP_CAT(BOOST_FUSION_FOLD_NAME,_impl)<0,StateRef,It0,F>
{
typedef StateRef type;
static StateRef
call(StateRef state, It0 const&, F)
{
return static_cast<StateRef>(state);
}
};
}
namespace result_of
{
template<typename Seq, typename State, typename F>
struct BOOST_FUSION_FOLD_NAME
: detail::BOOST_PP_CAT(BOOST_FUSION_FOLD_NAME,_impl)<
size<Seq>::value
, typename add_reference<
typename add_const<State>::type
>::type
, typename BOOST_FUSION_FOLD_IMPL_FIRST_IT_FUNCTION<Seq>::type
, F
>
{};
}
template<typename Seq, typename State, typename F>
inline typename result_of::fold<Seq,State const,F>::type
BOOST_FUSION_FOLD_NAME(Seq& seq,State const& state,F f)
{
return result_of::BOOST_FUSION_FOLD_NAME<Seq,State const,F>::call(
state,
fusion::BOOST_FUSION_FOLD_IMPL_FIRST_IT_FUNCTION(seq),
f);
}
template<typename Seq, typename State, typename F>
inline typename result_of::fold<Seq const,State const,F>::type
BOOST_FUSION_FOLD_NAME(Seq const& seq,State const& state,F f)
{
return result_of::BOOST_FUSION_FOLD_NAME<Seq const,State const,F>::call(
state,
fusion::BOOST_FUSION_FOLD_IMPL_FIRST_IT_FUNCTION(seq),
f);
}
}}
#undef BOOST_FUSION_FOLD_NAME
#undef BOOST_FUSION_FOLD_IMPL_FIRST_IT_FUNCTION
#undef BOOST_FUSION_FOLD_IMPL_NEXT_IT_FUNCTION
#undef BOOST_FUSION_FOLD_IMPL_FIRST_IT_META_TRANSFORM
#undef BOOST_FUSION_FOLD_IMPL_FIRST_IT_TRANSFORM
#undef BOOST_FUSION_FOLD_IMPL_INVOKE_IT_META_TRANSFORM
#undef BOOST_FUSION_FOLD_IMPL_INVOKE_IT_TRANSFORM

View File

@ -1,48 +1,15 @@
/*=============================================================================
Copyright (c) 2001-2007 Joel de Guzman
Copyright (c) 2007 Dan Marsden
Copyright (c) 2009-2010 Christopher Schmidt
Distributed under the Boost Software License, Version 1.0. (See accompanying
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#if !defined(BOOST_FUSION_FOLD_05052005_1214)
#define BOOST_FUSION_FOLD_05052005_1214
#ifndef BOOST_FUSION_ALGORITHM_ITERATION_FOLD_HPP
#define BOOST_FUSION_ALGORITHM_ITERATION_FOLD_HPP
#include <boost/fusion/algorithm/iteration/detail/fold.hpp>
#include <boost/fusion/sequence/intrinsic/size.hpp>
#include <boost/fusion/support/category_of.hpp>
#include <boost/type_traits/is_base_of.hpp>
#include <boost/static_assert.hpp>
namespace boost { namespace fusion {
struct random_access_traversal_tag;
namespace result_of
{
template <typename Sequence, typename State, typename F>
struct fold
: fusion::detail::choose_fold<
Sequence, State, F
, is_base_of<random_access_traversal_tag, typename traits::category_of<Sequence>::type>::value>
{};
}
template <typename Sequence, typename State, typename F>
inline typename result_of::fold<Sequence, State, F>::type
fold(Sequence& seq, State const& state, F f)
{
return detail::fold(seq, state, f, typename traits::category_of<Sequence>::type());
}
template <typename Sequence, typename State, typename F>
inline typename result_of::fold<Sequence const, State, F>::type
fold(Sequence const& seq, State const& state, F f)
{
return detail::fold(seq, state, f, typename traits::category_of<Sequence>::type());
}
}}
#endif

View File

@ -0,0 +1,17 @@
/*=============================================================================
Copyright (c) 2010 Christopher Schmidt
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#ifndef BOOST_FUSION_ALGORITHM_ITERATION_ITER_FOLD_HPP
#define BOOST_FUSION_ALGORITHM_ITERATION_ITER_FOLD_HPP
#define BOOST_FUSION_ITER_FOLD
#include <boost/fusion/algorithm/iteration/detail/fold.hpp>
#undef BOOST_FUSION_REVERSE_FOLD
#endif

View File

@ -0,0 +1,17 @@
/*=============================================================================
Copyright (c) 2010 Christopher Schmidt
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#ifndef BOOST_FUSION_ALGORITHM_ITERATION_REVERSE_FOLD_HPP
#define BOOST_FUSION_ALGORITHM_ITERATION_REVERSE_FOLD_HPP
#define BOOST_FUSION_REVERSE_FOLD
#include <boost/fusion/algorithm/iteration/detail/fold.hpp>
#undef BOOST_FUSION_REVERSE_FOLD
#endif

View File

@ -0,0 +1,19 @@
/*=============================================================================
Copyright (c) 2010 Christopher Schmidt
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#ifndef BOOST_FUSION_ALGORITHM_ITERATION_REVERSE_ITER_FOLD_HPP
#define BOOST_FUSION_ALGORITHM_ITERATION_REVERSE_ITER_FOLD_HPP
#define BOOST_FUSION_REVERSE_FOLD
#define BOOST_FUSION_ITER_FOLD
#include <boost/fusion/algorithm/iteration/detail/fold.hpp>
#undef BOOST_FUSION_REVERSE_FOLD
#undef BOOST_FUSION_ITER_FOLD
#endif

View File

@ -0,0 +1,13 @@
/*=============================================================================
Copyright (c) 2010 Christopher Schmidt
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#ifndef BOOST_FUSION_INCLUDE_ITER_FOLD_HPP
#define BOOST_FUSION_INCLUDE_ITER_FOLD_HPP
#include <boost/fusion/algorithm/iteration/iter_fold.hpp>
#endif

View File

@ -0,0 +1,13 @@
/*=============================================================================
Copyright (c) 2010 Christopher Schmidt
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#ifndef BOOST_FUSION_INCLUDE_REVERSE_FOLD_HPP
#define BOOST_FUSION_INCLUDE_REVERSE_FOLD_HPP
#include <boost/fusion/algorithm/iteration/reverse_fold.hpp>
#endif

View File

@ -0,0 +1,13 @@
/*=============================================================================
Copyright (c) 2010 Christopher Schmidt
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#ifndef BOOST_FUSION_INCLUDE_REVERSE_ITER_FOLD_HPP
#define BOOST_FUSION_INCLUDE_REVERSE_ITER_FOLD_HPP
#include <boost/fusion/algorithm/iteration/reverse_iter_fold.hpp>
#endif

View File

@ -23,9 +23,11 @@ import testing ;
[ run algorithm/find.cpp : : : : ]
[ run algorithm/find_if.cpp : : : : ]
[ run algorithm/fold.cpp : : : : ]
[ run algorithm/fold2.cpp : : : : ]
[ run algorithm/for_each.cpp : : : : ]
[ run algorithm/insert.cpp : : : : ]
[ run algorithm/insert_range.cpp : : : : ]
[ run algorithm/iter_fold.cpp : : : : ]
[ run algorithm/none.cpp : : : : ]
[ run algorithm/pop_back.cpp : : : : ]
[ run algorithm/pop_front.cpp : : : : ]
@ -35,6 +37,8 @@ import testing ;
[ run algorithm/remove_if.cpp : : : : ]
[ run algorithm/replace.cpp : : : : ]
[ run algorithm/replace_if.cpp : : : : ]
[ run algorithm/reverse_fold.cpp : : : : ]
[ run algorithm/reverse_iter_fold.cpp : : : : ]
[ run algorithm/reverse.cpp : : : : ]
[ run algorithm/transform.cpp : : : : ]
[ run algorithm/join.cpp : : : : ]

212
test/algorithm/fold.hpp Normal file
View File

@ -0,0 +1,212 @@
/*=============================================================================
Copyright (c) 2010 Christopher Schmidt
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#include <boost/config.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <boost/fusion/sequence.hpp>
#include <boost/fusion/iterator.hpp>
#include <boost/fusion/algorithm/transformation/reverse.hpp>
#include <boost/fusion/algorithm/iteration/fold.hpp>
#include <boost/fusion/algorithm/iteration/reverse_fold.hpp>
#include <boost/fusion/algorithm/iteration/iter_fold.hpp>
#include <boost/fusion/algorithm/iteration/reverse_iter_fold.hpp>
#include <boost/fusion/container/vector/convert.hpp>
#include <boost/fusion/container/vector/vector.hpp>
#include <boost/fusion/adapted/mpl.hpp>
#include <boost/fusion/support/pair.hpp>
#include <boost/fusion/mpl.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/back.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/for_each.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/back_inserter.hpp>
#include <boost/mpl/always.hpp>
#include <boost/mpl/copy.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/remove_reference.hpp>
#include <iostream>
namespace mpl=boost::mpl;
namespace fusion=boost::fusion;
#ifdef BOOST_FUSION_TEST_REVERSE_FOLD
# ifdef BOOST_FUSION_TEST_ITER_FOLD
# define BOOST_FUSION_TEST_FOLD_NAME reverse_iter_fold
# else
# define BOOST_FUSION_TEST_FOLD_NAME reverse_fold
# endif
#else
# ifdef BOOST_FUSION_TEST_ITER_FOLD
# define BOOST_FUSION_TEST_FOLD_NAME iter_fold
# else
# define BOOST_FUSION_TEST_FOLD_NAME fold
# endif
#endif
struct sum
{
template<typename Sig>
struct result;
template<typename Self, typename State, typename T>
struct result<Self(State,T)>
: fusion::result_of::make_pair<
mpl::int_<
boost::remove_reference<
State
>::type::first_type::value+1
>
, int
>
{
BOOST_MPL_ASSERT((typename boost::is_reference<State>::type));
BOOST_MPL_ASSERT((typename boost::is_reference<T>::type));
};
#ifdef BOOST_FUSION_TEST_ITER_FOLD
template<typename State, typename It>
typename result<sum const&(State const&,It const&)>::type
operator()(State const& state, It const& it)const
{
static const int n=State::first_type::value;
return fusion::make_pair<mpl::int_<n+1> >(
state.second+fusion::deref(it)*n);
}
#else
template<typename State>
typename result<sum const&(State const&, int const&)>::type
operator()(State const& state, int const& e)const
{
static const int n=State::first_type::value;
return fusion::make_pair<mpl::int_<n+1> >(state.second+e*n);
}
#endif
};
struct meta_sum
{
template<typename Sig>
struct result;
template<typename Self, typename State, typename T>
struct result<Self(State,T)>
{
BOOST_MPL_ASSERT((typename boost::is_reference<State>::type));
BOOST_MPL_ASSERT((typename boost::is_reference<T>::type));
typedef typename boost::remove_reference<State>::type state;
static const int n=mpl::front<state>::type::value;
#ifdef BOOST_FUSION_TEST_ITER_FOLD
typedef typename
fusion::result_of::value_of<
typename boost::remove_reference<T>::type
>::type
t;
#else
typedef typename boost::remove_reference<T>::type t;
#endif
typedef
mpl::vector<
mpl::int_<n+1>
, mpl::int_<
mpl::back<state>::type::value+t::value*n
>
>
type;
};
template<typename State, typename T>
typename result<meta_sum const&(State const&,T const&)>::type
operator()(State const&, T const&)const;
};
struct fold_test_n
{
template<typename I>
void
operator()(I)const
{
static const int n=I::value;
typedef mpl::range_c<int, 0, n> range;
static const int squares_sum=n*(n+1)*(2*n+1)/6;
{
mpl::range_c<int, 1, n+1> init_range;
typename fusion::result_of::as_vector<
typename mpl::transform<
range
, mpl::always<int>
, mpl::back_inserter<mpl::vector<> >
>::type
>::type vec(
#ifdef BOOST_FUSION_TEST_REVERSE_FOLD
fusion::reverse(init_range)
#else
init_range
#endif
);
int result=BOOST_FUSION_TEST_FOLD_NAME(
vec,
fusion::make_pair<mpl::int_<1> >(0),
sum()).second;
std::cout << n << ": " << result << std::endl;
BOOST_TEST(result==squares_sum);
}
{
typedef typename
#ifdef BOOST_FUSION_TEST_REVERSE_FOLD
fusion::result_of::as_vector<
typename mpl::copy<
mpl::range_c<int, 1, n+1>
, mpl::front_inserter<fusion::vector<> >
>::type
>::type
#else
fusion::result_of::as_vector<mpl::range_c<int, 1, n+1> >::type
#endif
vec;
typedef
boost::is_same<
typename fusion::result_of::BOOST_FUSION_TEST_FOLD_NAME<
vec
, mpl::vector<mpl::int_<1>, mpl::int_<0> >
, meta_sum
>::type
, typename mpl::if_c<
!n
, mpl::vector<mpl::int_<1>, mpl::int_<0> > const&
, mpl::vector<mpl::int_<n+1>, mpl::int_<squares_sum> >
>::type
>
result_test;
BOOST_MPL_ASSERT((result_test));
}
}
};
int
main()
{
mpl::for_each<mpl::range_c<int, 0, 10> >(fold_test_n());
return boost::report_errors();
}
#undef BOOST_FUSION_TEST_FOLD_NAME

8
test/algorithm/fold2.cpp Normal file
View File

@ -0,0 +1,8 @@
/*=============================================================================
Copyright (c) 2010 Christopher Schmidt
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#include "fold.hpp"

View File

@ -0,0 +1,10 @@
/*=============================================================================
Copyright (c) 2010 Christopher Schmidt
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#define BOOST_FUSION_TEST_ITER_FOLD
#include "fold.hpp"
#undef BOOST_FUSION_TEST_ITER_FOLD

View File

@ -0,0 +1,10 @@
/*=============================================================================
Copyright (c) 2010 Christopher Schmidt
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#define BOOST_FUSION_TEST_REVERSE_FOLD
#include "fold.hpp"
#undef BOOST_FUSION_TEST_REVERSE_FOLD

View File

@ -0,0 +1,12 @@
/*=============================================================================
Copyright (c) 2010 Christopher Schmidt
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
#define BOOST_FUSION_TEST_REVERSE_FOLD
#define BOOST_FUSION_TEST_ITER_FOLD
#include "fold.hpp"
#undef BOOST_FUSION_TEST_ITER_FOLD
#undef BOOST_FUSION_TEST_REVERSE_FOLD

View File

@ -15,6 +15,7 @@
#include <boost/utility/result_of.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/placeholders.hpp>
#include <boost/utility/result_of.hpp>

View File

@ -80,7 +80,9 @@ main()
BOOST_STATIC_ASSERT(result_of::size<filter_view_type>::value == 4);
}
{
//cschmidt: This is illegal C++. ADL instantiates less<_, int_<3> > - which
//leads to compile errors.
/*{
// $$$ JDG $$$ For some obscure reason, EDG based compilers
// (e.g. comeau 4.3.3, intel) have problems with this.
// vc7.1 and g++ are ok. The errors from comeau are useless.
@ -94,7 +96,7 @@ main()
BOOST_TEST((view == make_vector(1, 2, 0, -1)));
BOOST_STATIC_ASSERT(result_of::size<filter_view_type>::value == 4);
#endif
}
}*/
{
// Previous filtering out all values caused problems as begin<seq> was not equal to end<seq>