Skip to content

Commit 3bd66ea

Browse files
committed
Simplify rotate_left and rotate_right
1 parent 476d5ab commit 3bd66ea

File tree

2 files changed

+22
-62
lines changed

2 files changed

+22
-62
lines changed

include/cpp-sort/detail/rotate_left.h

Lines changed: 11 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015-2022 Morwenn
2+
* Copyright (c) 2015-2025 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55
#ifndef CPPSORT_DETAIL_ROTATE_LEFT_H_
@@ -17,41 +17,20 @@ namespace cppsort
1717
{
1818
namespace detail
1919
{
20-
template<std::size_t N>
21-
struct rotate_left_n
22-
{
23-
template<typename RandomAccessIterator>
24-
auto operator()(RandomAccessIterator first) const
25-
-> void
26-
{
27-
using utility::iter_move;
28-
using difference_type = difference_type_t<RandomAccessIterator>;
29-
30-
auto tmp = iter_move(first);
31-
for (difference_type i = 0 ;
32-
i < static_cast<difference_type>(N - 1) ;
33-
++i)
34-
{
35-
first[i] = iter_move(first + (i + 1));
36-
}
37-
first[N-1] = std::move(tmp);
38-
}
39-
};
40-
41-
template<>
42-
struct rotate_left_n<0u>
43-
{
44-
template<typename RandomAccessIterator>
45-
auto operator()(RandomAccessIterator) const noexcept
46-
-> void
47-
{}
48-
};
49-
5020
template<std::size_t N, typename RandomAccessIterator>
5121
auto rotate_left(RandomAccessIterator first)
5222
-> void
5323
{
54-
return rotate_left_n<N>{}(first);
24+
static_assert(N > 1);
25+
26+
using utility::iter_move;
27+
using difference_type = difference_type_t<RandomAccessIterator>;
28+
29+
auto tmp = iter_move(first);
30+
for (difference_type i = 0; i < static_cast<difference_type>(N - 1); ++i) {
31+
first[i] = iter_move(first + (i + 1));
32+
}
33+
first[N-1] = std::move(tmp);
5534
}
5635
}}
5736

include/cpp-sort/detail/rotate_right.h

Lines changed: 11 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015-2022 Morwenn
2+
* Copyright (c) 2015-2025 Morwenn
33
* SPDX-License-Identifier: MIT
44
*/
55
#ifndef CPPSORT_DETAIL_ROTATE_RIGHT_H_
@@ -17,39 +17,20 @@ namespace cppsort
1717
{
1818
namespace detail
1919
{
20-
template<std::size_t N>
21-
struct rotate_right_n
22-
{
23-
template<typename RandomAccessIterator>
24-
auto operator()(RandomAccessIterator first) const
25-
-> void
26-
{
27-
using utility::iter_move;
28-
using difference_type = difference_type_t<RandomAccessIterator>;
29-
30-
auto tmp = iter_move(first + N - 1);
31-
for (difference_type i = N - 1 ; i > 0 ; --i)
32-
{
33-
first[i] = iter_move(first + (i - 1));
34-
}
35-
first[0] = std::move(tmp);
36-
}
37-
};
38-
39-
template<>
40-
struct rotate_right_n<0u>
41-
{
42-
template<typename RandomAccessIterator>
43-
auto operator()(RandomAccessIterator) const noexcept
44-
-> void
45-
{}
46-
};
47-
4820
template<std::size_t N, typename RandomAccessIterator>
4921
auto rotate_right(RandomAccessIterator first)
5022
-> void
5123
{
52-
return rotate_right_n<N>{}(first);
24+
static_assert(N > 1);
25+
26+
using utility::iter_move;
27+
using difference_type = difference_type_t<RandomAccessIterator>;
28+
29+
auto tmp = iter_move(first + N - 1);
30+
for (difference_type i = N - 1; i > 0; --i) {
31+
first[i] = iter_move(first + (i - 1));
32+
}
33+
first[0] = std::move(tmp);
5334
}
5435
}}
5536

0 commit comments

Comments
 (0)