From 3966f053a14c5f1c57738143fea3d239070e5517 Mon Sep 17 00:00:00 2001 From: Caio Marcelo de Oliveira Filho Date: Wed, 12 Sep 2018 14:57:35 -0700 Subject: util: Add foreach_reverse for dynarray Useful to walk the array removing elements by swapping them with the last element. v2: Change iteration to make sure we never underflow. (Jason) Reviewed-by: Jason Ekstrand --- src/util/u_dynarray.h | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'src/util') diff --git a/src/util/u_dynarray.h b/src/util/u_dynarray.h index 512c0efc762..c1aa79c8ac6 100644 --- a/src/util/u_dynarray.h +++ b/src/util/u_dynarray.h @@ -155,6 +155,12 @@ util_dynarray_trim(struct util_dynarray *buf) for (type *elem = (type *)(buf)->data; \ elem < (type *)((char *)(buf)->data + (buf)->size); elem++) +#define util_dynarray_foreach_reverse(buf, type, elem) \ + if ((buf)->size > 0) \ + for (type *elem = util_dynarray_top_ptr(buf, type); \ + elem; \ + elem = elem > (type *)(buf)->data ? elem - 1 : NULL) + #define util_dynarray_delete_unordered(buf, type, v) \ do { \ unsigned num_elements = (buf)->size / sizeof(type); \ -- cgit v1.2.3