-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshift_proposal.h
113 lines (96 loc) · 3.42 KB
/
shift_proposal.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#ifndef SHIFT_PROPOSAL_H
#define SHIFT_PROPOSAL_H
#include <algorithm>
#include <iterator>
#include <type_traits>
#include <utility>
template<class I>
using difference_type_t = typename std::iterator_traits<I>::difference_type;
template<class I>
using iterator_category_t = typename std::iterator_traits<I>::iterator_category;
template<class I, class Tag, class = void>
constexpr bool is_category = false;
template<class I, class Tag>
constexpr bool is_category<I, Tag, std::enable_if_t<
std::is_convertible_v<iterator_category_t<I>, Tag>>> = true;
/// Increment (decrement for negative n) i |n| times or until i == bound,
/// whichever comes first. Returns n - the difference between i's final position
/// and its initial position. (Note: "advance" has overloads with this behavior
/// in the Ranges TS.)
template<class I>
constexpr difference_type_t<I> bounded_advance(
I& i, difference_type_t<I> n, I const bound)
{
if constexpr (is_category<I, std::bidirectional_iterator_tag>) {
for (; n < 0 && i != bound; ++n, void(--i)) {
;
}
}
for(; n > 0 && i != bound; --n, void(++i)) {
;
}
return n;
}
template<class ForwardIt>
ForwardIt shift_left(ForwardIt first, ForwardIt last, difference_type_t<ForwardIt> n)
{
if (n <= 0) {
return last;
}
auto mid = first;
if (::bounded_advance(mid, n, last)) {
return first;
}
return std::move(std::move(mid), std::move(last), std::move(first));
}
template<class ForwardIt>
ForwardIt shift_right(ForwardIt first, ForwardIt last, difference_type_t<ForwardIt> n)
{
if (n <= 0) {
return first;
}
if constexpr (is_category<ForwardIt, std::bidirectional_iterator_tag>) {
auto mid = last;
if (::bounded_advance(mid, -n, first)) {
return last;
}
return std::move_backward(std::move(first), std::move(mid), std::move(last));
} else {
auto result = first;
if (::bounded_advance(result, n, last)) {
return last;
}
// Invariant: next(first, n) == result
// Invariant: next(trail, n) == lead
auto lead = result;
auto trail = first;
for (; trail != result; ++lead, void(++trail)) {
if (lead == last) {
// The range looks like:
//
// |-- (n - k) elements --|-- k elements --|-- (n - k) elements --|
// ^-first trail-^ ^-result last-^
//
// Note that distance(first, trail) == distance(result, last)
std::move(std::move(first), std::move(trail), std::move(result));
return result;
}
}
for (;;) {
for (auto mid = first; mid != result; ++lead, void(++trail), ++mid) {
if (lead == last) {
// The range looks like:
//
// |-- (n - k) elements --|-- k elements --|-- ... --|-- n elements --|
// ^-first mid-^ result-^ ^-trail last-^
//
trail = std::move(mid, result, std::move(trail));
std::move(std::move(first), std::move(mid), std::move(trail));
return result;
}
std::iter_swap(mid, trail);
}
}
}
}
#endif // !defined(SHIFT_PROPOSAL_H)