drop_while.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /*!
  2. @file
  3. Defines `boost::hana::drop_while`.
  4. Copyright Louis Dionne 2013-2022
  5. Distributed under the Boost Software License, Version 1.0.
  6. (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
  7. */
  8. #ifndef BOOST_HANA_DROP_WHILE_HPP
  9. #define BOOST_HANA_DROP_WHILE_HPP
  10. #include <boost/hana/fwd/drop_while.hpp>
  11. #include <boost/hana/concept/foldable.hpp>
  12. #include <boost/hana/concept/iterable.hpp>
  13. #include <boost/hana/config.hpp>
  14. #include <boost/hana/core/dispatch.hpp>
  15. #include <boost/hana/detail/first_unsatisfied_index.hpp>
  16. #include <boost/hana/drop_front.hpp>
  17. #include <boost/hana/eval_if.hpp>
  18. #include <boost/hana/front.hpp>
  19. #include <boost/hana/is_empty.hpp>
  20. #include <boost/hana/lazy.hpp>
  21. namespace boost { namespace hana {
  22. //! @cond
  23. template <typename Xs, typename Pred>
  24. constexpr auto drop_while_t::operator()(Xs&& xs, Pred&& pred) const {
  25. using It = typename hana::tag_of<Xs>::type;
  26. using DropWhile = BOOST_HANA_DISPATCH_IF(drop_while_impl<It>,
  27. hana::Iterable<It>::value
  28. );
  29. #ifndef BOOST_HANA_CONFIG_DISABLE_CONCEPT_CHECKS
  30. static_assert(hana::Iterable<It>::value,
  31. "hana::drop_while(xs, pred) requires 'xs' to be an Iterable");
  32. #endif
  33. return DropWhile::apply(static_cast<Xs&&>(xs), static_cast<Pred&&>(pred));
  34. }
  35. //! @endcond
  36. namespace iterable_detail {
  37. struct drop_while_helper {
  38. struct next {
  39. template <typename Xs, typename Pred>
  40. constexpr decltype(auto) operator()(Xs&& xs, Pred&& pred) const {
  41. return hana::drop_while(
  42. hana::drop_front(static_cast<Xs&&>(xs)),
  43. static_cast<Pred&&>(pred)
  44. );
  45. }
  46. };
  47. template <typename Xs, typename Pred>
  48. constexpr decltype(auto) operator()(Xs&& xs, Pred&& pred) const {
  49. return hana::eval_if(pred(hana::front(xs)),
  50. hana::make_lazy(next{})(xs, pred),
  51. hana::make_lazy(xs)
  52. );
  53. }
  54. };
  55. }
  56. template <typename It, bool condition>
  57. struct drop_while_impl<It, when<condition>> : default_ {
  58. template <typename Xs, typename Pred>
  59. static constexpr auto apply(Xs&& xs, Pred&& pred) {
  60. return hana::eval_if(hana::is_empty(xs),
  61. hana::make_lazy(xs),
  62. hana::make_lazy(iterable_detail::drop_while_helper{})(
  63. xs, static_cast<Pred&&>(pred))
  64. );
  65. }
  66. };
  67. template <typename S>
  68. struct drop_while_impl<S, when<hana::Foldable<S>::value>> {
  69. template <typename Xs, typename Pred>
  70. static constexpr auto apply(Xs&& xs, Pred&&) {
  71. using FirstUnsatisfied = decltype(
  72. hana::unpack(static_cast<Xs&&>(xs),
  73. detail::first_unsatisfied_index<Pred&&>{})
  74. );
  75. return hana::drop_front(static_cast<Xs&&>(xs),
  76. FirstUnsatisfied{});
  77. }
  78. };
  79. }} // end namespace boost::hana
  80. #endif // !BOOST_HANA_DROP_WHILE_HPP