all.hpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. #if !defined(FUSION_ALL_05052005_1237)
  8. #define FUSION_ALL_05052005_1237
  9. #include <boost/fusion/support/config.hpp>
  10. #include <boost/mpl/bool.hpp>
  11. #include <boost/fusion/sequence/intrinsic/begin.hpp>
  12. #include <boost/fusion/sequence/intrinsic/end.hpp>
  13. #include <boost/fusion/iterator/advance.hpp>
  14. #include <boost/fusion/iterator/equal_to.hpp>
  15. #include <boost/fusion/iterator/next.hpp>
  16. #include <boost/fusion/iterator/deref.hpp>
  17. #include <boost/fusion/iterator/distance.hpp>
  18. namespace boost { namespace fusion { namespace detail
  19. {
  20. template <typename First, typename Last, typename F>
  21. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  22. inline bool
  23. linear_all(First const&, Last const&, F const&, mpl::true_)
  24. {
  25. return true;
  26. }
  27. template <typename First, typename Last, typename F>
  28. BOOST_CXX14_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  29. inline bool
  30. linear_all(First const& first, Last const& last, F& f, mpl::false_)
  31. {
  32. typename result_of::deref<First>::type x = *first;
  33. return f(x) &&
  34. detail::linear_all(
  35. fusion::next(first)
  36. , last
  37. , f
  38. , result_of::equal_to<typename result_of::next<First>::type, Last>());
  39. }
  40. template <typename Sequence, typename F, typename Tag>
  41. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  42. inline bool
  43. all(Sequence const& seq, F f, Tag)
  44. {
  45. return detail::linear_all(
  46. fusion::begin(seq)
  47. , fusion::end(seq)
  48. , f
  49. , result_of::equal_to<
  50. typename result_of::begin<Sequence>::type
  51. , typename result_of::end<Sequence>::type>());
  52. }
  53. template<int N>
  54. struct unrolled_all
  55. {
  56. template <typename It, typename F>
  57. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  58. static bool call(It const& it, F f)
  59. {
  60. return
  61. f(*it) &&
  62. f(*fusion::advance_c<1>(it))&&
  63. f(*fusion::advance_c<2>(it)) &&
  64. f(*fusion::advance_c<3>(it)) &&
  65. detail::unrolled_all<N-4>::call(fusion::advance_c<4>(it), f);
  66. }
  67. };
  68. template<>
  69. struct unrolled_all<3>
  70. {
  71. template <typename It, typename F>
  72. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  73. static bool call(It const& it, F f)
  74. {
  75. return
  76. f(*it) &&
  77. f(*fusion::advance_c<1>(it)) &&
  78. f(*fusion::advance_c<2>(it));
  79. }
  80. };
  81. template<>
  82. struct unrolled_all<2>
  83. {
  84. template <typename It, typename F>
  85. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  86. static bool call(It const& it, F f)
  87. {
  88. return
  89. f(*it) &&
  90. f(*fusion::advance_c<1>(it));
  91. }
  92. };
  93. template<>
  94. struct unrolled_all<1>
  95. {
  96. template <typename It, typename F>
  97. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  98. static bool call(It const& it, F f)
  99. {
  100. return f(*it);
  101. }
  102. };
  103. template<>
  104. struct unrolled_all<0>
  105. {
  106. template <typename It, typename F>
  107. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  108. static bool call(It const& /*it*/, F /*f*/)
  109. {
  110. return true;
  111. }
  112. };
  113. template <typename Sequence, typename F>
  114. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  115. inline bool
  116. all(Sequence const& seq, F f, random_access_traversal_tag)
  117. {
  118. typedef typename result_of::begin<Sequence>::type begin;
  119. typedef typename result_of::end<Sequence>::type end;
  120. return detail::unrolled_all<result_of::distance<begin, end>::type::value>::call(
  121. fusion::begin(seq), f);
  122. }
  123. }}}
  124. #endif