Last active
January 3, 2020 12:14
-
-
Save MisterDA/c7c9e59310c4fc0fa45b754c07d1a8fc to your computer and use it in GitHub Desktop.
A type-safe, growable array for C, generated with macros.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
MIT License | |
Copyright (c) 2019 Antonin Décimo | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
#include <stdio.h> | |
#include "array.h" | |
struct stuff { | |
int a; | |
int b; | |
unsigned char *foo; | |
}; | |
static int | |
compare_stuff(const struct stuff *s1, const struct stuff *s2) | |
{ | |
if(s1->a < s2->a) { | |
return -1; | |
} else if(s1->a == s2->a) { | |
if(s1->b < s2->b) | |
return -1; | |
else if(s1->b == s2->b) | |
return 0; | |
else | |
return 1; | |
} else { | |
return 1; | |
} | |
} | |
array_struct(array_, struct stuff, stuff); | |
array(static, array_, struct stuff, stuff, 8); | |
array_struct(array_sorted_, struct stuff, stuff); | |
array_sorted(static, array_sorted_, struct stuff, stuff, 8); | |
struct bigger_stuff { | |
struct array_stuff stuffs; | |
unsigned char *bar; | |
}; | |
int | |
main(void) | |
{ | |
unsigned cap = 8; | |
/* Array of structures, allocated on the heap. */ | |
{ | |
struct array_stuff *stuffs = array_init_stuff(NULL); | |
assert(stuffs); | |
for(unsigned i = 0; i < cap; i++) { | |
struct stuff t = {.a = 42, .b = i, .foo = malloc(42)}; | |
array_push_stuff(stuffs, t); | |
} | |
struct stuff *s = stuffs->data + 3; | |
free(s->foo); | |
array_pop_stuff(stuffs, 3); | |
for(unsigned i = 0; i < stuffs->len; i++) { | |
s = stuffs->data + i; | |
free(s->foo); | |
} | |
free(stuffs->data); | |
free(stuffs); | |
} | |
/* Array of structures inside a bigger struct (= allocated on the | |
* stack) */ | |
{ | |
struct bigger_stuff bs = {0}; | |
struct array_stuff *stuffs = array_init_stuff(&bs.stuffs); | |
assert(stuffs); | |
assert(stuffs == &bs.stuffs); | |
for(unsigned i = 0; i < cap; i++) { | |
struct stuff t = {.a = 42, .b = i, .foo = malloc(42)}; | |
array_push_stuff(stuffs, t); | |
} | |
struct stuff *s = stuffs->data + 3; | |
free(s->foo); | |
array_pop_stuff(stuffs, 3); | |
for(unsigned i = 0; i < stuffs->len; i++) { | |
s = stuffs->data + i; | |
free(s->foo); | |
} | |
free(stuffs->data); | |
} | |
{ | |
struct array_sorted_stuff stuffs; | |
array_sorted_init_stuff(&stuffs); | |
for(int i = 0; i < 8; i++) { | |
struct stuff stuff = {.a = i * 2, .b = i * 10, .foo = NULL}; | |
array_sorted_add_stuff(&stuffs, stuff); | |
} | |
printf("{.data=%p, .len=%zu, .cap=%zu}\n", stuffs.data, stuffs.len, stuffs.cap); | |
for(int i = 0; i < 8; i++) { | |
struct stuff *stuff = stuffs.data + i; | |
printf("{.a=%d, .b=%d, .foo=%p}\n", stuff->a, stuff->b, stuff->foo); | |
} | |
putchar('\n'); | |
struct stuff *stuff = array_sorted_find_stuff(&stuffs, (struct stuff){.a = 4, .b = 20}); | |
if(stuff) | |
printf("{.a=%d, .b=%d, .foo=%p}\n", stuff->a, stuff->b, stuff->foo); | |
array_sorted_flush_stuff(&stuffs, stuff); | |
printf("{.data=%p, .len=%zu, .cap=%zu}\n", stuffs.data, stuffs.len, stuffs.cap); | |
for(int i = 0; i < 8; i++) { | |
stuff = stuffs.data + i; | |
printf("{.a=%d, .b=%d, .foo=%p}\n", stuff->a, stuff->b, stuff->foo); | |
} | |
free(stuffs.data); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef ARRAY_H | |
#define ARRAY_H | |
/* | |
MIT License | |
Copyright (c) 2019 Antonin Décimo | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
SOFTWARE. | |
*/ | |
/* A type-safe, growable array for C, generated with macros. */ | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stddef.h> | |
#include <assert.h> | |
#define array_struct(prefix, type, name) \ | |
struct prefix ## name { \ | |
type *data; \ | |
size_t len, cap; \ | |
}; | |
#define array_init(prefix, type, name, capacity) \ | |
struct prefix ## name * \ | |
prefix ## init_ ## name(struct prefix ## name *arr) \ | |
{ \ | |
int alloc = 0; \ | |
if(!arr) { \ | |
alloc = 1; \ | |
arr = malloc(sizeof(struct prefix ## name)); \ | |
if(!arr) \ | |
return NULL; \ | |
} \ | |
arr->len = 0; \ | |
arr->cap = capacity; \ | |
arr->data = malloc(arr->cap * sizeof(type)); \ | |
if(!arr->data) { \ | |
if(alloc) \ | |
free(arr); \ | |
return NULL; \ | |
} \ | |
return arr; \ | |
} | |
#define array_push(prefix, type, name, capacity) \ | |
int \ | |
prefix ## push_ ## name(struct prefix ## name *arr, type e) \ | |
{ \ | |
if(arr->len >= arr->cap) { \ | |
size_t cap = arr->cap >= 1 ? 2 * arr->cap : capacity; \ | |
type *data = realloc(arr->data, cap * sizeof(type)); \ | |
if(!data) \ | |
return -1; \ | |
arr->cap = cap; \ | |
arr->data = data; \ | |
} \ | |
arr->data[arr->len++] = e; \ | |
return 0; \ | |
} | |
#define array_pop(prefix, type, name) \ | |
void \ | |
prefix ## pop_ ## name(struct prefix ## name *arr, size_t i) \ | |
{ \ | |
arr->data[i] = arr->data[--arr->len]; \ | |
} | |
#define array_find_slot(prefix, type, name) \ | |
ssize_t \ | |
prefix ## find_slot_ ## name(const struct prefix ## name *arr, \ | |
const type e, size_t *new_return) \ | |
{ \ | |
ssize_t p, g; \ | |
\ | |
if(arr->len == 0) { \ | |
if(new_return) \ | |
*new_return = 0; \ | |
return -1; \ | |
} \ | |
\ | |
p = 0; g = arr->len - 1; \ | |
\ | |
do { \ | |
ssize_t m = (p + g) / 2; \ | |
int c = compare_ ## name(&e, &arr->data[m]); \ | |
if(c == 0) \ | |
return m; \ | |
else if(c < 0) \ | |
g = m - 1; \ | |
else \ | |
p = m + 1; \ | |
} while(p <= g); \ | |
\ | |
if(new_return) \ | |
*new_return = p; \ | |
\ | |
return -1; \ | |
} | |
#define array_find(prefix, type, name) \ | |
type * \ | |
prefix ## find_ ## name(const struct prefix ## name *arr, \ | |
const type e) \ | |
{ \ | |
ssize_t i; \ | |
i = prefix ## find_slot_ ## name(arr, e, NULL); \ | |
if(i >= 0) \ | |
return arr->data + i; \ | |
return NULL; \ | |
} | |
#define array_add(prefix, type, name, capacity) \ | |
int \ | |
prefix ## add_ ## name(struct prefix ## name *arr, type e) \ | |
{ \ | |
size_t n; \ | |
ssize_t i = prefix ## find_slot_ ## name(arr, e, &n); \ | |
\ | |
if(i >= 0) \ | |
return -1; \ | |
\ | |
if(arr->len >= arr->cap) { \ | |
size_t cap = arr->cap >= 1 ? 2 * arr->cap : capacity; \ | |
type *data = realloc(arr->data, cap * sizeof(type)); \ | |
if(!data) \ | |
return -1; \ | |
arr->cap = cap; \ | |
arr->data = data; \ | |
} \ | |
\ | |
if(n < arr->len) \ | |
memmove(arr->data + n + 1, arr->data + n, \ | |
(arr->len - n) * sizeof(type)); \ | |
arr->len++; \ | |
arr->data[n] = e; \ | |
return 0; \ | |
} | |
#define array_flush(prefix, type, name, capacity) \ | |
void \ | |
prefix ## flush_ ## name(struct prefix ## name *arr, type *e) \ | |
{ \ | |
ptrdiff_t i = e - arr->data; \ | |
assert(i >= 0 && (size_t)i < arr->len); \ | |
\ | |
if((size_t)i != arr->len - 1) \ | |
memmove(arr->data + i, arr->data + i + 1, \ | |
(arr->len - i - 1) * sizeof(type)); \ | |
arr->len--; \ | |
\ | |
if(arr->cap > capacity && arr->len < arr->cap / 4) { \ | |
size_t cap = arr->cap / 2; \ | |
type *data = realloc(arr->data, cap * sizeof(type)); \ | |
if(!data) \ | |
return; \ | |
arr->cap = cap; \ | |
arr->data = data; \ | |
} \ | |
} | |
#define array_noaccess | |
#define array(access, prefix, type, name, capacity) \ | |
access array_init(prefix, type, name, capacity); \ | |
access array_push(prefix, type, name, capacity); \ | |
access array_pop(prefix, type, name); | |
#define array_sorted(access, prefix, type, name, capacity) \ | |
access array_init(prefix, type, stuff, capacity); \ | |
access array_find_slot(prefix, type, name); \ | |
access array_find(prefix, type, name); \ | |
access array_add(prefix, type, name, capacity); \ | |
access array_flush(prefix, type, name, capacity); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment