Custom allocation node to hashmap, hashset and heap
This commit is contained in:
1 parent
3b8182d751
commit
f550b0dfa3
3 files changed
+703
-320
No files matched your search
+304
-180
@@ -24,11 +24,43 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.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
|
#ifndef CMC_IMPL_HASHTABLE_STATE
|
||||||
#define CMC_IMPL_HASHTABLE_STATE
|
#define CMC_IMPL_HASHTABLE_STATE
|
||||||
|
|
||||||
@@ -41,42 +73,51 @@ enum cmc_entry_state
|
|||||||
|
|
||||||
#endif /* CMC_IMPL_HASHTABLE_STATE */
|
#endif /* CMC_IMPL_HASHTABLE_STATE */
|
||||||
|
|
||||||
#ifndef CMC_IMPL_HASHTABLE_SETUP
|
#ifndef CMC_IMPL_HASHTABLE_PRIMES
|
||||||
#define CMC_IMPL_HASHTABLE_SETUP
|
#define CMC_IMPL_HASHTABLE_PRIMES
|
||||||
|
|
||||||
static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
|
static const size_t cmc_hashtable_primes[] =
|
||||||
3067, 6143, 12289, 24571, 49157,
|
{53, 97, 191, 383, 769, 1531, 3067, 6143, 12289, 24571, 49157, 98299, 196613, 393209, 786431,
|
||||||
98299, 196613, 393209, 786431,
|
1572869, 3145721, 6291449, 12582917, 25165813, 50331653, 100663291, 201326611, 402653189,
|
||||||
1572869, 3145721, 6291449,
|
805306357, 1610612741, 3221225473, 6442450939, 12884901893, 25769803799, 51539607551,
|
||||||
12582917, 25165813, 50331653,
|
103079215111, 206158430209, 412316860441, 824633720831, 1649267441651, 3298534883309,
|
||||||
100663291, 201326611, 402653189,
|
6597069766657, 13194139533299, 26388279066623, 52776558133303, 105553116266489,
|
||||||
805306357, 1610612741,
|
211106232532969, 422212465066001, 844424930131963, 1688849860263953, 3377699720527861,
|
||||||
3221225473, 6442450939,
|
6755399441055731, 13510798882111483, 27021597764222939, 54043195528445957, 108086391056891903,
|
||||||
12884901893, 25769803799,
|
216172782113783773, 432345564227567621, 864691128455135207, 1729382256910270481,
|
||||||
51539607551, 103079215111,
|
3458764513820540933, 6917529027641081903, 13835058055282163729llu};
|
||||||
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) \
|
#define CMC_GENERATE_HASHMAP(PFX, SNAME, K, V) \
|
||||||
CMC_GENERATE_HASHMAP_HEADER(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) \
|
#define CMC_WRAPGEN_HASHMAP_SOURCE(PFX, SNAME, K, V) \
|
||||||
CMC_GENERATE_HASHMAP_SOURCE(PFX, SNAME, K, V)
|
CMC_GENERATE_HASHMAP_SOURCE(PFX, SNAME, K, V)
|
||||||
|
|
||||||
/* HEADER ********************************************************************/
|
/* -------------------------------------------------------------------------------------------------
|
||||||
#define CMC_GENERATE_HASHMAP_HEADER(PFX, SNAME, K, V) \
|
* Header
|
||||||
\
|
------------------------------------------------------------------------------------------------ */
|
||||||
/* Hashmap Structure */ \
|
#define CMC_GENERATE_HASHMAP_HEADER(PFX, SNAME, K, V) \
|
||||||
struct SNAME \
|
\
|
||||||
{ \
|
/* Hashmap Structure */ \
|
||||||
/* Array of Entries */ \
|
struct SNAME \
|
||||||
struct SNAME##_entry *buffer; \
|
{ \
|
||||||
\
|
/* Array of Entries */ \
|
||||||
/* Current array capacity */ \
|
struct SNAME##_entry *buffer; \
|
||||||
size_t capacity; \
|
\
|
||||||
\
|
/* Current array capacity */ \
|
||||||
/* Current amount of keys */ \
|
size_t capacity; \
|
||||||
size_t count; \
|
\
|
||||||
\
|
/* Current amount of keys */ \
|
||||||
/* Load factor in range (0.0, 1.0) */ \
|
size_t count; \
|
||||||
double load; \
|
\
|
||||||
\
|
/* Load factor in range (0.0, 1.0) */ \
|
||||||
/* Key comparison function */ \
|
double load; \
|
||||||
int (*cmp)(K, K); \
|
\
|
||||||
\
|
/* Key comparison function */ \
|
||||||
/* Key hash function */ \
|
int (*cmp)(K, K); \
|
||||||
size_t (*hash)(K); \
|
\
|
||||||
\
|
/* Key hash function */ \
|
||||||
/* Function that returns an iterator to the start of the hashmap */ \
|
size_t (*hash)(K); \
|
||||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
\
|
||||||
\
|
/* Function that returns an iterator to the start of the hashmap */ \
|
||||||
/* Function that returns an iterator to the end of the hashmap */ \
|
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||||
struct SNAME##_iter (*it_end)(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 \
|
/* Custom allocation functions */ \
|
||||||
{ \
|
struct cmc_alloc_node *alloc; \
|
||||||
/* Entry Key */ \
|
\
|
||||||
K key; \
|
/* Custom callback functions */ \
|
||||||
\
|
struct cmc_callbacks_hashmap *callbacks; \
|
||||||
/* Entry Value */ \
|
}; \
|
||||||
V value; \
|
\
|
||||||
\
|
/* Hashmap Entry */ \
|
||||||
/* The distance of this node to its original position, used by */ \
|
struct SNAME##_entry \
|
||||||
/* robin-hood hashing */ \
|
{ \
|
||||||
size_t dist; \
|
/* Entry Key */ \
|
||||||
\
|
K key; \
|
||||||
/* The sate of this node (DELETED, EMPTY, FILLED) */ \
|
\
|
||||||
enum cmc_entry_state state; \
|
/* Entry Value */ \
|
||||||
}; \
|
V value; \
|
||||||
\
|
\
|
||||||
/* Hashmap Iterator */ \
|
/* The distance of this node to its original position, used by */ \
|
||||||
struct SNAME##_iter \
|
/* robin-hood hashing */ \
|
||||||
{ \
|
size_t dist; \
|
||||||
/* Target hashmap */ \
|
\
|
||||||
struct SNAME *target; \
|
/* The sate of this node (DELETED, EMPTY, FILLED) */ \
|
||||||
\
|
enum cmc_entry_state state; \
|
||||||
/* Cursor's position (index) */ \
|
}; \
|
||||||
size_t cursor; \
|
\
|
||||||
\
|
/* Hashmap Iterator */ \
|
||||||
/* Keeps track of relative index to the iteration of elements */ \
|
struct SNAME##_iter \
|
||||||
size_t index; \
|
{ \
|
||||||
\
|
/* Target hashmap */ \
|
||||||
/* The index of the first element */ \
|
struct SNAME *target; \
|
||||||
size_t first; \
|
\
|
||||||
\
|
/* Cursor's position (index) */ \
|
||||||
/* The index of the last element */ \
|
size_t cursor; \
|
||||||
size_t last; \
|
\
|
||||||
\
|
/* Keeps track of relative index to the iteration of elements */ \
|
||||||
/* If the iterator has reached the start of the iteration */ \
|
size_t index; \
|
||||||
bool start; \
|
\
|
||||||
\
|
/* The index of the first element */ \
|
||||||
/* If the iterator has reached the end of the iteration */ \
|
size_t first; \
|
||||||
bool end; \
|
\
|
||||||
}; \
|
/* The index of the last element */ \
|
||||||
\
|
size_t last; \
|
||||||
/* Collection Functions */ \
|
\
|
||||||
/* Collection Allocation and Deallocation */ \
|
/* If the iterator has reached the start of the iteration */ \
|
||||||
struct SNAME *PFX##_new(size_t capacity, double load, int (*compare)(K, K), \
|
bool start; \
|
||||||
size_t (*hash)(K)); \
|
\
|
||||||
void PFX##_clear(struct SNAME *_map_, void (*deallocator)(K, V)); \
|
/* If the iterator has reached the end of the iteration */ \
|
||||||
void PFX##_free(struct SNAME *_map_, void (*deallocator)(K, V)); \
|
bool end; \
|
||||||
/* 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); \
|
/* Collection Functions */ \
|
||||||
bool PFX##_remove(struct SNAME *_map_, K key, V *out_value); \
|
/* Collection Allocation and Deallocation */ \
|
||||||
/* Element Access */ \
|
struct SNAME *PFX##_new(size_t capacity, double load, int (*compare)(K, K), \
|
||||||
bool PFX##_max(struct SNAME *_map_, K *key, V *value); \
|
size_t (*hash)(K)); \
|
||||||
bool PFX##_min(struct SNAME *_map_, K *key, V *value); \
|
struct SNAME *PFX##_new_custom(size_t capacity, double load, int (*compare)(K, K), \
|
||||||
V PFX##_get(struct SNAME *_map_, K key); \
|
size_t (*hash)(K), struct cmc_alloc_node *alloc, \
|
||||||
V *PFX##_get_ref(struct SNAME *_map_, K key); \
|
struct cmc_callbacks_hashmap *callbacks); \
|
||||||
/* Collection State */ \
|
void PFX##_clear(struct SNAME *_map_, void (*deallocator)(K, V)); \
|
||||||
bool PFX##_contains(struct SNAME *_map_, K key); \
|
void PFX##_free(struct SNAME *_map_, void (*deallocator)(K, V)); \
|
||||||
bool PFX##_empty(struct SNAME *_map_); \
|
/* Customization of Allocation and Callbacks */ \
|
||||||
bool PFX##_full(struct SNAME *_map_); \
|
void PFX##_customize(struct SNAME *_map_, struct cmc_alloc_node *alloc, \
|
||||||
size_t PFX##_count(struct SNAME *_map_); \
|
struct cmc_callbacks_hashmap *callbacks); \
|
||||||
size_t PFX##_capacity(struct SNAME *_map_); \
|
/* Collection Input and Output */ \
|
||||||
double PFX##_load(struct SNAME *_map_); \
|
bool PFX##_insert(struct SNAME *_map_, K key, V value); \
|
||||||
/* Collection Utility */ \
|
bool PFX##_update(struct SNAME *_map_, K key, V new_value, V *old_value); \
|
||||||
bool PFX##_resize(struct SNAME *_map_, size_t capacity); \
|
bool PFX##_remove(struct SNAME *_map_, K key, V *out_value); \
|
||||||
struct SNAME *PFX##_copy_of(struct SNAME *_map_, K (*key_copy_func)(K), \
|
/* Element Access */ \
|
||||||
V (*value_copy_func)(V)); \
|
bool PFX##_max(struct SNAME *_map_, K *key, V *value); \
|
||||||
bool PFX##_equals(struct SNAME *_map1_, struct SNAME *_map2_, \
|
bool PFX##_min(struct SNAME *_map_, K *key, V *value); \
|
||||||
int (*value_comparator)(V, V)); \
|
V PFX##_get(struct SNAME *_map_, K key); \
|
||||||
struct cmc_string PFX##_to_string(struct SNAME *_map_); \
|
V *PFX##_get_ref(struct SNAME *_map_, K key); \
|
||||||
\
|
/* Collection State */ \
|
||||||
/* Iterator Functions */ \
|
bool PFX##_contains(struct SNAME *_map_, K key); \
|
||||||
/* Iterator Allocation and Deallocation */ \
|
bool PFX##_empty(struct SNAME *_map_); \
|
||||||
struct SNAME##_iter *PFX##_iter_new(struct SNAME *target); \
|
bool PFX##_full(struct SNAME *_map_); \
|
||||||
void PFX##_iter_free(struct SNAME##_iter *iter); \
|
size_t PFX##_count(struct SNAME *_map_); \
|
||||||
/* Iterator Initialization */ \
|
size_t PFX##_capacity(struct SNAME *_map_); \
|
||||||
void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target); \
|
double PFX##_load(struct SNAME *_map_); \
|
||||||
/* Iterator State */ \
|
/* Collection Utility */ \
|
||||||
bool PFX##_iter_start(struct SNAME##_iter *iter); \
|
bool PFX##_resize(struct SNAME *_map_, size_t capacity); \
|
||||||
bool PFX##_iter_end(struct SNAME##_iter *iter); \
|
struct SNAME *PFX##_copy_of(struct SNAME *_map_, K (*key_copy_func)(K), \
|
||||||
/* Iterator Movement */ \
|
V (*value_copy_func)(V)); \
|
||||||
void PFX##_iter_to_start(struct SNAME##_iter *iter); \
|
bool PFX##_equals(struct SNAME *_map1_, struct SNAME *_map2_, \
|
||||||
void PFX##_iter_to_end(struct SNAME##_iter *iter); \
|
int (*value_comparator)(V, V)); \
|
||||||
bool PFX##_iter_next(struct SNAME##_iter *iter); \
|
struct cmc_string PFX##_to_string(struct SNAME *_map_); \
|
||||||
bool PFX##_iter_prev(struct SNAME##_iter *iter); \
|
\
|
||||||
bool PFX##_iter_advance(struct SNAME##_iter *iter, size_t steps); \
|
/* Iterator Functions */ \
|
||||||
bool PFX##_iter_rewind(struct SNAME##_iter *iter, size_t steps); \
|
/* Iterator Allocation and Deallocation */ \
|
||||||
bool PFX##_iter_go_to(struct SNAME##_iter *iter, size_t index); \
|
struct SNAME##_iter *PFX##_iter_new(struct SNAME *target); \
|
||||||
/* Iterator Access */ \
|
void PFX##_iter_free(struct SNAME##_iter *iter); \
|
||||||
K PFX##_iter_key(struct SNAME##_iter *iter); \
|
/* Iterator Initialization */ \
|
||||||
V PFX##_iter_value(struct SNAME##_iter *iter); \
|
void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target); \
|
||||||
V *PFX##_iter_rvalue(struct SNAME##_iter *iter); \
|
/* Iterator State */ \
|
||||||
size_t PFX##_iter_index(struct SNAME##_iter *iter); \
|
bool PFX##_iter_start(struct SNAME##_iter *iter); \
|
||||||
\
|
bool PFX##_iter_end(struct SNAME##_iter *iter); \
|
||||||
/* SOURCE ********************************************************************/
|
/* 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) \
|
#define CMC_GENERATE_HASHMAP_SOURCE(PFX, SNAME, K, V) \
|
||||||
\
|
\
|
||||||
/* Implementation Detail Functions */ \
|
/* 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_start(struct SNAME *_map_); \
|
||||||
static struct SNAME##_iter PFX##_impl_it_end(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), \
|
struct SNAME *PFX##_new(size_t capacity, double load, int (*compare)(K, K), size_t (*hash)(K)) \
|
||||||
size_t (*hash)(K)) \
|
|
||||||
{ \
|
{ \
|
||||||
|
struct cmc_alloc_node *alloc = &cmc_alloc_node_default; \
|
||||||
|
\
|
||||||
if (capacity == 0 || load <= 0 || load >= 1) \
|
if (capacity == 0 || load <= 0 || load >= 1) \
|
||||||
return NULL; \
|
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); \
|
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_) \
|
if (!_map_) \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
\
|
\
|
||||||
_map_->buffer = calloc(real_capacity, sizeof(struct SNAME##_entry)); \
|
_map_->buffer = alloc->calloc(real_capacity, sizeof(struct SNAME##_entry)); \
|
||||||
\
|
\
|
||||||
if (!_map_->buffer) \
|
if (!_map_->buffer) \
|
||||||
{ \
|
{ \
|
||||||
free(_map_); \
|
alloc->free(_map_); \
|
||||||
return NULL; \
|
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_start = PFX##_impl_it_start; \
|
||||||
_map_->it_end = PFX##_impl_it_end; \
|
_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_; \
|
return _map_; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
@@ -295,8 +401,18 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
|
|||||||
} \
|
} \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
free(_map_->buffer); \
|
_map_->alloc->free(_map_->buffer); \
|
||||||
free(_map_); \
|
_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) \
|
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) \
|
if (PFX##_capacity(_map_) == capacity) \
|
||||||
return true; \
|
return true; \
|
||||||
\
|
\
|
||||||
if (PFX##_capacity(_map_) > capacity / PFX##_load(_map_)) \
|
if (PFX##_capacity(_map_) > capacity / _map_->load) \
|
||||||
return true; \
|
return true; \
|
||||||
\
|
\
|
||||||
/* Prevent integer overflow */ \
|
/* Prevent integer overflow */ \
|
||||||
if (capacity >= UINTMAX_MAX * PFX##_load(_map_)) \
|
if (capacity >= UINTMAX_MAX * _map_->load) \
|
||||||
return false; \
|
return false; \
|
||||||
\
|
\
|
||||||
/* Calculate required capacity based on the prime numbers */ \
|
/* Calculate required capacity based on the prime numbers */ \
|
||||||
size_t theoretical_size = PFX##_impl_calculate_size(capacity); \
|
size_t theoretical_size = PFX##_impl_calculate_size(capacity); \
|
||||||
\
|
\
|
||||||
/* Not possible to shrink with current available prime numbers */ \
|
/* 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; \
|
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_) \
|
if (!_new_map_) \
|
||||||
return false; \
|
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), \
|
struct SNAME *PFX##_copy_of(struct SNAME *_map_, K (*key_copy_func)(K), \
|
||||||
V (*value_copy_func)(V)) \
|
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) \
|
if (!result) \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
@@ -642,15 +760,21 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
|
|||||||
struct SNAME *m_ = _map_; \
|
struct SNAME *m_ = _map_; \
|
||||||
const char *name = #SNAME; \
|
const char *name = #SNAME; \
|
||||||
\
|
\
|
||||||
snprintf(str.s, cmc_string_len, cmc_string_fmt_hashmap, \
|
int n = snprintf(str.s, cmc_string_len, cmc_string_fmt_hashmap, \
|
||||||
name, m_, m_->buffer, m_->capacity, m_->count, m_->load, m_->cmp, m_->hash); \
|
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; \
|
return str; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
struct SNAME##_iter *PFX##_iter_new(struct SNAME *target) \
|
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) \
|
if (!iter) \
|
||||||
return NULL; \
|
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) \
|
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) \
|
void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target) \
|
||||||
|
|||||||
+168
-46
@@ -23,11 +23,43 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.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
|
#ifndef CMC_IMPL_HASHTABLE_STATE
|
||||||
#define CMC_IMPL_HASHTABLE_STATE
|
#define CMC_IMPL_HASHTABLE_STATE
|
||||||
|
|
||||||
@@ -40,42 +72,51 @@ enum cmc_entry_state
|
|||||||
|
|
||||||
#endif /* CMC_IMPL_HASHTABLE_STATE */
|
#endif /* CMC_IMPL_HASHTABLE_STATE */
|
||||||
|
|
||||||
#ifndef CMC_IMPL_HASHTABLE_SETUP
|
#ifndef CMC_IMPL_HASHTABLE_PRIMES
|
||||||
#define CMC_IMPL_HASHTABLE_SETUP
|
#define CMC_IMPL_HASHTABLE_PRIMES
|
||||||
|
|
||||||
static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
|
static const size_t cmc_hashtable_primes[] =
|
||||||
3067, 6143, 12289, 24571, 49157,
|
{53, 97, 191, 383, 769, 1531, 3067, 6143, 12289, 24571, 49157, 98299, 196613, 393209, 786431,
|
||||||
98299, 196613, 393209, 786431,
|
1572869, 3145721, 6291449, 12582917, 25165813, 50331653, 100663291, 201326611, 402653189,
|
||||||
1572869, 3145721, 6291449,
|
805306357, 1610612741, 3221225473, 6442450939, 12884901893, 25769803799, 51539607551,
|
||||||
12582917, 25165813, 50331653,
|
103079215111, 206158430209, 412316860441, 824633720831, 1649267441651, 3298534883309,
|
||||||
100663291, 201326611, 402653189,
|
6597069766657, 13194139533299, 26388279066623, 52776558133303, 105553116266489,
|
||||||
805306357, 1610612741,
|
211106232532969, 422212465066001, 844424930131963, 1688849860263953, 3377699720527861,
|
||||||
3221225473, 6442450939,
|
6755399441055731, 13510798882111483, 27021597764222939, 54043195528445957, 108086391056891903,
|
||||||
12884901893, 25769803799,
|
216172782113783773, 432345564227567621, 864691128455135207, 1729382256910270481,
|
||||||
51539607551, 103079215111,
|
3458764513820540933, 6917529027641081903, 13835058055282163729llu};
|
||||||
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) \
|
#define CMC_GENERATE_HASHSET(PFX, SNAME, V) \
|
||||||
CMC_GENERATE_HASHSET_HEADER(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) \
|
#define CMC_WRAPGEN_HASHSET_SOURCE(PFX, SNAME, K, V) \
|
||||||
CMC_GENERATE_HASHSET_SOURCE(PFX, SNAME, V)
|
CMC_GENERATE_HASHSET_SOURCE(PFX, SNAME, V)
|
||||||
|
|
||||||
/* HEADER ********************************************************************/
|
/* -------------------------------------------------------------------------------------------------
|
||||||
|
* Header
|
||||||
|
------------------------------------------------------------------------------------------------ */
|
||||||
#define CMC_GENERATE_HASHSET_HEADER(PFX, SNAME, V) \
|
#define CMC_GENERATE_HASHSET_HEADER(PFX, SNAME, V) \
|
||||||
\
|
\
|
||||||
/* Hashset Structure */ \
|
/* 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 */ \
|
/* Function that returns an iterator to the end of the hashset */ \
|
||||||
struct SNAME##_iter (*it_end)(struct SNAME *); \
|
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 \
|
struct SNAME##_entry \
|
||||||
@@ -159,8 +208,14 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
|
|||||||
/* Collection Allocation and Deallocation */ \
|
/* Collection Allocation and Deallocation */ \
|
||||||
struct SNAME *PFX##_new(size_t capacity, double load, int (*compare)(V, V), \
|
struct SNAME *PFX##_new(size_t capacity, double load, int (*compare)(V, V), \
|
||||||
size_t (*hash)(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##_clear(struct SNAME *_set_, void (*deallocator)(V)); \
|
||||||
void PFX##_free(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 */ \
|
/* Collection Input and Output */ \
|
||||||
bool PFX##_insert(struct SNAME *_set_, V element); \
|
bool PFX##_insert(struct SNAME *_set_, V element); \
|
||||||
bool PFX##_remove(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); \
|
bool PFX##_iter_go_to(struct SNAME##_iter *iter, size_t index); \
|
||||||
/* Iterator Access */ \
|
/* Iterator Access */ \
|
||||||
V PFX##_iter_value(struct SNAME##_iter *iter); \
|
V PFX##_iter_value(struct SNAME##_iter *iter); \
|
||||||
size_t PFX##_iter_index(struct SNAME##_iter *iter); \
|
size_t PFX##_iter_index(struct SNAME##_iter *iter);
|
||||||
\
|
|
||||||
/* SOURCE ********************************************************************/
|
/* -------------------------------------------------------------------------------------------------
|
||||||
|
* Source
|
||||||
|
------------------------------------------------------------------------------------------------ */
|
||||||
#define CMC_GENERATE_HASHSET_SOURCE(PFX, SNAME, V) \
|
#define CMC_GENERATE_HASHSET_SOURCE(PFX, SNAME, V) \
|
||||||
\
|
\
|
||||||
/* Implementation Detail Functions */ \
|
/* 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 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) \
|
if (capacity == 0 || load <= 0 || load >= 1) \
|
||||||
return NULL; \
|
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); \
|
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_) \
|
if (!_set_) \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
\
|
\
|
||||||
_set_->buffer = calloc(real_capacity, sizeof(struct SNAME##_entry)); \
|
_set_->buffer = alloc->calloc(real_capacity, sizeof(struct SNAME##_entry)); \
|
||||||
\
|
\
|
||||||
if (!_set_->buffer) \
|
if (!_set_->buffer) \
|
||||||
{ \
|
{ \
|
||||||
free(_set_); \
|
alloc->free(_set_); \
|
||||||
return NULL; \
|
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_start = PFX##_impl_it_start; \
|
||||||
_set_->it_end = PFX##_impl_it_end; \
|
_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_; \
|
return _set_; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
@@ -296,6 +402,16 @@ static const size_t cmc_hashtable_primes[] = {53, 97, 191, 383, 769, 1531,
|
|||||||
free(_set_); \
|
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) \
|
bool PFX##_insert(struct SNAME *_set_, V element) \
|
||||||
{ \
|
{ \
|
||||||
if (PFX##_full(_set_)) \
|
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_; \
|
struct SNAME *s_ = _set_; \
|
||||||
const char *name = #SNAME; \
|
const char *name = #SNAME; \
|
||||||
\
|
\
|
||||||
snprintf(str.s, cmc_string_len, cmc_string_fmt_hashset, \
|
int n = snprintf(str.s, cmc_string_len, cmc_string_fmt_hashset, \
|
||||||
name, s_, s_->buffer, s_->capacity, s_->count, s_->load, s_->cmp, s_->hash); \
|
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; \
|
return str; \
|
||||||
} \
|
} \
|
||||||
|
|||||||
+231
-94
@@ -35,10 +35,45 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.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
|
enum cmc_heap_order
|
||||||
{
|
{
|
||||||
@@ -46,6 +81,37 @@ enum cmc_heap_order
|
|||||||
cmc_min_heap = -1
|
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) \
|
#define CMC_GENERATE_HEAP(PFX, SNAME, V) \
|
||||||
CMC_GENERATE_HEAP_HEADER(PFX, SNAME, V) \
|
CMC_GENERATE_HEAP_HEADER(PFX, SNAME, V) \
|
||||||
CMC_GENERATE_HEAP_SOURCE(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) \
|
#define CMC_WRAPGEN_HEAP_SOURCE(PFX, SNAME, K, V) \
|
||||||
CMC_GENERATE_HEAP_SOURCE(PFX, SNAME, V)
|
CMC_GENERATE_HEAP_SOURCE(PFX, SNAME, V)
|
||||||
|
|
||||||
/* HEADER ********************************************************************/
|
/* -------------------------------------------------------------------------------------------------
|
||||||
#define CMC_GENERATE_HEAP_HEADER(PFX, SNAME, V) \
|
* Header
|
||||||
\
|
------------------------------------------------------------------------------------------------ */
|
||||||
/* Heap Structure */ \
|
#define CMC_GENERATE_HEAP_HEADER(PFX, SNAME, V) \
|
||||||
struct SNAME \
|
\
|
||||||
{ \
|
/* Heap Structure */ \
|
||||||
/* Dynamic array of elements */ \
|
struct SNAME \
|
||||||
V *buffer; \
|
{ \
|
||||||
\
|
/* Dynamic array of elements */ \
|
||||||
/* Current array capacity */ \
|
V *buffer; \
|
||||||
size_t capacity; \
|
\
|
||||||
\
|
/* Current array capacity */ \
|
||||||
/* Current amount of elements in the heap */ \
|
size_t capacity; \
|
||||||
size_t count; \
|
\
|
||||||
\
|
/* Current amount of elements in the heap */ \
|
||||||
/* Heap order (MaxHeap or MinHeap) */ \
|
size_t count; \
|
||||||
enum cmc_heap_order HO; \
|
\
|
||||||
\
|
/* Heap order (MaxHeap or MinHeap) */ \
|
||||||
/* Element comparison function */ \
|
enum cmc_heap_order HO; \
|
||||||
int (*cmp)(V, V); \
|
\
|
||||||
\
|
/* Element comparison function */ \
|
||||||
/* Function that returns an iterator to the start of the heap */ \
|
int (*cmp)(V, V); \
|
||||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
\
|
||||||
\
|
/* Function that returns an iterator to the start of the heap */ \
|
||||||
/* Function that returns an iterator to the end of the heap */ \
|
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||||
struct SNAME##_iter (*it_end)(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 \
|
/* Custom allocation functions */ \
|
||||||
{ \
|
struct cmc_alloc_node *alloc; \
|
||||||
/* Target heap */ \
|
\
|
||||||
struct SNAME *target; \
|
/* Custom callback functions */ \
|
||||||
\
|
struct cmc_callbacks_heap *callbacks; \
|
||||||
/* Cursor's position (index) */ \
|
}; \
|
||||||
size_t cursor; \
|
\
|
||||||
\
|
/* Heap Iterator */ \
|
||||||
/* If the iterator has reached the start of the iteration */ \
|
struct SNAME##_iter \
|
||||||
bool start; \
|
{ \
|
||||||
\
|
/* Target heap */ \
|
||||||
/* If the iterator has reached the end of the iteration */ \
|
struct SNAME *target; \
|
||||||
bool end; \
|
\
|
||||||
}; \
|
/* Cursor's position (index) */ \
|
||||||
\
|
size_t cursor; \
|
||||||
/* Collection Functions */ \
|
\
|
||||||
/* Collection Allocation and Deallocation */ \
|
/* If the iterator has reached the start of the iteration */ \
|
||||||
struct SNAME *PFX##_new(size_t capacity, enum cmc_heap_order HO, int (*compare)(V, V)); \
|
bool start; \
|
||||||
void PFX##_clear(struct SNAME *_heap_, void (*deallocator)(V)); \
|
\
|
||||||
void PFX##_free(struct SNAME *_heap_, void (*deallocator)(V)); \
|
/* If the iterator has reached the end of the iteration */ \
|
||||||
/* Collection Input and Output */ \
|
bool end; \
|
||||||
bool PFX##_insert(struct SNAME *_heap_, V element); \
|
}; \
|
||||||
bool PFX##_remove(struct SNAME *_heap_, V *result); \
|
\
|
||||||
/* Element Access */ \
|
/* Collection Functions */ \
|
||||||
V PFX##_peek(struct SNAME *_heap_); \
|
/* Collection Allocation and Deallocation */ \
|
||||||
/* Collection State */ \
|
struct SNAME *PFX##_new(size_t capacity, enum cmc_heap_order HO, int (*compare)(V, V)); \
|
||||||
bool PFX##_contains(struct SNAME *_heap_, V element); \
|
struct SNAME *PFX##_new_custom(size_t capacity, enum cmc_heap_order HO, int (*compare)(V, V), \
|
||||||
bool PFX##_empty(struct SNAME *_heap_); \
|
struct cmc_alloc_node *alloc, \
|
||||||
bool PFX##_full(struct SNAME *_heap_); \
|
struct cmc_callbacks_heap *callbacks); \
|
||||||
size_t PFX##_count(struct SNAME *_heap_); \
|
void PFX##_clear(struct SNAME *_heap_, void (*deallocator)(V)); \
|
||||||
size_t PFX##_capacity(struct SNAME *_heap_); \
|
void PFX##_free(struct SNAME *_heap_, void (*deallocator)(V)); \
|
||||||
/* Collection Utility */ \
|
/* Customization of Allocation and Callbacks */ \
|
||||||
bool PFX##_resize(struct SNAME *_heap_, size_t capacity); \
|
void PFX##_customize(struct SNAME *_heap_, struct cmc_alloc_node *alloc, \
|
||||||
struct SNAME *PFX##_copy_of(struct SNAME *_heap_, V (*copy_func)(V)); \
|
struct cmc_callbacks_heap *callbacks); \
|
||||||
bool PFX##_equals(struct SNAME *_heap1_, struct SNAME *_heap2_); \
|
/* Collection Input and Output */ \
|
||||||
struct cmc_string PFX##_to_string(struct SNAME *_heap_); \
|
bool PFX##_insert(struct SNAME *_heap_, V element); \
|
||||||
\
|
bool PFX##_remove(struct SNAME *_heap_, V *result); \
|
||||||
/* Iterator Functions */ \
|
/* Element Access */ \
|
||||||
/* Iterator Allocation and Deallocation */ \
|
V PFX##_peek(struct SNAME *_heap_); \
|
||||||
struct SNAME##_iter *PFX##_iter_new(struct SNAME *target); \
|
/* Collection State */ \
|
||||||
void PFX##_iter_free(struct SNAME##_iter *iter); \
|
bool PFX##_contains(struct SNAME *_heap_, V element); \
|
||||||
/* Iterator Initialization */ \
|
bool PFX##_empty(struct SNAME *_heap_); \
|
||||||
void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target); \
|
bool PFX##_full(struct SNAME *_heap_); \
|
||||||
/* Iterator State */ \
|
size_t PFX##_count(struct SNAME *_heap_); \
|
||||||
bool PFX##_iter_start(struct SNAME##_iter *iter); \
|
size_t PFX##_capacity(struct SNAME *_heap_); \
|
||||||
bool PFX##_iter_end(struct SNAME##_iter *iter); \
|
/* Collection Utility */ \
|
||||||
/* Iterator Movement */ \
|
bool PFX##_resize(struct SNAME *_heap_, size_t capacity); \
|
||||||
void PFX##_iter_to_start(struct SNAME##_iter *iter); \
|
struct SNAME *PFX##_copy_of(struct SNAME *_heap_, V (*copy_func)(V)); \
|
||||||
void PFX##_iter_to_end(struct SNAME##_iter *iter); \
|
bool PFX##_equals(struct SNAME *_heap1_, struct SNAME *_heap2_); \
|
||||||
bool PFX##_iter_next(struct SNAME##_iter *iter); \
|
struct cmc_string PFX##_to_string(struct SNAME *_heap_); \
|
||||||
bool PFX##_iter_prev(struct SNAME##_iter *iter); \
|
\
|
||||||
bool PFX##_iter_advance(struct SNAME##_iter *iter, size_t steps); \
|
/* Iterator Functions */ \
|
||||||
bool PFX##_iter_rewind(struct SNAME##_iter *iter, size_t steps); \
|
/* Iterator Allocation and Deallocation */ \
|
||||||
bool PFX##_iter_go_to(struct SNAME##_iter *iter, size_t index); \
|
struct SNAME##_iter *PFX##_iter_new(struct SNAME *target); \
|
||||||
/* Iterator Access */ \
|
void PFX##_iter_free(struct SNAME##_iter *iter); \
|
||||||
V PFX##_iter_value(struct SNAME##_iter *iter); \
|
/* Iterator Initialization */ \
|
||||||
size_t PFX##_iter_index(struct SNAME##_iter *iter); \
|
void PFX##_iter_init(struct SNAME##_iter *iter, struct SNAME *target); \
|
||||||
\
|
/* Iterator State */ \
|
||||||
/* SOURCE ********************************************************************/
|
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) \
|
#define CMC_GENERATE_HEAP_SOURCE(PFX, SNAME, V) \
|
||||||
\
|
\
|
||||||
/* Implementation Detail Functions */ \
|
/* 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 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) \
|
if (capacity < 1) \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
\
|
\
|
||||||
if (HO != cmc_min_heap && HO != cmc_max_heap) \
|
if (HO != cmc_min_heap && HO != cmc_max_heap) \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
\
|
\
|
||||||
struct SNAME *_heap_ = malloc(sizeof(struct SNAME)); \
|
struct SNAME *_heap_ = alloc->malloc(sizeof(struct SNAME)); \
|
||||||
\
|
\
|
||||||
if (!_heap_) \
|
if (!_heap_) \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
\
|
\
|
||||||
_heap_->buffer = calloc(capacity, sizeof(V)); \
|
_heap_->buffer = alloc->calloc(capacity, sizeof(V)); \
|
||||||
\
|
\
|
||||||
if (!_heap_->buffer) \
|
if (!_heap_->buffer) \
|
||||||
{ \
|
{ \
|
||||||
free(_heap_); \
|
alloc->free(_heap_); \
|
||||||
return NULL; \
|
return NULL; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
@@ -181,6 +265,49 @@ enum cmc_heap_order
|
|||||||
_heap_->it_start = PFX##_impl_it_start; \
|
_heap_->it_start = PFX##_impl_it_start; \
|
||||||
_heap_->it_end = PFX##_impl_it_end; \
|
_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_; \
|
return _heap_; \
|
||||||
} \
|
} \
|
||||||
\
|
\
|
||||||
@@ -213,6 +340,16 @@ enum cmc_heap_order
|
|||||||
free(_heap_); \
|
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) \
|
bool PFX##_insert(struct SNAME *_heap_, V element) \
|
||||||
{ \
|
{ \
|
||||||
if (PFX##_full(_heap_)) \
|
if (PFX##_full(_heap_)) \
|
||||||
|
|||||||
Reference in new issue
Block a user