Hi Bruno, Am Sa., 2. Mai 2020 um 17:49 Uhr schrieb Bruno Haible <br...@clisp.org>: > > Hi Marc, > > > Okay; I agree that a separate stack or FIFO module can make more > > sense; in particular when it only has to deal with a single > > implementation of an underlying data structure. At the moment I do > > have other work to finish first, but maybe I will find some time in > > the near future for a stack module.
Alternatively, one could implement a universally usable stack through the following header file (mimicking somewhat C++ templates). What do you think? It will be a lot faster than using the general list modules of Gnulib. It would be used like: STACK (int) stack; STACK_INIT (stack); assert (STACK_EMPTY (stack)); STACK_PUSH (stack, 4) assert (!STACK_EMPTY (stack)); assert (STACK_TOP (stack) == 4); assert (STACK_POP (stack) == 4); assert (STACK_EMPTY (stack)); STACK_CLEAR (stack); So long, Marc **** #ifndef _GL_STACK_H #define _GL_STACK_H #include <stddef.h> #include <stdlib.h> #include <xalloc.h> #define STACK(type) \ struct { \ type *base; \ size_t size; \ size_t allocated; \ } #define STACK_INIT(stack) \ do \ { \ (stack).base = NULL; \ (stack).size = 0; \ (stack).allocated = 0; \ } \ while (0) #define STACK_CLEAR(stack) \ free ((stack).base) #define STACK_EMPTY(stack) \ ((stack).size == 0) #define STACK_BASE(stack) \ ((stack).base) #define STACK_PUSH(stack, item) \ do \ { \ if ((stack).size == (stack).allocated) \ (stack).base = x2nrealloc ((stack).base, &(stack).allocated, sizeof (item)); \ (stack).base [(stack).size++] = item; \ } \ while (0) #define STACK_POP(stack) \ (stack).base [--(stack).size] #define STACK_DISCARD(stack) \ (--(stack).size) #define STACK_TOP(stack) \ (stack).base[(stack).size - 1] #define STACK_SIZE(stack) \ ((stack).size) #endif /* _GL_STACK_H */