aocc23

Advent of Code 2023
git clone git://www.tkruger.se/aocc23.git
Log | Files | Refs | README

stack_u64.c (800B)


      1 #include "stack_u64.h"
      2 
      3 void stack_u64_init(stack_u64 *d) {
      4   d->nmemb = 0;
      5   d->alloc = BASE_STACK_SIZE;
      6 
      7   d->data = calloc(d->alloc, sizeof(*(d->data)));
      8 }
      9 
     10 void stack_u64_clear(stack_u64 *d) {
     11   d->nmemb = 0;
     12   free(d->data);
     13   d->data = NULL;
     14 }
     15 
     16 void stack_u64_push(stack_u64 *d, const uint64_t x) {
     17   if (d->alloc <= d->nmemb) {
     18     d->alloc <<= 1;
     19     d->data = realloc(d->data, d->alloc * sizeof(*(d->data)));
     20   }
     21 
     22   d->data[d->nmemb] = x;
     23   d->nmemb++;
     24 }
     25 
     26 uint64_t stack_u64_get(const stack_u64 *d, const size_t i) {
     27   return d->data[i];
     28 }
     29 
     30 uint64_t stack_u64_getlast(const stack_u64 *d) {
     31   return stack_u64_get(d, d->nmemb - 1);
     32 }
     33 
     34 uint64_t stack_u64_pop(stack_u64 *d) {
     35   if (d->nmemb == 0)
     36     return STACK_U64_EMPTY_POP;
     37   uint64_t r = stack_u64_getlast(d);
     38   d->nmemb--;
     39   return r;
     40 }