From: Matthew Fernandez Date: Thu, 8 Dec 2022 16:03:22 +0000 (-0800) Subject: cgraph: add push and pop functions to the generic list X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5592da0fb3728f9c4300e41c6cd5c08a89490679;p=graphviz cgraph: add push and pop functions to the generic list This enables using this data structure as a stack as well. In future it could grow to replace `gv_stack_t`. --- diff --git a/lib/cgraph/list.h b/lib/cgraph/list.h index 5dba00ba4..e7f4c2351 100644 --- a/lib/cgraph/list.h +++ b/lib/cgraph/list.h @@ -173,6 +173,26 @@ *list = (name##_t){0}; \ } \ \ + /** alias for append */ \ + static inline LIST_UNUSED void name##_push(name##_t *list, type value) { \ + name##_append(list, value); \ + } \ + \ + /** remove and return last element */ \ + static inline LIST_UNUSED type name##_pop(name##_t *list) { \ + assert(list != NULL); \ + assert(list->size > 0); \ + \ + type value = list->data[list->size - 1]; \ + \ + /* do not call `list->dtor` because we are transferring ownership of the \ + * removed element to the caller \ + */ \ + --list->size; \ + \ + return value; \ + } \ + \ /** create a new list from a bare array and element count \ * \ * This can be useful when receiving data from a caller who does not use \