From 6ac4deb451ccd4ed11fb0d022b83710b5b0522fe Mon Sep 17 00:00:00 2001 From: Tavian Barnes Date: Tue, 18 Jan 2022 12:01:56 -0500 Subject: darray: New DARRAY_POP() macro --- darray.c | 11 ++++++++++- darray.h | 25 ++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/darray.c b/darray.c index 846d825..8f796d6 100644 --- a/darray.c +++ b/darray.c @@ -1,6 +1,6 @@ /**************************************************************************** * bfs * - * Copyright (C) 2019 Tavian Barnes * + * Copyright (C) 2019-2022 Tavian Barnes * * * * Permission to use, copy, modify, and/or distribute this software for any * * purpose with or without fee is hereby granted. * @@ -15,6 +15,7 @@ ****************************************************************************/ #include "darray.h" +#include #include #include @@ -96,6 +97,14 @@ int darray_check(void *da) { } } +size_t darray_pop(void *da) { + assert(da); + + struct darray *header = darray_header(da); + assert(header->length > 0); + return --header->length; +} + void darray_free(void *da) { if (da) { free(darray_header(da)); diff --git a/darray.h b/darray.h index 6ab8199..e2d0e10 100644 --- a/darray.h +++ b/darray.h @@ -1,6 +1,6 @@ /**************************************************************************** * bfs * - * Copyright (C) 2019 Tavian Barnes * + * Copyright (C) 2019-2022 Tavian Barnes * * * * Permission to use, copy, modify, and/or distribute this software for any * * purpose with or without fee is hereby granted. * @@ -86,6 +86,18 @@ void *darray_push(void *da, const void *item, size_t size); */ int darray_check(void *da); +/** + * @internal Use DARRAY_POP(). + * + * Pop an element from an array. + * + * @param da + * The array in question. + * @return + * The (new) length of the array. + */ +size_t darray_pop(void *da); + /** * Free a darray. * @@ -107,4 +119,15 @@ void darray_free(void *da); #define DARRAY_PUSH(da, item) \ (darray_check(*(da) = darray_push(*(da), (item), sizeof(**(da) = *(item))))) +/** + * Pop an item from a darray. + * + * @param da + * The array to pop from. + * @return + * The popped item. + */ +#define DARRAY_POP(da) \ + ((da)[darray_pop((da))]) + #endif // BFS_DARRAY_H -- cgit v1.2.3