fold.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Copyright (c) 2007 Dan Marsden
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #include <boost/detail/lightweight_test.hpp>
  8. #include <boost/fusion/container/vector/vector.hpp>
  9. #include <boost/fusion/adapted/mpl.hpp>
  10. #include <boost/fusion/sequence/io/out.hpp>
  11. #include <boost/fusion/sequence/intrinsic/at.hpp>
  12. #include <boost/fusion/container/generation/make_vector.hpp>
  13. #include <boost/fusion/algorithm/iteration/fold.hpp>
  14. #include <boost/fusion/algorithm/iteration/accumulate.hpp>
  15. #include <boost/type_traits/is_same.hpp>
  16. #include <boost/mpl/if.hpp>
  17. #include <boost/mpl/next.hpp>
  18. #include <boost/mpl/int.hpp>
  19. #include <boost/mpl/vector.hpp>
  20. #include <boost/type_traits/remove_const.hpp>
  21. #include <boost/type_traits/remove_reference.hpp>
  22. #include <boost/type_traits/is_reference.hpp>
  23. #include <string>
  24. using boost::mpl::if_;
  25. using boost::mpl::int_;
  26. using boost::is_same;
  27. struct add_ints_only
  28. {
  29. template<typename T>
  30. struct result;
  31. template <typename State, typename T>
  32. struct result<add_ints_only(State, T)>
  33. {
  34. typedef typename boost::remove_const<
  35. typename boost::remove_reference<State>::type>::type type;
  36. };
  37. template <typename State, typename T>
  38. State
  39. operator()(State const& state, T const& /*x*/) const
  40. {
  41. return state;
  42. }
  43. int
  44. operator()(int state, int x) const
  45. {
  46. return x + state;
  47. }
  48. };
  49. struct count_ints
  50. {
  51. template<typename T>
  52. struct result;
  53. template <typename CountT, typename T>
  54. struct result<count_ints(CountT, T)>
  55. {
  56. typedef typename boost::remove_const<
  57. typename boost::remove_reference<CountT>::type>::type state;
  58. typedef typename boost::remove_const<
  59. typename boost::remove_reference<T>::type>::type elem;
  60. typedef typename
  61. if_<
  62. is_same<elem, int>
  63. , typename boost::mpl::next<state>::type
  64. , state
  65. >::type
  66. type;
  67. };
  68. template <typename CountT, typename T>
  69. typename result<count_ints(CountT, T)>::type
  70. operator()(CountT const&, T const&) const
  71. {
  72. typedef typename result<count_ints(CountT, T)>::type result_;
  73. return result_();
  74. }
  75. };
  76. struct appender
  77. {
  78. typedef std::string result_type;
  79. std::string operator()(std::string const& str, char c) const
  80. {
  81. return str + c;
  82. }
  83. };
  84. struct lvalue_adder
  85. {
  86. template<typename Sig>
  87. struct result;
  88. template<typename T0, typename T1>
  89. struct result<lvalue_adder(T0, T1&)>
  90. {
  91. // Second argument still needs to support rvalues - see definition of fusion::fold
  92. typedef T1 type;
  93. };
  94. template<typename T0, typename T1>
  95. T1 operator()(T0 const& lhs, T1& rhs) const
  96. {
  97. return lhs + rhs;
  98. }
  99. };
  100. int add(int lhs, int rhs)
  101. {
  102. return lhs + rhs;
  103. }
  104. struct functor
  105. {
  106. template<typename T>
  107. int
  108. operator() (int hitherho, T const& cur) const
  109. {
  110. return int(hitherho + cur);
  111. }
  112. };
  113. struct visitor
  114. {
  115. typedef int result_type;
  116. int operator()(int sum, long&)
  117. {
  118. return sum;
  119. }
  120. };
  121. int
  122. main()
  123. {
  124. using namespace boost::fusion;
  125. namespace fusion = boost::fusion;
  126. {
  127. typedef vector<int, char, int, double> vector_type;
  128. vector_type v(12345, 'x', 678910, 3.36);
  129. int result = fold(v, 0, add_ints_only());
  130. std::cout << result << std::endl;
  131. BOOST_TEST(result == 12345+678910);
  132. }
  133. {
  134. typedef vector<int> vector_type;
  135. vector_type v(12345);
  136. int n = fusion::fold(v, int_<0>(), count_ints());
  137. std::cout << n << std::endl;
  138. BOOST_TEST(n == 1);
  139. }
  140. {
  141. typedef vector<int, char, int, double, int> vector_type;
  142. vector_type v(12345, 'x', 678910, 3.36, 8756);
  143. int n = fusion::fold(v, int_<0>(), count_ints());
  144. std::cout << n << std::endl;
  145. BOOST_TEST(n == 3);
  146. }
  147. {
  148. typedef boost::mpl::vector<int, char, int, double, int> mpl_vec;
  149. int n = fusion::fold(mpl_vec(), int_<0>(), count_ints());
  150. std::cout << n << std::endl;
  151. BOOST_TEST(n == 3);
  152. }
  153. {
  154. BOOST_TEST(fusion::fold(fusion::make_vector('a','b','c','d','e'), std::string(""), appender())
  155. == "abcde");
  156. }
  157. {
  158. vector<int, int> vec(1,2);
  159. BOOST_TEST(fusion::fold(vec, 0, lvalue_adder()) == 3);
  160. }
  161. {
  162. vector<int, int> vec(1,2);
  163. BOOST_TEST(fusion::fold(vec, 0, add) == 3);
  164. }
  165. {
  166. typedef vector<int, char, int, double> vector_type;
  167. vector_type v(12345, 'x', 678910, 3.36);
  168. int result = accumulate(v, 0, add_ints_only());
  169. std::cout << result << std::endl;
  170. BOOST_TEST(result == 12345+678910);
  171. }
  172. {
  173. typedef vector<int> vector_type;
  174. vector_type v(12345);
  175. int n = fusion::accumulate(v, int_<0>(), count_ints());
  176. std::cout << n << std::endl;
  177. BOOST_TEST(n == 1);
  178. }
  179. {
  180. typedef vector<int, char, int, double, int> vector_type;
  181. vector_type v(12345, 'x', 678910, 3.36, 8756);
  182. int n = fusion::accumulate(v, int_<0>(), count_ints());
  183. std::cout << n << std::endl;
  184. BOOST_TEST(n == 3);
  185. }
  186. {
  187. typedef boost::mpl::vector<int, char, int, double, int> mpl_vec;
  188. int n = fusion::accumulate(mpl_vec(), int_<0>(), count_ints());
  189. std::cout << n << std::endl;
  190. BOOST_TEST(n == 3);
  191. }
  192. {
  193. BOOST_TEST(fusion::accumulate(fusion::make_vector('a','b','c','d','e'), std::string(""), appender())
  194. == "abcde");
  195. }
  196. {
  197. vector<int, int> vec(1,2);
  198. BOOST_TEST(fusion::accumulate(vec, 0, add) == 3);
  199. }
  200. {
  201. #if defined(BOOST_RESULT_OF_USE_DECLTYPE)
  202. {
  203. boost::fusion::vector<int, double, long> container{1, 2, 3};
  204. functor f;
  205. boost::fusion::fold(container, 0, f);
  206. }
  207. #endif
  208. {
  209. boost::fusion::vector<long> vec;
  210. visitor v;
  211. boost::fusion::fold(vec, 0, v);
  212. }
  213. }
  214. return boost::report_errors();
  215. }