Skip to content

Commit

Permalink
Reorder the parameters of fold and transforme
Browse files Browse the repository at this point in the history
Close #74
  • Loading branch information
edouarda committed Sep 24, 2015
1 parent 4465d6b commit f441fd8
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 27 deletions.
2 changes: 1 addition & 1 deletion brigand/adapted/integral_list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ namespace brigand
struct make_integral : std::integral_constant < typename T::value_type, T::value > {};

template <typename L>
using as_integral_list = transform < make_integral<brigand::_1>, L > ;
using as_integral_list = transform <L, make_integral<brigand::_1>> ;

}
2 changes: 1 addition & 1 deletion brigand/algorithms/fold.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@
namespace brigand
{
// Main fold entry point
template<template<class,class> class Functor, class State, class Sequence>
template <class Sequence, class State, template<class,class> class Functor>
using fold = typename detail::fold_impl<Functor, State, Sequence>::type;
}
35 changes: 28 additions & 7 deletions brigand/algorithms/transform.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,55 @@ namespace brigand
{
namespace detail
{
template<typename Functor, typename... Sequence>
struct transform_impl;

template <typename Functor, typename Sequence>
struct unary_transform_impl;

// Unary transform implementation
template< typename Functor
, template<class...> class Sequence, typename... T
>
struct transform_impl<Functor, Sequence<T...>>
struct unary_transform_impl<Functor, Sequence<T...>>
{
using type = Sequence< brigand::apply<Functor,T>... >;
};

template<typename Functor, typename Sequence1, typename Sequence2>
struct binary_transform_impl;

// Binary transform implementation
template< typename Functor
, template<class...> class Sequence1, typename... T1
, template<class...> class Sequence2, typename... T2
>
struct transform_impl<Functor, Sequence1<T1...>, Sequence2<T2...>>
struct binary_transform_impl<Functor, Sequence1<T1...>, Sequence2<T2...>>
{
static_assert ( sizeof...(T1) == sizeof...(T2)
, "The arguments of transform should be of the same size"
);

using type = Sequence1< brigand::apply<Functor,T1,T2>... >;
};
};

template <std::size_t Selector, typename Sequence1, typename Functor, typename Sequence2 = void>
struct transform_selector;

template <typename Sequence1, typename Functor>
struct transform_selector <0, Sequence1, Functor, void>
{
using type = typename unary_transform_impl<Functor, Sequence1>::type;
};

template <typename Sequence1, typename Sequence2, typename Functor>
struct transform_selector<1, Sequence1, Sequence2, Functor>
{
using type = typename binary_transform_impl<Functor, Sequence1, Sequence2>::type;
};

}

// Main transform entry point
template<typename Functor, typename... Sequence >
using transform = typename detail::transform_impl<Functor, Sequence...>::type;
template<typename Sequence1, typename OpSeq1, typename... OpSeq2>
using transform = typename detail::transform_selector<sizeof...(OpSeq2), Sequence1, OpSeq1, OpSeq2...>::type;

}
10 changes: 5 additions & 5 deletions test/fold.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ using f_ = typename std::conditional < std::is_floating_point<Element>::value
, State
>::type;

static_assert(brigand::fold<f_,leet_,list0>::type::value == 1337 , "invalid fold on empty list" );
static_assert(brigand::fold<f_,zero_,list1y>::type::value == 1 , "invalid fold on 1 element list");
static_assert(brigand::fold<f_,zero_,list1n>::type::value == 0 , "invalid fold on 1 element list");
static_assert(brigand::fold<f_,zero_,list6>::type::value == 3 , "invalid fold on list" );
static_assert(brigand::fold<f_,c88,list12>::type::value == 100 , "invalid fold with state" );
static_assert(brigand::fold<list0, leet_, f_>::type::value == 1337, "invalid fold on empty list");
static_assert(brigand::fold<list1y, zero_, f_>::type::value == 1, "invalid fold on 1 element list");
static_assert(brigand::fold<list1n, zero_, f_>::type::value == 0, "invalid fold on 1 element list");
static_assert(brigand::fold<list6, zero_, f_>::type::value == 3, "invalid fold on list");
static_assert(brigand::fold<list12, c88, f_>::type::value == 100, "invalid fold with state");
28 changes: 15 additions & 13 deletions test/transform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,75 +27,77 @@ using expect2c = brigand::list<float*,double*,int*,char*,void*>;
using ptr_t = std::add_pointer<brigand::_1>;
using pair_t = std::pair<brigand::_1,brigand::_2>;

static_assert ( std::is_same< brigand::transform<std::add_pointer<brigand::_1>,list0>
static_assert ( std::is_same< brigand::transform<list0, std::add_pointer<brigand::_1>>
, list0
>::value
, "invalid unary transform on empty list"
);

static_assert ( std::is_same< brigand::transform<ptr_t,list0>
static_assert ( std::is_same< brigand::transform<list0, ptr_t>
, list0
>::value
, "invalid unary transform on empty list"
);

static_assert ( std::is_same< brigand::transform<pair_t,list0,list0>
static_assert ( std::is_same< brigand::transform<list0,list0, pair_t>
, list0
>::value
, "invalid binary transform on empty list"
);

static_assert ( std::is_same< brigand::transform<ptr_t,list1>
static_assert ( std::is_same< brigand::transform<list1, ptr_t>
, expect1
>::value
, "invalid unary transform on list"
);


static_assert ( std::is_same< brigand::transform<std::add_pointer<brigand::_1>,list1>
static_assert (std::is_same< brigand::transform<list1, std::add_pointer<brigand::_1>>
, expect1
>::value
, "invalid unary transform on list"
);

static_assert ( std::is_same< brigand::transform<pair_t,list1,list1>
static_assert ( std::is_same< brigand::transform<list1,list1, pair_t>
, expect1b
>::value
, "invalid binary transform on list"
);

static_assert ( std::is_same< brigand::transform<ptr_t,list2>
static_assert (std::is_same< brigand::transform<list2, ptr_t>
, expect2
>::value
, "invalid unary transform on list"
);

static_assert ( std::is_same< brigand::transform<pair_t,list2,list2>
static_assert (std::is_same< brigand::transform<list2, list2, pair_t>
, expect2b
>::value
, "invalid binary transform on list"
);

static_assert ( std::is_same< brigand::transform<pair_t,list2,list2>
static_assert (std::is_same< brigand::transform<list2, list2, pair_t>
, expect2b
>::value
, "invalid binary transform on list"
);

static_assert (std::is_same < brigand::transform < std::add_pointer<std::add_pointer<std::add_pointer<brigand::_1>>>,
brigand::list<void, char, int>>,
static_assert (std::is_same <
brigand::transform <brigand::list<void, char, int>,
std::add_pointer<std::add_pointer<std::add_pointer<brigand::_1>>>
>,
brigand::list<void ***, char ***, int ***>>::value
, "invalid transform on list"
);


static_assert ( std::is_same< brigand::transform< brigand::if_< std::is_pointer<brigand::_1>
static_assert ( std::is_same< brigand::transform<list2, brigand::if_< std::is_pointer<brigand::_1>
, brigand::identity<brigand::_1>
, std::add_pointer < brigand::_1 >
>
, list2
>
, expect2c
>::value
, "invalid binary transform on list"
);

0 comments on commit f441fd8

Please sign in to comment.