-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.h
315 lines (258 loc) · 6.46 KB
/
vector.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
315
#ifndef VECTOR_H
#define VECTOR_H
#pragma once
#include <new>
#include <initializer_list>
#include <string.h>
#include "assert.h"
#include "macros.h"
template<typename T>
struct Vector{
T* data;
int count;
int capacity;
Vector(){
data = nullptr;
count = 0;
capacity = 0;
}
void Assign(const Vector& other){
if (other.capacity > 0) {
data = (T*)malloc(other.capacity*sizeof(T));
count = other.count;
capacity = other.capacity;
for (int i = 0; i < other.count; i++) {
new(&data[i]) T(other.data[i]);
}
}
else {
data = nullptr;
count = 0;
capacity = 0;
}
}
Vector(const Vector& other){
Assign(other);
}
Vector& operator=(const Vector& other){
if(other.data != data){
Destroy();
Assign(other);
}
return *this;
}
void Resize(int size) {
ASSERT(size >= 0);
EnsureCapacity(size);
if (count < size) {
while (count < size) {
new(&data[count]) T();
count++;
}
}
else if (count > size) {
while (count > size) {
count--;
data[count].~T();
}
}
}
void EnsureCapacity(int newCapacity){
if(newCapacity > capacity){
T* newData = (T*)malloc(newCapacity*sizeof(T));
if (data != nullptr) {
BNS_MEMCPY(newData, data, count*sizeof(T));
free(data);
}
data = newData;
capacity = newCapacity;
}
}
void Clear(){
for(int i = 0; i < count; i++){
data[i].~T();
}
count = 0;
}
void Swap(Vector& other) {
int _capacity = capacity;
int _count = count;
T* _data = data;
capacity = other.capacity;
count = other.count;
data = other.data;
other.capacity = _capacity;
other.count = _count;
other.data = _data;
}
void PopBack(){
ASSERT(count > 0);
count--;
data[count].~T();
}
T& Back() {
ASSERT(count > 0);
return data[count - 1];
}
const T& Back() const {
ASSERT(count > 0);
return data[count - 1];
}
void Remove(int index){
ASSERT(index >= 0 && index < count);
data[index].~T();
memmove(&data[index], &data[index+1], sizeof(T)*(count - index - 1));
count--;
}
// Lower-Inclusive, Upper-Exclusive: RemoveRange(5, 6) removes 1 element: 5
// RemoveRange(4, 4) does nothing
void RemoveRange(int lIdx, int hIdx) {
ASSERT(lIdx >= 0 && lIdx <= count);
ASSERT(hIdx >= 0 && hIdx <= count);
if (lIdx < hIdx) {
for (int i = lIdx; i < hIdx; i++) {
data[i].~T();
}
memmove(&data[lIdx], &data[hIdx], sizeof(T)*(count - hIdx));
count -= (hIdx - lIdx);
}
}
void PushBack(const T& elem){
if(count >= capacity){
EnsureCapacity(capacity == 0 ? 2 : capacity * 2);
}
new(&data[count]) T(elem);
count++;
}
T& EmplaceBack(){
if(count >= capacity){
EnsureCapacity(capacity == 0 ? 2 : capacity * 2);
}
new(&data[count]) T();
count++;
return data[count - 1];
}
void Insert(int index, const T& elem){
ASSERT(index >= 0 && index <= count);
if(count >= capacity){
EnsureCapacity(capacity == 0 ? 2 : capacity * 2);
}
char* insertLoc = (char*)&data[index];
for(int i = (sizeof(T) * (count - index)) - 1; i >= 0; i--){
insertLoc[i + sizeof(T)] = insertLoc[i];
}
new(&data[index]) T(elem);
count++;
}
void InsertVector(int index, const Vector<T>& vec) {
InsertArray(index, vec.data, vec.count);
}
void InsertArray(int index, const T* elem, int arrCount) {
if (count + arrCount >= capacity) {
EnsureCapacity(count + arrCount);
}
memmove(&data[index + arrCount], &data[index], (count - index)*sizeof(T));
for (int i = 0; i < arrCount; i++) {
new(&data[index+i]) T(elem[i]);
}
count += arrCount;
}
void Destroy(){
Clear();
if(data != nullptr){
free(data);
data = nullptr;
}
else{
ASSERT(capacity == 0);
}
capacity = 0;
}
explicit Vector(int initialCapacity){
count = 0;
capacity = 0;
data = nullptr;
EnsureCapacity(initialCapacity);
}
Vector(std::initializer_list<T> l){
count = 0;
capacity = 0;
data = nullptr;
EnsureCapacity(l.size());
for (int i = 0; i < l.size(); i++) {
new(&data[i]) T(l.begin()[i]);
}
count = l.size();
}
void AppendList(std::initializer_list<T> l){
InsertArray(count, l.begin(), l.size());
}
T& Get(int idx){
ASSERT(idx >= 0 && idx < count);
return data[idx];
}
Vector<T> GetSubVector(int idx, int length) const {
ASSERT(idx >= 0 && idx + length <= count);
Vector<T> subVec;
subVec.EnsureCapacity(length);
subVec.count = length;
for (int i = 0; i < length; i++) {
new(&subVec.data[i]) T(data[i + idx]);
}
return subVec;
}
const T& Get(int idx) const{
ASSERT(idx >= 0 && idx < count);
return data[idx];
}
~Vector(){
Destroy();
}
};
#define BNS_VEC_MAP(from, to, action) (to).Clear(); (to).EnsureCapacity((from).count); \
for (int index = 0; index < (from).count; index++){ \
auto& item = (from).Get(index);\
(to).EmplaceBack() = action; \
}
#define BNS_VEC_FOLDR(result, from, base, action) { \
auto& acc = result; acc = base; \
for (int index = 0; index < (from).count; index++){ \
auto& item = (from).Get(index); \
acc = action; \
} \
}
#define BNS_VEC_FILTER(from, good, pred) (good).Clear(); (good).EnsureCapacity((from).count); \
for (int index = 0; index < (from).count; index++){ \
auto& item = (from).data[index]; \
if (pred) { \
(good).PushBack(item); \
}\
}
#define BNS_VEC_FOREACH_NAME(vec, varName) for (auto varName = &(vec).data[0]; varName != &(vec).data[(vec).count]; varName++)
#define BNS_VEC_FOREACH(vec) BNS_VEC_FOREACH_NAME(vec, ptr)
#define BNS_FOR_NAME(var, count) for (int var = 0; var < (count); var++)
#define BNS_FOR_I(count) BNS_FOR_NAME(i, count)
#define BNS_FOR_J(count) BNS_FOR_NAME(j, count)
#define BNS_ARRAY_FOR_NAME(var, arr) BNS_FOR_NAME(var, BNS_ARRAY_COUNT(arr))
#define BNS_ARRAY_FOR_I(arr) BNS_ARRAY_FOR_NAME(i, arr)
#define BNS_ARRAY_FOR_J(arr) BNS_ARRAY_FOR_NAME(j, arr)
#define BNS_VEC_FOR_NAME(var, vec) BNS_FOR_NAME(var, (vec).count)
#define BNS_VEC_FOR_I(vec) BNS_VEC_FOR_NAME(i, vec)
#define BNS_VEC_FOR_J(vec) BNS_VEC_FOR_NAME(j, vec)
#define BNS_VEC_DUMB_SORT(vec, pred) do { \
BNS_FOR_I((vec).count) { \
for (int j = i; j > 0; j--) { \
auto l = (vec).data[j - 1]; \
auto r = (vec).data[j ]; \
if (!(pred)) { \
auto tmp = (vec).data[j]; \
(vec).data[j] = (vec).data[j - 1]; \
(vec).data[j - 1] = tmp; \
} \
else { \
break; \
} \
} \
} \
} while(0)
#endif