Hi Marc, > 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.
I agree that a generic 'stack' module is useful. I also agree that a single implementation, based on an array, is the way to go. Then it is already faster than the generic list module. In Gnulib, we usually avoid extensive use of multi-line macros, because it hampers debuggability. Here, however, one needs macros, in order to accommodate the TYPE argument, which is not necessarily compatible to 'void *'. Nevertheless, we would try to put as much code as possible into functions. The STACK_INIT macro, for example, could be implemented as a function. > #define STACK_CLEAR(stack) \ > free ((stack).base) Shouldn't this one also set .size and .allocated to 0 ? > #define STACK_POP(stack) \ > (stack).base [--(stack).size] > > #define STACK_DISCARD(stack) \ > (--(stack).size) > > #define STACK_TOP(stack) \ > (stack).base[(stack).size - 1] In these three macros, I would consider to abort() when (stack).size == 0. Bruno