-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector_old.hpp
356 lines (273 loc) · 7.21 KB
/
vector_old.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
#ifndef UUID_C8B889F6B0254CE3F7AE58B920794A20
#define UUID_C8B889F6B0254CE3F7AE58B920794A20
#include "std.h"
#include <stddef.h>
#include <algorithm>
#include <utility>
#include "cstdint.h"
#include "platform.h"
using std::move;
namespace tl {
template<typename T>
struct vector_slice {
T *b, *e;
vector_slice()
: b(0), e(0) {
}
vector_slice(T* b_init, T* e_init)
: b(b_init), e(e_init) {
}
T* begin() const {
return this->b;
}
T* end() const {
return this->e;
}
usize size() const {
return this->e - this->b;
}
usize size_in_bytes() const {
return (u8 const *)this->e - (u8 const *)this->b;
}
bool empty() const {
return this->b == this->e;
}
T& operator[](usize index) {
assert(index < size());
return this->b[index];
}
T const& operator[](usize index) const {
assert(index < size());
return this->b[index];
}
vector_slice<T> unsafe_cut_front_in_bytes(usize bytes) {
return vector_slice((T *)((u8 *)this->b + bytes), this->e);
}
vector_slice<T> unsafe_limit_size_in_bytes(usize bytes) {
return vector_slice(this->b, (T *)((u8 *)this->b + bytes));
}
template<typename U>
U* unsafe_read() {
TL_STATIC_ASSERT((sizeof(U) / sizeof(T)) * sizeof(T) == sizeof(U));
if (size_in_bytes() >= sizeof(U)) {
U *cur = (U *)this->b;
this->b = (T *)((u8 *)this->b + sizeof(U));
return cur;
} else {
return 0;
}
}
};
struct memalloc_allocator {
template<typename T>
void alloc_empty(T&) {
}
template<typename T>
void* alloc_slice(T& slice, usize cap_in_bytes) {
return malloc(cap_in_bytes);
}
template<typename T>
void* realloc_slice(T& slice, usize new_cap_in_bytes, usize old_cap_in_bytes) {
void* new_b;
usize size_bytes = slice.size_in_bytes();
if (new_cap_in_bytes < size_bytes
|| !(new_b = memrealloc(slice.b, new_cap_in_bytes, old_cap_in_bytes))) {
free(slice.b);
new_b = 0;
}
return new_b;
}
template<typename T>
void free_slice(T& slice) {
free(slice.b);
}
};
template<typename T, typename Allocator = memalloc_allocator>
struct vector : protected vector_slice<T>, private Allocator {
T *c;
using vector_slice::begin;
using vector_slice::end;
using vector_slice::size;
using vector_slice::empty;
using vector_slice::size_in_bytes;
using vector_slice::operator[];
vector(vector const&) = delete;
vector& operator=(vector const& other) = delete;
vector()
: c(0) {
this->Allocator::alloc_empty(static_cast<vector_slice<T>&>(*this));
}
// TODO: How to limit this to T with trivial copy?
vector(T const* src, usize len) {
usize len_in_bytes = len * sizeof(T);
this->b = (T *)this->Allocator::alloc_slice(static_cast<vector_slice<T>&>(*this), len_in_bytes);
memcpy(this->b, src, len_in_bytes);
this->c = this->e = (T *)((u8 *)this->b + len_in_bytes);
}
vector(vector&& other)
: vector_slice(other.b, other.e), c(other.c) {
other.b = other.e = other.c = 0;
}
vector_slice slice() {
return *this;
}
vector& operator=(vector&& other) {
this->b = other.b;
this->e = other.e;
this->c = other.c;
other.b = other.e = other.c = 0;
return *this;
}
void push_back(T const& value) {
if(this->cap_left_in_bytes() < sizeof(T))
enlarge(sizeof(T));
new (this->e, non_null()) T(value);
++this->e;
}
void push_back(T&& value) {
if(this->cap_left_in_bytes() < sizeof(T))
enlarge(sizeof(T));
new (this->e++, non_null()) T(move(value));
}
T* cap_end() {
return this->c;
}
usize cap_in_bytes() const {
return (u8 const *)this->c - (u8 const *)this->b;
}
usize cap_left_in_bytes() const {
return (u8 const *)this->c - (u8 const *)this->e;
}
void reserve(usize new_cap) {
if (new_cap * sizeof(T) > cap_in_bytes()) {
reserve_bytes(new_cap * sizeof(T));
}
}
void reserve_in_bytes(usize new_cap_in_bytes) {
if (new_cap_in_bytes > cap_in_bytes()) {
reserve_bytes(new_cap_in_bytes);
}
}
void reserve_bytes(usize new_cap_in_bytes) {
usize size_bytes = size_in_bytes();
T *new_b = (T *)this->Allocator::realloc_slice(static_cast<vector_slice<T>&>(*this), new_cap_in_bytes, cap_in_bytes());
this->b = new_b;
this->e = (T *)((u8 *)new_b + size_bytes);
this->c = (T *)((u8 *)new_b + new_cap_in_bytes);
}
void enlarge(usize extra_in_bytes) {
reserve_in_bytes(size_in_bytes() * 2 + extra_in_bytes);
}
void unsafe_set_size(usize new_size) {
this->e = this->b + new_size;
}
void unsafe_set(T* b, T* e, T* c) {
this->b = b;
this->e = e;
this->c = c;
}
T* unsafe_alloc(usize count) {
if (this->cap_left_in_bytes() < count * sizeof(T))
enlarge(count * sizeof(T));
T* p = end();
this->e += count;
return p;
}
void clear() {
destroy_all();
this->e = this->b;
}
~vector() {
destroy_all();
this->Allocator::free_slice(static_cast<vector_slice<T>&>(*this));
}
private:
void destroy_all() {
for (T* p = this->b; p != this->e; ++p) {
p->~T();
}
}
};
struct mixed_buffer : tl::vector<u8> {
mixed_buffer() {
}
mixed_buffer(mixed_buffer&& other)
: vector(move(other)) {
}
template<typename U>
void unsafe_push(U const& v) {
if(this->cap_left_in_bytes() < sizeof(U))
enlarge(sizeof(U));
new (this->e, non_null()) U(v);
this->e += sizeof(U);
}
/*
u8* unsafe_alloc(usize count) {
if(this->cap_left_in_bytes() < count)
enlarge(count);
u8* p = end();
this->e += count;
return p;
}*/
vector& operator=(mixed_buffer&& other) {
return vector::operator=(move(other));
}
};
template<typename T, usize InlineSize>
struct small_vector;
template<typename ElemT, usize InlineSize>
struct inline_allocator : protected memalloc_allocator {
static usize const inline_size_in_bytes = (InlineSize + sizeof(ElemT) - 1) / sizeof(ElemT) * sizeof(ElemT);
template<typename T>
void alloc_empty(T& slice) {
auto& inline_slice = static_cast<small_vector<ElemT, InlineSize>&>(slice);
inline_slice.unsafe_set(
(ElemT *)inline_slice.inline_data,
(ElemT *)inline_slice.inline_data,
(ElemT *)(inline_slice.inline_data + inline_size_in_bytes));
}
template<typename T>
void* alloc_slice(T& slice, usize cap_in_bytes) {
if (cap_in_bytes <= inline_size_in_bytes) {
auto& inline_slice = static_cast<small_vector<ElemT, InlineSize>&>(slice);
return inline_slice.inline_data;
}
return memalloc(cap_in_bytes);
}
template<typename T>
void* realloc_slice(T& slice, usize new_cap_in_bytes, usize old_cap_in_bytes) {
void* new_b;
usize size_bytes = slice.size_in_bytes();
void* old_b = slice.b;
auto& inline_slice = static_cast<small_vector<ElemT, InlineSize>&>(slice);
if (old_b == inline_slice.inline_data) {
old_b = NULL;
}
if (new_cap_in_bytes < size_bytes
|| !(new_b = memrealloc(old_b, new_cap_in_bytes, old_cap_in_bytes))) {
memfree(old_b);
new_b = 0;
} else if (!old_b) {
memcpy(new_b, slice.b, size_bytes);
}
return new_b;
}
template<typename T>
void free_slice(T& slice) {
auto& inline_slice = static_cast<small_vector<ElemT, InlineSize>&>(slice);
if ((u8 *)slice.b != inline_slice.inline_data) {
memfree(slice.b);
}
}
};
template<typename T, usize InlineSize = 128>
struct small_vector : tl::vector<T, inline_allocator<T, InlineSize> > {
small_vector() {
}
union {
T inline_data_[(InlineSize + sizeof(T) - 1) / sizeof(T)];
u8 inline_data[];
};
};
}
#endif // UUID_C8B889F6B0254CE3F7AE58B920794A20