tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
shift_right.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_ALGORITHM_SHIFT_RIGHT_HPP
4#define TETL_ALGORITHM_SHIFT_RIGHT_HPP
5
12
13namespace etl {
14
29template <typename BidiIt>
30constexpr auto shift_right(BidiIt first, BidiIt last, typename etl::iterator_traits<BidiIt>::difference_type n)
31 -> BidiIt
32{
33 // The standard only checks for n == 0. n < 0 would be undefined behavior.
34 // This implementation does nothing if n < 0.
35 if (n <= 0 or n >= etl::distance(first, last)) {
36 return last;
37 }
38
39 auto dest = etl::prev(last);
40 auto src = etl::prev(dest, n);
41 for (; src != first; --dest, (void)--src) {
42 *dest = etl::move(*src);
43 }
44
45 // Elements outside the new range should be left in a valid but unspecified state.
46 // If the value type has a default constructor we do a little cleanup.
47 using value_type = typename etl::iterator_traits<BidiIt>::value_type;
49 for (; dest != first; --dest) {
50 *dest = value_type{};
51 }
52 }
53
54 return etl::next(first, n);
55}
56
57} // namespace etl
58
59#endif // TETL_ALGORITHM_SHIFT_RIGHT_HPP
constexpr auto shift_right(BidiIt first, BidiIt last, typename etl::iterator_traits< BidiIt >::difference_type n) -> BidiIt
Shifts the elements in the range [first, last) by n positions.
Definition shift_right.hpp:30
constexpr auto move(InputIt first, InputIt last, OutputIt destination) -> OutputIt
Moves the elements in the range [first, last), to another range beginning at destination,...
Definition move.hpp:26
constexpr auto prev(BidirIt it, typename iterator_traits< BidirIt >::difference_type n=1) -> BidirIt
Return the nth predecessor of iterator it.
Definition prev.hpp:14
constexpr auto next(InputIt it, typename iterator_traits< InputIt >::difference_type n=1) -> InputIt
Return the nth successor of iterator it.
Definition next.hpp:14
constexpr auto distance(It first, It last) -> typename iterator_traits< It >::difference_type
Returns the number of hops from first to last.
Definition distance.hpp:16
Definition adjacent_find.hpp:8
constexpr bool is_default_constructible_v
Definition is_default_constructible.hpp:26
iterator_traits is the type trait class that provides uniform interface to the properties of LegacyIt...
Definition iterator_traits.hpp:47