bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Add gl_list_remove_last to list/xlist


From: Marc Nieper-Wißkirchen
Subject: Re: Add gl_list_remove_last to list/xlist
Date: Fri, 22 May 2020 17:59:41 +0200

Hi Bruno,

Am Sa., 2. Mai 2020 um 17:49 Uhr schrieb Bruno Haible <address@hidden>:
>
> 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 */



reply via email to

[Prev in Thread] Current Thread [Next in Thread]