stride.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_STRIDE_HPP
  14. #define RANGES_V3_ACTION_STRIDE_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 stride_fn
  31. {
  32. template(typename D)(
  33. requires detail::integer_like_<D>)
  34. constexpr auto operator()(D step) const
  35. {
  36. return make_action_closure(bind_back(stride_fn{}, step));
  37. }
  38. template(typename Rng, typename D = range_difference_t<Rng>)(
  39. requires forward_range<Rng> AND
  40. erasable_range<Rng &, iterator_t<Rng>, sentinel_t<Rng>> AND
  41. permutable<iterator_t<Rng>>)
  42. Rng operator()(Rng && rng, range_difference_t<Rng> const step) const
  43. {
  44. using I = iterator_t<Rng>;
  45. using S = sentinel_t<Rng>;
  46. RANGES_EXPECT(0 < step);
  47. if(1 < step)
  48. {
  49. I first = ranges::begin(rng);
  50. S const last = ranges::end(rng);
  51. if(first != last)
  52. {
  53. for(I i = ranges::next(++first, step - 1, last); i != last;
  54. advance(i, step, last), ++first)
  55. {
  56. *first = iter_move(i);
  57. }
  58. }
  59. ranges::actions::erase(rng, first, last);
  60. }
  61. return static_cast<Rng &&>(rng);
  62. }
  63. };
  64. /// \relates actions::stride_fn
  65. RANGES_INLINE_VARIABLE(stride_fn, stride)
  66. } // namespace actions
  67. /// @}
  68. } // namespace ranges
  69. #include <range/v3/detail/epilogue.hpp>
  70. #endif