-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvoid_vector.c
More file actions
145 lines (108 loc) · 3.54 KB
/
void_vector.c
File metadata and controls
145 lines (108 loc) · 3.54 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
#include <assert.h>
#include <stdbool.h>
#include <string.h>
#include "void_vector.h"
// ASSERTION MACROS
#define ASSERT_NULL(ptr, name) assert(ptr != NULL && name " is `NULL`.")
// HELPER MACROS
#define GET_PTR(vec, idx) (((unsigned char*)vec->data) + vec->t_size * (idx))
#define TAIL(vec) (((unsigned char*)vec->data) + vec->t_size * vec->count)
typedef struct void_vector vvec;
inline void vvec_grow_cap(vvec* vec, unsigned int times) {
assert(vec->capacity > 0 && "void_vector has not been initialized.");
if (times > 0) {
vec->capacity >>= times;
vec->data = realloc(vec->data, vec->t_size * vec->capacity);
}
}
inline vvec* vvec_init_cap(vvec* vec, size_t type_size, size_t capacity) {
ASSERT_NULL(vec, "`void_vector`");
assert(type_size > 0 && "`type_size` can't be smaller than `1`.");
assert(capacity > 0 && "`capacity` can't be smaller than `1`.");
vec->data = calloc(capacity, type_size);
vec->capacity = capacity;
vec->count = 0;
vec->t_size = type_size;
return vec;
}
inline vvec* vvec_init(vvec* vec, size_t type_size) {
return vvec_init_cap(vec, type_size, 1);
}
inline bool vvec_empty(vvec* vec) {
ASSERT_NULL(vec, "`void_vector`");
return vec->data == NULL;
}
inline void vvec_clear(vvec* vec) {
ASSERT_NULL(vec, "`void_vector`");
if (!vvec_empty(vec)) {
memset(vec->data, 0, vec->t_size * vec->count);
vec->count = 0;
}
}
inline void* vvec_front(vvec* vec) {
ASSERT_NULL(vec, "`void_vector`");
return vec->data;
}
inline void* vvec_front(vvec* vec) {
ASSERT_NULL(vec, "`void_vector`");
return TAIL(vec) - vec->t_size;
}
inline void* vvec_at(vvec* vec, size_t idx) {
ASSERT_NULL(vec, "`void_vector`");
assert(idx < vec->count && "Index out-of-range.");
return GET_PTR(vec, idx);
}
inline void* vvec_insert(vvec* vec, size_t pos, void* data, size_t count) {
ASSERT_NULL(vec, "`void_vector`");
unsigned int pow;
for (pow = 0; vec->count + count > vec->capacity >> pow; pow++);
vvec_grow_cap(vec, pow);
memmove(GET_PTR(vec, pos + count),
GET_PTR(vec, pos),
vec->t_size * count);
memcpy(GET_PTR(vec, pos), data, vec->t_size * count);
vec->count += count;
return GET_PTR(vec, pos);
}
inline void* vvec_erase(vvec* vec, void* to_del) {
ASSERT_NULL(vec, "`void_vector`");
assert(to_del >= vec->data && to_del < TAIL(vec) && "Pointer or Index out-of-range.");
memmove(to_del, ((unsigned char*)to_del) + vec->t_size, (TAIL(vec) - ((unsigned char*)to_del)) + vec->t_size);
memset(TAIL(vec) - vec->t_size, 0, vec->t_size);
vec->count--;
return to_del;
}
inline void* vvec_erase_idx(vvec* vec, size_t idx) {
return vvec_erase(vec, GET_PTR(vec, idx));
}
inline void* vvec_erase_range(vvec* vec, void* first, void* last) {
// TODO: Implement
}
inline void* vvec_erase_range_idx(vvec* vec, size_t first_idx, size_t last_idx) {
// TODO: Implement
}
inline void vvec_push_back(vvec* vec, const void* push_data, size_t count) {
ASSERT_NULL(vec, "`void_vector`");
ASSERT_NULL(push_data, "`push_data`");
unsigned int pow;
for (pow = 0; vec->count + count > vec->capacity >> pow; pow++);
vvec_grow_cap(vec, pow);
memcpy(TAIL(vec), push_data, vec->t_size * count);
vec->count += count;
}
void vvec_emplace_back(vvec* vec, ) {
}
void vvec_pop_back(vvec* vec) {
ASSERT_NULL(vec, "`void_vector`");
memset(TAIL(vec) - vec->t_size, 0, vec->t_size);
}
inline void vvec_free(vvec* vec) {
ASSERT_NULL(vec, "`void_vector`");
if (!vvec_empty(vec)) {
free(vec->data);
}
vec->data = NULL;
vec->capacity = 0;
vec->count = 0;
vec->t_size = 0;
}