pop_back.hpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*=============================================================================
  2. Copyright (c) 2001-2011 Joel de Guzman
  3. Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. ==============================================================================*/
  6. #if !defined(FUSION_POP_BACK_09172005_1038)
  7. #define FUSION_POP_BACK_09172005_1038
  8. #include <boost/fusion/support/config.hpp>
  9. #include <boost/fusion/view/iterator_range/iterator_range.hpp>
  10. #include <boost/fusion/sequence/intrinsic/begin.hpp>
  11. #include <boost/fusion/sequence/intrinsic/end.hpp>
  12. #include <boost/fusion/sequence/intrinsic/empty.hpp>
  13. #include <boost/fusion/iterator/iterator_adapter.hpp>
  14. #include <boost/fusion/iterator/next.hpp>
  15. #include <boost/mpl/minus.hpp>
  16. #include <boost/mpl/int.hpp>
  17. #include <boost/mpl/if.hpp>
  18. namespace boost { namespace fusion
  19. {
  20. template <typename Iterator_, bool IsLast>
  21. struct pop_back_iterator
  22. : iterator_adapter<
  23. pop_back_iterator<Iterator_, IsLast>
  24. , Iterator_>
  25. {
  26. typedef iterator_adapter<
  27. pop_back_iterator<Iterator_, IsLast>
  28. , Iterator_>
  29. base_type;
  30. static bool const is_last = IsLast;
  31. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  32. pop_back_iterator(Iterator_ const& iterator_base)
  33. : base_type(iterator_base) {}
  34. template <typename BaseIterator>
  35. struct make
  36. {
  37. typedef pop_back_iterator<BaseIterator, is_last> type;
  38. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  39. static type
  40. call(BaseIterator const& i)
  41. {
  42. return type(i);
  43. }
  44. };
  45. template <typename I, bool IsLast_>
  46. struct equal_to_helper
  47. : mpl::identity<typename I::iterator_base_type>
  48. {};
  49. template <typename I>
  50. struct equal_to_helper<I, true>
  51. : result_of::next<
  52. typename I::iterator_base_type>
  53. {};
  54. template <typename I1, typename I2>
  55. struct equal_to
  56. : result_of::equal_to<
  57. typename equal_to_helper<I1,
  58. (I2::is_last && !I1::is_last)>::type
  59. , typename equal_to_helper<I2,
  60. (I1::is_last && !I2::is_last)>::type
  61. >
  62. {};
  63. template <typename First, typename Last>
  64. struct distance
  65. : mpl::minus<
  66. typename result_of::distance<
  67. typename First::iterator_base_type
  68. , typename Last::iterator_base_type
  69. >::type
  70. , mpl::int_<(Last::is_last?1:0)>
  71. >::type
  72. {};
  73. template <typename Iterator, bool IsLast_>
  74. struct prior_impl
  75. {
  76. typedef typename Iterator::iterator_base_type base_type;
  77. typedef typename
  78. result_of::prior<base_type>::type
  79. base_prior;
  80. typedef pop_back_iterator<base_prior, false> type;
  81. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  82. static type
  83. call(Iterator const& i)
  84. {
  85. return type(fusion::prior(i.iterator_base));
  86. }
  87. };
  88. template <typename Iterator>
  89. struct prior_impl<Iterator, true>
  90. {
  91. // If this is the last iterator, we'll have to double back
  92. typedef typename Iterator::iterator_base_type base_type;
  93. typedef typename
  94. result_of::prior<
  95. typename result_of::prior<base_type>::type
  96. >::type
  97. base_prior;
  98. typedef pop_back_iterator<base_prior, false> type;
  99. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  100. static type
  101. call(Iterator const& i)
  102. {
  103. return type(fusion::prior(
  104. fusion::prior(i.iterator_base)));
  105. }
  106. };
  107. template <typename Iterator>
  108. struct prior : prior_impl<Iterator, Iterator::is_last>
  109. {};
  110. };
  111. namespace result_of
  112. {
  113. template <typename Sequence>
  114. struct pop_back
  115. {
  116. BOOST_MPL_ASSERT_NOT((result_of::empty<Sequence>));
  117. typedef pop_back_iterator<
  118. typename begin<Sequence>::type, false>
  119. begin_type;
  120. typedef pop_back_iterator<
  121. typename end<Sequence>::type, true>
  122. end_type;
  123. typedef
  124. iterator_range<begin_type, end_type>
  125. type;
  126. };
  127. }
  128. template <typename Sequence>
  129. BOOST_CONSTEXPR BOOST_FUSION_GPU_ENABLED
  130. inline typename result_of::pop_back<Sequence const>::type
  131. pop_back(Sequence const& seq)
  132. {
  133. typedef result_of::pop_back<Sequence const> comp;
  134. typedef typename comp::begin_type begin_type;
  135. typedef typename comp::end_type end_type;
  136. typedef typename comp::type result;
  137. return result(
  138. begin_type(fusion::begin(seq))
  139. , end_type(fusion::end(seq))
  140. );
  141. }
  142. }}
  143. #endif