[Mesa-dev] [PATCH 01/11] util: Add foreach_reverse for dynarray
Jason Ekstrand
jason at jlekstrand.net
Thu Oct 4 20:21:50 UTC 2018
On Fri, Sep 14, 2018 at 10:46 PM Caio Marcelo de Oliveira Filho <
caio.oliveira at intel.com> wrote:
> Useful to walk the array removing elements by swapping them with the
> last element.
> ---
> src/util/u_dynarray.h | 6 ++++++
> 1 file changed, 6 insertions(+)
>
> diff --git a/src/util/u_dynarray.h b/src/util/u_dynarray.h
> index 6bea481d44b..f74bfc7080b 100644
> --- a/src/util/u_dynarray.h
> +++ b/src/util/u_dynarray.h
> @@ -154,6 +154,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 >= (type *)(buf)->data; \
>
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.
> + elem--)
>
We could do "elem; elem = elem > (type *)(buf)->data ? elem - 1 : NULL"?
Maybe that's more complicated than it's worth. Meh.
Reviewed-by: Jason Ekstrand <jason at jlekstrand.net>
--Jason
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.freedesktop.org/archives/mesa-dev/attachments/20181004/456050a4/attachment.html>
More information about the mesa-dev
mailing list