You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stride.hpp 2.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. namespace ranges
  24. {
  25. /// \addtogroup group-actions
  26. /// @{
  27. namespace actions
  28. {
  29. struct stride_fn
  30. {
  31. private:
  32. friend action_access;
  33. template<typename D>
  34. static auto CPP_fun(bind)(stride_fn stride, D step)( //
  35. requires integral<D>)
  36. {
  37. return bind_back(stride, step);
  38. }
  39. public:
  40. template<typename Rng, typename D = range_difference_t<Rng>>
  41. auto operator()(Rng && rng, range_difference_t<Rng> const step) const
  42. -> CPP_ret(Rng)( //
  43. requires forward_range<Rng> &&
  44. erasable_range<Rng &, iterator_t<Rng>, sentinel_t<Rng>> &&
  45. permutable<iterator_t<Rng>>)
  46. {
  47. using I = iterator_t<Rng>;
  48. using S = sentinel_t<Rng>;
  49. RANGES_EXPECT(0 < step);
  50. if(1 < step)
  51. {
  52. I first = ranges::begin(rng);
  53. S const last = ranges::end(rng);
  54. if(first != last)
  55. {
  56. for(I i = ranges::next(++first, step - 1, last); i != last;
  57. advance(i, step, last), ++first)
  58. {
  59. *first = iter_move(i);
  60. }
  61. }
  62. ranges::actions::erase(rng, first, last);
  63. }
  64. return static_cast<Rng &&>(rng);
  65. }
  66. };
  67. /// \ingroup group-actions
  68. /// \relates stride_fn
  69. /// \sa action
  70. RANGES_INLINE_VARIABLE(action<stride_fn>, stride)
  71. } // namespace actions
  72. /// @}
  73. } // namespace ranges
  74. #endif