aboutsummaryrefslogtreecommitdiff
path: root/da.h
diff options
context:
space:
mode:
authorThomas Voss <mail@thomasvoss.com> 2024-02-23 16:48:18 +0100
committerThomas Voss <mail@thomasvoss.com> 2024-02-23 16:48:18 +0100
commitd4dade8830ff49907aab402c08d52923f2ac6c8f (patch)
tree2d469962701815a0fdd1a678e1e199575895fb6c /da.h
parent4075d0829e545ee92398bb2c21880efc7edb3b34 (diff)
Add daextend()HEADmaster
Diffstat (limited to 'da.h')
-rw-r--r--da.h30
1 files changed, 22 insertions, 8 deletions
diff --git a/da.h b/da.h
index 57b7ed1..8fea4db 100644
--- a/da.h
+++ b/da.h
@@ -23,15 +23,17 @@
* The argument ‘a’ to all of the below macros is a pointer to the dynamic array
* structure.
*
- * 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.
+ * dainit(a, n) Initialize the array with a capacity of ‘n’ items
+ * dapush(a, x) Append the item ‘x’ to the array
+ * daextend(a, xs, n) Append the ‘n’ items pointed to by ‘xs’ 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.
+ * The ‘dapush()’ and ‘daextend()’ macros 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
@@ -106,6 +108,18 @@
(a)->buf[(a)->len++] = (x); \
} while (0)
+#define daextend(a, xs, n) \
+ do { \
+ if ((a)->len + (n) >= (a)->cap) { \
+ do \
+ (a)->cap = (a)->cap ? (a)->cap * 2 : 1; \
+ while ((a)->len + (n) >= (a)->cap); \
+ DA_ALLOC((a)->buf, (a)->cap); \
+ } \
+ memcpy((a)->buf + (a)->len, (xs), (n)); \
+ (a)->len += (n); \
+ } while (0)
+
#define daremove(a, i) da_remove_range((a), (i), (i) + 1)
#define da_remove_range(a, i, j) \