Use header files properly
This commit is contained in:
parent
89789e5191
commit
0579cf7c05
@ -4,6 +4,7 @@
|
|||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "../../lib/aoc.h"
|
#include "../../lib/aoc.h"
|
||||||
|
#include "../../lib/hashmap.h"
|
||||||
|
|
||||||
typedef char Towel[10];
|
typedef char Towel[10];
|
||||||
|
|
||||||
|
@ -11,5 +11,5 @@ if [[ -z $source_file ]] ; then
|
|||||||
exit 1
|
exit 1
|
||||||
fi
|
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)
|
time (cat ../data/$year/$day/input.txt | ./$year/$day/problem)
|
||||||
|
73
c/lib/aoc.c
Normal file
73
c/lib/aoc.c
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#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;
|
||||||
|
}
|
||||||
|
|
79
c/lib/aoc.h
79
c/lib/aoc.h
@ -1,72 +1,11 @@
|
|||||||
#include <stdlib.h>
|
#ifndef AOC_H
|
||||||
#include <stdio.h>
|
#define AOC_H
|
||||||
#include <unistd.h>
|
|
||||||
|
|
||||||
#include "hashmap.h"
|
char *aoc_read_input(void);
|
||||||
|
char *aoc_read_line(void);
|
||||||
#define INITIAL_BUFFER_SIZE 2
|
void aoc_reset_read_line(void);
|
||||||
|
void aoc_free(void);
|
||||||
char *input_buffer;
|
int aoc_line_count(void);
|
||||||
|
int aoc_sort_int(const void *a, const void *b);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
56
c/lib/hashmap.c
Normal file
56
c/lib/hashmap.c
Normal file
@ -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);
|
||||||
|
}
|
@ -1,16 +1,9 @@
|
|||||||
|
#ifndef AOC_HASHMAP
|
||||||
|
#define AOC_HASHMAP
|
||||||
|
|
||||||
#define AOC_HASHMAP_RADIX 1024
|
#define AOC_HASHMAP_RADIX 1024
|
||||||
|
|
||||||
int aoc_hash(const char *string) {
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
typedef struct AocHashmapNode AocHashmapNode;
|
typedef struct AocHashmapNode AocHashmapNode;
|
||||||
|
|
||||||
@ -24,45 +17,8 @@ typedef struct AocHashmap {
|
|||||||
AocHashmapNode* buckets[AOC_HASHMAP_RADIX];
|
AocHashmapNode* buckets[AOC_HASHMAP_RADIX];
|
||||||
} AocHashmap;
|
} AocHashmap;
|
||||||
|
|
||||||
AocHashmapNode *aoc_hashmap_get(AocHashmap *map, char *key) {
|
AocHashmapNode *aoc_hashmap_get(AocHashmap *map, char *key);
|
||||||
int hash = aoc_hash(key);
|
void aoc_hashmap_put(AocHashmap *map, char *key, void *value);
|
||||||
AocHashmapNode *node = map->buckets[hash];
|
void aoc_hashmap_free(AocHashmap *map);
|
||||||
while (node != NULL && strcmp(node->key, key) != 0) node = node->next;
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
void aoc_hashmap_put(AocHashmap *map, char *key, void *value) {
|
#endif
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user