take.hpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /// \file
  2. // Range v3 library
  3. //
  4. // Copyright Eric Niebler 2013-present
  5. //
  6. // Use, modification and distribution is subject to the
  7. // Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. //
  11. // Project home: https://github.com/ericniebler/range-v3
  12. //
  13. #ifndef RANGES_V3_ACTION_TAKE_HPP
  14. #define RANGES_V3_ACTION_TAKE_HPP
  15. #include <range/v3/range_fwd.hpp>
  16. #include <range/v3/action/action.hpp>
  17. #include <range/v3/action/erase.hpp>
  18. #include <range/v3/functional/bind_back.hpp>
  19. #include <range/v3/iterator/concepts.hpp>
  20. #include <range/v3/iterator/operations.hpp>
  21. #include <range/v3/iterator/traits.hpp>
  22. #include <range/v3/utility/static_const.hpp>
  23. #include <range/v3/detail/prologue.hpp>
  24. namespace ranges
  25. {
  26. /// \addtogroup group-actions
  27. /// @{
  28. namespace actions
  29. {
  30. struct take_fn
  31. {
  32. template(typename Int)(
  33. requires detail::integer_like_<Int>)
  34. constexpr auto operator()(Int n) const
  35. {
  36. return make_action_closure(bind_back(take_fn{}, n));
  37. }
  38. template(typename Rng)(
  39. requires forward_range<Rng> AND
  40. erasable_range<Rng &, iterator_t<Rng>, sentinel_t<Rng>>)
  41. Rng operator()(Rng && rng, range_difference_t<Rng> n) const
  42. {
  43. RANGES_EXPECT(n >= 0);
  44. ranges::actions::erase(
  45. rng, ranges::next(begin(rng), n, end(rng)), end(rng));
  46. return static_cast<Rng &&>(rng);
  47. }
  48. };
  49. /// \relates actions::take_fn
  50. RANGES_INLINE_VARIABLE(take_fn, take)
  51. } // namespace actions
  52. /// @}
  53. } // namespace ranges
  54. #include <range/v3/detail/epilogue.hpp>
  55. #endif