This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.h
75 lines (61 loc) · 1.68 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
#include <stdlib.h>
#include <string.h>
#ifndef VECTOR_NAME
#error "Must declare VECTOR_NAME"
#endif
#ifndef VECTOR_TYPE
#error "Must declare VECTOR_TYPE"
#endif
#define VECTOR_CONCAT(x, y) x ## _ ## y
#define VECTOR_1(x, y) VECTOR_CONCAT(x, y)
#define VECTOR_(x) VECTOR_1(vec, VECTOR_1(VECTOR_NAME,x))
#define VECTOR VECTOR_(t)
typedef struct {
VECTOR_TYPE *arr;
size_t size;
size_t alloc;
} VECTOR_(struct);
typedef VECTOR_(struct) VECTOR[1];
static inline void VECTOR_(init)(VECTOR vec) {
vec[0].alloc = 8;
vec[0].arr = calloc(vec->alloc, sizeof(VECTOR_TYPE));
vec[0].size = 0;
}
static inline void VECTOR_(append)(VECTOR vec, VECTOR_TYPE elt) {
if (vec[0].size == vec[0].alloc) {
vec[0].alloc += (vec[0].alloc >> 1);
vec[0].arr = realloc(vec[0].arr, vec[0].alloc * sizeof(VECTOR_TYPE));
}
vec[0].arr[vec[0].size++] = elt;
}
static inline VECTOR_TYPE VECTOR_(get)(VECTOR vec, int i) {
return vec[0].arr[i];
}
static inline VECTOR_TYPE VECTOR_(pop)(VECTOR vec) {
return vec[0].arr[vec[0].size--];
}
static inline void VECTOR_(swap)(VECTOR vec, int i, int j) {
VECTOR_TYPE v = vec[0].arr[i];
vec[0].arr[i] = vec[0].arr[j];
vec[0].arr[j] = v;
}
static inline void VECTOR_(del)(VECTOR vec, int i) {
if (i == vec[0].size) {
VECTOR_(pop)(vec);
} else {
memmove(&(vec[0].arr[i]), &(vec[0].arr[i + 1]), sizeof(VECTOR_TYPE) * vec[0].size - i - 1);
vec[0].size--;
}
}
static inline size_t VECTOR_(size)(VECTOR vec) {
return vec[0].size;
}
static inline void VECTOR_(shuf)(VECTOR vec) {
for (int i = 0; i < VECTOR_(size)(vec); i++)
VECTOR_(swap)(vec, i, rand() % (i+1));
}
#undef VECTOR_1
#undef VECTOR_
#undef VECTOR_NAME
#undef VECTOR_TYPE
#undef VECTOR