advent2021

Advent of Code 2021 Solutions
git clone git://bsandro.tech/advent2021
Log | Files | Refs | README | LICENSE

util.h (1192B)


      1 #pragma once
      2 
      3 #include <stdio.h>
      4 #include <assert.h>
      5 
      6 #define MIN(a,b) (((a)<(b))?(a):(b))
      7 #define MAX(a,b) (((a)>(b))?(a):(b))
      8 
      9 #define STR_LEN_LIMIT 8192
     10 #define PRINT_ERR(func) fprintf(stderr, #func "() error: %s\n", strerror(errno));
     11 
     12 #define FMT_(l) #l
     13 #define FMT(l) FMT_(l)
     14 
     15 /* ************** simple arrays **********************/
     16 struct array_t {
     17 	void *data;
     18 	size_t elem_size;
     19 	size_t count;
     20 	size_t cap;
     21 };
     22 
     23 #define PRINT_ARRAY(array, type) do { \
     24 	printf("array count %zu, cap %zu, elem size %zu, data %p\n", \
     25 		array.count, array.cap, array.elem_size, &array); \
     26 	for (size_t i = 0; i < array.count; ++i) { \
     27 		type *data = (type *)array.data; \
     28 		printf("%lld ", data[i]); \
     29 	} \
     30 	printf("\n"); \
     31 } while (0);
     32 
     33 void array_init(struct array_t *array, size_t elem_size, size_t cap);
     34 void array_expand(struct array_t *array);
     35 void array_expand1(struct array_t *array, size_t incr);
     36 struct array_t array_copy(struct array_t *array);
     37 
     38 /* ******************* parse plain numbers array *******************/
     39 void parse_numbers_array(struct array_t *numbers, const char *str, const char *delim);
     40 void parse_numbers_array_ll(struct array_t *numbers, const char *str, const char *delim);