From 63ae64d8bf7fa06eecfc2c50e3999800720c9173 Mon Sep 17 00:00:00 2001 From: Thomas Voss Date: Sat, 9 Dec 2023 23:22:12 +0100 Subject: Genesis commit --- LICENSE | 14 +++++++++ README.md | 13 +++++++++ da.h | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 da.h diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..276994d --- /dev/null +++ b/LICENSE @@ -0,0 +1,14 @@ +BSD Zero Clause License + +Copyright © 2023 Thomas Voss + +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted. + +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH +REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY +AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, +INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM +LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR +OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR +PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..e4a9c88 --- /dev/null +++ b/README.md @@ -0,0 +1,13 @@ +# DA — Simple & Stupid Dynamic Array + +DA is a simple and easy-to-use single-header library for a dynamic array. +It’s not meant to be the worlds most efficent or optimal implementation; +it’s just stupid and simple. This means that it is perfect for 99% of +your dynamic array needs. + +The library is documented in full in `da.h`, and you can easily grab it +and include it in your project with the following command: + +```sh +wget 'https://git.sr.ht/~mango/da/blob/master/da.h' +``` diff --git a/da.h b/da.h new file mode 100644 index 0000000..888ac1a --- /dev/null +++ b/da.h @@ -0,0 +1,97 @@ +/* + * Simple & stupid dynamic array single-header implementation. You can use the + * macros defined in this file with any structure that has the following fields: + * + * struct dyn_array { + * T *buf // Array of items + * N len // Length of array + * N cap // Capacity of array + * } + * + * The type ‘T’ is whatever type you want to store. The type ‘N’ is any numeric + * type — most likely ‘size_t’ — but it could be sized as well. + * + * You should include ‘err.h’ and ‘stdlib.h’ along with this file. If you want + * to use da_remove(), include ‘string.h’. The da_remove() macro also doesn’t + * bother with shrinking your array when the length is far lower than the + * capacity. If you care about that, do it yourself. + * + * Remember to call free() on your dynamic arrays ‘buf’ field after use. + * + * Macro Overview + * ―――――――――――――― + * The argument ‘a’ to all of the below macros is a pointer to the dynamic array + * structure. + * + * da_init(a, n) Initialize the array with a capacity of ‘n’ items. + * da_append(a, x) Append the item ‘x’ to the array + * da_remove(a, x) Remove the item ‘x’ from the array + * da_remove_range(a, x, y) Remove the items between the range [x, y) + * + * The ‘da_append()’ macro will by default double the arrays capacity when it + * gets full. If you would like to use a different growth factor instead of 2, + * you can define the DA_FACTOR macro to your desired growth factor before + * including this file. + * + * + * Example + * ――――――― + * + * struct { + * int *buf; + * size_t len, cap; + * } nums; + * + * // Initialize nums with capacity == 4 + * da_init(&nums, 4); + * + * // Append 69, 1337, and 420 to nums + * da_append(&nums, 69); + * da_append(&nums, 1337); + * da_append(&nums, 420); + * + * // Remove 1337 and 420 from nums + * da_remove_range(&nums, 1, 3); + * + * // Remove 69 from nums + * da_remove(&nums, 0); + */ + +#ifndef MANGO_DA_H +#define MANGO_DA_H + +#ifndef DA_FACTOR +# define DA_FACTOR 2 +#endif + +#define __da_s(a) (sizeof(*(a)->buf)) + +#define da_init(a, n) \ + do { \ + (a)->cap = n; \ + (a)->len = 0; \ + (a)->buf = malloc((a)->cap * __da_s(a)); \ + if ((a)->buf == NULL) \ + err(EXIT_FAILURE, "malloc"); \ + } while (0) + +#define da_append(a, x) \ + do { \ + if ((a)->len >= (a)->cap) { \ + (a)->cap = (a)->cap * DA_FACTOR + 1; \ + (a)->buf = realloc((a)->buf, (a)->cap * __da_s(a)); \ + if ((a)->buf == NULL) \ + err(EXIT_FAILURE, "realloc"); \ + } \ + (a)->buf[(a)->len++] = (x); \ + } while (0) + +#define da_remove(a, i) da_remove_range((a), (i), (i) + 1) + +#define da_remove_range(a, i, j) \ + do { \ + memmove((a)->buf + (i), (a)->buf + (j), ((a)->len - (j)) * __da_s(a)); \ + (a)->len -= j - i; \ + } while (0) + +#endif /* !MANGO_DA_H */ -- cgit v1.2.3