forked from msinilo/rdestl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
algorithm.h
293 lines (265 loc) · 8.2 KB
/
algorithm.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
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#ifndef RDESTL_ALGORITHM_H
#define RDESTL_ALGORITHM_H
#include "utility.h"
#include "int_to_type.h"
#include "iterator.h"
#include "type_traits.h"
namespace rde
{
//-----------------------------------------------------------------------------
// Modern C++ version that does constructor/copy constructor/move constructor in 1
template<typename T, typename... Args> RDE_FORCEINLINE
void construct_args(T* p, Args&&... args)
{
::new (static_cast<void*>(p)) T(std::forward<Args>(args)...);
}
//-----------------------------------------------------------------------------
template<typename T> RDE_FORCEINLINE
void copy_construct(T* mem, const T& orig)
{
//new (mem) T(orig);
internal::copy_construct(mem, orig, int_to_type<has_trivial_copy<T>::value>());
}
//-----------------------------------------------------------------------------
template<typename T> RDE_FORCEINLINE
void construct(T* mem)
{
internal::construct(mem, int_to_type<has_trivial_constructor<T>::value>());
}
//-----------------------------------------------------------------------------
template<typename T> RDE_FORCEINLINE
void destruct(T* mem)
{
internal::destruct(mem, int_to_type<has_trivial_destructor<T>::value>());
}
//-----------------------------------------------------------------------------
template<typename T>
void copy_n(const T* first, size_t n, T* result)
{
internal::copy_n(first, n, result, int_to_type<has_trivial_copy<T>::value>());
}
//-----------------------------------------------------------------------------
template<typename T>
void copy(const T* first, const T* last, T* result)
{
internal::copy(first, last, result, int_to_type<has_trivial_copy<T>::value>());
}
//-----------------------------------------------------------------------------
template<typename T>
void copy_construct_n(T* first, size_t n, T* result)
{
internal::copy_construct_n(first, n, result, int_to_type<has_trivial_copy<T>::value>());
}
//-----------------------------------------------------------------------------
// Modern C++ version that does constructor / copy constructor / move constructor in 1
template <typename T>
void move_construct_n(T* first, size_t n, T* result)
{
internal::move_construct_n(first, n, result, int_to_type<has_trivial_copy<T>::value>());
}
//-----------------------------------------------------------------------------
template<typename T>
void move_n(const T* from, size_t n, T* result)
{
RDE_ASSERT(from != result || n == 0);
// Overlap?
if (result + n >= from && result < from + n)
{
internal::move_n(from, n, result, int_to_type<has_trivial_copy<T>::value>());
}
else
{
internal::copy_n(from, n, result, int_to_type<has_trivial_copy<T>::value>());
}
}
//-----------------------------------------------------------------------------
template<typename T>
inline void move(const T* first, const T* last, T* result)
{
RDE_ASSERT(first != result || first == last);
const size_t n = reinterpret_cast<uintptr_t>(last) - reinterpret_cast<uintptr_t>(first);
const unsigned char* resultEnd = reinterpret_cast<const unsigned char*>(result) + n;
if (resultEnd >= reinterpret_cast<const unsigned char*>(first) && result < last)
{
internal::move(first, last, result, int_to_type<has_trivial_copy<T>::value>());
}
else
{
internal::copy(first, last, result, int_to_type<has_trivial_copy<T>::value>());
}
}
//-----------------------------------------------------------------------------
template<typename T>
void construct_n(T* first, size_t n)
{
internal::construct_n(first, n, int_to_type<has_trivial_constructor<T>::value>());
}
//-----------------------------------------------------------------------------
template<typename T>
void destruct_n(T* first, size_t n)
{
internal::destruct_n(first, n, int_to_type<has_trivial_destructor<T>::value>());
}
//-----------------------------------------------------------------------------
template<typename T> RDE_FORCEINLINE
void fill_n(T* first, size_t n, const T& val)
{
//for (size_t i = 0; i < n; ++i)
// first[i] = val;
// Loop unrolling with Duff's Device.
T* last = first + n;
switch (n & 0x7)
{
case 0:
while (first != last)
{
*first = val; ++first;
case 7: *first = val; ++first;
case 6: *first = val; ++first;
case 5: *first = val; ++first;
case 4: *first = val; ++first;
case 3: *first = val; ++first;
case 2: *first = val; ++first;
case 1: *first = val; ++first;
}
}
}
//-----------------------------------------------------------------------------
template<typename TIter, typename TDist> inline
void distance(TIter first, TIter last, TDist& dist)
{
internal::distance(first, last, dist, typename iterator_traits<TIter>::iterator_category());
}
//-----------------------------------------------------------------------------
template<typename TIter, typename TDist> inline
void advance(TIter& iter, TDist off)
{
internal::advance(iter, off, typename iterator_traits<TIter>::iterator_category());
}
//-----------------------------------------------------------------------------
template<class TIter, typename T, class TPred> inline
TIter lower_bound(TIter first, TIter last, const T& val, const TPred& pred)
{
internal::test_ordering(first, last, pred);
ptrdiff_t dist(0);
distance(first, last, dist);
while (dist > 0)
{
const ptrdiff_t halfDist = dist >> 1;
TIter mid = first;
rde::advance(mid, halfDist);
if (internal::debug_pred(pred, *mid, val))
first = ++mid, dist -= halfDist + 1;
else
dist = halfDist;
}
return first;
}
//-----------------------------------------------------------------------------
template<class TIter, typename T, class TPred> inline
TIter upper_bound(TIter first, TIter last, const T& val, const TPred& pred)
{
internal::test_ordering(first, last, pred);
ptrdiff_t dist(0);
distance(first, last, dist);
while (dist > 0)
{
const ptrdiff_t halfDist = dist >> 1;
TIter mid = first;
advance(mid, halfDist);
if (!internal::debug_pred(pred, val, *mid))
first = ++mid, dist -= halfDist + 1;
else
dist = halfDist;
}
return first;
}
//-----------------------------------------------------------------------------
template<class TIter, typename T>
TIter find(TIter first, TIter last, const T& val)
{
while (first != last)
{
if ((*first) == val)
return first;
++first;
}
return last;
}
//-----------------------------------------------------------------------------
template<class TIter, typename T, class TPred>
TIter find_if(TIter first, TIter last, const T& val, const TPred& pred)
{
while (first != last)
{
if (pred(*first, val))
return first;
++first;
}
return last;
}
//-----------------------------------------------------------------------------
template<class TIter, typename T>
void accumulate(TIter first, TIter last, T& result)
{
while (first != last)
{
result += *first;
++first;
}
}
//-----------------------------------------------------------------------------
template<typename T>
T abs(const T& t)
{
return t >= T(0) ? t : -t;
}
// No branches, Hacker's Delight way.
RDE_FORCEINLINE int abs(int x)
{
const int y = x >> 31;
return (x ^ y) - y;
}
RDE_FORCEINLINE short abs(short x)
{
const short y = x >> 15;
return (x ^ y) - y;
}
//-----------------------------------------------------------------------------
template<typename T> inline
T max(const T& x, const T& y)
{
return x > y ? x : y;
}
//-----------------------------------------------------------------------------
template<typename T> inline
T min(const T& x, const T& y)
{
return x < y ? x : y;
}
// @TODO: determine if it REALLY is quicker than version with branches.
/*RDE_FORCEINLINE float min(float x, float y)
{
float result;
__asm
{
fld [x]
fld [y]
fcomi st(0), st(1)
fcmovnb st(0), st(1)
fstp [result]
fcomp
}
return result;
}*/
//-----------------------------------------------------------------------------
template<typename TAssignable>
void swap(TAssignable& a, TAssignable& b)
{
TAssignable tmp(a);
a = b;
b = tmp;
}
} // namespace rde
//-----------------------------------------------------------------------------
#endif // #ifndef RDESTL_ALGORITHM_H