-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathswap.hpp
180 lines (152 loc) · 4.62 KB
/
swap.hpp
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* @file swap.hpp
* @brief
* @date 2020-06-17
* @author Peter
* @copyright
* Peter of [ThinkSpirit Laboratory](http://thinkspirit.org/)
* of [Nanjing University of Information Science & Technology](http://www.nuist.edu.cn/)
* all rights reserved
*/
#ifndef KERBAL_ALGORITHM_SWAP_HPP
#define KERBAL_ALGORITHM_SWAP_HPP
#include <kerbal/compatibility/constexpr.hpp>
#include <kerbal/compatibility/move.hpp>
#include <kerbal/compatibility/noexcept.hpp>
#include <kerbal/iterator/iterator.hpp>
#include <kerbal/type_traits/tribool_constant.hpp>
#include <cstddef>
#if __cplusplus >= 201103L
# include <kerbal/type_traits/is_nothrow_move_assignable.hpp>
# include <kerbal/type_traits/is_nothrow_move_constructible.hpp>
#else
# include <kerbal/type_traits/is_nothrow_copy_assignable.hpp>
# include <kerbal/type_traits/is_nothrow_copy_constructible.hpp>
#endif
namespace kerbal
{
namespace algorithm
{
template <typename T>
struct try_test_is_nothrow_swappable :
kerbal::type_traits::tribool_conjunction<
# if __cplusplus >= 201103L
kerbal::type_traits::try_test_is_nothrow_move_constructible<T>,
kerbal::type_traits::try_test_is_nothrow_move_assignable<T>
# else
kerbal::type_traits::try_test_is_nothrow_copy_constructible<T>,
kerbal::type_traits::try_test_is_nothrow_copy_assignable<T>
# endif
>::result
{
};
template <typename T>
KERBAL_CONSTEXPR14
void swap(T & lhs, T & rhs)
KERBAL_CONDITIONAL_NOEXCEPT(
try_test_is_nothrow_swappable<T>::IS_TRUE::value
)
;
template <typename T, std::size_t N>
KERBAL_CONSTEXPR14
void swap(T (&lhs)[N], T (&rhs)[N]);
template <typename T>
KERBAL_CONSTEXPR14
void swap(T & lhs, T & rhs)
KERBAL_CONDITIONAL_NOEXCEPT(
try_test_is_nothrow_swappable<T>::IS_TRUE::value
)
{
T t(kerbal::compatibility::to_xvalue(lhs));
lhs = kerbal::compatibility::to_xvalue(rhs);
rhs = kerbal::compatibility::to_xvalue(t);
}
template <typename ForwardIterator1, typename ForwardIterator2>
KERBAL_CONSTEXPR14
void iter_swap(ForwardIterator1 lhs, ForwardIterator2 rhs)
KERBAL_CONDITIONAL_NOEXCEPT(
noexcept(kerbal::algorithm::swap(*lhs, *rhs))
)
{
kerbal::algorithm::swap(*lhs, *rhs);
}
namespace detail
{
template <typename ForwardIterator1, typename ForwardIterator2>
KERBAL_CONSTEXPR14
ForwardIterator2
range_swap_helper(
ForwardIterator1 a_first, ForwardIterator1 a_last, ForwardIterator2 b_first,
std::forward_iterator_tag
)
KERBAL_CONDITIONAL_NOEXCEPT(
noexcept(static_cast<bool>(a_first != a_last)) &&
noexcept(kerbal::algorithm::iter_swap(a_first, b_first)) &&
noexcept(++a_first) &&
noexcept(++b_first)
)
{
while (a_first != a_last) {
kerbal::algorithm::iter_swap(a_first, b_first);
++a_first;
++b_first;
}
return b_first;
}
template <typename RandomAccessIterator1, typename ForwardIterator2>
KERBAL_CONSTEXPR14
ForwardIterator2
range_swap_helper(
RandomAccessIterator1 a_first, RandomAccessIterator1 a_last, ForwardIterator2 b_first,
std::random_access_iterator_tag
)
{
typedef RandomAccessIterator1 iterator;
typedef typename kerbal::iterator::iterator_traits<iterator>::difference_type difference_type;
# define EACH() do { \
kerbal::algorithm::iter_swap(a_first, b_first); \
++a_first; \
++b_first; \
} while (false)
difference_type trip_count(kerbal::iterator::distance(a_first, a_last));
difference_type remain(trip_count & 3);
for (trip_count >>= 2; trip_count > 0; --trip_count) {
EACH();
EACH();
EACH();
EACH();
}
if (remain >= 2) {
EACH();
EACH();
remain -= 2;
}
if (remain >= 1) {
EACH();
}
# undef EACH
return b_first;
}
} // namespace detail
template <typename ForwardIterator1, typename ForwardIterator2>
KERBAL_CONSTEXPR14
ForwardIterator2
range_swap(ForwardIterator1 a_first, ForwardIterator1 a_last, ForwardIterator2 b_first)
{
return kerbal::algorithm::detail::range_swap_helper(
a_first, a_last, b_first,
kerbal::iterator::iterator_category(a_first)
);
}
template <typename T, std::size_t N>
KERBAL_CONSTEXPR14
void swap(T (&lhs)[N], T (&rhs)[N])
{
kerbal::algorithm::range_swap(lhs + 0, lhs + N, rhs + 0);
}
} // namespace algorithm
} // namespace kerbal
#include <kerbal/memory/allocator/fixed_size_node_allocator/fixed_size_node_allocator.fwd.hpp>
#include <kerbal/memory/allocator/monotonic_allocator/monotonic_allocator.fwd.hpp>
#include <kerbal/memory/allocator/over_aligned_allocator/over_aligned_allocator.fwd.hpp>
#endif // KERBAL_ALGORITHM_SWAP_HPP