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