aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorThomas Voss <thomas.voss@humanwave.nl> 2024-01-17 17:02:11 +0100
committerThomas Voss <thomas.voss@humanwave.nl> 2024-01-17 17:02:11 +0100
commit79d872a0d9f6e48825645b8cf80d947938d9c8b1 (patch)
tree88ecbab1aca293515cbc2ac5277477c303385d32 /src
parent7207d3d8d0b75b980d5bb1f6a6c2f22dc30af20c (diff)
Grab the latest version of da.h
Diffstat (limited to 'src')
-rw-r--r--src/da.h99
-rw-r--r--src/grab.c4
2 files changed, 80 insertions, 23 deletions
diff --git a/src/da.h b/src/da.h
index 1587ece..57b7ed1 100644
--- a/src/da.h
+++ b/src/da.h
@@ -9,12 +9,13 @@
* }
*
* The type ‘T’ is whatever type you want to store. The type ‘N’ is any numeric
- * type, most likely ‘size_t’.
+ * 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.
+ * The daremove() 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
@@ -22,43 +23,99 @@
* 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
+ * dainit(a, n) Initialize the array with a capacity of ‘n’ items.
+ * dapush(a, x) Append the item ‘x’ to the array
+ * daremove(a, x) Remove the item at index ‘x’ from the array
* da_remove_range(a, x, y) Remove the items between the range [x, y)
+ * da_foreach(a, p) Iterate the pointer ‘p’ over each element of the
+ * array. The type of ‘p’ is inferred.
+ *
+ * The ‘dapush()’ macro will double the arrays capacity when it gets full. If
+ * you would like your arrays to grow with a different scale, edit this file.
+ *
+ *
+ * Example
+ * ―――――――
+ *
+ * struct {
+ * int *buf;
+ * size_t len, cap;
+ * } nums;
+ *
+ * // Initialize nums with capacity == 4
+ * dainit(&nums, 4);
+ *
+ * // Append 69, 1337, and 420 to nums
+ * dapush(&nums, 69);
+ * dapush(&nums, 1337);
+ * dapush(&nums, 420);
+ *
+ * da_foreach (&nums, n) {
+ * int x = *n << 1;
+ * printf("n = %d; n² = %d\n", *n, x);
+ * }
+ *
+ * // Remove 1337 and 420 from nums
+ * da_remove_range(&nums, 1, 3);
+ *
+ * // Remove 69 from nums
+ * daremove(&nums, 0);
*/
#ifndef MANGO_DA_H
#define MANGO_DA_H
-#define __da_s(a) (sizeof(*(a)->buf))
+#include <err.h>
+#include <errno.h>
+#include <stddef.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+
+#if __STDC_VERSION__ >= 202311L
+# define DA_NULL nullptr
+#else
+# define DA_NULL NULL
+#endif
+
+#define DA_ALLOC(p, n) \
+ do { \
+ if ((n) && SIZE_MAX / (n) < sizeof(*(p))) { \
+ errno = EOVERFLOW; \
+ err(EXIT_FAILURE, "realloc"); \
+ } \
+ if (!((p) = realloc((p), (n) * sizeof(*(p))))) \
+ err(EXIT_FAILURE, "realloc"); \
+ } while (0)
-#define da_init(a, n) \
+#define dainit(a, n) \
do { \
- (a)->cap = n; \
+ (a)->buf = DA_NULL; \
+ (a)->cap = (n); \
(a)->len = 0; \
- (a)->buf = malloc((a)->cap * __da_s(a)); \
- if ((a)->buf == NULL) \
- err(EXIT_FAILURE, "malloc"); \
+ if (n) \
+ DA_ALLOC((a)->buf, (a)->cap); \
} while (0)
-#define da_append(a, x) \
+#define dapush(a, x) \
do { \
if ((a)->len >= (a)->cap) { \
- (a)->cap = (a)->cap * 2 + 1; \
- (a)->buf = realloc((a)->buf, (a)->cap * __da_s(a)); \
- if ((a)->buf == NULL) \
- err(EXIT_FAILURE, "realloc"); \
+ (a)->cap = (a)->cap ? (a)->cap * 2 : 1; \
+ DA_ALLOC((a)->buf, (a)->cap); \
} \
(a)->buf[(a)->len++] = (x); \
} while (0)
-#define da_remove(a, i) da_remove_range((a), (i), (i) + 1)
+#define daremove(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)); \
+ memmove((a)->buf + (i), (a)->buf + (j), \
+ ((a)->len - (j)) * sizeof(*(a)->buf)); \
(a)->len -= j - i; \
} while (0)
+#define da_foreach(a, p) \
+ for (auto p = (a)->buf; (size_t)(p - (a)->buf) < (a)->len; p++)
+
#endif /* !MANGO_DA_H */
diff --git a/src/grab.c b/src/grab.c
index f47a375..f04f75f 100644
--- a/src/grab.c
+++ b/src/grab.c
@@ -223,7 +223,7 @@ comppat(char *s)
#define skip_ws(p) for (; *(p) && xisspace(*(p)); (p)++)
struct ops ops;
- da_init(&ops, 8);
+ dainit(&ops, 8);
skip_ws(s);
if (!*s)
diex(EEARLY);
@@ -242,7 +242,7 @@ comppat(char *s)
p = ++s;
s = xstrchrnul(s, delim);
op.pat = mkregex(p, s - p);
- da_append(&ops, op);
+ dapush(&ops, op);
if (*s)
s++;