vis

a vi-like editor based on Plan 9's structural regular expressions

git clone https://9o.is/git/vis.git

commit 90beca207c297399d1c959c7b9feea3f275de2f5
parent 6a977de9250888ef99d8946f7d021e225c069b18
Author: Marc André Tanner <mat@brain-dump.org>
Date:   Mon, 10 Jul 2017 17:08:58 +0200

array: add helper functions for LIFO usage

Diffstat:
Marray.c | 18++++++++++++++++++
Marray.h | 21+++++++++++++++++++++
2 files changed, 39 insertions(+), 0 deletions(-)

diff --git a/array.c b/array.c @@ -151,3 +151,21 @@ bool array_resize(Array *arr, size_t len) { void array_sort(Array *arr, int (*compar)(const void*, const void*)) { qsort(arr->items, arr->len, arr->elem_size, compar); } + +bool array_push(Array *arr, void *item) { + return array_add(arr, item); +} + +void *array_pop(Array *arr) { + void *item = array_peek(arr); + if (!item) + return NULL; + arr->len--; + return item; +} + +void *array_peek(Array *arr) { + if (arr->len == 0) + return NULL; + return array_get(arr, arr->len - 1); +} diff --git a/array.h b/array.h @@ -108,5 +108,26 @@ bool array_resize(Array*, size_t length); * Sort array, the comparision function works as for `qsort(3)`. */ void array_sort(Array*, int (*compar)(const void*, const void*)); +/** + * Push item onto the top of the stack. + * @rst + * .. note:: Is equivalent to ``array_add(arr, item)``. + * @endrst + */ +bool array_push(Array*, void *item); +/** + * Get and remove item at the top of the stack. + * @rst + * .. warning:: The same ownership rules as for ``array_get`` apply. + * @endrst + */ +void *array_pop(Array*); +/** + * Get item at the top of the stack without removing it. + * @rst + * .. warning:: The same ownership rules as for ``array_get`` apply. + * @endrst + */ +void *array_peek(Array*); #endif