forked from graphitemaster/neothyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
u_buffer.h
314 lines (268 loc) · 7.99 KB
/
u_buffer.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#ifndef U_BUFFER_HDR
#define U_BUFFER_HDR
#include <assert.h>
#include <stdlib.h>
#include "u_new.h"
#include "u_traits.h"
namespace u {
namespace detail {
template <typename T, bool pod = u::is_pod<T>::value>
struct is_pod { };
template <typename T, T value>
struct swap_test;
template <typename T>
static inline void move_construct(T *a, T &b, ...) {
new(a) T(b);
}
template <typename T>
static inline void move_construct(T *a, T &b, void *,
swap_test<void (T::*)(T&), &T::swap> * = 0)
{
new(a) T(b);
a->swap(b);
}
template <typename T>
static inline void move_impl(T &a, T &b, ...) {
a = b;
}
template <typename T>
static inline void move_impl(T &a, T &b, T*, swap_test<void (T::*)(T&), &T::swap>* = 0) {
a.swap(b);
}
template <typename T>
static inline void move(T &a, T &b) {
move_impl(a, b, (T*)0);
}
}
template <typename T>
static inline void move_construct(T *a, T& b) {
detail::move_construct(a, b, (T*)0);
}
template <typename T>
struct buffer {
buffer();
buffer(buffer &&other);
~buffer();
T *first;
T *last;
T *capacity;
buffer &operator=(buffer &&other);
void destroy();
void destroy_range(T *first, T *last);
void move_urange(T *dest, T *first, T *last);
void bmove_urange(T *dest, T *first, T *last);
void fill_urange(T *first, T *last, const T &value);
void resize(size_t size, const T &value);
void reserve(size_t icapacity);
void clear();
template <typename I>
void insert(T *where, const I *ifirst, const I *ilast);
void swap(buffer &other);
T *erase(T *ifirst, T*ilast);
private:
void reserve_traits(size_t icapacity, detail::is_pod<T, false>);
void reserve_traits(size_t icapacity, detail::is_pod<T, true>);
void destroy_range_traits(T *first, T *last, detail::is_pod<T, false>);
void destroy_range_traits(T*, T*, detail::is_pod<T, true>);
void fill_urange_traits(T *first, T *last, const T &value,
detail::is_pod<T, false>);
void fill_urange_traits(T *first, T *last, const T &value,
detail::is_pod<T, true>);
void move_urange_traits(T *dest, T *first, T *last,
detail::is_pod<T, false>);
void move_urange_traits(T *dest, T *first, T *last,
detail::is_pod<T, true>);
void bmove_urange_traits(T *dest, T *first, T *last,
detail::is_pod<T, false>);
void bmove_urange_traits(T *dest, T *first, T *last,
detail::is_pod<T, true>);
};
template <typename T>
inline buffer<T>::buffer()
: first(nullptr)
, last(nullptr)
, capacity(nullptr)
{
}
template <typename T>
inline buffer<T>::~buffer() {
destroy_range(first, last);
neoFree(first);
}
template <typename T>
inline buffer<T>::buffer(buffer<T> &&other) {
first = other.first;
last = other.last;
capacity = other.capacity;
other.first = nullptr;
other.last = nullptr;
other.capacity = nullptr;
}
template <typename T>
inline buffer<T> &buffer<T>::operator=(buffer<T> &&other) {
assert(this != &other);
destroy();
first = other.first;
last = other.last;
capacity = other.capacity;
other.first = nullptr;
other.last = nullptr;
other.capacity = nullptr;
return *this;
}
template <typename T>
inline void buffer<T>::destroy_range(T *first, T *last) {
destroy_range_traits(first, last, detail::is_pod<T>());
}
template <typename T>
inline void buffer<T>::move_urange(T *dest, T *first, T *last) {
move_urange_traits(dest, first, last, detail::is_pod<T>());
}
template <typename T>
inline void buffer<T>::bmove_urange(T *dest, T *first, T *last) {
bmove_urange_traits(dest, first, last, detail::is_pod<T>());
}
template <typename T>
inline void buffer<T>::fill_urange(T *first, T *last, const T &value) {
fill_urange_traits(first, last, value, detail::is_pod<T>());
}
template <typename T>
inline void buffer<T>::destroy() {
destroy_range(first, last);
neoFree(first);
first = nullptr;
last = nullptr;
capacity = nullptr;
}
template <typename T>
inline void buffer<T>::reserve(size_t icapacity) {
reserve_traits(icapacity, detail::is_pod<T>());
}
template <typename T>
inline void buffer<T>::resize(size_t size, const T &value) {
reserve(size);
fill_urange(last, first + size, value);
destroy_range(first + size, last);
last = first + size;
}
template <typename T>
inline void buffer<T>::clear() {
destroy_range(first, last);
last = first;
}
template <typename T>
template <typename I>
inline void buffer<T>::insert(T *where, const I *ifirst, const I *ilast) {
const size_t offset = size_t(where - first);
const size_t newsize = size_t((last - first) + (ilast - ifirst));
if (first + newsize > capacity)
reserve((newsize * 3) / 2);
where = first + offset;
const size_t count = size_t(ilast - ifirst);
if (where != last)
bmove_urange(where + count, where, last);
for (; ifirst != ilast; ++ifirst, ++where)
new(where) T(*ifirst);
last = first + newsize;
}
template <typename T>
inline void buffer<T>::swap(buffer<T> &other) {
T *tfirst = first;
T *tlast = last;
T *tcapacity = capacity;
first = other.first;
last = other.last;
capacity = other.capacity;
other.first = tfirst;
other.last = tlast;
other.capacity = tcapacity;
}
template <typename T>
inline void buffer<T>::reserve_traits(size_t icapacity, detail::is_pod<T, false>) {
if (first + icapacity <= capacity)
return;
T *newfirst = neoMalloc(sizeof(T) * icapacity);
const size_t size = size_t(last - first);
move_urange(newfirst, first, last);
neoFree(first);
first = newfirst;
last = newfirst + size;
capacity = newfirst + icapacity;
}
template <typename T>
inline void buffer<T>::reserve_traits(size_t icapacity, detail::is_pod<T, true>) {
if (first + icapacity <= capacity)
return;
T *newfirst = neoRealloc(first, sizeof(T) * icapacity);
const size_t size = size_t(last - first);
first = newfirst;
last = newfirst + size;
capacity = newfirst + icapacity;
}
template <typename T>
inline void buffer<T>::destroy_range_traits(T *first, T *last, detail::is_pod<T, false>) {
for (; first < last; ++first)
first->~T();
}
template <typename T>
inline void buffer<T>::destroy_range_traits(T*, T*, detail::is_pod<T, true>) {
// Empty
}
template <typename T>
inline void buffer<T>::fill_urange_traits(T *first, T *last, const T &value,
detail::is_pod<T, false>)
{
for (; first < last; ++first)
new(first) T(value);
}
template <typename T>
inline void buffer<T>::fill_urange_traits(T *first, T *last, const T &value,
detail::is_pod<T, true>)
{
for (; first < last; ++first)
*first = value;
}
template <typename T>
inline void buffer<T>::move_urange_traits(T *dest, T *first, T *last,
detail::is_pod<T, false>)
{
for (T* it = first; it != last; ++it, ++dest)
move_construct(dest, *it);
destroy_range(first, last);
}
template <typename T>
inline void buffer<T>::move_urange_traits(T *dest, T *first, T *last,
detail::is_pod<T, true>)
{
for (; first != last; ++first, ++dest)
*dest = *first;
}
template <typename T>
inline void buffer<T>::bmove_urange_traits(T *dest, T *first, T *last,
detail::is_pod<T, false>)
{
dest += (last - first);
for (T *it = last; it != first; --it, --dest) {
move_construct(dest - 1, *(it - 1));
destroy_range(it - 1, it);
}
}
template <typename T>
inline void buffer<T>::bmove_urange_traits(T *dest, T *first, T *last,
detail::is_pod<T, true>)
{
dest += (last - first);
for (T* it = last; it != first; --it, --dest)
*(dest - 1) = *(it - 1);
}
template <typename T>
inline T *buffer<T>::erase(T *ifirst, T*ilast) {
const size_t range = ilast - ifirst;
for (T *it = ilast, *end = last, *dest = ifirst; it != end; ++it, ++dest)
detail::move(*dest, *it);
destroy_range(last - range, last);
last -= range;
return ifirst;
}
}
#endif