Custom allocation node to hashmap, hashset and heap

This commit is contained in:
Leonardo committed 2019-11-22 00:51:23 -03:00
1 parent 3b8182d751
commit f550b0dfa3
3 files changed
+703 -320

No files matched your search

+304 -180
View File
@@ -24,11 +24,43 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../utl/cmc_string.h"
/* to_string format */
static const char *cmc_string_fmt_hashmap = "%s at %p { buffer:%p, capacity:%" PRIuMAX ", count:%" PRIuMAX ", load:%lf, cmp:%p, hash:%p }";
/* -------------------------------------------------------------------------------------------------
* Core functionalities of the C Macro Collections Library
------------------------------------------------------------------------------------------------ */
#ifndef CMC_CORE_H
#define CMC_CORE_H
/**
* struct cmc_string
*
* Used by all collections when calling the to_string function.
*/
struct cmc_string
{
char s[400];
};
static const size_t cmc_string_len = 400;
/**
* struct cmc_alloc_node
*
* Custom allocation node. Allows collections to use custom allocation functions.
*/
struct cmc_alloc_node
{
void *(*malloc)(size_t);
void *(*calloc)(size_t, size_t);
void *(*realloc)(void *, size_t);
void (*free)(void *);
} cmc_alloc_node_default = {malloc, calloc, realloc, free};
#endif /* CMC_CORE_H */
/* -------------------------------------------------------------------------------------------------
* Hashtable Implementation
------------------------------------------------------------------------------------------------ */
#ifndef CMC_IMPL_HASHTABLE_STATE
#define CMC_IMPL_HASHTABLE_STATE
@@ -41,42 +73,51 @@ enum cmc_entry_state
#endif /* CMC_IMPL_HASHTABLE_STATE */
#ifndef CMC_IMPL_HASHTABLE_SETUP
#define CMC_IMPL_HASHTABLE_SETUP
#ifndef CMC_IMPL_HASHTABLE_PRIMES
#define CMC_IMPL_HASHTABLE_PRIMES
static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
3067, 6143, 12289, 24571, 49157,
98299, 196613, 393209, 786431,
1572869, 3145721, 6291449,
12582917, 25165813, 50331653,
100663291, 201326611, 402653189,
805306357, 1610612741,
3221225473, 6442450939,
12884901893, 25769803799,
51539607551, 103079215111,
206158430209, 412316860441,
824633720831, 1649267441651,
3298534883309, 6597069766657,
13194139533299, 26388279066623,
52776558133303, 105553116266489,
211106232532969, 422212465066001,
844424930131963,
1688849860263953,
3377699720527861,
6755399441055731,
13510798882111483,
27021597764222939,
54043195528445957,
108086391056891903,
216172782113783773,
432345564227567621,
864691128455135207,
1729382256910270481,
3458764513820540933,
6917529027641081903,
13835058055282163729llu};
static const size_t cmc_hashtable_primes[] =
{53, 97, 191, 383, 769, 1531, 3067, 6143, 12289, 24571, 49157, 98299, 196613, 393209, 786431,
1572869, 3145721, 6291449, 12582917, 25165813, 50331653, 100663291, 201326611, 402653189,
805306357, 1610612741, 3221225473, 6442450939, 12884901893, 25769803799, 51539607551,
103079215111, 206158430209, 412316860441, 824633720831, 1649267441651, 3298534883309,
6597069766657, 13194139533299, 26388279066623, 52776558133303, 105553116266489,
211106232532969, 422212465066001, 844424930131963, 1688849860263953, 3377699720527861,
6755399441055731, 13510798882111483, 27021597764222939, 54043195528445957, 108086391056891903,
216172782113783773, 432345564227567621, 864691128455135207, 1729382256910270481,
3458764513820540933, 6917529027641081903, 13835058055282163729llu};
#endif /* CMC_IMPL_HASHTABLE_SETUP */
#endif /* CMC_IMPL_HASHTABLE_PRIMES */
/* -------------------------------------------------------------------------------------------------
* HashMap specific
------------------------------------------------------------------------------------------------ */
/* to_string format */
static const char *cmc_string_fmt_hashmap = "struct %s<%s, %s> "
"at %p { "
"buffer:%p, "
"capacity:%" PRIuMAX ", "
"count:%" PRIuMAX ", "
"load:%lf, "
"cmp:%p, "
"hash:%p "
"alloc:%p, "
"callbacks:%p }";
/**
* Custom HashMap callbacks.
*
* There are two types of callbacks, 'before' and 'after':
* <before|after>_<function_name>
*/
struct cmc_callbacks_hashmap
{
void (*before_clear)(void *);
void (*after_clear)(void *);
void (*before_free)(void *);
void (*after_free)(void *);
// TODO implement all callbacks
};
#define CMC_GENERATE_HASHMAP(PFX, SNAME, K, V) \
CMC_GENERATE_HASHMAP_HEADER(PFX, SNAME, K, V) \
@@ -88,133 +129,149 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
#define CMC_WRAPGEN_HASHMAP_SOURCE(PFX, SNAME, K, V) \
CMC_GENERATE_HASHMAP_SOURCE(PFX, SNAME, K, V)
/* HEADER ********************************************************************/
#define CMC_GENERATE_HASHMAP_HEADER(PFX, SNAME, K, V) \
\
/* Hashmap Structure */ \
struct SNAME \
{ \
/* Array of Entries */ \
struct SNAME##_entry *buffer; \
\
/* Current array capacity */ \
size_t capacity; \
\
/* Current amount of keys */ \
size_t count; \
\
/* Load factor in range (0.0, 1.0) */ \
double load; \
\
/* Key comparison function */ \
int (*cmp)(K, K); \
\
/* Key hash function */ \
size_t (*hash)(K); \
\
/* Function that returns an iterator to the start of the hashmap */ \
struct SNAME##_iter (*it_start)(struct SNAME *); \
\
/* Function that returns an iterator to the end of the hashmap */ \
struct SNAME##_iter (*it_end)(struct SNAME *); \
}; \
\
/* Hashmap Entry */ \
struct SNAME##_entry \
{ \
/* Entry Key */ \
K key; \
\
/* Entry Value */ \
V value; \
\
/* The distance of this node to its original position, used by */ \
/* robin-hood hashing */ \
size_t dist; \
\
/* The sate of this node (DELETED, EMPTY, FILLED) */ \
enum cmc_entry_state state; \
}; \
\
/* Hashmap Iterator */ \
struct SNAME##_iter \
{ \
/* Target hashmap */ \
struct SNAME *target; \
\
/* Cursor's position (index) */ \
size_t cursor; \
\
/* Keeps track of relative index to the iteration of elements */ \
size_t index; \
\
/* The index of the first element */ \
size_t first; \
\
/* The index of the last element */ \
size_t last; \
\
/* If the iterator has reached the start of the iteration */ \
bool start; \
\
/* If the iterator has reached the end of the iteration */ \
bool end; \
}; \
\
/* Collection Functions */ \
/* Collection Allocation and Deallocation */ \
struct SNAME *PFX##_new(size_t capacity, double load, int (*compare)(K, K), \
size_t (*hash)(K)); \
void PFX##_clear(struct SNAME *_map_, void (*deallocator)(K, V)); \
void PFX##_free(struct SNAME *_map_, void (*deallocator)(K, V)); \
/* Collection Input and Output */ \
bool PFX##_insert(struct SNAME *_map_, K key, V value); \
bool PFX##_update(struct SNAME *_map_, K key, V new_value, V *old_value); \
bool PFX##_remove(struct SNAME *_map_, K key, V *out_value); \
/* Element Access */ \
bool PFX##_max(struct SNAME *_map_, K *key, V *value); \
bool PFX##_min(struct SNAME *_map_, K *key, V *value); \
V PFX##_get(struct SNAME *_map_, K key); \
V *PFX##_get_ref(struct SNAME *_map_, K key); \
/* Collection State */ \
bool PFX##_contains(struct SNAME *_map_, K key); \
bool PFX##_empty(struct SNAME *_map_); \
bool PFX##_full(struct SNAME *_map_); \
size_t PFX##_count(struct SNAME *_map_); \
size_t PFX##_capacity(struct SNAME *_map_); \
double PFX##_load(struct SNAME *_map_); \
/* Collection Utility */ \
bool PFX##_resize(struct SNAME *_map_, size_t capacity); \
struct SNAME *PFX##_copy_of(struct SNAME *_map_, K (*key_copy_func)(K), \
V (*value_copy_func)(V)); \
bool PFX##_equals(struct SNAME *_map1_, struct SNAME *_map2_, \
int (*value_comparator)(V, V)); \
struct cmc_string PFX##_to_string(struct SNAME *_map_); \
\
/* Iterator Functions */ \
/* Iterator Allocation and Deallocation */ \
struct SNAME##_iter *PFX##_iter_new(struct SNAME *target); \
void PFX##_iter_free(struct SNAME##_iter *iter); \
/* Iterator Initialization */ \
void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target); \
/* Iterator State */ \
bool PFX##_iter_start(struct SNAME##_iter *iter); \
bool PFX##_iter_end(struct SNAME##_iter *iter); \
/* Iterator Movement */ \
void PFX##_iter_to_start(struct SNAME##_iter *iter); \
void PFX##_iter_to_end(struct SNAME##_iter *iter); \
bool PFX##_iter_next(struct SNAME##_iter *iter); \
bool PFX##_iter_prev(struct SNAME##_iter *iter); \
bool PFX##_iter_advance(struct SNAME##_iter *iter, size_t steps); \
bool PFX##_iter_rewind(struct SNAME##_iter *iter, size_t steps); \
bool PFX##_iter_go_to(struct SNAME##_iter *iter, size_t index); \
/* Iterator Access */ \
K PFX##_iter_key(struct SNAME##_iter *iter); \
V PFX##_iter_value(struct SNAME##_iter *iter); \
V *PFX##_iter_rvalue(struct SNAME##_iter *iter); \
size_t PFX##_iter_index(struct SNAME##_iter *iter); \
\
/* SOURCE ********************************************************************/
/* -------------------------------------------------------------------------------------------------
* Header
------------------------------------------------------------------------------------------------ */
#define CMC_GENERATE_HASHMAP_HEADER(PFX, SNAME, K, V) \
\
/* Hashmap Structure */ \
struct SNAME \
{ \
/* Array of Entries */ \
struct SNAME##_entry *buffer; \
\
/* Current array capacity */ \
size_t capacity; \
\
/* Current amount of keys */ \
size_t count; \
\
/* Load factor in range (0.0, 1.0) */ \
double load; \
\
/* Key comparison function */ \
int (*cmp)(K, K); \
\
/* Key hash function */ \
size_t (*hash)(K); \
\
/* Function that returns an iterator to the start of the hashmap */ \
struct SNAME##_iter (*it_start)(struct SNAME *); \
\
/* Function that returns an iterator to the end of the hashmap */ \
struct SNAME##_iter (*it_end)(struct SNAME *); \
\
/* Custom allocation functions */ \
struct cmc_alloc_node *alloc; \
\
/* Custom callback functions */ \
struct cmc_callbacks_hashmap *callbacks; \
}; \
\
/* Hashmap Entry */ \
struct SNAME##_entry \
{ \
/* Entry Key */ \
K key; \
\
/* Entry Value */ \
V value; \
\
/* The distance of this node to its original position, used by */ \
/* robin-hood hashing */ \
size_t dist; \
\
/* The sate of this node (DELETED, EMPTY, FILLED) */ \
enum cmc_entry_state state; \
}; \
\
/* Hashmap Iterator */ \
struct SNAME##_iter \
{ \
/* Target hashmap */ \
struct SNAME *target; \
\
/* Cursor's position (index) */ \
size_t cursor; \
\
/* Keeps track of relative index to the iteration of elements */ \
size_t index; \
\
/* The index of the first element */ \
size_t first; \
\
/* The index of the last element */ \
size_t last; \
\
/* If the iterator has reached the start of the iteration */ \
bool start; \
\
/* If the iterator has reached the end of the iteration */ \
bool end; \
}; \
\
/* Collection Functions */ \
/* Collection Allocation and Deallocation */ \
struct SNAME *PFX##_new(size_t capacity, double load, int (*compare)(K, K), \
size_t (*hash)(K)); \
struct SNAME *PFX##_new_custom(size_t capacity, double load, int (*compare)(K, K), \
size_t (*hash)(K), struct cmc_alloc_node *alloc, \
struct cmc_callbacks_hashmap *callbacks); \
void PFX##_clear(struct SNAME *_map_, void (*deallocator)(K, V)); \
void PFX##_free(struct SNAME *_map_, void (*deallocator)(K, V)); \
/* Customization of Allocation and Callbacks */ \
void PFX##_customize(struct SNAME *_map_, struct cmc_alloc_node *alloc, \
struct cmc_callbacks_hashmap *callbacks); \
/* Collection Input and Output */ \
bool PFX##_insert(struct SNAME *_map_, K key, V value); \
bool PFX##_update(struct SNAME *_map_, K key, V new_value, V *old_value); \
bool PFX##_remove(struct SNAME *_map_, K key, V *out_value); \
/* Element Access */ \
bool PFX##_max(struct SNAME *_map_, K *key, V *value); \
bool PFX##_min(struct SNAME *_map_, K *key, V *value); \
V PFX##_get(struct SNAME *_map_, K key); \
V *PFX##_get_ref(struct SNAME *_map_, K key); \
/* Collection State */ \
bool PFX##_contains(struct SNAME *_map_, K key); \
bool PFX##_empty(struct SNAME *_map_); \
bool PFX##_full(struct SNAME *_map_); \
size_t PFX##_count(struct SNAME *_map_); \
size_t PFX##_capacity(struct SNAME *_map_); \
double PFX##_load(struct SNAME *_map_); \
/* Collection Utility */ \
bool PFX##_resize(struct SNAME *_map_, size_t capacity); \
struct SNAME *PFX##_copy_of(struct SNAME *_map_, K (*key_copy_func)(K), \
V (*value_copy_func)(V)); \
bool PFX##_equals(struct SNAME *_map1_, struct SNAME *_map2_, \
int (*value_comparator)(V, V)); \
struct cmc_string PFX##_to_string(struct SNAME *_map_); \
\
/* Iterator Functions */ \
/* Iterator Allocation and Deallocation */ \
struct SNAME##_iter *PFX##_iter_new(struct SNAME *target); \
void PFX##_iter_free(struct SNAME##_iter *iter); \
/* Iterator Initialization */ \
void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target); \
/* Iterator State */ \
bool PFX##_iter_start(struct SNAME##_iter *iter); \
bool PFX##_iter_end(struct SNAME##_iter *iter); \
/* Iterator Movement */ \
void PFX##_iter_to_start(struct SNAME##_iter *iter); \
void PFX##_iter_to_end(struct SNAME##_iter *iter); \
bool PFX##_iter_next(struct SNAME##_iter *iter); \
bool PFX##_iter_prev(struct SNAME##_iter *iter); \
bool PFX##_iter_advance(struct SNAME##_iter *iter, size_t steps); \
bool PFX##_iter_rewind(struct SNAME##_iter *iter, size_t steps); \
bool PFX##_iter_go_to(struct SNAME##_iter *iter, size_t index); \
/* Iterator Access */ \
K PFX##_iter_key(struct SNAME##_iter *iter); \
V PFX##_iter_value(struct SNAME##_iter *iter); \
V *PFX##_iter_rvalue(struct SNAME##_iter *iter); \
size_t PFX##_iter_index(struct SNAME##_iter *iter);
/* -------------------------------------------------------------------------------------------------
* Source
------------------------------------------------------------------------------------------------ */
#define CMC_GENERATE_HASHMAP_SOURCE(PFX, SNAME, K, V) \
\
/* Implementation Detail Functions */ \
@@ -223,9 +280,10 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
static struct SNAME##_iter PFX##_impl_it_start(struct SNAME *_map_); \
static struct SNAME##_iter PFX##_impl_it_end(struct SNAME *_map_); \
\
struct SNAME *PFX##_new(size_t capacity, double load, int (*compare)(K, K), \
size_t (*hash)(K)) \
struct SNAME *PFX##_new(size_t capacity, double load, int (*compare)(K, K), size_t (*hash)(K)) \
{ \
struct cmc_alloc_node *alloc = &cmc_alloc_node_default; \
\
if (capacity == 0 || load <= 0 || load >= 1) \
return NULL; \
\
@@ -235,16 +293,16 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
\
size_t real_capacity = PFX##_impl_calculate_size(capacity / load); \
\
struct SNAME *_map_ = malloc(sizeof(struct SNAME)); \
struct SNAME *_map_ = alloc->malloc(sizeof(struct SNAME)); \
\
if (!_map_) \
return NULL; \
\
_map_->buffer = calloc(real_capacity, sizeof(struct SNAME##_entry)); \
_map_->buffer = alloc->calloc(real_capacity, sizeof(struct SNAME##_entry)); \
\
if (!_map_->buffer) \
{ \
free(_map_); \
alloc->free(_map_); \
return NULL; \
} \
\
@@ -257,6 +315,54 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
_map_->it_start = PFX##_impl_it_start; \
_map_->it_end = PFX##_impl_it_end; \
\
_map_->alloc = alloc; \
_map_->callbacks = NULL; \
\
return _map_; \
} \
\
struct SNAME *PFX##_new_custom(size_t capacity, double load, \
int (*compare)(K, K), size_t (*hash)(K), \
struct cmc_alloc_node *alloc, \
struct cmc_callbacks_hashmap *callbacks) \
{ \
if (capacity == 0 || load <= 0 || load >= 1) \
return NULL; \
\
/* Prevent integer overflow */ \
if (capacity >= UINTMAX_MAX * load) \
return NULL; \
\
size_t real_capacity = PFX##_impl_calculate_size(capacity / load); \
\
if (!alloc) \
alloc = &cmc_alloc_node_default; \
\
struct SNAME *_map_ = alloc->malloc(sizeof(struct SNAME)); \
\
if (!_map_) \
return NULL; \
\
_map_->buffer = alloc->calloc(real_capacity, sizeof(struct SNAME##_entry)); \
\
if (!_map_->buffer) \
{ \
alloc->free(_map_); \
return NULL; \
} \
\
_map_->count = 0; \
_map_->capacity = real_capacity; \
_map_->load = load; \
_map_->cmp = compare; \
_map_->hash = hash; \
\
_map_->it_start = PFX##_impl_it_start; \
_map_->it_end = PFX##_impl_it_end; \
\
_map_->alloc = alloc; \
_map_->callbacks = callbacks; \
\
return _map_; \
} \
\
@@ -295,8 +401,18 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
} \
} \
\
free(_map_->buffer); \
free(_map_); \
_map_->alloc->free(_map_->buffer); \
_map_->alloc->free(_map_); \
} \
\
void PFX##_customize(struct SNAME *_map_, struct cmc_alloc_node *alloc, \
struct cmc_callbacks_hashmap *callbacks) \
{ \
if (alloc) \
_map_->alloc = alloc; \
\
if (callbacks) \
_map_->callbacks = callbacks; \
} \
\
bool PFX##_insert(struct SNAME *_map_, K key, V value) \
@@ -517,21 +633,22 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
if (PFX##_capacity(_map_) == capacity) \
return true; \
\
if (PFX##_capacity(_map_) > capacity / PFX##_load(_map_)) \
if (PFX##_capacity(_map_) > capacity / _map_->load) \
return true; \
\
/* Prevent integer overflow */ \
if (capacity >= UINTMAX_MAX * PFX##_load(_map_)) \
if (capacity >= UINTMAX_MAX * _map_->load) \
return false; \
\
/* Calculate required capacity based on the prime numbers */ \
size_t theoretical_size = PFX##_impl_calculate_size(capacity); \
\
/* Not possible to shrink with current available prime numbers */ \
if (theoretical_size < PFX##_count(_map_) / PFX##_load(_map_)) \
if (theoretical_size < _map_->count / _map_->load) \
return false; \
\
struct SNAME *_new_map_ = PFX##_new(capacity, PFX##_load(_map_), _map_->cmp, _map_->hash); \
struct SNAME *_new_map_ = PFX##_new_custom(capacity, _map_->load, _map_->cmp, _map_->hash, \
_map_->alloc, _map_->callbacks); \
\
if (!_new_map_) \
return false; \
@@ -568,7 +685,8 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
struct SNAME *PFX##_copy_of(struct SNAME *_map_, K (*key_copy_func)(K), \
V (*value_copy_func)(V)) \
{ \
struct SNAME *result = PFX##_new(_map_->capacity, _map_->load, _map_->cmp, _map_->hash); \
struct SNAME *result = PFX##_new_custom(_map_->capacity, _map_->load, _map_->cmp, \
_map_->hash, _map_->alloc, _map_->callbacks); \
\
if (!result) \
return NULL; \
@@ -642,15 +760,21 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
struct SNAME *m_ = _map_; \
const char *name = #SNAME; \
\
snprintf(str.s, cmc_string_len, cmc_string_fmt_hashmap, \
name, m_, m_->buffer, m_->capacity, m_->count, m_->load, m_->cmp, m_->hash); \
int n = snprintf(str.s, cmc_string_len, cmc_string_fmt_hashmap, \
name, #K, #V, m_, m_->buffer, m_->capacity, m_->count, \
m_->load, m_->cmp, m_->hash, m_->alloc, m_->callbacks); \
\
if (n < 0 || n == cmc_string_len) \
return (struct cmc_string){0}; \
\
str.s[n] = '\0'; \
\
return str; \
} \
\
struct SNAME##_iter *PFX##_iter_new(struct SNAME *target) \
{ \
struct SNAME##_iter *iter = malloc(sizeof(struct SNAME##_iter)); \
struct SNAME##_iter *iter = target->alloc->malloc(sizeof(struct SNAME##_iter)); \
\
if (!iter) \
return NULL; \
@@ -662,7 +786,7 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
\
void PFX##_iter_free(struct SNAME##_iter *iter) \
{ \
free(iter); \
iter->target->alloc->free(iter); \
} \
\
void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target) \
+168 -46
View File
@@ -23,11 +23,43 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../utl/cmc_string.h"
/* to_string format */
static const char *cmc_string_fmt_hashset = "%s at %p { buffer:%p, capacity:%" PRIuMAX ", count:%" PRIuMAX ", load:%lf, cmp:%p, hash:%p }";
/* -------------------------------------------------------------------------------------------------
* Core functionalities of the C Macro Collections Library
------------------------------------------------------------------------------------------------ */
#ifndef CMC_CORE_H
#define CMC_CORE_H
/**
* struct cmc_string
*
* Used by all collections when calling the to_string function.
*/
struct cmc_string
{
char s[400];
};
static const size_t cmc_string_len = 400;
/**
* struct cmc_alloc_node
*
* Custom allocation node. Allows collections to use custom allocation functions.
*/
struct cmc_alloc_node
{
void *(*malloc)(size_t);
void *(*calloc)(size_t, size_t);
void *(*realloc)(void *, size_t);
void (*free)(void *);
} cmc_alloc_node_default = {malloc, calloc, realloc, free};
#endif /* CMC_CORE_H */
/* -------------------------------------------------------------------------------------------------
* Hashtable Implementation
------------------------------------------------------------------------------------------------ */
#ifndef CMC_IMPL_HASHTABLE_STATE
#define CMC_IMPL_HASHTABLE_STATE
@@ -40,42 +72,51 @@ enum cmc_entry_state
#endif /* CMC_IMPL_HASHTABLE_STATE */
#ifndef CMC_IMPL_HASHTABLE_SETUP
#define CMC_IMPL_HASHTABLE_SETUP
#ifndef CMC_IMPL_HASHTABLE_PRIMES
#define CMC_IMPL_HASHTABLE_PRIMES
static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
3067, 6143, 12289, 24571, 49157,
98299, 196613, 393209, 786431,
1572869, 3145721, 6291449,
12582917, 25165813, 50331653,
100663291, 201326611, 402653189,
805306357, 1610612741,
3221225473, 6442450939,
12884901893, 25769803799,
51539607551, 103079215111,
206158430209, 412316860441,
824633720831, 1649267441651,
3298534883309, 6597069766657,
13194139533299, 26388279066623,
52776558133303, 105553116266489,
211106232532969, 422212465066001,
844424930131963,
1688849860263953,
3377699720527861,
6755399441055731,
13510798882111483,
27021597764222939,
54043195528445957,
108086391056891903,
216172782113783773,
432345564227567621,
864691128455135207,
1729382256910270481,
3458764513820540933,
6917529027641081903,
13835058055282163729llu};
static const size_t cmc_hashtable_primes[] =
{53, 97, 191, 383, 769, 1531, 3067, 6143, 12289, 24571, 49157, 98299, 196613, 393209, 786431,
1572869, 3145721, 6291449, 12582917, 25165813, 50331653, 100663291, 201326611, 402653189,
805306357, 1610612741, 3221225473, 6442450939, 12884901893, 25769803799, 51539607551,
103079215111, 206158430209, 412316860441, 824633720831, 1649267441651, 3298534883309,
6597069766657, 13194139533299, 26388279066623, 52776558133303, 105553116266489,
211106232532969, 422212465066001, 844424930131963, 1688849860263953, 3377699720527861,
6755399441055731, 13510798882111483, 27021597764222939, 54043195528445957, 108086391056891903,
216172782113783773, 432345564227567621, 864691128455135207, 1729382256910270481,
3458764513820540933, 6917529027641081903, 13835058055282163729llu};
#endif /* CMC_IMPL_HASHTABLE_SETUP */
#endif /* CMC_IMPL_HASHTABLE_PRIMES */
/* -------------------------------------------------------------------------------------------------
* HashSet specific
------------------------------------------------------------------------------------------------ */
/* to_string format */
static const char *cmc_string_fmt_hashset = "struct %s<%s> "
"at %p { "
"buffer:%p, "
"capacity:%" PRIuMAX ", "
"count:%" PRIuMAX ", "
"load:%lf, "
"cmp:%p, "
"hash:%p "
"alloc:%p, "
"callbacks: %p }";
/**
* Custom HashSet callbacks.
*
* There are two types of callbacks, 'before' and 'after':
* <before|after>_<function_name>
*/
struct cmc_callbacks_hashset
{
void (*before_clear)(void *);
void (*after_clear)(void *);
void (*before_free)(void *);
void (*after_free)(void *);
// TODO implement all callbacks
};
#define CMC_GENERATE_HASHSET(PFX, SNAME, V) \
CMC_GENERATE_HASHSET_HEADER(PFX, SNAME, V) \
@@ -87,7 +128,9 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
#define CMC_WRAPGEN_HASHSET_SOURCE(PFX, SNAME, K, V) \
CMC_GENERATE_HASHSET_SOURCE(PFX, SNAME, V)
/* HEADER ********************************************************************/
/* -------------------------------------------------------------------------------------------------
* Header
------------------------------------------------------------------------------------------------ */
#define CMC_GENERATE_HASHSET_HEADER(PFX, SNAME, V) \
\
/* Hashset Structure */ \
@@ -116,6 +159,12 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
\
/* Function that returns an iterator to the end of the hashset */ \
struct SNAME##_iter (*it_end)(struct SNAME *); \
\
/* Custom allocation functions */ \
struct cmc_alloc_node *alloc; \
\
/* Custom callback functions */ \
struct cmc_callbacks_hashset *callbacks; \
}; \
\
struct SNAME##_entry \
@@ -159,8 +208,14 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
/* Collection Allocation and Deallocation */ \
struct SNAME *PFX##_new(size_t capacity, double load, int (*compare)(V, V), \
size_t (*hash)(V)); \
struct SNAME *PFX##_new_custom(size_t capacity, double load, int (*compare)(V, V), \
size_t (*hash)(V), struct cmc_alloc_node *alloc, \
struct cmc_callbacks_hashset *callbacks); \
void PFX##_clear(struct SNAME *_set_, void (*deallocator)(V)); \
void PFX##_free(struct SNAME *_set_, void (*deallocator)(V)); \
/* Customization of Allocation and Callbacks */ \
void PFX##_customize(struct SNAME *_set_, struct cmc_alloc_node *alloc, \
struct cmc_callbacks_hashset *callbacks); \
/* Collection Input and Output */ \
bool PFX##_insert(struct SNAME *_set_, V element); \
bool PFX##_remove(struct SNAME *_set_, V element); \
@@ -210,9 +265,11 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
bool PFX##_iter_go_to(struct SNAME##_iter *iter, size_t index); \
/* Iterator Access */ \
V PFX##_iter_value(struct SNAME##_iter *iter); \
size_t PFX##_iter_index(struct SNAME##_iter *iter); \
\
/* SOURCE ********************************************************************/
size_t PFX##_iter_index(struct SNAME##_iter *iter);
/* -------------------------------------------------------------------------------------------------
* Source
------------------------------------------------------------------------------------------------ */
#define CMC_GENERATE_HASHSET_SOURCE(PFX, SNAME, V) \
\
/* Implementation Detail Functions */ \
@@ -223,6 +280,8 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
\
struct SNAME *PFX##_new(size_t capacity, double load, int (*compare)(V, V), size_t (*hash)(V)) \
{ \
struct cmc_alloc_node *alloc = &cmc_alloc_node_default; \
\
if (capacity == 0 || load <= 0 || load >= 1) \
return NULL; \
\
@@ -232,16 +291,16 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
\
size_t real_capacity = PFX##_impl_calculate_size(capacity / load); \
\
struct SNAME *_set_ = malloc(sizeof(struct SNAME)); \
struct SNAME *_set_ = alloc->malloc(sizeof(struct SNAME)); \
\
if (!_set_) \
return NULL; \
\
_set_->buffer = calloc(real_capacity, sizeof(struct SNAME##_entry)); \
_set_->buffer = alloc->calloc(real_capacity, sizeof(struct SNAME##_entry)); \
\
if (!_set_->buffer) \
{ \
free(_set_); \
alloc->free(_set_); \
return NULL; \
} \
\
@@ -254,6 +313,53 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
_set_->it_start = PFX##_impl_it_start; \
_set_->it_end = PFX##_impl_it_end; \
\
_set_->alloc = alloc; \
_set_->callbacks = NULL; \
\
return _set_; \
} \
\
struct SNAME *PFX##_new_custom(size_t capacity, double load, int (*compare)(V, V), \
size_t (*hash)(V), struct cmc_alloc_node *alloc, \
struct cmc_callbacks_hashset *callbacks) \
{ \
if (capacity == 0 || load <= 0 || load >= 1) \
return NULL; \
\
/* Prevent integer overflow */ \
if (capacity >= UINTMAX_MAX * load) \
return NULL; \
\
size_t real_capacity = PFX##_impl_calculate_size(capacity / load); \
\
if (!alloc) \
alloc = &cmc_alloc_node_default; \
\
struct SNAME *_set_ = alloc->malloc(sizeof(struct SNAME)); \
\
if (!_set_) \
return NULL; \
\
_set_->buffer = alloc->calloc(real_capacity, sizeof(struct SNAME##_entry)); \
\
if (!_set_->buffer) \
{ \
alloc->free(_set_); \
return NULL; \
} \
\
_set_->count = 0; \
_set_->capacity = real_capacity; \
_set_->load = load; \
_set_->cmp = compare; \
_set_->hash = hash; \
\
_set_->it_start = PFX##_impl_it_start; \
_set_->it_end = PFX##_impl_it_end; \
\
_set_->alloc = alloc; \
_set_->callbacks = callbacks; \
\
return _set_; \
} \
\
@@ -296,6 +402,16 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
free(_set_); \
} \
\
void PFX##_customize(struct SNAME *_set_, struct cmc_alloc_node *alloc, \
struct cmc_callbacks_hashset *callbacks) \
{ \
if (alloc) \
_set_->alloc = alloc; \
\
if (callbacks) \
_set_->callbacks = callbacks; \
} \
\
bool PFX##_insert(struct SNAME *_set_, V element) \
{ \
if (PFX##_full(_set_)) \
@@ -556,8 +672,14 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
struct SNAME *s_ = _set_; \
const char *name = #SNAME; \
\
snprintf(str.s, cmc_string_len, cmc_string_fmt_hashset, \
name, s_, s_->buffer, s_->capacity, s_->count, s_->load, s_->cmp, s_->hash); \
int n = snprintf(str.s, cmc_string_len, cmc_string_fmt_hashset, \
name, #V, s_, s_->buffer, s_->capacity, s_->count, \
s_->load, s_->cmp, s_->hash, s_->alloc, s_->callbacks); \
\
if (n < 0 || n == cmc_string_len) \
return (struct cmc_string){0}; \
\
str.s[n] = '\0'; \
\
return str; \
} \
+231 -94
View File
@@ -35,10 +35,45 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "../utl/cmc_string.h"
/* to_string format */
static const char *cmc_string_fmt_heap = "%s at %p { buffer:%p, capacity:%" PRIuMAX ", count:%" PRIuMAX ", type:%s, cmp:%p }";
/* -------------------------------------------------------------------------------------------------
* Core functionalities of the C Macro Collections Library
------------------------------------------------------------------------------------------------ */
#ifndef CMC_CORE_H
#define CMC_CORE_H
/**
* struct cmc_string
*
* Used by all collections when calling the to_string function.
*/
struct cmc_string
{
char s[400];
};
static const size_t cmc_string_len = 400;
/**
* struct cmc_alloc_node
*
* Custom allocation node. Allows collections to use custom allocation functions.
*/
struct cmc_alloc_node
{
void *(*malloc)(size_t);
void *(*calloc)(size_t, size_t);
void *(*realloc)(void *, size_t);
void (*free)(void *);
} cmc_alloc_node_default = {malloc, calloc, realloc, free};
#endif /* CMC_CORE_H */
/* -------------------------------------------------------------------------------------------------
* Heap Implementation
------------------------------------------------------------------------------------------------ */
#ifndef CMC_IMPL_HEAP_ORDER
#define CMC_IMPL_HEAP_ORDER
enum cmc_heap_order
{
@@ -46,6 +81,37 @@ enum cmc_heap_order
cmc_min_heap = -1
};
#endif /* CMC_IMPL_HEAP_ORDER */
/* -------------------------------------------------------------------------------------------------
* Heap specific
------------------------------------------------------------------------------------------------ */
/* to_string format */
static const char *cmc_string_fmt_heap = "struct %s<%s> "
"at %p { "
"buffer:%p, "
"capacity:%" PRIuMAX ", "
"count:%" PRIuMAX ", "
"type:%s, "
"cmp:%p "
"alloc:%p, "
"callbacks: %p}";
/**
* Custom Heap callbacks.
*
* There are two types of callbacks, 'before' and 'after':
* <before|after>_<function_name>
*/
struct cmc_callbacks_heap
{
void (*before_clear)(void *);
void (*after_clear)(void *);
void (*before_free)(void *);
void (*after_free)(void *);
// TODO implement all callbacks
};
#define CMC_GENERATE_HEAP(PFX, SNAME, V) \
CMC_GENERATE_HEAP_HEADER(PFX, SNAME, V) \
CMC_GENERATE_HEAP_SOURCE(PFX, SNAME, V)
@@ -56,94 +122,110 @@ enum cmc_heap_order
#define CMC_WRAPGEN_HEAP_SOURCE(PFX, SNAME, K, V) \
CMC_GENERATE_HEAP_SOURCE(PFX, SNAME, V)
/* HEADER ********************************************************************/
#define CMC_GENERATE_HEAP_HEADER(PFX, SNAME, V) \
\
/* Heap Structure */ \
struct SNAME \
{ \
/* Dynamic array of elements */ \
V *buffer; \
\
/* Current array capacity */ \
size_t capacity; \
\
/* Current amount of elements in the heap */ \
size_t count; \
\
/* Heap order (MaxHeap or MinHeap) */ \
enum cmc_heap_order HO; \
\
/* Element comparison function */ \
int (*cmp)(V, V); \
\
/* Function that returns an iterator to the start of the heap */ \
struct SNAME##_iter (*it_start)(struct SNAME *); \
\
/* Function that returns an iterator to the end of the heap */ \
struct SNAME##_iter (*it_end)(struct SNAME *); \
}; \
\
/* Heap Iterator */ \
struct SNAME##_iter \
{ \
/* Target heap */ \
struct SNAME *target; \
\
/* Cursor's position (index) */ \
size_t cursor; \
\
/* If the iterator has reached the start of the iteration */ \
bool start; \
\
/* If the iterator has reached the end of the iteration */ \
bool end; \
}; \
\
/* Collection Functions */ \
/* Collection Allocation and Deallocation */ \
struct SNAME *PFX##_new(size_t capacity, enum cmc_heap_order HO, int (*compare)(V, V)); \
void PFX##_clear(struct SNAME *_heap_, void (*deallocator)(V)); \
void PFX##_free(struct SNAME *_heap_, void (*deallocator)(V)); \
/* Collection Input and Output */ \
bool PFX##_insert(struct SNAME *_heap_, V element); \
bool PFX##_remove(struct SNAME *_heap_, V *result); \
/* Element Access */ \
V PFX##_peek(struct SNAME *_heap_); \
/* Collection State */ \
bool PFX##_contains(struct SNAME *_heap_, V element); \
bool PFX##_empty(struct SNAME *_heap_); \
bool PFX##_full(struct SNAME *_heap_); \
size_t PFX##_count(struct SNAME *_heap_); \
size_t PFX##_capacity(struct SNAME *_heap_); \
/* Collection Utility */ \
bool PFX##_resize(struct SNAME *_heap_, size_t capacity); \
struct SNAME *PFX##_copy_of(struct SNAME *_heap_, V (*copy_func)(V)); \
bool PFX##_equals(struct SNAME *_heap1_, struct SNAME *_heap2_); \
struct cmc_string PFX##_to_string(struct SNAME *_heap_); \
\
/* Iterator Functions */ \
/* Iterator Allocation and Deallocation */ \
struct SNAME##_iter *PFX##_iter_new(struct SNAME *target); \
void PFX##_iter_free(struct SNAME##_iter *iter); \
/* Iterator Initialization */ \
void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target); \
/* Iterator State */ \
bool PFX##_iter_start(struct SNAME##_iter *iter); \
bool PFX##_iter_end(struct SNAME##_iter *iter); \
/* Iterator Movement */ \
void PFX##_iter_to_start(struct SNAME##_iter *iter); \
void PFX##_iter_to_end(struct SNAME##_iter *iter); \
bool PFX##_iter_next(struct SNAME##_iter *iter); \
bool PFX##_iter_prev(struct SNAME##_iter *iter); \
bool PFX##_iter_advance(struct SNAME##_iter *iter, size_t steps); \
bool PFX##_iter_rewind(struct SNAME##_iter *iter, size_t steps); \
bool PFX##_iter_go_to(struct SNAME##_iter *iter, size_t index); \
/* Iterator Access */ \
V PFX##_iter_value(struct SNAME##_iter *iter); \
size_t PFX##_iter_index(struct SNAME##_iter *iter); \
\
/* SOURCE ********************************************************************/
/* -------------------------------------------------------------------------------------------------
* Header
------------------------------------------------------------------------------------------------ */
#define CMC_GENERATE_HEAP_HEADER(PFX, SNAME, V) \
\
/* Heap Structure */ \
struct SNAME \
{ \
/* Dynamic array of elements */ \
V *buffer; \
\
/* Current array capacity */ \
size_t capacity; \
\
/* Current amount of elements in the heap */ \
size_t count; \
\
/* Heap order (MaxHeap or MinHeap) */ \
enum cmc_heap_order HO; \
\
/* Element comparison function */ \
int (*cmp)(V, V); \
\
/* Function that returns an iterator to the start of the heap */ \
struct SNAME##_iter (*it_start)(struct SNAME *); \
\
/* Function that returns an iterator to the end of the heap */ \
struct SNAME##_iter (*it_end)(struct SNAME *); \
\
/* Custom allocation functions */ \
struct cmc_alloc_node *alloc; \
\
/* Custom callback functions */ \
struct cmc_callbacks_heap *callbacks; \
}; \
\
/* Heap Iterator */ \
struct SNAME##_iter \
{ \
/* Target heap */ \
struct SNAME *target; \
\
/* Cursor's position (index) */ \
size_t cursor; \
\
/* If the iterator has reached the start of the iteration */ \
bool start; \
\
/* If the iterator has reached the end of the iteration */ \
bool end; \
}; \
\
/* Collection Functions */ \
/* Collection Allocation and Deallocation */ \
struct SNAME *PFX##_new(size_t capacity, enum cmc_heap_order HO, int (*compare)(V, V)); \
struct SNAME *PFX##_new_custom(size_t capacity, enum cmc_heap_order HO, int (*compare)(V, V), \
struct cmc_alloc_node *alloc, \
struct cmc_callbacks_heap *callbacks); \
void PFX##_clear(struct SNAME *_heap_, void (*deallocator)(V)); \
void PFX##_free(struct SNAME *_heap_, void (*deallocator)(V)); \
/* Customization of Allocation and Callbacks */ \
void PFX##_customize(struct SNAME *_heap_, struct cmc_alloc_node *alloc, \
struct cmc_callbacks_heap *callbacks); \
/* Collection Input and Output */ \
bool PFX##_insert(struct SNAME *_heap_, V element); \
bool PFX##_remove(struct SNAME *_heap_, V *result); \
/* Element Access */ \
V PFX##_peek(struct SNAME *_heap_); \
/* Collection State */ \
bool PFX##_contains(struct SNAME *_heap_, V element); \
bool PFX##_empty(struct SNAME *_heap_); \
bool PFX##_full(struct SNAME *_heap_); \
size_t PFX##_count(struct SNAME *_heap_); \
size_t PFX##_capacity(struct SNAME *_heap_); \
/* Collection Utility */ \
bool PFX##_resize(struct SNAME *_heap_, size_t capacity); \
struct SNAME *PFX##_copy_of(struct SNAME *_heap_, V (*copy_func)(V)); \
bool PFX##_equals(struct SNAME *_heap1_, struct SNAME *_heap2_); \
struct cmc_string PFX##_to_string(struct SNAME *_heap_); \
\
/* Iterator Functions */ \
/* Iterator Allocation and Deallocation */ \
struct SNAME##_iter *PFX##_iter_new(struct SNAME *target); \
void PFX##_iter_free(struct SNAME##_iter *iter); \
/* Iterator Initialization */ \
void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target); \
/* Iterator State */ \
bool PFX##_iter_start(struct SNAME##_iter *iter); \
bool PFX##_iter_end(struct SNAME##_iter *iter); \
/* Iterator Movement */ \
void PFX##_iter_to_start(struct SNAME##_iter *iter); \
void PFX##_iter_to_end(struct SNAME##_iter *iter); \
bool PFX##_iter_next(struct SNAME##_iter *iter); \
bool PFX##_iter_prev(struct SNAME##_iter *iter); \
bool PFX##_iter_advance(struct SNAME##_iter *iter, size_t steps); \
bool PFX##_iter_rewind(struct SNAME##_iter *iter, size_t steps); \
bool PFX##_iter_go_to(struct SNAME##_iter *iter, size_t index); \
/* Iterator Access */ \
V PFX##_iter_value(struct SNAME##_iter *iter); \
size_t PFX##_iter_index(struct SNAME##_iter *iter);
/* -------------------------------------------------------------------------------------------------
* Source
------------------------------------------------------------------------------------------------ */
#define CMC_GENERATE_HEAP_SOURCE(PFX, SNAME, V) \
\
/* Implementation Detail Functions */ \
@@ -154,22 +236,24 @@ enum cmc_heap_order
\
struct SNAME *PFX##_new(size_t capacity, enum cmc_heap_order HO, int (*compare)(V, V)) \
{ \
struct cmc_alloc_node *alloc = &cmc_alloc_node_default; \
\
if (capacity < 1) \
return NULL; \
\
if (HO != cmc_min_heap && HO != cmc_max_heap) \
return NULL; \
\
struct SNAME *_heap_ = malloc(sizeof(struct SNAME)); \
struct SNAME *_heap_ = alloc->malloc(sizeof(struct SNAME)); \
\
if (!_heap_) \
return NULL; \
\
_heap_->buffer = calloc(capacity, sizeof(V)); \
_heap_->buffer = alloc->calloc(capacity, sizeof(V)); \
\
if (!_heap_->buffer) \
{ \
free(_heap_); \
alloc->free(_heap_); \
return NULL; \
} \
\
@@ -181,6 +265,49 @@ enum cmc_heap_order
_heap_->it_start = PFX##_impl_it_start; \
_heap_->it_end = PFX##_impl_it_end; \
\
_heap_->alloc = alloc; \
_heap_->callbacks = NULL; \
\
return _heap_; \
} \
\
struct SNAME *PFX##_new_custom(size_t capacity, enum cmc_heap_order HO, int (*compare)(V, V), \
struct cmc_alloc_node *alloc, \
struct cmc_callbacks_heap *callbacks) \
{ \
if (capacity < 1) \
return NULL; \
\
if (HO != cmc_min_heap && HO != cmc_max_heap) \
return NULL; \
\
if (!alloc) \
alloc = &cmc_alloc_node_default; \
\
struct SNAME *_heap_ = alloc->malloc(sizeof(struct SNAME)); \
\
if (!_heap_) \
return NULL; \
\
_heap_->buffer = alloc->calloc(capacity, sizeof(V)); \
\
if (!_heap_->buffer) \
{ \
alloc->free(_heap_); \
return NULL; \
} \
\
_heap_->capacity = capacity; \
_heap_->count = 0; \
_heap_->HO = HO; \
_heap_->cmp = compare; \
\
_heap_->it_start = PFX##_impl_it_start; \
_heap_->it_end = PFX##_impl_it_end; \
\
_heap_->alloc = alloc; \
_heap_->callbacks = callbacks; \
\
return _heap_; \
} \
\
@@ -213,6 +340,16 @@ enum cmc_heap_order
free(_heap_); \
} \
\
void PFX##_customize(struct SNAME *_heap_, struct cmc_alloc_node *alloc, \
struct cmc_callbacks_heap *callbacks) \
{ \
if (alloc) \
_heap_->alloc = alloc; \
\
if (callbacks) \
_heap_->callbacks = callbacks; \
} \
\
bool PFX##_insert(struct SNAME *_heap_, V element) \
{ \
if (PFX##_full(_heap_)) \