all.hpp 3.6 KB

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