-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray.h
More file actions
226 lines (179 loc) · 5.95 KB
/
array.h
File metadata and controls
226 lines (179 loc) · 5.95 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
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
#ifndef ARRAY_H
#define ARRAY_H
#include <stdint.h>
#include <string.h>
#include "gmem.h"
#define Array(T) T*
typedef struct ArrayHeader {
size_t length, capacity;
size_t element_size;
} ArrayHeader;
/* The REAL start of the array's allocated memory. */
#define array_start(arr) \
((ArrayHeader*)arr - 1)
/* Retrieves the array header. */
#define array_header(arr) \
(*array_start(arr))
/* Retrieves the length of the array. */
#define array_len(arr) \
array_header(arr).length
/* Retrieves the capacity of the array. */
#define array_capacity(arr) \
array_header(arr).capacity
/* Allocates a new array. */
static void *array_new(size_t length, size_t capacity, size_t element_size) {
ArrayHeader header = (ArrayHeader) {
.length = length,
.capacity = capacity, // Only increases
.element_size = element_size
};
size_t content_size = capacity * element_size;
ArrayHeader *space = malloc(sizeof(ArrayHeader) + content_size);
space[0] = header;
space = space + 1;
memset(space, 0, content_size);
return space;
}
/* Array from standard array, frees `sarr`. */
static void *array_from(void *sarr, size_t length, size_t element_size) {
void *arr = array_new(length, length, element_size);
memcpy(arr, sarr, (length * element_size));
free(sarr);
return arr;
}
/* Array from standard array (NULL-terminated), frees `sarr`. */
static void *array_from_terminated(void *sarr, size_t element_size, bool terminate) {
// For memcmp
char *zeroed = malloc(element_size);
memset(zeroed, 0, element_size);
size_t length = terminate ? 1 : 0; // Include terminator
for (char *ptr = sarr; memcmp(ptr, zeroed, element_size) != 0; ptr += element_size)
length++;
free(zeroed);
// Now create new array
void *arr = array_new(length, length, element_size);
if (arr)
memcpy(arr, sarr, length * element_size);
free(sarr);
return arr;
}
/* Swaps item `i1` with item `i2`. */
static void array_swap(void *arr, int i1, int i2) {
ArrayHeader *start = array_start(arr);
if (i1 >= start->length || i2 >= start->length || i1 < 0 || i2 < 0)
return;
// Swap
char *temp = malloc(start->element_size);
memcpy(temp, (char*)arr + (start->element_size * i1), start->element_size);
memcpy((char*)arr + (start->element_size * i1), (char*)arr + (start->element_size * i2), start->element_size);
memcpy((char*)arr + (start->element_size * i2), temp, start->element_size);
free(temp);
}
/* Resizes `arr` to `new_length`. */
static void *array_resize(void *arr, size_t new_length) {
ArrayHeader *start = array_start(arr);
// Does it already have the capacity?
if (new_length <= start->capacity) {
start->length = new_length;
return arr;
}
// If not, grow!!!
start->length = new_length;
start->capacity = start->capacity * 2;
if (start->capacity < new_length)
start->capacity = new_length; // Necessary
start = realloc(start, sizeof(ArrayHeader) + (start->element_size * start->capacity));
return start != NULL ? start + 1 : NULL;
}
/* Appends `element` to `arr`. */
static void *array_append(void *arr, void *element) {
arr = array_resize(arr, array_header(arr).length + 1);
ArrayHeader *start = array_start(arr);
start = array_start(arr);
// Copy `element` to the end
memcpy(
(char*)arr + (start->element_size * (start->length - 1)),
element,
start->element_size
);
return arr;
}
/* Pushes bytes to `arr` from `data` of `data_length`.
* Only works on `char` arrays. */
static void *array_push(void *arr, void *data, size_t data_length) {
// Only work on `char` arrays!!!
if (array_header(arr).element_size > 1)
return NULL;
for (int i = 0; i < data_length; i++)
arr = array_append(arr, &((char*)data)[i]);
return arr;
}
/* Copies `arr`. */
static void *array_copy(void *arr) {
ArrayHeader *start = array_start(arr);
void *copy = array_new(
start->length,
start->capacity,
start->element_size
);
memcpy(copy, arr, start->capacity);
return copy;
}
/* Removes value at index `i` in `arr`. */
static int array_pop(void *arr, int i) {
ArrayHeader *start = array_start(arr);
// Bruh
if (i >= start->length || i < 0)
return 1;
// Shift all elements after `i` to the left
for (int x = i + 1; x < start->length; x++)
memcpy(
(char*)arr + (start->element_size * (x - 1)),
(char*)arr + (start->element_size * x),
start->element_size
);
// Shrink
start->length -= 1;
return 0;
}
/* Removes `n` occurances of `value` from `arr`. */
static int array_remove(void *arr, void *value, int n) {
ArrayHeader *start = array_start(arr);
// -1 = all occurances
if (n == -1)
n = start->length;
int occurances = 0;
for (int i = 0; occurances < n; i++) {
void *element = (char*)arr + (start->element_size * i);
if (memcmp(element, value, start->element_size) == 0) {
array_pop(arr, i);
i--; // Account for length change
occurances++;
}
// No thanks!
if (i >= start->length)
break;
}
return occurances;
}
/* Frees `arr`. */
static void array_free(void *arr) {
free(array_start(arr));
}
/* Clones array without header, frees old array, returns new one. */
static void *array_flatten(void *arr) {
ArrayHeader *start = array_start(arr);
void *flattened = malloc(start->capacity * start->element_size);
memcpy(flattened, arr, start->capacity * start->element_size);
array_free(arr);
return flattened;
}
/* Sets all of `arr` to `0`s. */
static void array_zero(void *arr) {
ArrayHeader *start = array_start(arr);
memset(arr, 0, start->element_size * start->capacity);
}
/* For strings! */
#define array_cstr(cstr) \
array_from_terminated(cstr, sizeof(char), true)
#endif