std::ranges::shift_left() algorithm
- od C++20
- Simplified
- Detailed
// (1)
constexpr ranges::subrange<I>
shift_left( I first, S last, std::iter_difference_t<I> n );
// (2)
constexpr ranges::borrowed_subrange_t<R>
shift_left( R&& r, ranges::range_difference_t<R> n );
The type of arguments are generic and have the following constraints:
I
-std::permutable
S
-std::sentinel_for<I>
R
-std::ranges::forward_range
Additionally, each overload has the following constraints:
- (2) -
std::permutable<ranges::iterator_t<R>>
(The std::
namespace was ommitted here for readability)
// (1)
template<
std::permutable I,
std::sentinel_for<I> S
>
constexpr ranges::subrange<I>
shift_left( I first, S last, std::iter_difference_t<I> n );
// (2)
template<
ranges::forward_range R
>
requires std::permutable<ranges::iterator_t<R>>
constexpr ranges::borrowed_subrange_t<R>
shift_left( R&& r, ranges::range_difference_t<R> n );
Shifts the elements in the range [first
; last
) or r
by n
positions.
-
(1) Shifts the elements towards the beginning of the range:
- If
n == 0 || n >= last - first
, there are no effects. - If
n < 0
, the behavior is undefined. - Otherwise, for every integer
i
in [0
;last - first - n
), moves the element originally at positionfirst + n + i
to positionfirst + i
. The moves are performed in increasing order ofi
starting from0
.
- If
-
** (2)** Same as (1), but uses
r
as the range, as if usingranges::begin(r)
asfirst
andranges::end(r)
aslast
.
The function-like entities described on this page are niebloids.
The behavior is undefined
if [first
; last
) is not a valid range.Parameters
first last | The original range. |
r | The range of elements to shift. |
n | The number of positions to shift. |
Return value
{
first,
/*NEW_LAST*/
}
Where NEW_LAST
is the end of the resulting range and equivalent to:
- If
n
is less thanlast - first
,first + (last - first - n)
. - Otherwise,
first
.
Complexity
At most ranges::distance(first, last) - n
assignments.
Exceptions
(none)
Possible implementation
shift_left(1) and shift_left(2)
struct shift_left_fn
{
template<std::bidirectional_iterator I, std::sentinel_for<I> S>
requires std::permutable<I>
constexpr I operator()(I first, S last) const
{
auto last2 {ranges::next(first, last)};
for (auto tail {last2}; !(first == tail or first == --tail); ++first)
ranges::iter_swap(first, tail);
return last2;
}
template<ranges::bidirectional_range R>
requires std::permutable<ranges::iterator_t<R>>
constexpr ranges::borrowed_iterator_t<R>
operator()(R&& r) const
{
return (*this)(ranges::begin(r), ranges::end(r));
}
};
inline constexpr shift_left_fn shift_left {};
Notes
ranges::shift_left
/ ranges::shift_right
has better efficiency on common implementations if I
models bidirectional_iterator
or random_access_iterator
.
Implementations (e.g. MSVC STL) may enable
vectorization when the iterator type models contiguous_iterator
and swapping its value type calls neither non-trivial special member function nor ADL-found swap.
Examples
#include <algorithm>
#include <iostream>
#include <string>
#include <type_traits>
#include <vector>
struct S
{
int value {0};
bool specified_state {true};
S(int v = 0) : value {v} {}
S(S const& rhs) = default;
S(S&& rhs) { *this = std::move(rhs); }
S& operator=(S const& rhs) = default;
S& operator=(S&& rhs)
{
if (this != &rhs)
{
value = rhs.value;
specified_state = rhs.specified_state;
rhs.specified_state = false;
}
return *this;
}
};
template<typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> const& v)
{
for (const auto& s : v)
{
if constexpr (std::is_same_v<T, S>)
s.specified_state ? os << s.value << ' ' : os << ". ";
else if constexpr (std::is_same_v<T, std::string>)
os << (s.empty() ? "." : s) << ' ';
else
os << s << ' ';
}
return os;
}
int main()
{
std::cout << std::left;
std::vector<S> a {1, 2, 3, 4, 5, 6, 7};
std::vector<int> b {1, 2, 3, 4, 5, 6, 7};
std::vector<std::string> c {"α", "β", "γ", "δ", "ε", "ζ", "η"};
std::cout << "vector<S> \tvector<int> \tvector<string>\n";
std::cout << a << " " << b << " " << c << '\n';
std::ranges::shift_left(a, 3);
std::ranges::shift_left(b, 3);
std::ranges::shift_left(c, 3);
std::cout << a << " " << b << " " << c << '\n';
std::ranges::shift_right(a, 2);
std::ranges::shift_right(b, 2);
std::ranges::shift_right(c, 2);
std::cout << a << " " << b << " " << c << '\n';
std::ranges::shift_left(a, 8); // has no effect: n >= last - first
std::ranges::shift_left(b, 8); // ditto
std::ranges::shift_left(c, 8); // ditto
std::cout << a << " " << b << " " << c << '\n';
// std::ranges::shift_left(a, -3); // UB
}
vector<S> vector<int> vector<string>
1 2 3 4 5 6 7 1 2 3 4 5 6 7 α β γ δ ε ζ η
4 5 6 7 . . . 4 5 6 7 5 6 7 δ ε ζ η . . .
. . 4 5 6 7 . 4 5 4 5 6 7 5 . . δ ε ζ η .
. . 4 5 6 7 . 4 5 4 5 6 7 5 . . δ ε ζ η .
Hover to see the original license.