50#define fstack_for_each(self, index, value) \
51 for ((index) = (self)->count; (index) > 0 && ((value) = (self)->values[(index) - 1], true); (index)--)
63#define fstack_for_each_reverse(self, index, value) \
64 for ((index) = 0; (index) < (self)->count && ((value) = (self)->values[(index)], true); (index)++)
76#define fstack_calc_sizeof(fstack_name, capacity) \
77 (uint32_t)(offsetof(struct fstack_name, values) + capacity * sizeof(((struct fstack_name *)0)->values[0]))
89#define fstack_calc_sizeof_overflows(fstack_name, capacity) \
90 (capacity > (UINT32_MAX - offsetof(struct fstack_name, values)) / sizeof(((struct fstack_name *)0)->values[0]))
103#error "Must define NAME."
105#define FSTACK_NAME NAME
116#define VALUE_TYPE int
117#error "Must define VALUE_TYPE."
121#define FSTACK_TYPE struct FSTACK_NAME
122#define FSTACK_IS_EMPTY JOIN(FSTACK_NAME, is_empty)
123#define FSTACK_IS_FULL JOIN(FSTACK_NAME, is_full)
124#define FSTACK_INIT JOIN(FSTACK_NAME, init)
125#define FSTACK_CALC_SIZEOF JOIN(FSTACK_NAME, calc_sizeof)
151static inline FSTACK_TYPE *
JOIN(FSTACK_NAME, init)(FSTACK_TYPE *self,
const uint32_t capacity)
156 self->capacity = capacity;
171static inline FSTACK_TYPE *
JOIN(FSTACK_NAME, create)(
const uint32_t capacity)
179 FSTACK_TYPE *self = (FSTACK_TYPE *)calloc(1, size);
185 FSTACK_INIT(self, capacity);
197static inline void JOIN(FSTACK_NAME, destroy)(FSTACK_TYPE *self)
199 assert(self != NULL);
211static inline bool JOIN(FSTACK_NAME, is_empty)(
const FSTACK_TYPE *self)
213 assert(self != NULL);
215 return self->count == 0;
225static inline bool JOIN(FSTACK_NAME, is_full)(
const FSTACK_TYPE *self)
227 assert(self != NULL);
229 return self->count == self->capacity;
243static inline VALUE_TYPE JOIN(FSTACK_NAME, at)(
const FSTACK_TYPE *self,
const uint32_t index)
245 assert(self != NULL);
246 assert(index < self->count);
248 return self->values[self->count - 1 - index];
260 assert(self != NULL);
261 assert(FSTACK_IS_EMPTY(self) ==
false);
263 return self->values[self->count - 1];
275 assert(self != NULL);
276 assert(FSTACK_IS_EMPTY(self) ==
false);
278 return self->values[0];
290 return JOIN(FSTACK_NAME, get_top)(self);
299static inline void JOIN(FSTACK_NAME, push)(FSTACK_TYPE *self,
const VALUE_TYPE value)
301 assert(self != NULL);
302 assert(FSTACK_IS_FULL(self) ==
false);
304 self->values[self->count++] = value;
316 assert(self != NULL);
317 assert(FSTACK_IS_EMPTY(self) ==
false);
319 return self->values[--self->count];
327static inline void JOIN(FSTACK_NAME, clear)(FSTACK_TYPE *self)
329 assert(self != NULL);
339static inline void JOIN(FSTACK_NAME, copy)(FSTACK_TYPE *restrict dest_ptr,
const FSTACK_TYPE *restrict src_ptr)
341 assert(src_ptr != NULL);
342 assert(dest_ptr != NULL);
343 assert(src_ptr->count <= dest_ptr->capacity);
344 assert(FSTACK_IS_EMPTY(dest_ptr));
346 for (uint32_t i = 0; i < src_ptr->count; i++) {
347 dest_ptr->values[i] = src_ptr->values[i];
349 dest_ptr->count = src_ptr->count;
360#undef FSTACK_IS_EMPTY
363#undef FSTACK_CALC_SIZEOF
#define VALUE_TYPE
The value type. This must be manually defined before including this header file.
Definition fhashtable.h:144
#define fstack_calc_sizeof(fstack_name, capacity)
Calculate the size of the stack struct. No overflow checks.
Definition fstack.h:76
#define VALUE_TYPE
Stack value type. This must be manually defined before including this header file.
Definition fstack.h:116
#define fstack_calc_sizeof_overflows(fstack_name, capacity)
Check for a given capacity, if the equivalent size of the stack struct overflows.
Definition fstack.h:89
C preprocessor macros for pasting tokens together.
#define JOIN(a, b)
First expand tokens, then paste them together with a _ in between.
Definition paste.h:35
uint32_t capacity
maximum number of values allocated for.
Definition fstack.h:137
uint32_t count
number of values.
Definition fstack.h:136