/// \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_ACTION_SLICE_HPP #define RANGES_V3_ACTION_SLICE_HPP #include #include #include #include #include #include #include #include #include #include namespace ranges { /// \addtogroup group-actions /// @{ namespace actions { struct slice_fn { private: friend action_access; template using diff_t = range_difference_t; // Overloads for the pipe syntax: rng | actions::slice(from, to) template static auto CPP_fun(bind)(slice_fn slice, D from, D to)( // requires integral) { return bind_back(slice, from, to); } template static auto CPP_fun(bind)(slice_fn slice, D from, detail::from_end_ to)( // requires integral) { return bind_back(slice, from, to); } template static auto CPP_fun(bind)(slice_fn slice, detail::from_end_ from, detail::from_end_ to)( // requires integral) { return bind_back(slice, from, to); } template static auto CPP_fun(bind)(slice_fn slice, D from, end_fn const & to)( // requires integral) { return bind_back(slice, from, to); } template static auto CPP_fun(bind)(slice_fn slice, detail::from_end_ from, end_fn const & to)( // requires integral) { return bind_back(slice, from, to); } public: template> auto operator()(Rng && rng, diff_t from, diff_t to) const -> CPP_ret(Rng)( // requires forward_range && erasable_range) { RANGES_EXPECT(0 <= from && 0 <= to && from <= to); RANGES_EXPECT(!sized_range || to <= distance(rng)); ranges::actions::erase(rng, begin(rng), next(begin(rng), from)); ranges::actions::erase(rng, next(begin(rng), to - from), end(rng)); return static_cast(rng); } template> auto operator()(Rng && rng, diff_t from, detail::from_end_> to) const -> CPP_ret(Rng)( // requires bidirectional_range && erasable_range) { RANGES_EXPECT(0 <= from && to.dist_ <= 0); RANGES_EXPECT(!sized_range || from - to.dist_ <= distance(rng)); ranges::actions::erase(rng, begin(rng), next(begin(rng), from)); if(to.dist_ != 0) { auto const last = next(begin(rng), end(rng)); ranges::actions::erase(rng, prev(last, -to.dist_), last); } return static_cast(rng); } template> auto operator()(Rng && rng, detail::from_end_> from, detail::from_end_> to) const -> CPP_ret(Rng)( // requires bidirectional_range && erasable_range) { RANGES_EXPECT(from.dist_ <= 0 && to.dist_ <= 0 && from.dist_ <= to.dist_); RANGES_EXPECT(!sized_range || 0 <= distance(rng) + from.dist_); auto last = next(begin(rng), end(rng)); ranges::actions::erase(rng, prev(last, -to.dist_), last); last = next(begin(rng), end(rng)); ranges::actions::erase( rng, begin(rng), prev(last, to.dist_ - from.dist_)); return static_cast(rng); } template> auto operator()(Rng && rng, diff_t from, end_fn const &) const -> CPP_ret(Rng)( // requires forward_range && erasable_range) { RANGES_EXPECT(0 <= from); RANGES_EXPECT(!sized_range || from <= distance(rng)); ranges::actions::erase(rng, begin(rng), next(begin(rng), from)); return static_cast(rng); } template> auto operator()(Rng && rng, detail::from_end_> from, end_fn const &) const -> CPP_ret(Rng)( // requires bidirectional_range && erasable_range) { RANGES_EXPECT(from.dist_ <= 0); RANGES_EXPECT(!sized_range || 0 <= distance(rng) + from.dist_); auto const last = next(begin(rng), end(rng)); ranges::actions::erase(rng, begin(rng), prev(last, -from.dist_)); return static_cast(rng); } }; /// \ingroup group-actions /// \relates slice_fn /// \sa action RANGES_INLINE_VARIABLE(action, slice) } // namespace actions /// @} } // namespace ranges #endif