-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfector.hpp
366 lines (301 loc) · 6.87 KB
/
fector.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
357
358
359
360
361
362
363
364
365
366
// LGPL 3 or higher Robert Burner Schadek [email protected]
#pragma once
#include <cstddef>
#include <stdexcept>
#include <iostream>
#include <new>
#include <type_traits>
#include <format.hpp>
namespace sweet {
template<typename T, size_t Capacity = 128>
class Fector {
public:
typedef T value_type;
typedef size_t size_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
typedef T* iterator;
typedef const T* const_iterator;
private:
union Data {
char data[sizeof(T)*Capacity];
T dataT[Capacity];
inline Data() {}
inline Data(const Data&) {}
inline ~Data() {}
};
Data data;
uint16_t idx;
/*T* dataT() {
return reinterpret_cast<T*>(this->data);
}
const T* dataT() const {
return reinterpret_cast<const T*>(this->data);
}*/
public:
// constructor
inline Fector() : idx(0) {
}
inline Fector(std::initializer_list<T> l) : Fector() {
for(auto& it : l) {
this->push_back(it);
}
}
inline ~Fector() {
if(std::is_class<T>::value) {
while(this->size()) {
this->pop_back();
}
}
}
inline Fector(const Fector& other) : idx(0) {
size_t otherSize = other.size();
for(size_t i = 0; i < otherSize; ++i) {
this->push_back(other[i]);
}
}
/*
inline Fector(Fector&& other) : idx(0) {
size_t otherSize = other.size();
for(size_t i = 0; i < otherSize; ++i) {
this->push_back(other[i]);
}
}*/
inline Fector(const size_t cnt, const T& value) : Fector() {
this->resize(cnt, value);
}
inline void resize(const size_t cnt, const T& value) {
while(cnt < this->idx) {
this->pop_back();
}
while(cnt > this->idx) {
this->push_back(value);
}
}
//access
inline T& operator[](const size_t i) {
return this->data.dataT[i];
}
inline const T& operator[](const size_t i) const {
return this->data.dataT[i];
}
inline T& at(const size_t i) {
if(i < this->idx) {
return this->data.dataT[i];
} else {
throw std::out_of_range(format("at index(%u) out of range size(%u)",
i, this->size())
);
}
}
inline const T& at(const size_t i) const {
if(i < this->idx) {
return this->data.dataT[i];
} else {
throw std::out_of_range(format("at index(%u) out of range size(%u)",
i, this->size())
);
}
}
inline T& front() {
return this->data.dataT[0u];
}
inline const T& front() const {
return this->data.dataT[0u];
}
inline T& back() {
return this->data.dataT[idx-1];
}
inline const T& back() const {
return this->data.dataT[idx-1];
}
// capacity
inline bool empty() const noexcept {
return this->idx == 0;
}
inline size_t size() const noexcept {
return this->idx;
}
inline constexpr size_t max_size() const noexcept {
return Capacity;
}
inline constexpr size_t capacity() const noexcept {
return Capacity;
}
// modifier
inline void clear() {
this->idx = 0;
}
inline void push_back(const T& value) {
if(idx < Capacity) {
new (&this->data.dataT[idx]) T();
this->data.dataT[idx] = value;
++idx;
} else {
throw std::out_of_range("fector full can not push_back");
}
}
inline void push_back(T&& value) {
if(idx < Capacity) {
new (&this->data.dataT[idx]) T();
this->data.dataT[idx] = std::move(value);
++idx;
} else {
throw std::out_of_range("fector full can not push_back(&&)");
}
}
inline void push_backUnsafe(const T& value) {
new (&this->data.dataT[idx]) T();
this->data.dataT[idx] = value;
++idx;
}
inline void push_backUnsafe(T&& value) {
new (&this->data.dataT[idx]) T();
this->data.dataT[idx] = std::move(value);
++idx;
}
template<typename... Args>
inline void emplace(Args... args) {
if(idx < Capacity) {
new (&this->data.dataT[idx]) T(args...);
++idx;
} else {
throw std::out_of_range("fector full can not push_back(&&)");
}
}
inline void pop_back() {
this->data.dataT[this->idx - 1].~T();
--this->idx;
}
inline void pop_back(const size_t size) {
for(size_t i = 1; i <= size; ++i) {
this->data.dataT[this->idx - i].~T();
}
this->idx -= size;
}
inline void pop_front() {
this->erase(this->begin());
}
inline void pop_front(const size_t size) {
this->erase(this->begin(), size);
}
inline iterator insert(iterator it, const T& value) {
if(this->empty()) {
this->push_back(value);
return this->begin();
} else if(idx <= Capacity) {
auto en(this->end());
do {
auto enm = en;
--enm;
*en = *enm;
--en;
} while(en!= it);
*it = value;
++this->idx;
return it;
} else {
throw std::out_of_range("fector full can not insert");
}
}
inline iterator erase(iterator pos, const size_t size = 1u) {
if(size == 0u) {
return pos;
}
const_iterator e = this->end();
auto ret = pos;
auto next = pos + size;
while(next < e) {
*pos = *next;
++pos;
++next;
}
this->pop_back(size);
return ret;
}
// iterator
iterator begin() {
return &this->data.dataT[0];
}
iterator end() {
return &this->data.dataT[this->idx];
}
const_iterator begin() const {
return &this->data.dataT[0];
}
const_iterator end() const {
return &this->data.dataT[this->idx];
}
const_iterator cbegin() const {
return &this->data.dataT[0];
}
const_iterator cend() const {
return &this->data.dataT[this->idx];
}
iterator rbegin() {
return &this->data.dataT[this->idx-1u];
}
iterator rend() {
return &this->data.dataT[0];
}
const_iterator rbegin() const {
return &this->data.dataT[this->idx-1u];
}
const_iterator rend() const {
return &this->data.dataT[0];
}
bool operator==(const Fector<T,Capacity>& other) const {
if(this->size() != other.size()) {
return false;
} else {
for(size_t i = 0; i < this->size(); ++i) {
if(this->operator[](i) != other[i]) {
return false;
}
}
return true;
}
}
};
template<typename T>
bool compare(const T& a, const T& b) {
if(a.size() == b.size()) {
for(const auto& it : a) {
bool found = false;
for(const auto& jt : b) {
if(it == jt) {
found = true;
break;
}
}
if(!found) {
return false;
}
}
return true;
}
return false;
}
}
namespace sweet {
template<typename>
struct is_sweet_Fector : std::false_type {};
template<typename T, std::size_t N>
struct is_sweet_Fector<sweet::Fector<T,N>> : std::true_type {};
template<typename T, size_t S>
std::ostream& operator<<(std::ostream& out, const sweet::Fector<T,S>& v) {
for(typename sweet::Fector<T,S>::const_pointer it = v.begin();
it != v.end(); ++it)
{
typename sweet::Fector<T,S>::const_reference tmp = *it;
if(is_sweet_Fector<typename sweet::Fector<T,S>::value_type>::value) {
format(out, "%s", tmp);
} else {
format(out, "%s ", tmp);
}
}
return out;
}
}