From 0579cf7c0504d02897becac68d78ac3558a4f6e1 Mon Sep 17 00:00:00 2001 From: Bill Rossi Date: Fri, 27 Dec 2024 05:30:39 -0500 Subject: [PATCH] Use header files properly --- c/2024/19/linen_layout.c | 1 + c/bin/run | 2 +- c/lib/aoc.c | 73 +++++++++++++++++++++++++++++++++++++ c/lib/aoc.h | 79 +++++----------------------------------- c/lib/hashmap.c | 56 ++++++++++++++++++++++++++++ c/lib/hashmap.h | 60 ++++-------------------------- 6 files changed, 148 insertions(+), 123 deletions(-) create mode 100644 c/lib/aoc.c create mode 100644 c/lib/hashmap.c diff --git a/c/2024/19/linen_layout.c b/c/2024/19/linen_layout.c index 7564bcb..4e925a3 100644 --- a/c/2024/19/linen_layout.c +++ b/c/2024/19/linen_layout.c @@ -4,6 +4,7 @@ #include #include "../../lib/aoc.h" +#include "../../lib/hashmap.h" typedef char Towel[10]; diff --git a/c/bin/run b/c/bin/run index dfa7cf1..f0f273b 100755 --- a/c/bin/run +++ b/c/bin/run @@ -11,5 +11,5 @@ if [[ -z $source_file ]] ; then exit 1 fi -gcc -o $year/$day/problem $source_file -lm && +gcc -o $year/$day/problem $source_file lib/aoc.c -lm && time (cat ../data/$year/$day/input.txt | ./$year/$day/problem) diff --git a/c/lib/aoc.c b/c/lib/aoc.c new file mode 100644 index 0000000..274956c --- /dev/null +++ b/c/lib/aoc.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +#include "./hashmap.c" + +#define INITIAL_BUFFER_SIZE 2048 + +char *input_buffer; + +char *aoc_read_input(void) { + int buffer_size = INITIAL_BUFFER_SIZE; + input_buffer = malloc(buffer_size); + int chars_read = read(STDIN_FILENO, input_buffer, buffer_size); + char *buffer_position = &input_buffer[chars_read]; + + while (chars_read == buffer_size) { + buffer_size *= 2; + input_buffer = realloc(input_buffer, buffer_size); + buffer_position = &input_buffer[chars_read]; + chars_read += read(STDIN_FILENO, buffer_position, buffer_size / 2); + } + + input_buffer[chars_read] = 0; + return input_buffer; +} + +char *read_line_buffer = NULL; +char *seek = NULL; + +char *aoc_read_line(void) { + if (input_buffer == NULL) aoc_read_input(); + if (read_line_buffer == NULL) { + read_line_buffer = malloc(strlen(input_buffer) + 1); + strcpy(read_line_buffer, input_buffer); + } + + if (seek == NULL) seek = read_line_buffer; + + char *line_break = strstr(seek, "\n"); + if (line_break == NULL) return NULL; + + *line_break = '\0'; + char *token = seek; + seek = line_break + 1; + return token; +} + +void aoc_reset_read_line(void) { + strcpy(read_line_buffer, input_buffer); + seek = NULL; +} + +void aoc_free(void) { + free(input_buffer); + free(read_line_buffer); +} + +int aoc_line_count(void) { + if (input_buffer == NULL) aoc_read_input(); + + int count = 0; + for (int i = 0; i < strlen(input_buffer); i++) { + if (input_buffer[i] == '\n') count++; + } + return count; +} + +int aoc_sort_int(const void *a, const void *b) { + return *(int*) a - *(int*) b; +} + diff --git a/c/lib/aoc.h b/c/lib/aoc.h index 1576529..646ef1b 100644 --- a/c/lib/aoc.h +++ b/c/lib/aoc.h @@ -1,72 +1,11 @@ -#include -#include -#include +#ifndef AOC_H +#define AOC_H -#include "hashmap.h" - -#define INITIAL_BUFFER_SIZE 2 - -char *input_buffer; - -char *aoc_read_input(void) { - int buffer_size = INITIAL_BUFFER_SIZE; - input_buffer = malloc(buffer_size); - int chars_read = read(STDIN_FILENO, input_buffer, buffer_size); - char *buffer_position = &input_buffer[chars_read]; - - while (chars_read == buffer_size) { - buffer_size *= 2; - input_buffer = realloc(input_buffer, buffer_size); - buffer_position = &input_buffer[chars_read]; - chars_read += read(STDIN_FILENO, buffer_position, buffer_size / 2); - } - - input_buffer[chars_read] = 0; - return input_buffer; -} - -char *read_line_buffer = NULL; -char *seek = NULL; - -char *aoc_read_line(void) { - if (input_buffer == NULL) aoc_read_input(); - if (read_line_buffer == NULL) { - read_line_buffer = malloc(strlen(input_buffer) + 1); - strcpy(read_line_buffer, input_buffer); - } - - if (seek == NULL) seek = read_line_buffer; - - char *line_break = strstr(seek, "\n"); - if (line_break == NULL) return NULL; - - *line_break = '\0'; - char *token = seek; - seek = line_break + 1; - return token; -} - -void aoc_reset_read_line(void) { - strcpy(read_line_buffer, input_buffer); - seek = NULL; -} - -void aoc_free(void) { - free(input_buffer); - free(read_line_buffer); -} - -int aoc_line_count(void) { - if (input_buffer == NULL) aoc_read_input(); - - int count = 0; - for (int i = 0; i < strlen(input_buffer); i++) { - if (input_buffer[i] == '\n') count++; - } - return count; -} - -int aoc_sort_int(const void *a, const void *b) { - return *(int*) a - *(int*) b; -} +char *aoc_read_input(void); +char *aoc_read_line(void); +void aoc_reset_read_line(void); +void aoc_free(void); +int aoc_line_count(void); +int aoc_sort_int(const void *a, const void *b); +#endif diff --git a/c/lib/hashmap.c b/c/lib/hashmap.c new file mode 100644 index 0000000..45e9729 --- /dev/null +++ b/c/lib/hashmap.c @@ -0,0 +1,56 @@ +#include "./hashmap.h" + +int aoc_hash(const char *string) { + int len = strlen(string); + int hash = 1; + for (int i = 0; i < len; i++) { + hash *= 7; + hash += string[i] * 13; + hash %= AOC_HASHMAP_RADIX; + } + + return hash; +} + +AocHashmapNode *aoc_hashmap_get(AocHashmap *map, char *key) { + int hash = aoc_hash(key); + AocHashmapNode *node = map->buckets[hash]; + while (node != NULL && strcmp(node->key, key) != 0) node = node->next; + return node; +} + +void aoc_hashmap_put(AocHashmap *map, char *key, void *value) { + int hash = aoc_hash(key); + AocHashmapNode *node = map->buckets[hash]; + if (node == NULL) { + map->buckets[hash] = malloc(sizeof(AocHashmapNode)); + node = map->buckets[hash]; + node->key = malloc(strlen(key) + 1); + strcpy(node->key, key); + node->value = value; + node->next = NULL; + } else { + while (node->next != NULL) { + node = node->next; + } + node->next = malloc(sizeof(AocHashmapNode)); + node->next->key = malloc(strlen(key) + 1); + strcpy(node->next->key, key); + node->next->value = value; + node->next->next = NULL; + } +} + +void aoc_hashmap_free(AocHashmap *map) { + for (int i = 0; i < AOC_HASHMAP_RADIX; i++) { + AocHashmapNode *node = map->buckets[i]; + while (node != NULL) { + AocHashmapNode *next = node->next; + free(node->key); + free(node->value); + free(node); + node = next; + } + } + free(map); +} diff --git a/c/lib/hashmap.h b/c/lib/hashmap.h index e7e6008..cb05902 100644 --- a/c/lib/hashmap.h +++ b/c/lib/hashmap.h @@ -1,16 +1,9 @@ +#ifndef AOC_HASHMAP +#define AOC_HASHMAP + #define AOC_HASHMAP_RADIX 1024 -int aoc_hash(const char *string) { - int len = strlen(string); - int hash = 1; - for (int i = 0; i < len; i++) { - hash *= 7; - hash += string[i] * 13; - hash %= AOC_HASHMAP_RADIX; - } - - return hash; -} +int aoc_hash(const char *string); typedef struct AocHashmapNode AocHashmapNode; @@ -24,45 +17,8 @@ typedef struct AocHashmap { AocHashmapNode* buckets[AOC_HASHMAP_RADIX]; } AocHashmap; -AocHashmapNode *aoc_hashmap_get(AocHashmap *map, char *key) { - int hash = aoc_hash(key); - AocHashmapNode *node = map->buckets[hash]; - while (node != NULL && strcmp(node->key, key) != 0) node = node->next; - return node; -} +AocHashmapNode *aoc_hashmap_get(AocHashmap *map, char *key); +void aoc_hashmap_put(AocHashmap *map, char *key, void *value); +void aoc_hashmap_free(AocHashmap *map); -void aoc_hashmap_put(AocHashmap *map, char *key, void *value) { - int hash = aoc_hash(key); - AocHashmapNode *node = map->buckets[hash]; - if (node == NULL) { - map->buckets[hash] = malloc(sizeof(AocHashmapNode)); - node = map->buckets[hash]; - node->key = malloc(strlen(key) + 1); - strcpy(node->key, key); - node->value = value; - node->next = NULL; - } else { - while (node->next != NULL) { - node = node->next; - } - node->next = malloc(sizeof(AocHashmapNode)); - node->next->key = malloc(strlen(key) + 1); - strcpy(node->next->key, key); - node->next->value = value; - node->next->next = NULL; - } -} - -void aoc_hashmap_free(AocHashmap *map) { - for (int i = 0; i < AOC_HASHMAP_RADIX; i++) { - AocHashmapNode *node = map->buckets[i]; - while (node != NULL) { - AocHashmapNode *next = node->next; - free(node->key); - free(node->value); - free(node); - node = next; - } - } - free(map); -} +#endif