]> granicus.if.org Git - graphviz/commitdiff
cgraph: add push and pop functions to the generic list
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Thu, 8 Dec 2022 16:03:22 +0000 (08:03 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 11 Dec 2022 18:37:29 +0000 (10:37 -0800)
This enables using this data structure as a stack as well. In future it could
grow to replace `gv_stack_t`.

lib/cgraph/list.h

index 5dba00ba400ff72eae22d0642df2d39634e61410..e7f4c2351dff30bb4026f5195def32dfb2d0dae6 100644 (file)
     *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     \