Horizon
swap_ranges.hpp
Go to the documentation of this file.
1 // 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_ALGORITHM_SWAP_RANGES_HPP
14 #define RANGES_V3_ALGORITHM_SWAP_RANGES_HPP
15 
16 #include <range/v3/range_fwd.hpp>
17 
18 #include <range/v3/algorithm/result_types.hpp>
25 #include <range/v3/utility/static_const.hpp>
26 
27 #include <range/v3/detail/prologue.hpp>
28 
29 namespace ranges
30 {
33  template<typename I1, typename I2>
34  using swap_ranges_result = detail::in1_in2_result<I1, I2>;
35 
36  RANGES_FUNC_BEGIN(swap_ranges)
37 
38 
39  template(typename I1, typename S1, typename I2)(
40  requires input_iterator<I1> AND sentinel_for<S1, I1> AND
41  input_iterator<I2> AND indirectly_swappable<I1, I2>)
42  constexpr swap_ranges_result<I1, I2> //
43  RANGES_FUNC(swap_ranges)(I1 begin1, S1 end1, I2 begin2) //
44  {
45  for(; begin1 != end1; ++begin1, ++begin2)
46  ranges::iter_swap(begin1, begin2);
47  return {begin1, begin2};
48  }
49 
51  template(typename I1, typename S1, typename I2, typename S2)(
52  requires input_iterator<I1> AND sentinel_for<S1, I1> AND
53  input_iterator<I2> AND sentinel_for<S2, I2> AND
54  indirectly_swappable<I1, I2>)
55  constexpr swap_ranges_result<I1, I2> RANGES_FUNC(swap_ranges)(I1 begin1,
56  S1 end1,
57  I2 begin2,
58  S2 end2) //
59  {
60  for(; begin1 != end1 && begin2 != end2; ++begin1, ++begin2)
61  ranges::iter_swap(begin1, begin2);
62  return {begin1, begin2};
63  }
64 
65  template(typename Rng1, typename I2_)(
66  requires input_range<Rng1> AND input_iterator<uncvref_t<I2_>> AND
67  indirectly_swappable<iterator_t<Rng1>, uncvref_t<I2_>>)
68  constexpr swap_ranges_result<iterator_t<Rng1>, uncvref_t<I2_>> //
69  RANGES_FUNC(swap_ranges)(Rng1 && rng1, I2_ && begin2) //
70  {
71  return (*this)(begin(rng1), end(rng1), (I2_ &&) begin2);
72  }
73 
74  template(typename Rng1, typename Rng2)(
75  requires input_range<Rng1> AND input_range<Rng2> AND
76  indirectly_swappable<iterator_t<Rng1>, iterator_t<Rng2>>)
77  constexpr swap_ranges_result<borrowed_iterator_t<Rng1>, borrowed_iterator_t<Rng2>> //
78  RANGES_FUNC(swap_ranges)(Rng1 && rng1, Rng2 && rng2) //
79  {
80  return (*this)(begin(rng1), end(rng1), begin(rng2), end(rng2));
81  }
82 
83  RANGES_FUNC_END(swap_ranges)
84 
85  namespace cpp20
86  {
87  using ranges::swap_ranges;
88  using ranges::swap_ranges_result;
89  } // namespace cpp20
91 } // namespace ranges
92 
93 #include <range/v3/detail/epilogue.hpp>
94 
95 #endif
CPP_concept indirectly_swappable
\concept indirectly_swappable
Definition: concepts.hpp:816
CPP_concept sentinel_for
\concept sentinel_for
Definition: concepts.hpp:306
CPP_concept input_iterator
\concept input_iterator
Definition: concepts.hpp:362