/// \file
// Range v3 library
//
//  Copyright Eric Niebler 2013-present
//
//  Use, modification and distribution is subject to the
//  Boost Software License, Version 1.0. (See accompanying
//  file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/ericniebler/range-v3
//

#ifndef RANGES_V3_VIEW_DROP_WHILE_HPP
#define RANGES_V3_VIEW_DROP_WHILE_HPP

#include <utility>
#include <functional>
#include <meta/meta.hpp>
#include <range/v3/range/traits.hpp>
#include <range/v3/range/concepts.hpp>
#include <range/v3/view/interface.hpp>
#include <range/v3/functional/compose.hpp>
#include <range/v3/utility/optional.hpp>
#include <range/v3/utility/semiregular.hpp>
#include <range/v3/utility/static_const.hpp>
#include <range/v3/algorithm/find_if_not.hpp>
#include <range/v3/view/all.hpp>
#include <range/v3/view/view.hpp>
#include <range/v3/range_fwd.hpp>

namespace ranges
{
    /// \addtogroup group-views
    /// @{
    template<typename Rng, typename Pred>
    struct drop_while_view
      : view_interface<drop_while_view<Rng, Pred>, is_finite<Rng>::value ? finite : unknown>
    {
    private:
        Rng rng_;
        semiregular_t<Pred> pred_;
        detail::non_propagating_cache<iterator_t<Rng>> begin_;

        iterator_t<Rng> get_begin_()
        {
            if(!begin_)
                begin_ = find_if_not(rng_, std::ref(pred_));
            return *begin_;
        }
    public:
        drop_while_view() = default;
        drop_while_view(Rng rng, Pred pred)
          : rng_(std::move(rng)), pred_(std::move(pred))
        {}
        iterator_t<Rng> begin()
        {
            return get_begin_();
        }
        sentinel_t<Rng> end()
        {
            return ranges::end(rng_);
        }
        Rng base() const
        {
            return rng_;
        }
    };

    namespace view
    {
        struct drop_while_fn
        {
        private:
            friend view_access;
            template<typename Pred>
            static auto bind(drop_while_fn drop_while, Pred pred)
            {
                return make_pipeable(
                    std::bind(drop_while, std::placeholders::_1, protect(std::move(pred))));
            }
            template<typename Pred, typename Proj>
            static auto bind(drop_while_fn drop_while, Pred pred, Proj proj)
            {
                return make_pipeable(
                    std::bind(drop_while, std::placeholders::_1, protect(std::move(pred)),
                    protect(std::move(proj))));
            }
        public:
            template<typename Rng, typename Pred>
            auto operator()(Rng &&rng, Pred pred) const ->
                CPP_ret(drop_while_view<all_t<Rng>, Pred>)(
                    requires ViewableRange<Rng> && InputRange<Rng> &&
                        IndirectUnaryPredicate<Pred, iterator_t<Rng>>)
            {
                return {all(static_cast<Rng &&>(rng)), std::move(pred)};
            }
            template<typename Rng, typename Pred, typename Proj>
            auto operator()(Rng &&rng, Pred pred, Proj proj) const ->
                CPP_ret(drop_while_view<all_t<Rng>, composed<Pred, Proj>>)(
                    requires ViewableRange<Rng> && InputRange<Rng> &&
                        IndirectUnaryPredicate<composed<Pred, Proj>, iterator_t<Rng>>)
            {
                return {
                    all(static_cast<Rng &&>(rng)),
                    compose(std::move(pred), std::move(proj))
                };
            }
        };

        /// \relates drop_while_fn
        /// \ingroup group-views
        RANGES_INLINE_VARIABLE(view<drop_while_fn>, drop_while)
    }
    /// @}
}

#include <range/v3/detail/satisfy_boost_range.hpp>
RANGES_SATISFY_BOOST_RANGE(::ranges::drop_while_view)

#endif
