aocc23

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

stack_sd.h (2220B)


      1 #ifndef STACK_SD_H
      2 #define STACK_SD_H
      3 
      4 #include "sd.h"
      5 #include <stdint.h>
      6 #include <stdlib.h>
      7 
      8 #define BASE_STACK_SD_SIZE 256
      9 #define STACK_SD_LOOKUP_NOT_FOUND (-1)
     10 #define STACK_SD_EMPTY_POP ((sd){.size = 0, .data = NULL})
     11 
     12 typedef struct {
     13   size_t nmemb;
     14   size_t alloc;
     15 
     16   sd *data;
     17 } stack_sd;
     18 
     19 /**
     20  * Initialization of stack.
     21  *
     22  * Initialises memory for the stack. Use clear.
     23  *
     24  * @param d pointer to the address where to write the new stack
     25  */
     26 void stack_sd_init(stack_sd *d);
     27 
     28 /**
     29  * Initialization of stack, with size.
     30  *
     31  * Initialises memory for the stack. Use clear.
     32  *
     33  * @param d pointer to the address where to write the new stack
     34  * @param size initial size of stack alloc
     35  */
     36 void stack_sd_init_size(stack_sd *d, const size_t size);
     37 
     38 /**
     39  * Clear a stack
     40  *
     41  * @param d pointer to the stack to clear
     42  */
     43 void stack_sd_clear(stack_sd *d);
     44 
     45 /**
     46  * Pushes a sd pointer onto the stack.
     47  *
     48  * Note that this does not copy the sd data, it just pushes the sd
     49  * struct including the pointer to the stack.
     50  *
     51  * @param d pointer to the stack
     52  * @param x the sd to push
     53  */
     54 void stack_sd_push(stack_sd *d, sd x);
     55 
     56 /**
     57  * Gets the element at index i in the stack.
     58  *
     59  * @param d pointer to the stack
     60  * @param i the index to get element at
     61  * @return the value of the sd at index i
     62  */
     63 sd stack_sd_get(const stack_sd *d, const size_t i);
     64 
     65 /**
     66  * Get the last (top) element of a stack.
     67  *
     68  * @param d pointer to the stack
     69  * @return the value of the sd at the top
     70  */
     71 sd stack_sd_getlast(const stack_sd *d);
     72 
     73 /**
     74  * Pop an element of the stack.
     75  *
     76  * Note that popping never decreases the amount of memory allocated.
     77  * If memory is an issue the stacks have to be destroyed and replaced.
     78  * Returns STACK_SD_POP_EMPY if stack is empty.
     79  *
     80  * @param d pointer to the stack
     81  * @return the value of the popped sd
     82  */
     83 sd stack_sd_pop(stack_sd *d);
     84 
     85 /**
     86  * Lookup a sd on a stack.
     87  *
     88  * Returns the first index on the stack at which a sd appears. Returns
     89  * STACK_SD_LOOKUP_NOT_FOUND (== -1) if element is not in stack. It
     90  * compares the sd elements (i.e. not only the pointers).
     91  *
     92  * @param d the stack to lookup in
     93  * @param s the sd to look for in the stack
     94  */
     95 int stack_sd_lookup(stack_sd *d, sd s);
     96 
     97 #endif