-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstackallocator.h
More file actions
434 lines (408 loc) · 11 KB
/
stackallocator.h
File metadata and controls
434 lines (408 loc) · 11 KB
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
#include <iterator>
#include <compare>
#include <memory>
template <size_t N>
struct StackStorage {
char storage[N];
StackStorage() = default;
~StackStorage() = default;
StackStorage(const StackStorage& other) = delete;
StackStorage operator=(const StackStorage<N>& other) = delete;
size_t start = 0;
char* GetReference(size_t pos) {
return &storage[pos];
}
};
template <typename T, size_t N>
struct StackAllocator {
StackStorage<N>* storage = nullptr;
size_t space = 0;
using value_type = T;
using pointer = value_type*;
using reference = T&;
StackAllocator() {
if (N < this->storage->start) {
throw std::bad_alloc();
}
this->space = N - this->storage->start;
}
StackAllocator(StackStorage<N>& other): storage(&other) {}
template <typename U>
StackAllocator(const StackAllocator<U, N>& other)
: storage(other.storage), space(other.space) {}
pointer allocate(size_t the_sz) {
if (the_sz > N) {
throw std::bad_alloc();
}
size_t sz = N - (storage->GetReference(storage->start) - storage->GetReference(0));
char* ptr = storage->GetReference(storage->start);
std::align(alignof(T), sizeof(T), reinterpret_cast<void*&>(ptr), sz);
storage->start += ptr - storage->GetReference(storage->start);
T* final_pos = reinterpret_cast<T*>(storage->GetReference(storage->start));
storage->start += the_sz * sizeof(T);
return final_pos;
}
void deallocate(T*, size_t) {}
bool operator==(const StackAllocator& other) const {
return storage == other.storage;
}
template <typename U>
StackAllocator<T, N>& operator=(const StackAllocator<U, N>& other) {
this->storage = other.storage;
return *this;
}
template <typename U>
struct rebind {
using other = StackAllocator<U, N>;
};
};
template <typename T, typename the_alloc = std::allocator<T>>
struct List {
private:
struct BaseNode;
struct Node;
using allocator_type = typename std::allocator_traits<the_alloc>::template rebind_alloc<Node>;
using allocator_traits = typename std::allocator_traits<the_alloc>::template rebind_traits<Node>;
void AddVertex(Node* pos);
void AddVertexVal(Node* pos, const T& val);
void DeleteVertex(Node* pos);
void GetCleared();
void GetSwapped(List&);
size_t the_size = 0;
BaseNode fakeNode = BaseNode();
allocator_type alloc;
public:
List();
List(size_t size);
List(const List<T, the_alloc>& other);
List(size_t size, const T& val);
List(const the_alloc& allocator);
List(size_t size, const the_alloc& allocator);
List(size_t size, const T& val, const the_alloc& allocator);
~List();
List<T, the_alloc>& operator=(const List<T, the_alloc>& list);
allocator_type get_allocator() const {
return this->alloc;
}
void push_back(const T& val);
void push_front(const T& val);
void pop_back();
void pop_front();
size_t size() const {
return this->the_size;
}
template <bool IterType>
class base_iterator;
using iterator = base_iterator<false>;
using const_iterator = base_iterator<true>;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
void erase(const_iterator it);
void insert(const_iterator it, const T& value);
iterator begin() {
return iterator(fakeNode.next, 0);
}
iterator end() {
return iterator(fakeNode.prev->next, this->the_size);
}
const_iterator cbegin() const {
return const_iterator(fakeNode.next, 0);
}
const_iterator cend() const {
return const_iterator(fakeNode.prev->next, this->the_size);
}
const_iterator begin() const {
return const_iterator(fakeNode.next, 0);
}
const_iterator end() const {
return cend();
}
reverse_iterator rend() {
return std::make_reverse_iterator(this->begin());
}
reverse_iterator rbegin() {
return std::make_reverse_iterator(this->end());
}
const_reverse_iterator rbegin() const {
return std::make_reverse_iterator(this->cend());
}
const_reverse_iterator rend() const {
return std::make_reverse_iterator(this->cbegin());
}
const_reverse_iterator crbegin() const {
return std::make_reverse_iterator(this->cend());
}
const_reverse_iterator crend() const {
return std::make_reverse_iterator(this->cbegin());
}
};
template <typename T, typename the_alloc>
void List<T, the_alloc>::GetSwapped(List& other) {
std::swap(alloc, other.alloc);
std::swap(the_size, other.the_size);
std::swap(fakeNode, other.fakeNode);
}
template <typename T, typename the_alloc>
void List<T, the_alloc>::GetCleared() {
while (the_size != 0) {
pop_back();
}
}
template <typename T, typename the_alloc>
void List<T, the_alloc>::DeleteVertex(List<T, the_alloc>::Node* pos) {
pos->next->prev = pos->prev;
pos->prev->next = pos->next;
--this->the_size;
allocator_traits::destroy(alloc, &pos->value);
allocator_traits::deallocate(alloc, pos, 1);
}
template <typename T, typename the_alloc>
void List<T, the_alloc>::AddVertex(List::Node* pos) {
Node* ptr = nullptr;
try {
ptr = alloc.allocate((size_t)1);
allocator_traits::construct(alloc, &ptr->value);
} catch (...) {
throw;
}
pos->prev->next = ptr;
ptr->next = pos;
ptr->prev = pos->prev;
pos->prev = ptr;
++this->the_size;
}
template <typename T, typename the_alloc>
void List<T, the_alloc>::AddVertexVal(List::Node* pos, const T& val) {
Node* ptr = nullptr;
try {
ptr = alloc.allocate((size_t)1);
allocator_traits::construct(alloc, &ptr->value, val);
} catch (...) {
throw;
}
pos->prev->next = ptr;
ptr->next = pos;
ptr->prev = pos->prev;
pos->prev = ptr;
++this->the_size;
}
template <typename T, typename the_alloc>
struct List<T, the_alloc>::BaseNode {
Node* prev = nullptr;
Node* next = nullptr;
BaseNode() {
this->prev = static_cast<Node*>(this);
this->next = static_cast<Node*>(this);
}
};
template <typename T, typename the_alloc>
struct List<T, the_alloc>::Node: BaseNode {
T value;
Node() = default;
};
template <typename T, typename the_alloc>
List<T, the_alloc>::List() = default;
template <typename T, typename the_alloc>
List<T, the_alloc>::List(size_t sz) {
for (size_t tmp = 0; tmp < sz; ++tmp) {
try {
this->AddVertex(fakeNode.prev);
} catch (...) {
GetCleared();
throw;
}
}
}
template <typename T, typename the_alloc>
List<T, the_alloc>::List(size_t sz, const the_alloc& allocator): alloc(allocator) {
for (size_t tmp = 0; tmp < sz; ++tmp) {
try {
this->AddVertex(fakeNode.prev);
} catch (...) {
GetCleared();
throw;
}
}
}
template <typename T, typename the_alloc>
List<T, the_alloc>::List(size_t sz, const T& val) {
for (size_t tmp = 0; tmp < sz; ++tmp) {
try {
push_back(val);
} catch (...) {
GetCleared();
throw;
}
}
}
template <typename T, typename the_alloc>
List<T, the_alloc>::List(const the_alloc& allocator): alloc(allocator) {}
template <typename T, typename the_alloc>
List<T, the_alloc>::List(const List& other):
List(allocator_traits::select_on_container_copy_construction(other.get_allocator())) {
for (const auto& value : other) {
try {
push_back(value);
} catch (...) {
GetCleared();
throw;
}
}
}
template <typename T, typename the_alloc>
List<T, the_alloc>::List(size_t size, const T& val, const the_alloc& allocator): alloc(allocator) {
for (size_t tmp = 0; tmp < size; ++tmp) {
try {
push_back(val);
} catch (...) {
GetCleared();
throw;
}
}
}
template <typename T, typename the_alloc>
List<T, the_alloc>::~List() {
while(this->the_size != 0) {
this->pop_back();
}
}
template <typename T, typename the_alloc>
void List<T, the_alloc>::push_back(const T& val) {
try {
this->AddVertexVal(static_cast<Node*>(&fakeNode), val);
} catch (...) {
throw;
}
}
template <typename T, typename the_alloc>
void List<T, the_alloc>::push_front(const T& val) {
try {
this->AddVertexVal(fakeNode.next, val);
} catch (...) {
throw;
}
}
template <typename T, typename the_alloc>
List<T, the_alloc>& List<T, the_alloc>::operator=(const List<T, the_alloc>& other) {
if (this == &other) {
return *this;
}
size_t temp_sz = the_size;
if (allocator_traits::propagate_on_container_copy_assignment::value) {
this->alloc = other.alloc;
}
size_t pushed = 0;
for (const auto& elem : other) {
try {
this->push_back(elem);
++pushed;
} catch (...) {
for (size_t tmp = 0; tmp < pushed; ++tmp) {
pop_back();
}
throw;
}
}
for (size_t tmp = 0; tmp < temp_sz; ++tmp) {
pop_front();
}
return *this;
}
template <typename T, typename the_alloc>
void List<T, the_alloc>::pop_back() {
this->DeleteVertex(fakeNode.prev);
}
template <typename T, typename the_alloc>
void List<T, the_alloc>::pop_front() {
this->erase(cbegin());
}
template <typename T, typename the_alloc>
template <bool IterType>
class List<T, the_alloc>::base_iterator {
public:
using difference_type = int;
using value_type = std::conditional_t<IterType, const T, T>;
using pointer = value_type*;
using reference = value_type&;
using the_iter = base_iterator<IterType>;
using iterator_category = std::bidirectional_iterator_tag;
base_iterator(Node* ptr, difference_type pos): ptr(ptr), position(pos) {}
Node* operator->() {
return this->ptr;
}
reference operator*() {
return this->ptr->value;
}
difference_type operator-(const base_iterator& other) const {
return (this->position - other.position);
}
operator const_iterator() const {
return const_iterator(this->ptr, this->position);
}
the_iter& operator+=(int delta) {
if (!delta) {
return *this;
}
if (delta > 0) {
while (delta) {
this->ptr = this->ptr->next;
++this->position;
--delta;
}
return *this;
}
while (delta) {
this->ptr = this->ptr->prev;
--this->position;
++delta;
}
return *this;
}
the_iter& operator-=(int delta) {
return *this += (-1) * delta;
}
the_iter& operator++() {
return *this += 1;
}
the_iter operator++(int) {
the_iter ans = *this;
*this += 1;
return ans;
}
the_iter& operator--() {
return *this -= 1;
}
the_iter operator--(int) {
the_iter ans = *this;
*this -= 1;
return ans;
}
bool operator==(const base_iterator& other) const {
return this->ptr == other.ptr;
}
bool operator!=(const base_iterator& other) const {
return !(*this == other);
}
bool operator<(const base_iterator& other) {
return (*this - other) < 0;
}
bool operator>(const base_iterator& other) {
return (*this - other) > 0;
}
bool operator>=(const base_iterator& other) {
return !(*this < other);
}
bool operator<=(const base_iterator& other) {
return !(*this > other);
}
Node* ptr;
difference_type position;
};
template <typename T, typename the_alloc>
void List<T, the_alloc>::erase(const_iterator it) {
this->DeleteVertex(it.ptr);
}
template <typename T, typename the_alloc>
void List<T, the_alloc>::insert(const_iterator iter, const T& val) {
this->AddVertexVal(iter.operator->(), val);
}