diff options
Diffstat (limited to 'include/array.h')
-rw-r--r-- | include/array.h | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/include/array.h b/include/array.h new file mode 100644 index 0000000..c382f1b --- /dev/null +++ b/include/array.h @@ -0,0 +1,58 @@ +/* + ihex + Copyright (C) 2025 Thomas Albers Raviola <thomas@thomaslabs.org> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + */ + +#ifndef ARRAY_H +#define ARRAY_H + +#include <stddef.h> + +struct array { + size_t length; + size_t elem_size; + size_t capacity; + void *data; +}; + +void +init_array(struct array *arr, size_t elem_size); + +void +free_array(struct array *arr); + +void * +array_append(struct array *arr, const void *data); + +void +array_remove(struct array *arr, size_t n, void *dest); + +void +array_sort(struct array *arr, int (*compar)(const void *, const void *)); + +void * +array_search(struct array *arr, const void *key, + int (*compar)(const void *, const void *)); + +static inline void * +array_elt(struct array *arr, size_t n) +{ + return (void *)((char *)arr->data + n * arr->elem_size); +} + +#define ARRAY_ELT(ARRAY, TYPE, N) ((TYPE *)(ARRAY)->data)[N] + +#endif /* ARRAY_H */ |