<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Fri, Sep 14, 2018 at 10:46 PM Caio Marcelo de Oliveira Filho <<a href="mailto:caio.oliveira@intel.com">caio.oliveira@intel.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Useful to walk the array removing elements by swapping them with the<br>
last element.<br>
---<br>
src/util/u_dynarray.h | 6 ++++++<br>
1 file changed, 6 insertions(+)<br>
<br>
diff --git a/src/util/u_dynarray.h b/src/util/u_dynarray.h<br>
index 6bea481d44b..f74bfc7080b 100644<br>
--- a/src/util/u_dynarray.h<br>
+++ b/src/util/u_dynarray.h<br>
@@ -154,6 +154,12 @@ util_dynarray_trim(struct util_dynarray *buf)<br>
for (type *elem = (type *)(buf)->data; \<br>
elem < (type *)((char *)(buf)->data + (buf)->size); elem++)<br>
<br>
+#define util_dynarray_foreach_reverse(buf, type, elem) \<br>
+ if ((buf)->size > 0) \<br>
+ for (type *elem = util_dynarray_top_ptr(buf, type); \<br>
+ elem >= (type *)(buf)->data; \<br></blockquote><div><br></div><div>I think this works... I suppose it's theoretically possible that elem-- could underflow. However, the only way that could happen is if the element size is really large and the data pointer ends up being really low.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
+ elem--)<br></blockquote><div><br></div><div>We could do "elem; elem = elem > (type *)(buf)->data ? elem - 1 : NULL"? Maybe that's more complicated than it's worth. Meh.</div><div><br></div><div>Reviewed-by: Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>><br></div><div><br></div><div>--Jason<br></div></div></div>