array: add deloop
authorNiki <niki@nikiroo.be>
Sat, 5 Apr 2025 07:39:39 +0000 (09:39 +0200)
committerNiki <niki@nikiroo.be>
Sat, 5 Apr 2025 07:39:39 +0000 (09:39 +0200)
array.h

diff --git a/array.h b/array.h
index 363856268d8ef64e9c447e18ab3b35531c1802ac..d98989fc44955ec2d291ba421e5f844da9121dcf 100644 (file)
--- a/array.h
+++ b/array.h
@@ -20,7 +20,7 @@
 /** 
  * @file array.h
  * @author Niki
- * @date 2020 - 2022
+ * @date 2020 - 2025
  *
  * @brief A simple auto-growing array-list
  * 
@@ -92,6 +92,20 @@ extern "C" {
 #define array_loop(me, ptr, TYPE) \
        for (TYPE *ptr = array_first(me); ptr; ptr = array_next(me, ptr))
 
+/**
+ * Declare a new <tt>TYPE *</tt> pointer and loop through the array with it
+ * in reverse order (last is index 0).
+ *
+ * How to use:
+ * ```C
+ * array_reloop(me, line, char) {
+ *     printf("Item: %s\n", line);
+ * }
+ * ```
+ */
+#define array_deloop(me, ptr, TYPE) \
+       for (TYPE *ptr = array_last(me); ptr; ptr = array_prev(me, ptr))
+
 /**
  * Similar to <tt>array_loop</tt>, but add a counter <tt>i</tt> starting at 0.
  *
@@ -110,6 +124,24 @@ extern "C" {
        size_t i = 0; \
        for (TYPE *ptr = array_first(me); ptr; ptr = array_next(me, ptr), i++)
 
+/**
+ * Similar to <tt>array_deloop</tt>, but add a counter <tt>i</tt> ending at 0.
+ *
+ * @see array_deloop
+ *
+ * How to use:
+ * ```C
+ * array_deloop_i(me, line, char, i) {
+ *     printf("Item n°%d: %s\n", i, line);
+ * }
+ * ```
+ *
+ * @note this macro does expand as 2 separate lines, surround with { } if needed
+ */
+#define array_deloop_i(me, ptr, TYPE, i) \
+       size_t i = 0; \
+       for (TYPE *ptr = array_last(me); ptr; ptr = array_prev(me, ptr), i++)
+
 /**
  * @brief A simple auto-growing array-list
  *