From bd338f2fe3a2cb3ef2cde89e10bbcd385593d60b Mon Sep 17 00:00:00 2001 From: Leonardo Vencovsky Date: Wed, 25 Jan 2023 13:35:16 +0100 Subject: [PATCH] Examples and fixing stuff --- examples/Makefile | 35 +++++++++++++----------------- examples/dfs.c | 2 +- examples/hanoi.c | 8 +++---- src/cmc/cmc/hashbidimap/code.h | 12 +++++----- src/cmc/cmc/hashbidimap/ext/code.h | 2 +- src/cmc/cmc/hashmap/code.h | 8 +++---- src/cmc/cmc/hashmap/ext/code.h | 2 +- src/cmc/cmc/hashmultimap/code.h | 8 +++---- src/cmc/cmc/hashmultiset/code.h | 6 ++--- src/cmc/cmc/hashset/code.h | 6 ++--- 10 files changed, 42 insertions(+), 47 deletions(-) diff --git a/examples/Makefile b/examples/Makefile index 04e73bf..61a643b 100644 --- a/examples/Makefile +++ b/examples/Makefile @@ -2,36 +2,31 @@ CC=gcc CFLAGS=-Wall -Wextra -Werror -O3 INCLUDE=-I ../src -dfs: dfs.c - $(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE) - ./$@.exe - -example: example.c - $(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE) - ./$@.exe - -hanoi: hanoi.c - $(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE) - ./$@.exe - -heapsort: heapsort.c - $(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE) - ./$@.exe - -sieve: sieve.c +ifndef FILE +main: + @echo Please define a FILE parameter to run. Examples: + @echo " dfs" + @echo " example" + @echo " hanoi" + @echo " heapsort" + @echo " ice_cream" + @echo " sieve" +else +$(FILE): $(FILE).c $(CC) $< -o $@.exe $(CFLAGS) $(INCLUDE) ./$@.exe +endif clean: - rm *.exe - rm *.txt + rm *.exe -f + rm *.txt -f memcheck: ifndef FILE $(error FILE parameter must be set first to check for leaks) else - $(CC) $(FILE) -o mem.exe $(CFLAGS) $(INCLUDE) -g + $(CC) $(FILE).c -o mem.exe $(CFLAGS) $(INCLUDE) -g valgrind --leak-check=yes ./mem.exe endif diff --git a/examples/dfs.c b/examples/dfs.c index 474e97d..ef571bb 100644 --- a/examples/dfs.c +++ b/examples/dfs.c @@ -61,7 +61,7 @@ int main(void) printf("Stack:\n"); fprintf(stdout, "[ "); - struct Stack *stack = stk_new(100, &(struct StackFVal){ NULL }); + struct Stack *stack = stk_new(100, &(struct StackFVal){ 0 }); struct BinaryTreeNode *node = tree->root; diff --git a/examples/hanoi.c b/examples/hanoi.c index 8099178..b921796 100644 --- a/examples/hanoi.c +++ b/examples/hanoi.c @@ -11,7 +11,7 @@ struct struct stack *from; struct stack *aux; struct stack *to; -} print_aux = { NULL }; +} print_aux = { 0 }; void print_stacks() { @@ -50,9 +50,9 @@ void hanoi(int n, struct stack *from, struct stack *aux, struct stack *to) int main() { - struct stack from = stk_init(10, &(struct stack_fval){ NULL }); - struct stack to = stk_init(10, &(struct stack_fval){ NULL }); - struct stack aux = stk_init(10, &(struct stack_fval){ NULL }); + struct stack from = stk_init(10, &(struct stack_fval){ 0 }); + struct stack to = stk_init(10, &(struct stack_fval){ 0 }); + struct stack aux = stk_init(10, &(struct stack_fval){ 0 }); for (int i = disks; i >= 1; i--) stk_push(&from, i); diff --git a/src/cmc/cmc/hashbidimap/code.h b/src/cmc/cmc/hashbidimap/code.h index 9e7d90b..998f7f1 100644 --- a/src/cmc/cmc/hashbidimap/code.h +++ b/src/cmc/cmc/hashbidimap/code.h @@ -48,7 +48,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, double load, struct CMC_DE return NULL; /* Prevent integer overflow */ - if (capacity >= UINTMAX_MAX * load) + if (capacity >= (double)UINTMAX_MAX * load) return NULL; if (!f_key || !f_val) @@ -488,7 +488,7 @@ bool CMC_(PFX, _resize)(struct SNAME *_map_, size_t capacity) goto success; /* Prevent integer overflow */ - if (capacity >= UINTMAX_MAX * _map_->load) + if (capacity >= (double)UINTMAX_MAX * _map_->load) { _map_->flag = CMC_FLAG_ERROR; return false; @@ -526,8 +526,8 @@ bool CMC_(PFX, _resize)(struct SNAME *_map_, size_t capacity) if (!e1 || !e2) { /* Prevent the map from freeing the data */ - _new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ NULL }; - _new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ NULL }; + _new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ 0 }; + _new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ 0 }; _map_->alloc->free(_new_map_->buffer); _map_->alloc->free(_new_map_); @@ -544,8 +544,8 @@ bool CMC_(PFX, _resize)(struct SNAME *_map_, size_t capacity) if (_map_->count != _new_map_->count) { /* Prevent the map from freeing the data */ - _new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ NULL }; - _new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ NULL }; + _new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ 0 }; + _new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ 0 }; _map_->alloc->free(_new_map_->buffer); _map_->alloc->free(_new_map_); diff --git a/src/cmc/cmc/hashbidimap/ext/code.h b/src/cmc/cmc/hashbidimap/ext/code.h index 54ab5cb..44db22f 100644 --- a/src/cmc/cmc/hashbidimap/ext/code.h +++ b/src/cmc/cmc/hashbidimap/ext/code.h @@ -48,7 +48,7 @@ struct SNAME CMC_(PFX, _init_custom)(size_t capacity, double load, struct CMC_DE return _map_; /* Prevent integer overflow */ - if (capacity >= UINTMAX_MAX * load) + if (capacity >= (double)UINTMAX_MAX * load) return _map_; if (!f_key || !f_val) diff --git a/src/cmc/cmc/hashmap/code.h b/src/cmc/cmc/hashmap/code.h index 515bff0..abea5c5 100644 --- a/src/cmc/cmc/hashmap/code.h +++ b/src/cmc/cmc/hashmap/code.h @@ -42,7 +42,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, double load, struct CMC_DE return NULL; /* Prevent integer overflow */ - if (capacity >= UINTMAX_MAX * load) + if (capacity >= (double)UINTMAX_MAX * load) return NULL; if (!f_key || !f_val) @@ -448,7 +448,7 @@ bool CMC_(PFX, _resize)(struct SNAME *_map_, size_t capacity) goto success; /* Prevent integer overflow */ - if (capacity >= UINTMAX_MAX * _map_->load) + if (capacity >= (double)UINTMAX_MAX * _map_->load) { _map_->flag = CMC_FLAG_ERROR; return false; @@ -504,8 +504,8 @@ bool CMC_(PFX, _resize)(struct SNAME *_map_, size_t capacity) _new_map_->capacity = tmp_c; /* Prevent the map from freeing the data */ - _new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ NULL }; - _new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ NULL }; + _new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ 0 }; + _new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ 0 }; CMC_(PFX, _free)(_new_map_); diff --git a/src/cmc/cmc/hashmap/ext/code.h b/src/cmc/cmc/hashmap/ext/code.h index 976180a..c9741e1 100644 --- a/src/cmc/cmc/hashmap/ext/code.h +++ b/src/cmc/cmc/hashmap/ext/code.h @@ -48,7 +48,7 @@ struct SNAME CMC_(PFX, _init_custom)(size_t capacity, double load, struct CMC_DE return _map_; /* Prevent integer overflow */ - if (capacity >= UINTMAX_MAX * load) + if (capacity >= (double)UINTMAX_MAX * load) return _map_; if (!f_key || !f_val) diff --git a/src/cmc/cmc/hashmultimap/code.h b/src/cmc/cmc/hashmultimap/code.h index 36a5e41..a38ff0a 100644 --- a/src/cmc/cmc/hashmultimap/code.h +++ b/src/cmc/cmc/hashmultimap/code.h @@ -44,7 +44,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, double load, struct CMC_DE return NULL; /* Prevent integer overflow */ - if (capacity >= UINTMAX_MAX * load) + if (capacity >= (double)UINTMAX_MAX * load) return NULL; if (!f_key || !f_val) @@ -658,7 +658,7 @@ bool CMC_(PFX, _resize)(struct SNAME *_map_, size_t capacity) goto success; /* Prevent integer overflow */ - if (capacity >= UINTMAX_MAX * _map_->load) + if (capacity >= (double)UINTMAX_MAX * _map_->load) { _map_->flag = CMC_FLAG_ERROR; return false; @@ -714,8 +714,8 @@ bool CMC_(PFX, _resize)(struct SNAME *_map_, size_t capacity) _new_map_->capacity = tmp_c; /* Prevent the map from freeing the data */ - _new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ NULL }; - _new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ NULL }; + _new_map_->f_key = &(struct CMC_DEF_FKEY(SNAME)){ 0 }; + _new_map_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ 0 }; CMC_(PFX, _free)(_new_map_); diff --git a/src/cmc/cmc/hashmultiset/code.h b/src/cmc/cmc/hashmultiset/code.h index 344d72f..ad50857 100644 --- a/src/cmc/cmc/hashmultiset/code.h +++ b/src/cmc/cmc/hashmultiset/code.h @@ -41,7 +41,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, double load, struct CMC_DE return NULL; /* Prevent integer overflow */ - if (capacity >= UINTMAX_MAX * load) + if (capacity >= (double)UINTMAX_MAX * load) return NULL; if (!f_val) @@ -443,7 +443,7 @@ bool CMC_(PFX, _resize)(struct SNAME *_set_, size_t capacity) goto success; /* Prevent integer overflow */ - if (capacity >= UINTMAX_MAX * _set_->load) + if (capacity >= (double)UINTMAX_MAX * _set_->load) { _set_->flag = CMC_FLAG_ERROR; return false; @@ -500,7 +500,7 @@ bool CMC_(PFX, _resize)(struct SNAME *_set_, size_t capacity) _new_set_->capacity = tmp_c; /* Prevent the set from freeing the data */ - _new_set_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ NULL }; + _new_set_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ 0 }; CMC_(PFX, _free)(_new_set_); diff --git a/src/cmc/cmc/hashset/code.h b/src/cmc/cmc/hashset/code.h index dafaa8c..82d8fef 100644 --- a/src/cmc/cmc/hashset/code.h +++ b/src/cmc/cmc/hashset/code.h @@ -40,7 +40,7 @@ struct SNAME *CMC_(PFX, _new_custom)(size_t capacity, double load, struct CMC_DE return NULL; /* Prevent integer overflow */ - if (capacity >= UINTMAX_MAX * load) + if (capacity >= (double)UINTMAX_MAX * load) return NULL; if (!f_val) @@ -346,7 +346,7 @@ bool CMC_(PFX, _resize)(struct SNAME *_set_, size_t capacity) goto success; /* Prevent integer overflow */ - if (capacity >= UINTMAX_MAX * _set_->load) + if (capacity >= (double)UINTMAX_MAX * _set_->load) { _set_->flag = CMC_FLAG_ERROR; return false; @@ -400,7 +400,7 @@ bool CMC_(PFX, _resize)(struct SNAME *_set_, size_t capacity) _new_set_->capacity = tmp_c; /* Prevent the set from freeing the data */ - _new_set_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ NULL }; + _new_set_->f_val = &(struct CMC_DEF_FVAL(SNAME)){ 0 }; CMC_(PFX, _free)(_new_set_);