From e9ee0a88b78c16a860fb1b67670ee99d0f63c640 Mon Sep 17 00:00:00 2001 From: Leonardo Vencovsky Date: Mon, 25 May 2020 22:09:45 -0300 Subject: [PATCH] Added ext folder with some initial parts already using the new API --- Makefile | 5 ++- src/ext/cmc/deque.h | 86 +++++++++++++++++++++++++++++++++++++ src/ext/cmc/hashmap.h | 99 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 189 insertions(+), 1 deletion(-) create mode 100644 src/ext/cmc/deque.h create mode 100644 src/ext/cmc/hashmap.h diff --git a/Makefile b/Makefile index 64ac000..971ce61 100644 --- a/Makefile +++ b/Makefile @@ -2,7 +2,7 @@ .PHONY: all cmc dev sac utl tests FORCE -all: cmc cor dev sac utl tests +all: cmc cor dev ext sac utl tests cmc: FORCE clang-format --style=file -i ./src/cmc/*.h @@ -13,6 +13,9 @@ cor: FORCE dev: FORCE clang-format --style=file -i ./src/dev/*.h +ext: FORCE + clang-format --style=file -i ./src/ext/**/*.h + sac: FORCE clang-format --style=file -i ./src/sac/*.h diff --git a/src/ext/cmc/deque.h b/src/ext/cmc/deque.h new file mode 100644 index 0000000..cf01c47 --- /dev/null +++ b/src/ext/cmc/deque.h @@ -0,0 +1,86 @@ +/** + * deque.h + * + * Creation Date: 25/05/2019 + * + * Authors: + * Leonardo Vencovsky (https://github.com/LeoVen) + * + */ + +/** + * Extensions to CMC + * + * - INIT + */ + +#ifndef CMC_EXT_CMC_DEQUE_H +#define CMC_EXT_CMC_DEQUE_H + +#include "../../cor/params.h" + +/** + * INIT + * + * The part 'INIT' gives a new way of initializing a collection. The collection + * struct is not heap allocated, only its internal structure (nodes, buffers) + */ +#define CMC_EXT_CMC_DEQUE_INIT(BODY) \ + CMC_EXT_CMC_DEQUE_INIT_HEADER(BODY) \ + CMC_EXT_CMC_DEQUE_INIT_SOURCE(BODY) + +#define CMC_EXT_CMC_DEQUE_INIT_HEADER(BODY) \ + CMC_EXT_CMC_DEQUE_INIT_HEADER_(CMC_PARAM_PFX(BODY), CMC_PARAM_SNAME(BODY), \ + CMC_PARAM_V(BODY)) + +#define CMC_EXT_CMC_DEQUE_INIT_SOURCE(BODY) \ + CMC_EXT_CMC_DEQUE_INIT_SOURCE_(CMC_PARAM_PFX(BODY), CMC_PARAM_SNAME(BODY), \ + CMC_PARAM_V(BODY)) + +#define CMC_EXT_CMC_DEQUE_INIT_HEADER_(PFX, SNAME, V) \ + \ + struct SNAME PFX##_init(size_t capacity, struct SNAME##_fval *f_val); \ + struct SNAME PFX##_init_custom( \ + size_t capacity, struct SNAME##_fval *f_val, \ + struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks); + +#define CMC_EXT_CMC_DEQUE_INIT_SOURCE_(PFX, SNAME, V) \ + \ + struct SNAME PFX##_init(size_t capacity, struct SNAME##_fval *f_val) \ + { \ + return PFX##_init_custom(capacity, f_val, NULL, NULL); \ + } \ + \ + struct SNAME PFX##_init_custom( \ + size_t capacity, struct SNAME##_fval *f_val, \ + struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks) \ + { \ + struct SNAME _deque_ = { 0 }; \ + \ + if (capacity < 1) \ + return _deque_; \ + \ + if (!f_val) \ + return _deque_; \ + \ + if (!alloc) \ + alloc = &cmc_alloc_node_default; \ + \ + _deque_.buffer = alloc->calloc(capacity, sizeof(V)); \ + \ + if (!_deque_.buffer) \ + return _deque_; \ + \ + _deque_.capacity = capacity; \ + _deque_.count = 0; \ + _deque_.front = 0; \ + _deque_.back = 0; \ + _deque_.flag = cmc_flags.OK; \ + _deque_.f_val = f_val; \ + _deque_.alloc = alloc; \ + _deque_.callbacks = callbacks; \ + \ + return _deque_; \ + } + +#endif /* CMC_EXT_CMC_DEQUE_H */ diff --git a/src/ext/cmc/hashmap.h b/src/ext/cmc/hashmap.h new file mode 100644 index 0000000..fde1147 --- /dev/null +++ b/src/ext/cmc/hashmap.h @@ -0,0 +1,99 @@ +/** + * hashmap.h + * + * Creation Date: 25/05/2019 + * + * Authors: + * Leonardo Vencovsky (https://github.com/LeoVen) + * + */ + +/** + * Extensions to CMC + * + * - INIT + */ + +#ifndef CMC_EXT_CMC_HASHMAP_H +#define CMC_EXT_CMC_HASHMAP_H + +#include "../../cor/params.h" + +/** + * INIT + * + * The part 'INIT' gives a new way of initializing a collection. The collection + * struct is not heap allocated, only its internal structure (nodes, buffers). + */ +#define CMC_EXT_CMC_HASHMAP_INIT(BODY) \ + CMC_EXT_CMC_HASHMAP_INIT_HEADER(BODY) \ + CMC_EXT_CMC_HASHMAP_INIT_SOURCE(BODY) + +#define CMC_EXT_CMC_HASHMAP_INIT_HEADER(BODY) \ + CMC_EXT_CMC_HASHMAP_INIT_HEADER_(CMC_PARAM_PFX(BODY), \ + CMC_PARAM_SNAME(BODY), CMC_PARAM_V(BODY)) + +#define CMC_EXT_CMC_HASHMAP_INIT_SOURCE(BODY) \ + CMC_EXT_CMC_HASHMAP_INIT_SOURCE_(CMC_PARAM_PFX(BODY), \ + CMC_PARAM_SNAME(BODY), CMC_PARAM_V(BODY)) + +#define CMC_EXT_CMC_HASHMAP_INIT_HEADER_(PFX, SNAME, V) \ + \ + struct SNAME PFX##_init(size_t capacity, double load, \ + struct SNAME##_fkey *f_key, \ + struct SNAME##_fval *f_val); \ + struct SNAME PFX##_init_custom( \ + size_t capacity, double load, struct SNAME##_fkey *f_key, \ + struct SNAME##_fval *f_val, struct cmc_alloc_node *alloc, \ + struct cmc_callbacks *callbacks); + +#define CMC_EXT_CMC_HASHMAP_INIT_SOURCE_(PFX, SNAME, V) \ + \ + struct SNAME PFX##_init(size_t capacity, double load, \ + struct SNAME##_fkey *f_key, \ + struct SNAME##_fval *f_val) \ + { \ + return PFX##_init_custom(capacity, load, f_key, f_val, NULL, NULL); \ + } \ + \ + struct SNAME PFX##_init_custom( \ + size_t capacity, double load, struct SNAME##_fkey *f_key, \ + struct SNAME##_fval *f_val, struct cmc_alloc_node *alloc, \ + struct cmc_callbacks *callbacks) \ + { \ + struct SNAME _map_ = { 0 }; \ + \ + if (capacity == 0 || load <= 0 || load >= 1) \ + return _map_; \ + \ + /* Prevent integer overflow */ \ + if (capacity >= UINTMAX_MAX * load) \ + return _map_; \ + \ + if (!f_key || !f_val) \ + return _map_; \ + \ + size_t real_capacity = PFX##_impl_calculate_size(capacity / load); \ + \ + if (!alloc) \ + alloc = &cmc_alloc_node_default; \ + \ + _map_.buffer = \ + alloc->calloc(real_capacity, sizeof(struct SNAME##_entry)); \ + \ + if (!_map_.buffer) \ + return _map_; \ + \ + _map_.count = 0; \ + _map_.capacity = real_capacity; \ + _map_.load = load; \ + _map_.flag = cmc_flags.OK; \ + _map_.f_key = f_key; \ + _map_.f_val = f_val; \ + _map_.alloc = alloc; \ + _map_.callbacks = callbacks; \ + \ + return _map_; \ + } + +#endif /* CMC_EXT_CMC_HASHMAP_H */