tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
shift_left.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2
3#ifndef TETL_ALGORITHM_SHIFT_LEFT_HPP
4#define TETL_ALGORITHM_SHIFT_LEFT_HPP
5
9
10namespace etl {
11
21template <typename ForwardIt>
22constexpr auto shift_left(ForwardIt first, ForwardIt const last, typename iterator_traits<ForwardIt>::difference_type n)
23 -> ForwardIt
24{
25 // The standard only checks for n == 0. n < 0 would be undefined behavior.
26 // This implementation does nothing if n < 0.
27 if (n <= 0) {
28 return last;
29 }
30
31 auto start = first;
32 if constexpr (etl::detail::RandomAccessIterator<ForwardIt>) {
33 if (n >= last - first) {
34 return first;
35 }
36 start += n;
37 } else {
38 for (; 0 < n; --n) {
39 if (start == last) {
40 return first;
41 }
42 ++start;
43 }
44 }
45
46 first = etl::move(start, last, first);
47 return first;
48}
49
50} // namespace etl
51
52#endif // TETL_ALGORITHM_SHIFT_LEFT_HPP
constexpr auto shift_left(ForwardIt first, ForwardIt const last, typename iterator_traits< ForwardIt >::difference_type n) -> ForwardIt
Shifts the elements in the range [first, last) by n positions.
Definition shift_left.hpp:22
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
Definition adjacent_find.hpp:8
iterator_traits is the type trait class that provides uniform interface to the properties of LegacyIt...
Definition iterator_traits.hpp:47