-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvector.c
213 lines (174 loc) · 4.57 KB
/
vector.c
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
#include "vector.h"
#include <assert.h>
#include <memory.h>
#include <stdint.h>
#include <stdlib.h>
#define VEC_GROW_RATE 2
#define VEC_DEF_SIZE 1
static void* offset(const Vector* vec, size_t i) {
assert(i <= vec->capacity);
return ((unsigned char*)vec->data) + vec->elemSize * i;
}
static void reallocVector(Vector* vec, size_t capacity) {
vec->capacity = capacity;
vec->data = realloc(vec->data, vec->elemSize * capacity);
assert(vec->data);
}
static void growVector(Vector* vec) {
size_t newCapacity = vec->capacity ? vec->capacity * VEC_GROW_RATE : VEC_DEF_SIZE;
reallocVector(vec, newCapacity);
}
static bool shouldGrow(Vector* vec, size_t required) {
return vec->size + required > vec->capacity;
}
static void reset(Vector* vec) {
vec->size = 0;
vec->capacity = 0;
vec->elemSize = 0;
vec->data = NULL;
}
void vecInit(Vector* vec, size_t elemSize) {
reset(vec);
vec->elemSize = elemSize;
}
Vector vecNew(size_t elemSize) {
Vector vec;
vecInit(&vec, elemSize);
return vec;
}
Vector vecCopy(const Vector* vec) {
Vector copy;
vecInit(©, vec->elemSize);
vecReserve(©, vec->size);
memcpy(copy.data, vec->data, vec->size * vec->elemSize);
copy.size = vec->size;
return copy;
}
void vecCopyAssign(Vector* dest, const Vector* src) {
assert(vecIsInitialized(dest));
free(dest->data);
vecInit(dest, src->elemSize);
vecReserve(dest, src->size);
memcpy(dest->data, src->data, src->size * src->elemSize);
dest->size = src->size;
}
Vector vecMove(Vector* vec) {
Vector moved = *vec;
reset(vec);
return moved;
}
void vecMoveAssign(Vector* dest, Vector* src) {
assert(vecIsInitialized(dest));
vecFree(dest);
*dest = *src;
reset(src);
}
void vecFree(Vector* vec) {
free(vec->data);
reset(vec);
}
void* vecGet(Vector* vec, size_t i) {
assert(i < vec->size);
return offset(vec, i);
}
const void* vecConstGet(const Vector* vec, size_t i) {
assert(i < vec->size);
return offset(vec, i);
}
void* vecData(Vector* vec) {
return vec->data;
}
const void* vecConstData(const Vector* vec) {
return vec->data;
}
void* vecFront(Vector* vec) {
return vec->data;
}
const void* vecConstFront(const Vector* vec) {
return vec->data;
}
void* vecBack(Vector* vec) {
return offset(vec, vec->size - 1);
}
const void* vecConstBack(const Vector* vec) {
return offset(vec, vec->size - 1);
}
void vecClear(Vector* vec) {
vec->size = 0;
}
size_t vecPush(Vector* vec, void* elem) {
if(shouldGrow(vec, 1)) growVector(vec);
memcpy(offset(vec, vec->size), elem, vec->elemSize);
return vec->size++;
}
void vecSet(Vector* vec, size_t i, void* elem) {
assert(i < vec->size);
memcpy(offset(vec, i), elem, vec->elemSize);
}
void* vecInsert(Vector* vec, size_t i, void* elem) {
assert(i <= vec->size);
if(shouldGrow(vec, 1)) growVector(vec);
size_t shiftRight = (vec->size - i) * vec->elemSize;
void* insertIt = offset(vec, i);
memmove(offset(vec, i + 1), insertIt, shiftRight);
memcpy(insertIt, elem, vec->elemSize);
vec->size++;
return insertIt;
}
void* vecErase(Vector* vec, size_t i) {
assert(i < vec->size);
size_t shiftLeft = (vec->size - i - 1) * vec->elemSize;
void* removeIt = offset(vec, i);
memmove(removeIt, offset(vec, i + 1), shiftLeft);
vec->size--;
return removeIt;
}
void vecPop(Vector* vec) {
assert(vec->size);
vec->size--;
}
void vecSwap(Vector* v1, Vector* v2) {
Vector tmp = *v1;
*v1 = *v2;
*v2 = tmp;
}
bool vecEmpty(const Vector* vec) {
return vec->size == 0;
}
size_t vecSize(const Vector* vec) {
return vec->size;
}
size_t vecCapacity(const Vector* vec) {
return vec->capacity;
}
bool vecIsInitialized(const Vector* vec) {
return vec->elemSize != 0;
}
void vecReserve(Vector* vec, size_t required) {
if(!shouldGrow(vec, required)) return;
size_t newCapacity = vec->capacity ? vec->capacity * VEC_GROW_RATE : VEC_DEF_SIZE;
while(newCapacity < vec->size + required) {
newCapacity <<= 1;
}
reallocVector(vec, newCapacity);
}
void vecShrinkToFit(Vector* vec) {
if(vec->capacity > vec->size) {
reallocVector(vec, vec->size);
}
}
void* vecBegin(Vector* vec) {
return vec->data;
}
void* vecEnd(Vector* vec) {
return offset(vec, vec->size);
}
void* vecIterator(Vector* vec, size_t i) {
assert(i <= vec->size);
return offset(vec, i);
}
size_t vecIteratorIndex(const Vector* vec, void* it) {
ptrdiff_t i = (intptr_t)it - (intptr_t)vec->data;
assert(i >= 0);
return i / vec->elemSize;
}