summaryrefslogtreecommitdiff
path: root/darray.h
diff options
context:
space:
mode:
authorPeter Zhu <peter@peterzhu.ca>2025-01-21 11:38:01 -0500
committerPeter Zhu <peter@peterzhu.ca>2025-12-25 09:18:17 -0500
commit16626d500da3f8a0536ed86813000cbcfc8acc79 (patch)
treea332dffdf9eb563765faee36111f088e981f12c9 /darray.h
parent099da884fe95ccf6c684a1563ed1c4b0fd8e1196 (diff)
Implement rb_darray_swap_remove
Diffstat (limited to 'darray.h')
-rw-r--r--darray.h14
1 files changed, 14 insertions, 0 deletions
diff --git a/darray.h b/darray.h
index c9035b74b6..dc9d282be2 100644
--- a/darray.h
+++ b/darray.h
@@ -72,6 +72,20 @@
(*(ptr_to_ary))->meta.size++; \
} while (0)
+/* Removes the element at idx and replaces it with the last element.
+ * ptr_to_ary and idx is evaluated multiple times.
+ * Warning: not bounds checked.
+ *
+ * void rb_darray_swap_remove(rb_darray(T) *ptr_to_ary, size_t idx);
+ */
+#define rb_darray_swap_remove(ptr_to_ary, idx) do { \
+ size_t _darray_size = rb_darray_size(*(ptr_to_ary)); \
+ if ((idx) != _darray_size - 1) { \
+ (*(ptr_to_ary))->data[idx] = (*(ptr_to_ary))->data[_darray_size - 1]; \
+ } \
+ (*(ptr_to_ary))->meta.size--; \
+} while (0)
+
// Iterate over items of the array in a for loop
//
#define rb_darray_foreach(ary, idx_name, elem_ptr_var) \