-
Notifications
You must be signed in to change notification settings - Fork 7
/
utility.h
210 lines (193 loc) · 5.1 KB
/
utility.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
#ifndef RDESTL_UTILITY_H
#define RDESTL_UTILITY_H
#include <new>
#include "rdestl_common.h"
#include "int_to_type.h"
namespace rde
{
namespace internal
{
template<typename T>
void copy_n(const T* first, size_t n, T* result, int_to_type<false>)
{
const T* last = first + n;
//while (first != last)
// *result++ = *first++;
switch (n & 0x3)
{
case 0:
while (first != last)
{
*result++ = *first++;
case 3: *result++ = *first++;
case 2: *result++ = *first++;
case 1: *result++ = *first++;
}
}
}
template<typename T>
void copy_n(const T* first, size_t n, T* result, int_to_type<true>)
{
RDE_ASSERT(result >= first + n || result < first);
Sys::MemCpy(result, first, n * sizeof(T));
}
template<typename T>
void copy(const T* first, const T* last, T* result, int_to_type<false>)
{
// Local variable helps MSVC keep stuff in registers
T* localResult = result;
while (first != last)
*localResult++ = *first++;
}
template<typename T>
void copy(const T* first, const T* last, T* result, int_to_type<true>)
{
const size_t n = reinterpret_cast<const char*>(last) - reinterpret_cast<const char*>(first);
Sys::MemCpy(result, first, n);
}
template<typename T> RDE_FORCEINLINE
void move_n(const T* from, size_t n, T* result, int_to_type<false>)
{
for (size_t i = n - 1; i != size_t(-1); --i)
result[i] = from[i];
}
template<typename T> RDE_FORCEINLINE
void move_n(const T* first, size_t n, T* result, int_to_type<true>)
{
Sys::MemMove(result, first, n * sizeof(T));
}
template<typename T> RDE_FORCEINLINE
void move(const T* first, const T* last, T* result, int_to_type<false>)
{
result += (last - first);
while (--last >= first)
*(--result) = *last;
}
template<typename T> RDE_FORCEINLINE
void move(const T* first, const T* last, T* result, int_to_type<true>)
{
// Meh, MSVC does pretty stupid things here.
//memmove(result, first, (last - first) * sizeof(T));
const size_t n = reinterpret_cast<uintptr_t>(last) - reinterpret_cast<uintptr_t>(first);
//const size_t n = (last - first) * sizeof(T);
Sys::MemMove(result, first, n);
}
template<typename T>
void copy_construct_n(const T* first, size_t n, T* result, int_to_type<false>)
{
for (size_t i = 0; i < n; ++i)
new (result + i) T(first[i]);
}
template<typename T>
void copy_construct_n(const T* first, size_t n, T* result, int_to_type<true>)
{
RDE_ASSERT(result >= first + n || result < first);
Sys::MemCpy(result, first, n * sizeof(T));
}
template<typename T>
void move_construct_n(T* first, size_t n, T* result, int_to_type<false>)
{
for (size_t i = 0; i < n; ++i)
new (result + i) T(std::move(first[i]));
}
template<typename T>
void move_construct_n(T* first, size_t n, T* result, int_to_type<true>)
{
RDE_ASSERT(result >= first + n || result < first);
Sys::MemCpy(result, first, n * sizeof(T));
}
template<typename T>
void destruct_n(T* first, size_t n, int_to_type<false>)
{
// For unknown reason MSVC cant see reference to first here...
sizeof(first);
for (size_t i = 0; i < n; ++i)
(first + i)->~T();
}
template<typename T> RDE_FORCEINLINE
void destruct_n(T*, size_t, int_to_type<true>)
{
// Nothing to do, no destructor needed.
}
template<typename T>
void destruct(T* mem, int_to_type<false>)
{
sizeof(mem);
mem->~T();
}
template<typename T> RDE_FORCEINLINE
void destruct(T*, int_to_type<true>)
{
// Nothing to do, no destructor needed.
}
template<typename T>
void construct(T* mem, int_to_type<false>)
{
new (mem) T();
}
template<typename T> RDE_FORCEINLINE
void construct(T*, int_to_type<true>)
{
// Nothing to do
}
template<typename T> RDE_FORCEINLINE
void copy_construct(T* mem, const T& orig, int_to_type<false>)
{
new (mem) T(orig);
}
template<typename T> RDE_FORCEINLINE
void copy_construct(T* mem, const T& orig, int_to_type<true>)
{
mem[0] = orig;
}
template<typename T>
void construct_n(T* to, size_t count, int_to_type<false>)
{
sizeof(to);
for (size_t i = 0; i < count; ++i)
new (to + i) T();
}
template<typename T> inline
void construct_n(T*, size_t, int_to_type<true>)
{
// trivial ctor, nothing to do.
}
// Tests if all elements in range are ordered according to pred.
template<class TIter, class TPred>
void test_ordering(TIter first, TIter last, const TPred& pred)
{
#if RDE_DEBUG
if (first != last)
{
TIter next = first;
if (++next != last)
{
RDE_ASSERT(pred(*first, *next));
first = next;
}
}
#else
sizeof(first); sizeof(last); sizeof(pred);
#endif
}
template<typename T1, typename T2, class TPred> inline
bool debug_pred(const TPred& pred, const T1& a, const T2& b)
{
#if RDE_DEBUG
if (pred(a, b))
{
RDE_ASSERT(!pred(b, a));
return true;
}
else
{
return false;
}
#else
return pred(a, b);
#endif
}
} // namespace internal
} // namespace rde
//-----------------------------------------------------------------------------
#endif // #ifndef RDESTL_UTILITY_H