From: Niki Date: Sat, 5 Apr 2025 07:39:39 +0000 (+0200) Subject: array: add deloop X-Git-Url: http://git.nikiroo.be/?a=commitdiff_plain;h=898baa7d0b1841a9d0ea76d14a9cbe49d05164ea;p=cutils.git array: add deloop --- diff --git a/array.h b/array.h index 3638562..d98989f 100644 --- 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 TYPE * 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 array_loop, but add a counter i 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 array_deloop, but add a counter i 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 *