aocc23

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

stack_str.h (1449B)


      1 #ifndef STACK_STR_H
      2 #define STACK_STR_H
      3 
      4 #include <stdint.h>
      5 #include <stdlib.h>
      6 
      7 #define BASE_STACK_STR_SIZE 256
      8 #define STACK_STR_EMPTY_POP (NULL)
      9 
     10 typedef struct {
     11   size_t nmemb;
     12   size_t alloc;
     13 
     14   char **data;
     15 } stack_str;
     16 
     17 /**
     18  * Initialization of stack.
     19  *
     20  * Initialises memory for the stack. Use clear.
     21  *
     22  * @param d pointer to the address where to write the new stack
     23  */
     24 void stack_str_init(stack_str *d);
     25 
     26 /**
     27  * Clear a stack
     28  *
     29  * @param d pointer to the stack to clear
     30  */
     31 void stack_str_clear(stack_str *d);
     32 
     33 /**
     34  * Pushes a string (char pointer) onto the stack.
     35  *
     36  * Note that this does not copy the string, it just pushes the pointer to the
     37  * stack.
     38  *
     39  * @param d pointer to the stack
     40  * @param x the string (char pointer) to push
     41  */
     42 void stack_str_push(stack_str *d, char *x);
     43 
     44 /**
     45  * Gets the element at index i in the stack.
     46  *
     47  * @param d pointer to the stack
     48  * @param i the index to get element at
     49  * @return the value (the char pointer) of the element at index i
     50  */
     51 char *stack_str_get(const stack_str *d, const size_t i);
     52 
     53 /**
     54  * Get the last (top) element of a stack.
     55  *
     56  * @param d pointer to the stack
     57  */
     58 char *stack_str_getlast(const stack_str *d);
     59 
     60 /**
     61  * Pop an element of the stack.
     62  *
     63  * Note that popping never decreases the amount of memory allocated.
     64  * If memory is an issue the stacks have to be destroyed and replaced.
     65  *
     66  * @param d pointer to the stack
     67  */
     68 char *stack_str_pop(stack_str *d);
     69 
     70 #endif