diff options
author | Caio Marcelo de Oliveira Filho <[email protected]> | 2018-09-12 14:57:35 -0700 |
---|---|---|
committer | Caio Marcelo de Oliveira Filho <[email protected]> | 2018-10-15 17:29:46 -0700 |
commit | 3966f053a14c5f1c57738143fea3d239070e5517 (patch) | |
tree | 923dd79c34c46b37b7ad9fb4224c17c67e82ff67 /src | |
parent | 8ec83dc51e407fa0987d636752497509278c0dbb (diff) |
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 <[email protected]>
Diffstat (limited to 'src')
-rw-r--r-- | src/util/u_dynarray.h | 6 |
1 files changed, 6 insertions, 0 deletions
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); \ |