Added cor folder and cmc_callbacks is now common to all collections
Now every file includes core.h and hashtable-based collections include hashtable.h The definition of cmc_heap_order has been kept inside heap.h because it is the only collection to use it
This commit is contained in:
1 parent
d7f44e0dbe
commit
0089abc84a
20 files changed
+291
-1407
No files matched your search
@@ -36,6 +36,7 @@
|
||||
* __examples__ - Examples using the C Macro Collections Library
|
||||
* __src__ - All headers part of the C Macro Collections Library
|
||||
* __cmc__ - The main C Macro Collections Library
|
||||
* __cor__ - Core includes in the C Macro Collections Library
|
||||
* __dev__ - The main C Macro Collections Library for development (containing logging)
|
||||
* __sac__ - Statically Allocated Collections
|
||||
* __utl__ - Utility like ForEach macros, logging, etc
|
||||
|
||||
+7
-126
@@ -26,119 +26,15 @@
|
||||
#ifndef CMC_BIDIMAP_H
|
||||
#define CMC_BIDIMAP_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* enum cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
#include "../cor/core.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* Hashtable Implementation
|
||||
* ------------------------------------------------------------------------- */
|
||||
#ifndef CMC_IMPL_ENTRY_DELETED
|
||||
#define CMC_IMPL_ENTRY_DELETED
|
||||
|
||||
#define CMC_ENTRY_DELETED ((void *)1)
|
||||
|
||||
#endif /* CMC_IMPL_ENTRY_DELETED */
|
||||
|
||||
#ifndef CMC_IMPL_HASHTABLE_PRIMES
|
||||
#define CMC_IMPL_HASHTABLE_PRIMES
|
||||
|
||||
// clang-format off
|
||||
static const size_t cmc_hashtable_primes[] = {
|
||||
/* < 1e3 */
|
||||
53, 97, 191, 383, 769,
|
||||
/* < 1e4 */
|
||||
1531, 3067, 6143,
|
||||
/* < 1e5 */
|
||||
12289, 24571, 49157, 98299,
|
||||
/* < 1e6 */
|
||||
196613, 393209, 786431,
|
||||
/* < 1e7 */
|
||||
1572869, 3145721, 6291449,
|
||||
/* < 1e8 */
|
||||
12582917, 25165813, 50331653,
|
||||
/* < 1e9 */
|
||||
100663291, 201326611, 402653189, 805306357,
|
||||
/* < 1e10 */
|
||||
1610612741, 3221225473, 6442450939,
|
||||
/* < 1e11 */
|
||||
12884901893, 25769803799, 51539607551,
|
||||
/* < 1e12 */
|
||||
103079215111, 206158430209, 412316860441, 824633720831,
|
||||
/* < 1e13 */
|
||||
1649267441651, 3298534883309, 6597069766657,
|
||||
/* < 1e14 */
|
||||
13194139533299, 26388279066623, 52776558133303,
|
||||
/* < 1e15 */
|
||||
105553116266489, 211106232532969, 422212465066001, 844424930131963,
|
||||
/* < 1e16 */
|
||||
1688849860263953, 3377699720527861, 6755399441055731,
|
||||
/* < 1e17 */
|
||||
13510798882111483, 27021597764222939, 54043195528445957,
|
||||
/* < 1e18 */
|
||||
108086391056891903, 216172782113783773, 432345564227567621, 864691128455135207,
|
||||
/* < 1e19 */
|
||||
1729382256910270481, 3458764513820540933, 6917529027641081903,
|
||||
/* < 1e20 */
|
||||
13835058055282163729llu,
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
#endif /* CMC_IMPL_HASHTABLE_PRIMES */
|
||||
#include "../cor/hashtable.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* Bidimap Specific
|
||||
@@ -157,21 +53,6 @@ static const char *cmc_string_fmt_bidimap = "struct %s<%s, %s> "
|
||||
"alloc:%p, "
|
||||
"callbacks:%p }";
|
||||
|
||||
/**
|
||||
* Custom BidiMap callbacks.
|
||||
*
|
||||
* There are two types of callbacks, 'before' and 'after':
|
||||
* <before|after>_<function_name>
|
||||
*/
|
||||
struct cmc_callbacks_bidimap
|
||||
{
|
||||
void (*before_clear)(void *);
|
||||
void (*after_clear)(void *);
|
||||
void (*before_free)(void *);
|
||||
void (*after_free)(void *);
|
||||
// TODO implement all callbacks
|
||||
};
|
||||
|
||||
#define CMC_GENERATE_BIDIMAP(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_BIDIMAP_HEADER(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_BIDIMAP_SOURCE(PFX, SNAME, K, V)
|
||||
@@ -218,7 +99,7 @@ struct cmc_callbacks_bidimap
|
||||
struct cmc_alloc_node *alloc; \
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks_bidimap *callbacks; \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
\
|
||||
/* Methods */ \
|
||||
/* Function that returns an iterator to the start of the bidimap */ \
|
||||
@@ -323,12 +204,12 @@ struct cmc_callbacks_bidimap
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, double load, struct SNAME##_ftab_key *f_key, \
|
||||
struct SNAME##_ftab_val *f_val, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_bidimap *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
void PFX##_clear(struct SNAME *_map_); \
|
||||
void PFX##_free(struct SNAME *_map_); \
|
||||
/* Customization of Allocation and Callbacks */ \
|
||||
void PFX##_customize(struct SNAME *_map_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_bidimap *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_insert(struct SNAME *_map_, K key, V value); \
|
||||
bool PFX##_update_key(struct SNAME *_map_, K key, K new_key); \
|
||||
@@ -453,7 +334,7 @@ struct cmc_callbacks_bidimap
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, double load, struct SNAME##_ftab_key *f_key, \
|
||||
struct SNAME##_ftab_val *f_val, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_bidimap *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (capacity == 0 || load <= 0 || load >= 1) \
|
||||
return NULL; \
|
||||
@@ -537,7 +418,7 @@ struct cmc_callbacks_bidimap
|
||||
} \
|
||||
\
|
||||
void PFX##_customize(struct SNAME *_map_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_bidimap *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (alloc) \
|
||||
_map_->alloc = alloc; \
|
||||
|
||||
+6
-74
@@ -31,63 +31,10 @@
|
||||
#ifndef CMC_DEQUE_H
|
||||
#define CMC_DEQUE_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* enum cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
#include "../cor/core.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* Deque Specific
|
||||
@@ -105,21 +52,6 @@ static const char *cmc_string_fmt_deque = "struct %s<%s> "
|
||||
"alloc:%p, "
|
||||
"callbacks:%p }";
|
||||
|
||||
/**
|
||||
* Custom Deque callbacks.
|
||||
*
|
||||
* There are two types of callbacks, 'before' and 'after':
|
||||
* <before|after>_<function_name>
|
||||
*/
|
||||
struct cmc_callbacks_deque
|
||||
{
|
||||
void (*before_clear)(void *);
|
||||
void (*after_clear)(void *);
|
||||
void (*before_free)(void *);
|
||||
void (*after_free)(void *);
|
||||
// TODO implement all callbacks
|
||||
};
|
||||
|
||||
#define CMC_GENERATE_DEQUE(PFX, SNAME, V) \
|
||||
CMC_GENERATE_DEQUE_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_DEQUE_SOURCE(PFX, SNAME, V)
|
||||
@@ -162,7 +94,7 @@ struct cmc_callbacks_deque
|
||||
struct cmc_alloc_node *alloc; \
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks_deque *callbacks; \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the deque */ \
|
||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||
@@ -216,12 +148,12 @@ struct cmc_callbacks_deque
|
||||
struct SNAME *PFX##_new(size_t capacity, struct SNAME##_ftab_val *f_val); \
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks_deque *callbacks); \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks); \
|
||||
void PFX##_clear(struct SNAME *_deque_); \
|
||||
void PFX##_free(struct SNAME *_deque_); \
|
||||
/* Customization of Allocation and Callbacks */ \
|
||||
void PFX##_customize(struct SNAME *_deque_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_deque *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_push_front(struct SNAME *_deque_, V element); \
|
||||
bool PFX##_push_back(struct SNAME *_deque_, V element); \
|
||||
@@ -314,7 +246,7 @@ struct cmc_callbacks_deque
|
||||
\
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks_deque *callbacks) \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (capacity < 1) \
|
||||
return NULL; \
|
||||
@@ -390,7 +322,7 @@ struct cmc_callbacks_deque
|
||||
} \
|
||||
\
|
||||
void PFX##_customize(struct SNAME *_deque_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_deque *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (alloc) \
|
||||
_deque_->alloc = alloc; \
|
||||
|
||||
+7
-131
@@ -19,124 +19,15 @@
|
||||
#ifndef CMC_HASHMAP_H
|
||||
#define CMC_HASHMAP_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* enum cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
#include "../cor/core.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* Hashtable Implementation
|
||||
* ------------------------------------------------------------------------- */
|
||||
#ifndef CMC_IMPL_HASHTABLE_STATE
|
||||
#define CMC_IMPL_HASHTABLE_STATE
|
||||
|
||||
enum cmc_entry_state
|
||||
{
|
||||
CMC_ES_DELETED = -1,
|
||||
CMC_ES_EMPTY = 0,
|
||||
CMC_ES_FILLED = 1
|
||||
};
|
||||
|
||||
#endif /* CMC_IMPL_HASHTABLE_STATE */
|
||||
|
||||
#ifndef CMC_IMPL_HASHTABLE_PRIMES
|
||||
#define CMC_IMPL_HASHTABLE_PRIMES
|
||||
|
||||
// clang-format off
|
||||
static const size_t cmc_hashtable_primes[] = {
|
||||
/* < 1e3 */
|
||||
53, 97, 191, 383, 769,
|
||||
/* < 1e4 */
|
||||
1531, 3067, 6143,
|
||||
/* < 1e5 */
|
||||
12289, 24571, 49157, 98299,
|
||||
/* < 1e6 */
|
||||
196613, 393209, 786431,
|
||||
/* < 1e7 */
|
||||
1572869, 3145721, 6291449,
|
||||
/* < 1e8 */
|
||||
12582917, 25165813, 50331653,
|
||||
/* < 1e9 */
|
||||
100663291, 201326611, 402653189, 805306357,
|
||||
/* < 1e10 */
|
||||
1610612741, 3221225473, 6442450939,
|
||||
/* < 1e11 */
|
||||
12884901893, 25769803799, 51539607551,
|
||||
/* < 1e12 */
|
||||
103079215111, 206158430209, 412316860441, 824633720831,
|
||||
/* < 1e13 */
|
||||
1649267441651, 3298534883309, 6597069766657,
|
||||
/* < 1e14 */
|
||||
13194139533299, 26388279066623, 52776558133303,
|
||||
/* < 1e15 */
|
||||
105553116266489, 211106232532969, 422212465066001, 844424930131963,
|
||||
/* < 1e16 */
|
||||
1688849860263953, 3377699720527861, 6755399441055731,
|
||||
/* < 1e17 */
|
||||
13510798882111483, 27021597764222939, 54043195528445957,
|
||||
/* < 1e18 */
|
||||
108086391056891903, 216172782113783773, 432345564227567621, 864691128455135207,
|
||||
/* < 1e19 */
|
||||
1729382256910270481, 3458764513820540933, 6917529027641081903,
|
||||
/* < 1e20 */
|
||||
13835058055282163729llu,
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
#endif /* CMC_IMPL_HASHTABLE_PRIMES */
|
||||
#include "../cor/hashtable.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* HashMap Specific
|
||||
@@ -154,21 +45,6 @@ static const char *cmc_string_fmt_hashmap = "struct %s<%s, %s> "
|
||||
"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) \
|
||||
CMC_GENERATE_HASHMAP_SOURCE(PFX, SNAME, K, V)
|
||||
@@ -212,7 +88,7 @@ struct cmc_callbacks_hashmap
|
||||
struct cmc_alloc_node *alloc; \
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks_hashmap *callbacks; \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the hashmap */ \
|
||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||
@@ -315,12 +191,12 @@ struct cmc_callbacks_hashmap
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, double load, struct SNAME##_ftab_key *f_key, \
|
||||
struct SNAME##_ftab_val *f_val, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_hashmap *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
void PFX##_clear(struct SNAME *_map_); \
|
||||
void PFX##_free(struct SNAME *_map_); \
|
||||
/* Customization of Allocation and Callbacks */ \
|
||||
void PFX##_customize(struct SNAME *_map_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_hashmap *callbacks); \
|
||||
struct cmc_callbacks *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); \
|
||||
@@ -429,7 +305,7 @@ struct cmc_callbacks_hashmap
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, double load, struct SNAME##_ftab_key *f_key, \
|
||||
struct SNAME##_ftab_val *f_val, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_hashmap *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (capacity == 0 || load <= 0 || load >= 1) \
|
||||
return NULL; \
|
||||
@@ -522,7 +398,7 @@ struct cmc_callbacks_hashmap
|
||||
} \
|
||||
\
|
||||
void PFX##_customize(struct SNAME *_map_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_hashmap *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (alloc) \
|
||||
_map_->alloc = alloc; \
|
||||
|
||||
+9
-134
@@ -19,124 +19,15 @@
|
||||
#ifndef CMC_HASHSET_H
|
||||
#define CMC_HASHSET_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* enum cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
#include "../cor/core.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* Hashtable Implementation
|
||||
* ------------------------------------------------------------------------- */
|
||||
#ifndef CMC_IMPL_HASHTABLE_STATE
|
||||
#define CMC_IMPL_HASHTABLE_STATE
|
||||
|
||||
enum cmc_entry_state
|
||||
{
|
||||
CMC_ES_DELETED = -1,
|
||||
CMC_ES_EMPTY = 0,
|
||||
CMC_ES_FILLED = 1
|
||||
};
|
||||
|
||||
#endif /* CMC_IMPL_HASHTABLE_STATE */
|
||||
|
||||
#ifndef CMC_IMPL_HASHTABLE_PRIMES
|
||||
#define CMC_IMPL_HASHTABLE_PRIMES
|
||||
|
||||
// clang-format off
|
||||
static const size_t cmc_hashtable_primes[] = {
|
||||
/* < 1e3 */
|
||||
53, 97, 191, 383, 769,
|
||||
/* < 1e4 */
|
||||
1531, 3067, 6143,
|
||||
/* < 1e5 */
|
||||
12289, 24571, 49157, 98299,
|
||||
/* < 1e6 */
|
||||
196613, 393209, 786431,
|
||||
/* < 1e7 */
|
||||
1572869, 3145721, 6291449,
|
||||
/* < 1e8 */
|
||||
12582917, 25165813, 50331653,
|
||||
/* < 1e9 */
|
||||
100663291, 201326611, 402653189, 805306357,
|
||||
/* < 1e10 */
|
||||
1610612741, 3221225473, 6442450939,
|
||||
/* < 1e11 */
|
||||
12884901893, 25769803799, 51539607551,
|
||||
/* < 1e12 */
|
||||
103079215111, 206158430209, 412316860441, 824633720831,
|
||||
/* < 1e13 */
|
||||
1649267441651, 3298534883309, 6597069766657,
|
||||
/* < 1e14 */
|
||||
13194139533299, 26388279066623, 52776558133303,
|
||||
/* < 1e15 */
|
||||
105553116266489, 211106232532969, 422212465066001, 844424930131963,
|
||||
/* < 1e16 */
|
||||
1688849860263953, 3377699720527861, 6755399441055731,
|
||||
/* < 1e17 */
|
||||
13510798882111483, 27021597764222939, 54043195528445957,
|
||||
/* < 1e18 */
|
||||
108086391056891903, 216172782113783773, 432345564227567621, 864691128455135207,
|
||||
/* < 1e19 */
|
||||
1729382256910270481, 3458764513820540933, 6917529027641081903,
|
||||
/* < 1e20 */
|
||||
13835058055282163729llu,
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
#endif /* CMC_IMPL_HASHTABLE_PRIMES */
|
||||
#include "../cor/hashtable.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* HashSet specific
|
||||
@@ -153,21 +44,6 @@ static const char *cmc_string_fmt_hashset = "struct %s<%s> "
|
||||
"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) \
|
||||
CMC_GENERATE_HASHSET_SOURCE(PFX, SNAME, V)
|
||||
@@ -208,7 +84,7 @@ struct cmc_callbacks_hashset
|
||||
struct cmc_alloc_node *alloc; \
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks_hashset *callbacks; \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the hashset */ \
|
||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||
@@ -281,15 +157,14 @@ struct cmc_callbacks_hashset
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
struct SNAME *PFX##_new(size_t capacity, double load, \
|
||||
struct SNAME##_ftab_val *f_val); \
|
||||
struct SNAME *PFX##_new_custom(size_t capacity, double load, \
|
||||
struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_hashset *callbacks); \
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, double load, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks); \
|
||||
void PFX##_clear(struct SNAME *_set_); \
|
||||
void PFX##_free(struct SNAME *_set_); \
|
||||
/* Customization of Allocation and Callbacks */ \
|
||||
void PFX##_customize(struct SNAME *_set_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_hashset *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_insert(struct SNAME *_set_, V element); \
|
||||
bool PFX##_remove(struct SNAME *_set_, V element); \
|
||||
@@ -404,7 +279,7 @@ struct cmc_callbacks_hashset
|
||||
\
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, double load, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks_hashset *callbacks) \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (capacity == 0 || load <= 0 || load >= 1) \
|
||||
return NULL; \
|
||||
@@ -490,7 +365,7 @@ struct cmc_callbacks_hashset
|
||||
} \
|
||||
\
|
||||
void PFX##_customize(struct SNAME *_set_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_hashset *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (alloc) \
|
||||
_set_->alloc = alloc; \
|
||||
|
||||
+13
-79
@@ -30,78 +30,27 @@
|
||||
#ifndef CMC_HEAP_H
|
||||
#define CMC_HEAP_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* enum cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
#include "../cor/core.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* Heap Implementation
|
||||
* ------------------------------------------------------------------------- */
|
||||
#ifndef CMC_IMPL_HEAP_ORDER
|
||||
#define CMC_IMPL_HEAP_ORDER
|
||||
|
||||
/**
|
||||
* enum cmc_heap_order
|
||||
*
|
||||
* Defines the two possible heaps:
|
||||
* - Max Heap has the greatest element at the top
|
||||
* - Min Heap has the smallest element at the top
|
||||
*/
|
||||
enum cmc_heap_order
|
||||
{
|
||||
cmc_max_heap = 1,
|
||||
cmc_min_heap = -1
|
||||
};
|
||||
|
||||
#endif /* CMC_IMPL_HEAP_ORDER */
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* Heap specific
|
||||
* ------------------------------------------------------------------------- */
|
||||
@@ -117,21 +66,6 @@ static const char *cmc_string_fmt_heap = "struct %s<%s> "
|
||||
"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)
|
||||
@@ -172,7 +106,7 @@ struct cmc_callbacks_heap
|
||||
struct cmc_alloc_node *alloc; \
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks_heap *callbacks; \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the heap */ \
|
||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||
@@ -226,12 +160,12 @@ struct cmc_callbacks_heap
|
||||
struct SNAME *PFX##_new_custom(size_t capacity, enum cmc_heap_order HO, \
|
||||
struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_heap *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
void PFX##_clear(struct SNAME *_heap_); \
|
||||
void PFX##_free(struct SNAME *_heap_); \
|
||||
/* Customization of Allocation and Callbacks */ \
|
||||
void PFX##_customize(struct SNAME *_heap_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_heap *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_insert(struct SNAME *_heap_, V element); \
|
||||
bool PFX##_remove(struct SNAME *_heap_, V *result); \
|
||||
@@ -326,7 +260,7 @@ struct cmc_callbacks_heap
|
||||
struct SNAME *PFX##_new_custom(size_t capacity, enum cmc_heap_order HO, \
|
||||
struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_heap *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (capacity < 1) \
|
||||
return NULL; \
|
||||
@@ -397,7 +331,7 @@ struct cmc_callbacks_heap
|
||||
} \
|
||||
\
|
||||
void PFX##_customize(struct SNAME *_heap_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_heap *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (alloc) \
|
||||
_heap_->alloc = alloc; \
|
||||
|
||||
+6
-76
@@ -21,63 +21,10 @@
|
||||
#ifndef CMC_INTERVALHEAP_H
|
||||
#define CMC_INTERVALHEAP_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* enum cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
#include "../cor/core.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* IntervalHeap specific
|
||||
@@ -94,21 +41,6 @@ static const char *cmc_string_fmt_intervalheap = "struct %s<%s> "
|
||||
"alloc:%p, "
|
||||
"callbacks:%p }";
|
||||
|
||||
/**
|
||||
* Custom IntervalHeap callbacks.
|
||||
*
|
||||
* There are two types of callbacks, 'before' and 'after':
|
||||
* <before|after>_<function_name>
|
||||
*/
|
||||
struct cmc_callbacks_intervalheap
|
||||
{
|
||||
void (*before_clear)(void *);
|
||||
void (*after_clear)(void *);
|
||||
void (*before_free)(void *);
|
||||
void (*after_free)(void *);
|
||||
// TODO implement all callbacks
|
||||
};
|
||||
|
||||
#define CMC_GENERATE_INTERVALHEAP(PFX, SNAME, V) \
|
||||
CMC_GENERATE_INTERVALHEAP_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_INTERVALHEAP_SOURCE(PFX, SNAME, V)
|
||||
@@ -155,7 +87,7 @@ struct cmc_callbacks_intervalheap
|
||||
struct cmc_alloc_node *alloc; \
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks_intervalheap *callbacks; \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
}; \
|
||||
\
|
||||
/* Heap Node */ \
|
||||
@@ -209,13 +141,12 @@ struct cmc_callbacks_intervalheap
|
||||
struct SNAME *PFX##_new(size_t capacity, struct SNAME##_ftab_val *f_val); \
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_intervalheap *callbacks); \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks); \
|
||||
void PFX##_clear(struct SNAME *_heap_); \
|
||||
void PFX##_free(struct SNAME *_heap_); \
|
||||
/* Customization of Allocation and Callbacks */ \
|
||||
void PFX##_customize(struct SNAME *_heap_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_intervalheap *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_insert(struct SNAME *_heap_, V element); \
|
||||
bool PFX##_remove_max(struct SNAME *_heap_); \
|
||||
@@ -317,8 +248,7 @@ struct cmc_callbacks_intervalheap
|
||||
\
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_intervalheap *callbacks) \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (capacity == 0 || capacity == UINT64_MAX) \
|
||||
return NULL; \
|
||||
@@ -393,7 +323,7 @@ struct cmc_callbacks_intervalheap
|
||||
} \
|
||||
\
|
||||
void PFX##_customize(struct SNAME *_heap_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_intervalheap *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (alloc) \
|
||||
_heap_->alloc = alloc; \
|
||||
|
||||
+8
-76
@@ -28,63 +28,10 @@
|
||||
#ifndef CMC_LINKEDLIST_H
|
||||
#define CMC_LINKEDLIST_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* enum cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
#include "../cor/core.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* LinkedList specific
|
||||
@@ -100,21 +47,6 @@ static const char *cmc_string_fmt_linkedlist = "struct %s<%s> "
|
||||
"alloc:%p, "
|
||||
"callbacks:%p }";
|
||||
|
||||
/**
|
||||
* Custom LinkedList callbacks.
|
||||
*
|
||||
* There are two types of callbacks, 'before' and 'after':
|
||||
* <before|after>_<function_name>
|
||||
*/
|
||||
struct cmc_callbacks_linkedlist
|
||||
{
|
||||
void (*before_clear)(void *);
|
||||
void (*after_clear)(void *);
|
||||
void (*before_free)(void *);
|
||||
void (*after_free)(void *);
|
||||
// TODO implement all callbacks
|
||||
};
|
||||
|
||||
#define CMC_GENERATE_LINKEDLIST(PFX, SNAME, V) \
|
||||
CMC_GENERATE_LINKEDLIST_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_LINKEDLIST_SOURCE(PFX, SNAME, V)
|
||||
@@ -152,7 +84,7 @@ struct cmc_callbacks_linkedlist
|
||||
struct cmc_alloc_node *alloc; \
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks_linkedlist *callbacks; \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the list */ \
|
||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||
@@ -218,14 +150,14 @@ struct cmc_callbacks_linkedlist
|
||||
/* Collection Functions */ \
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
struct SNAME *PFX##_new(struct SNAME##_ftab_val *f_val); \
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
struct SNAME##_ftab_val *f_val, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_linkedlist *callbacks); \
|
||||
struct SNAME *PFX##_new_custom(struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
void PFX##_clear(struct SNAME *_list_); \
|
||||
void PFX##_free(struct SNAME *_list_); \
|
||||
/* Customization of Allocation and Callbacks */ \
|
||||
void PFX##_customize(struct SNAME *_list_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_linkedlist *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_push_front(struct SNAME *_list_, V element); \
|
||||
bool PFX##_push_at(struct SNAME *_list_, V element, size_t index); \
|
||||
@@ -328,7 +260,7 @@ struct cmc_callbacks_linkedlist
|
||||
\
|
||||
struct SNAME *PFX##_new_custom(struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_linkedlist *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (!f_val) \
|
||||
return NULL; \
|
||||
@@ -386,7 +318,7 @@ struct cmc_callbacks_linkedlist
|
||||
} \
|
||||
\
|
||||
void PFX##_customize(struct SNAME *_list_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_linkedlist *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (alloc) \
|
||||
_list_->alloc = alloc; \
|
||||
|
||||
+6
-74
@@ -43,63 +43,10 @@
|
||||
#ifndef CMC_LIST_H
|
||||
#define CMC_LIST_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* enum cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
#include "../cor/core.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* List specific
|
||||
@@ -115,21 +62,6 @@ static const char *cmc_string_fmt_list = "struct %s<%s> "
|
||||
"alloc:%p, "
|
||||
"callbacks:%p }";
|
||||
|
||||
/**
|
||||
* Custom List callbacks.
|
||||
*
|
||||
* There are two types of callbacks, 'before' and 'after':
|
||||
* <before|after>_<function_name>
|
||||
*/
|
||||
struct cmc_callbacks_list
|
||||
{
|
||||
void (*before_clear)(void *);
|
||||
void (*after_clear)(void *);
|
||||
void (*before_free)(void *);
|
||||
void (*after_free)(void *);
|
||||
// TODO implement all callbacks
|
||||
};
|
||||
|
||||
#define CMC_GENERATE_LIST(PFX, SNAME, V) \
|
||||
CMC_GENERATE_LIST_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_LIST_SOURCE(PFX, SNAME, V)
|
||||
@@ -173,7 +105,7 @@ struct cmc_callbacks_list
|
||||
struct cmc_alloc_node *alloc; \
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks_list *callbacks; \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
}; \
|
||||
\
|
||||
/* Value struct function table */ \
|
||||
@@ -219,12 +151,12 @@ struct cmc_callbacks_list
|
||||
struct SNAME *PFX##_new(size_t capacity, struct SNAME##_ftab_val *f_val); \
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks_list *callbacks); \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks); \
|
||||
void PFX##_clear(struct SNAME *_list_); \
|
||||
void PFX##_free(struct SNAME *_list_); \
|
||||
/* Customization of Allocation and Callbacks */ \
|
||||
void PFX##_customize(struct SNAME *_list_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_list *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_push_front(struct SNAME *_list_, V element); \
|
||||
bool PFX##_push_at(struct SNAME *_list_, V element, size_t index); \
|
||||
@@ -326,7 +258,7 @@ struct cmc_callbacks_list
|
||||
\
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks_list *callbacks) \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (capacity < 1) \
|
||||
return NULL; \
|
||||
@@ -389,7 +321,7 @@ struct cmc_callbacks_list
|
||||
} \
|
||||
\
|
||||
void PFX##_customize(struct SNAME *_list_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_list *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (alloc) \
|
||||
_list_->alloc = alloc; \
|
||||
|
||||
+7
-119
@@ -43,112 +43,15 @@
|
||||
#ifndef CMC_MULTIMAP_H
|
||||
#define CMC_MULTIMAP_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* enum cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
#include "../cor/core.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* Hashtable Implementation
|
||||
* ------------------------------------------------------------------------- */
|
||||
#ifndef CMC_IMPL_HASHTABLE_PRIMES
|
||||
#define CMC_IMPL_HASHTABLE_PRIMES
|
||||
|
||||
// clang-format off
|
||||
static const size_t cmc_hashtable_primes[] = {
|
||||
/* < 1e3 */
|
||||
53, 97, 191, 383, 769,
|
||||
/* < 1e4 */
|
||||
1531, 3067, 6143,
|
||||
/* < 1e5 */
|
||||
12289, 24571, 49157, 98299,
|
||||
/* < 1e6 */
|
||||
196613, 393209, 786431,
|
||||
/* < 1e7 */
|
||||
1572869, 3145721, 6291449,
|
||||
/* < 1e8 */
|
||||
12582917, 25165813, 50331653,
|
||||
/* < 1e9 */
|
||||
100663291, 201326611, 402653189, 805306357,
|
||||
/* < 1e10 */
|
||||
1610612741, 3221225473, 6442450939,
|
||||
/* < 1e11 */
|
||||
12884901893, 25769803799, 51539607551,
|
||||
/* < 1e12 */
|
||||
103079215111, 206158430209, 412316860441, 824633720831,
|
||||
/* < 1e13 */
|
||||
1649267441651, 3298534883309, 6597069766657,
|
||||
/* < 1e14 */
|
||||
13194139533299, 26388279066623, 52776558133303,
|
||||
/* < 1e15 */
|
||||
105553116266489, 211106232532969, 422212465066001, 844424930131963,
|
||||
/* < 1e16 */
|
||||
1688849860263953, 3377699720527861, 6755399441055731,
|
||||
/* < 1e17 */
|
||||
13510798882111483, 27021597764222939, 54043195528445957,
|
||||
/* < 1e18 */
|
||||
108086391056891903, 216172782113783773, 432345564227567621, 864691128455135207,
|
||||
/* < 1e19 */
|
||||
1729382256910270481, 3458764513820540933, 6917529027641081903,
|
||||
/* < 1e20 */
|
||||
13835058055282163729llu,
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
#endif /* CMC_IMPL_HASHTABLE_PRIMES */
|
||||
#include "../cor/hashtable.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* MultiMap specific
|
||||
@@ -166,21 +69,6 @@ static const char *cmc_string_fmt_multimap = "struct %s<%s, %s> "
|
||||
"alloc:%p, "
|
||||
"callbacks:%p }";
|
||||
|
||||
/**
|
||||
* Custom MultiMap callbacks.
|
||||
*
|
||||
* There are two types of callbacks, 'before' and 'after':
|
||||
* <before|after>_<function_name>
|
||||
*/
|
||||
struct cmc_callbacks_multimap
|
||||
{
|
||||
void (*before_clear)(void *);
|
||||
void (*after_clear)(void *);
|
||||
void (*before_free)(void *);
|
||||
void (*after_free)(void *);
|
||||
// TODO implement all callbacks
|
||||
};
|
||||
|
||||
#define CMC_GENERATE_MULTIMAP(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_MULTIMAP_HEADER(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_MULTIMAP_SOURCE(PFX, SNAME, K, V)
|
||||
@@ -224,7 +112,7 @@ struct cmc_callbacks_multimap
|
||||
struct cmc_alloc_node *alloc; \
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks_multimap *callbacks; \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the multimap */ \
|
||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||
@@ -328,12 +216,12 @@ struct cmc_callbacks_multimap
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, double load, struct SNAME##_ftab_key *f_key, \
|
||||
struct SNAME##_ftab_val *f_val, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_multimap *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
void PFX##_clear(struct SNAME *_map_); \
|
||||
void PFX##_free(struct SNAME *_map_); \
|
||||
/* Customization of Allocation and Callbacks */ \
|
||||
void PFX##_customize(struct SNAME *_map_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_multimap *callbacks); \
|
||||
struct cmc_callbacks *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); \
|
||||
@@ -448,7 +336,7 @@ struct cmc_callbacks_multimap
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, double load, struct SNAME##_ftab_key *f_key, \
|
||||
struct SNAME##_ftab_val *f_val, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_multimap *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (capacity == 0 || load <= 0) \
|
||||
return NULL; \
|
||||
@@ -566,7 +454,7 @@ struct cmc_callbacks_multimap
|
||||
} \
|
||||
\
|
||||
void PFX##_customize(struct SNAME *_map_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_multimap *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (alloc) \
|
||||
_map_->alloc = alloc; \
|
||||
|
||||
+11
-137
@@ -21,124 +21,15 @@
|
||||
#ifndef CMC_MULTISET_H
|
||||
#define CMC_MULTISET_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* enum cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
#include "../cor/core.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* Hashtable Implementation
|
||||
* ------------------------------------------------------------------------- */
|
||||
#ifndef CMC_IMPL_HASHTABLE_STATE
|
||||
#define CMC_IMPL_HASHTABLE_STATE
|
||||
|
||||
enum cmc_entry_state
|
||||
{
|
||||
CMC_ES_DELETED = -1,
|
||||
CMC_ES_EMPTY = 0,
|
||||
CMC_ES_FILLED = 1
|
||||
};
|
||||
|
||||
#endif /* CMC_IMPL_HASHTABLE_STATE */
|
||||
|
||||
#ifndef CMC_IMPL_HASHTABLE_PRIMES
|
||||
#define CMC_IMPL_HASHTABLE_PRIMES
|
||||
|
||||
// clang-format off
|
||||
static const size_t cmc_hashtable_primes[] = {
|
||||
/* < 1e3 */
|
||||
53, 97, 191, 383, 769,
|
||||
/* < 1e4 */
|
||||
1531, 3067, 6143,
|
||||
/* < 1e5 */
|
||||
12289, 24571, 49157, 98299,
|
||||
/* < 1e6 */
|
||||
196613, 393209, 786431,
|
||||
/* < 1e7 */
|
||||
1572869, 3145721, 6291449,
|
||||
/* < 1e8 */
|
||||
12582917, 25165813, 50331653,
|
||||
/* < 1e9 */
|
||||
100663291, 201326611, 402653189, 805306357,
|
||||
/* < 1e10 */
|
||||
1610612741, 3221225473, 6442450939,
|
||||
/* < 1e11 */
|
||||
12884901893, 25769803799, 51539607551,
|
||||
/* < 1e12 */
|
||||
103079215111, 206158430209, 412316860441, 824633720831,
|
||||
/* < 1e13 */
|
||||
1649267441651, 3298534883309, 6597069766657,
|
||||
/* < 1e14 */
|
||||
13194139533299, 26388279066623, 52776558133303,
|
||||
/* < 1e15 */
|
||||
105553116266489, 211106232532969, 422212465066001, 844424930131963,
|
||||
/* < 1e16 */
|
||||
1688849860263953, 3377699720527861, 6755399441055731,
|
||||
/* < 1e17 */
|
||||
13510798882111483, 27021597764222939, 54043195528445957,
|
||||
/* < 1e18 */
|
||||
108086391056891903, 216172782113783773, 432345564227567621, 864691128455135207,
|
||||
/* < 1e19 */
|
||||
1729382256910270481, 3458764513820540933, 6917529027641081903,
|
||||
/* < 1e20 */
|
||||
13835058055282163729llu,
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
#endif /* CMC_IMPL_HASHTABLE_PRIMES */
|
||||
#include "../cor/hashtable.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* MultiSet specific
|
||||
@@ -156,21 +47,6 @@ static const char *cmc_string_fmt_multiset = "struct %s<%s> "
|
||||
"alloc:%p, "
|
||||
"callbacks:%p }";
|
||||
|
||||
/**
|
||||
* Custom MultiSet callbacks.
|
||||
*
|
||||
* There are two types of callbacks, 'before' and 'after':
|
||||
* <before|after>_<function_name>
|
||||
*/
|
||||
struct cmc_callbacks_multiset
|
||||
{
|
||||
void (*before_clear)(void *);
|
||||
void (*after_clear)(void *);
|
||||
void (*before_free)(void *);
|
||||
void (*after_free)(void *);
|
||||
// TODO implement all callbacks
|
||||
};
|
||||
|
||||
#define CMC_GENERATE_MULTISET(PFX, SNAME, V) \
|
||||
CMC_GENERATE_MULTISET_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_MULTISET_SOURCE(PFX, SNAME, V)
|
||||
@@ -214,7 +90,7 @@ struct cmc_callbacks_multiset
|
||||
struct cmc_alloc_node *alloc; \
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks_multiset *callbacks; \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the hashset */ \
|
||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||
@@ -290,15 +166,14 @@ struct cmc_callbacks_multiset
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
struct SNAME *PFX##_new(size_t capacity, double load, \
|
||||
struct SNAME##_ftab_val *f_val); \
|
||||
struct SNAME *PFX##_new_custom(size_t capacity, double load, \
|
||||
struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_multiset *callbacks); \
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, double load, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks); \
|
||||
void PFX##_clear(struct SNAME *_set_); \
|
||||
void PFX##_free(struct SNAME *_set_); \
|
||||
/* Customization of Allocation and Callbacks */ \
|
||||
void PFX##_customize(struct SNAME *_set_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_multiset *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_insert(struct SNAME *_set_, V element); \
|
||||
bool PFX##_insert_many(struct SNAME *_set_, V element, size_t count); \
|
||||
@@ -421,10 +296,9 @@ struct cmc_callbacks_multiset
|
||||
return _set_; \
|
||||
} \
|
||||
\
|
||||
struct SNAME *PFX##_new_custom(size_t capacity, double load, \
|
||||
struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_multiset *callbacks) \
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, double load, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (capacity == 0 || load <= 0 || load >= 1) \
|
||||
return NULL; \
|
||||
@@ -511,7 +385,7 @@ struct cmc_callbacks_multiset
|
||||
} \
|
||||
\
|
||||
void PFX##_customize(struct SNAME *_set_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_multiset *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (alloc) \
|
||||
_set_->alloc = alloc; \
|
||||
|
||||
+6
-74
@@ -43,63 +43,10 @@
|
||||
#ifndef CMC_QUEUE_H
|
||||
#define CMC_QUEUE_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* enum cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
#include "../cor/core.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* Queue specific
|
||||
@@ -117,21 +64,6 @@ static const char *cmc_string_fmt_queue = "struct %s<%s> "
|
||||
"alloc:%p, "
|
||||
"callbacks:%p }";
|
||||
|
||||
/**
|
||||
* Custom Queue callbacks.
|
||||
*
|
||||
* There are two types of callbacks, 'before' and 'after':
|
||||
* <before|after>_<function_name>
|
||||
*/
|
||||
struct cmc_callbacks_queue
|
||||
{
|
||||
void (*before_clear)(void *);
|
||||
void (*after_clear)(void *);
|
||||
void (*before_free)(void *);
|
||||
void (*after_free)(void *);
|
||||
// TODO implement all callbacks
|
||||
};
|
||||
|
||||
#define CMC_GENERATE_QUEUE(PFX, SNAME, V) \
|
||||
CMC_GENERATE_QUEUE_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_QUEUE_SOURCE(PFX, SNAME, V)
|
||||
@@ -175,7 +107,7 @@ struct cmc_callbacks_queue
|
||||
struct cmc_alloc_node *alloc; \
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks_queue *callbacks; \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the queue */ \
|
||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||
@@ -230,12 +162,12 @@ struct cmc_callbacks_queue
|
||||
struct SNAME *PFX##_new(size_t capacity, struct SNAME##_ftab_val *f_val); \
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks_queue *callbacks); \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks); \
|
||||
void PFX##_clear(struct SNAME *_queue_); \
|
||||
void PFX##_free(struct SNAME *_queue_); \
|
||||
/* Customization of Allocation and Callbacks */ \
|
||||
void PFX##_customize(struct SNAME *_queue_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_queue *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_enqueue(struct SNAME *_queue_, V element); \
|
||||
bool PFX##_dequeue(struct SNAME *_queue_); \
|
||||
@@ -325,7 +257,7 @@ struct cmc_callbacks_queue
|
||||
\
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks_queue *callbacks) \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (capacity < 1) \
|
||||
return NULL; \
|
||||
@@ -400,7 +332,7 @@ struct cmc_callbacks_queue
|
||||
} \
|
||||
\
|
||||
void PFX##_customize(struct SNAME *_queue_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_queue *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (alloc) \
|
||||
_queue_->alloc = alloc; \
|
||||
|
||||
+8
-78
@@ -22,63 +22,10 @@
|
||||
#ifndef CMC_SORTEDLIST_H
|
||||
#define CMC_SORTEDLIST_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* enum cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
#include "../cor/core.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* SortedList specific
|
||||
@@ -95,21 +42,6 @@ static const char *cmc_string_fmt_sortedlist = "struct %s<%s> "
|
||||
"alloc:%p, "
|
||||
"callbacks:%p }";
|
||||
|
||||
/**
|
||||
* Custom SortedList callbacks.
|
||||
*
|
||||
* There are two types of callbacks, 'before' and 'after':
|
||||
* <before|after>_<function_name>
|
||||
*/
|
||||
struct cmc_callbacks_sortedlist
|
||||
{
|
||||
void (*before_clear)(void *);
|
||||
void (*after_clear)(void *);
|
||||
void (*before_free)(void *);
|
||||
void (*after_free)(void *);
|
||||
// TODO implement all callbacks
|
||||
};
|
||||
|
||||
#define CMC_GENERATE_SORTEDLIST(PFX, SNAME, V) \
|
||||
CMC_GENERATE_SORTEDLIST_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_SORTEDLIST_SOURCE(PFX, SNAME, V)
|
||||
@@ -150,7 +82,7 @@ struct cmc_callbacks_sortedlist
|
||||
struct cmc_alloc_node *alloc; \
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks_sortedlist *callbacks; \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the list */ \
|
||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||
@@ -202,13 +134,12 @@ struct cmc_callbacks_sortedlist
|
||||
struct SNAME *PFX##_new(size_t capacity, struct SNAME##_ftab_val *f_val); \
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_sortedlist *callbacks); \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks); \
|
||||
void PFX##_clear(struct SNAME *_list_); \
|
||||
void PFX##_free(struct SNAME *_list_); \
|
||||
/* Customization of Allocation and Callbacks */ \
|
||||
void PFX##_customize(struct SNAME *_list_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_sortedlist *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_insert(struct SNAME *_list_, V element); \
|
||||
bool PFX##_remove(struct SNAME *_list_, size_t index); \
|
||||
@@ -306,10 +237,9 @@ struct cmc_callbacks_sortedlist
|
||||
return _list_; \
|
||||
} \
|
||||
\
|
||||
struct SNAME *PFX##_new_custom(size_t capacity, \
|
||||
struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_sortedlist *callbacks) \
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (capacity < 1) \
|
||||
return NULL; \
|
||||
@@ -370,7 +300,7 @@ struct cmc_callbacks_sortedlist
|
||||
} \
|
||||
\
|
||||
void PFX##_customize(struct SNAME *_list_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_sortedlist *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (alloc) \
|
||||
_list_->alloc = alloc; \
|
||||
|
||||
+6
-74
@@ -28,63 +28,10 @@
|
||||
#ifndef CMC_STACK_H
|
||||
#define CMC_STACK_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* enum cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
#include "../cor/core.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* Stack specific
|
||||
@@ -100,21 +47,6 @@ static const char *cmc_string_fmt_stack = "struct %s<%s> "
|
||||
"alloc:%p, "
|
||||
"callbacks:%p }";
|
||||
|
||||
/**
|
||||
* Custom Stack callbacks.
|
||||
*
|
||||
* There are two types of callbacks, 'before' and 'after':
|
||||
* <before|after>_<function_name>
|
||||
*/
|
||||
struct cmc_callbacks_stack
|
||||
{
|
||||
void (*before_clear)(void *);
|
||||
void (*after_clear)(void *);
|
||||
void (*before_free)(void *);
|
||||
void (*after_free)(void *);
|
||||
// TODO implement all callbacks
|
||||
};
|
||||
|
||||
#define CMC_GENERATE_STACK(PFX, SNAME, V) \
|
||||
CMC_GENERATE_STACK_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_STACK_SOURCE(PFX, SNAME, V)
|
||||
@@ -152,7 +84,7 @@ struct cmc_callbacks_stack
|
||||
struct cmc_alloc_node *alloc; \
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks_stack *callbacks; \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the stack */ \
|
||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||
@@ -204,12 +136,12 @@ struct cmc_callbacks_stack
|
||||
struct SNAME *PFX##_new(size_t capacity, struct SNAME##_ftab_val *f_val); \
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks_stack *callbacks); \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks); \
|
||||
void PFX##_clear(struct SNAME *_stack_); \
|
||||
void PFX##_free(struct SNAME *_stack_); \
|
||||
/* Customization of Allocation and Callbacks */ \
|
||||
void PFX##_customize(struct SNAME *_stack_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_stack *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_push(struct SNAME *_stack_, V element); \
|
||||
bool PFX##_pop(struct SNAME *_stack_); \
|
||||
@@ -297,7 +229,7 @@ struct cmc_callbacks_stack
|
||||
\
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
size_t capacity, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks_stack *callbacks) \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (capacity < 1) \
|
||||
return NULL; \
|
||||
@@ -359,7 +291,7 @@ struct cmc_callbacks_stack
|
||||
} \
|
||||
\
|
||||
void PFX##_customize(struct SNAME *_stack_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_stack *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (alloc) \
|
||||
_stack_->alloc = alloc; \
|
||||
|
||||
+7
-76
@@ -19,63 +19,10 @@
|
||||
#ifndef CMC_TREEMAP_H
|
||||
#define CMC_TREEMAP_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* enum cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
#include "../cor/core.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* TreeMap specific
|
||||
@@ -91,21 +38,6 @@ static const char *cmc_string_fmt_treemap = "struct %s<%s, %s> "
|
||||
"alloc:%p, "
|
||||
"callbacks:%p }";
|
||||
|
||||
/**
|
||||
* Custom TreeMap callbacks.
|
||||
*
|
||||
* There are two types of callbacks, 'before' and 'after':
|
||||
* <before|after>_<function_name>
|
||||
*/
|
||||
struct cmc_callbacks_treemap
|
||||
{
|
||||
void (*before_clear)(void *);
|
||||
void (*after_clear)(void *);
|
||||
void (*before_free)(void *);
|
||||
void (*after_free)(void *);
|
||||
// TODO implement all callbacks
|
||||
};
|
||||
|
||||
#define CMC_GENERATE_TREEMAP(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_TREEMAP_HEADER(PFX, SNAME, K, V) \
|
||||
CMC_GENERATE_TREEMAP_SOURCE(PFX, SNAME, K, V)
|
||||
@@ -143,7 +75,7 @@ struct cmc_callbacks_treemap
|
||||
struct cmc_alloc_node *alloc; \
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks_treemap *callbacks; \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the treemap */ \
|
||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||
@@ -247,15 +179,14 @@ struct cmc_callbacks_treemap
|
||||
/* Collection Allocation and Deallocation */ \
|
||||
struct SNAME *PFX##_new(struct SNAME##_ftab_key *f_key, \
|
||||
struct SNAME##_ftab_val *f_val); \
|
||||
struct SNAME *PFX##_new_custom(struct SNAME##_ftab_key *f_key, \
|
||||
struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_treemap *callbacks); \
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
struct SNAME##_ftab_key *f_key, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks); \
|
||||
void PFX##_clear(struct SNAME *_map_); \
|
||||
void PFX##_free(struct SNAME *_map_); \
|
||||
/* Customization of Allocation and Callbacks */ \
|
||||
void PFX##_customize(struct SNAME *_map_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_treemap *callbacks); \
|
||||
struct cmc_callbacks *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); \
|
||||
@@ -346,7 +277,7 @@ struct cmc_callbacks_treemap
|
||||
\
|
||||
struct SNAME *PFX##_new_custom( \
|
||||
struct SNAME##_ftab_key *f_key, struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks_treemap *callbacks) \
|
||||
struct cmc_alloc_node *alloc, struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (!f_key || !f_val) \
|
||||
return NULL; \
|
||||
|
||||
+5
-73
@@ -19,63 +19,10 @@
|
||||
#ifndef CMC_TREESET_H
|
||||
#define CMC_TREESET_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* 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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* enum cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
#include "../cor/core.h"
|
||||
|
||||
/* -------------------------------------------------------------------------
|
||||
* TreeSet specific
|
||||
@@ -90,21 +37,6 @@ static const char *cmc_string_fmt_treeset = "struct %s<%s> "
|
||||
"alloc:%p, "
|
||||
"callbacks:%p }";
|
||||
|
||||
/**
|
||||
* Custom TreeSet callbacks.
|
||||
*
|
||||
* There are two types of callbacks, 'before' and 'after':
|
||||
* <before|after>_<function_name>
|
||||
*/
|
||||
struct cmc_callbacks_treeset
|
||||
{
|
||||
void (*before_clear)(void *);
|
||||
void (*after_clear)(void *);
|
||||
void (*before_free)(void *);
|
||||
void (*after_free)(void *);
|
||||
// TODO implement all callbacks
|
||||
};
|
||||
|
||||
#define CMC_GENERATE_TREESET(PFX, SNAME, V) \
|
||||
CMC_GENERATE_TREESET_HEADER(PFX, SNAME, V) \
|
||||
CMC_GENERATE_TREESET_SOURCE(PFX, SNAME, V)
|
||||
@@ -139,7 +71,7 @@ struct cmc_callbacks_treeset
|
||||
struct cmc_alloc_node *alloc; \
|
||||
\
|
||||
/* Custom callback functions */ \
|
||||
struct cmc_callbacks_treeset *callbacks; \
|
||||
struct cmc_callbacks *callbacks; \
|
||||
\
|
||||
/* Function that returns an iterator to the start of the treeset */ \
|
||||
struct SNAME##_iter (*it_start)(struct SNAME *); \
|
||||
@@ -219,12 +151,12 @@ struct cmc_callbacks_treeset
|
||||
struct SNAME *PFX##_new(struct SNAME##_ftab_val *f_val); \
|
||||
struct SNAME *PFX##_new_custom(struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_treeset *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
void PFX##_clear(struct SNAME *_set_); \
|
||||
void PFX##_free(struct SNAME *_set_); \
|
||||
/* Customization of Allocation and Callbacks */ \
|
||||
void PFX##_customize(struct SNAME *_set_, struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_treeset *callbacks); \
|
||||
struct cmc_callbacks *callbacks); \
|
||||
/* Collection Input and Output */ \
|
||||
bool PFX##_insert(struct SNAME *_set_, V element); \
|
||||
bool PFX##_remove(struct SNAME *_set_, V element); \
|
||||
@@ -332,7 +264,7 @@ struct cmc_callbacks_treeset
|
||||
\
|
||||
struct SNAME *PFX##_new_custom(struct SNAME##_ftab_val *f_val, \
|
||||
struct cmc_alloc_node *alloc, \
|
||||
struct cmc_callbacks_treeset *callbacks) \
|
||||
struct cmc_callbacks *callbacks) \
|
||||
{ \
|
||||
if (!f_val) \
|
||||
return NULL; \
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* core.h
|
||||
*
|
||||
* Creation Date: 17/03/2020
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Core includes and structs used in the library.
|
||||
*/
|
||||
|
||||
#ifndef CMC_CORE_H
|
||||
#define CMC_CORE_H
|
||||
|
||||
#include <inttypes.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.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.
|
||||
*/
|
||||
static 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 };
|
||||
|
||||
/**
|
||||
* struct cmc_callbacks
|
||||
*
|
||||
* Callback node. Each collection will call one of these functions when:
|
||||
* - on_create : an element is about to be added to the collection
|
||||
* - on_read : the collection is about to be queried about an element
|
||||
* - on_update : an element in the collection is about to be updated
|
||||
* - on_delete : element is about to be removed from the collection
|
||||
* - on_resize : the collection is full and is about to get resized
|
||||
*/
|
||||
struct cmc_callbacks
|
||||
{
|
||||
void (*on_create)(void);
|
||||
void (*on_read)(void);
|
||||
void (*on_update)(void);
|
||||
void (*on_delete)(void);
|
||||
void (*on_resize)(void);
|
||||
};
|
||||
|
||||
/**
|
||||
* cmc_flags
|
||||
*
|
||||
* Defines common error codes used by all collections. These are flags that
|
||||
* indicate if something went wrong in the last operation by the collection.
|
||||
*/
|
||||
static struct
|
||||
{
|
||||
int OK; // No errors
|
||||
int ALLOC; // Allocation failed
|
||||
int EMPTY; // The collection is empty when it should not
|
||||
int NOT_FOUND; // Key or value not found
|
||||
int INVALID; // Invalid argument or operation
|
||||
int RANGE; // Index out of range
|
||||
int DUPLICATE; // Duplicate key or value
|
||||
int ERROR; // Generic error, usually caused by algorithm error
|
||||
} cmc_flags = { 0, 1, 2, 3, 4, 5, 6, 7 };
|
||||
|
||||
#endif /* CMC_CORE_H */
|
||||
@@ -0,0 +1,85 @@
|
||||
/**
|
||||
* hashtable.h
|
||||
*
|
||||
* Creation Date: 17/03/2020
|
||||
*
|
||||
* Authors:
|
||||
* Leonardo Vencovsky (https://github.com/LeoVen)
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Things commonly used by hashtable-based collections.
|
||||
*/
|
||||
|
||||
#ifndef CMC_IMPL_HASHTABLE_H
|
||||
#define CMC_IMPL_HASHTABLE_H
|
||||
|
||||
#include "core.h"
|
||||
|
||||
/**
|
||||
* CMC_ENTRY_DELETED
|
||||
*
|
||||
* Defines a pointer to an entry that has been deleted
|
||||
*/
|
||||
#define CMC_ENTRY_DELETED ((void *)1)
|
||||
|
||||
/**
|
||||
* enum cmc_entry_state
|
||||
*
|
||||
* Defines the three possible states for a node on a flat hashtable.
|
||||
*/
|
||||
enum cmc_entry_state
|
||||
{
|
||||
CMC_ES_DELETED = -1,
|
||||
CMC_ES_EMPTY = 0,
|
||||
CMC_ES_FILLED = 1
|
||||
};
|
||||
|
||||
/**
|
||||
* static const size_t cmc_hashtable_primes[59]
|
||||
*
|
||||
* Prime numbers used to define a capacity for the hashtables for better hashing.
|
||||
*/
|
||||
// clang-format off
|
||||
static const size_t cmc_hashtable_primes[] = {
|
||||
/* < 1e3 */
|
||||
53, 97, 191, 383, 769,
|
||||
/* < 1e4 */
|
||||
1531, 3067, 6143,
|
||||
/* < 1e5 */
|
||||
12289, 24571, 49157, 98299,
|
||||
/* < 1e6 */
|
||||
196613, 393209, 786431,
|
||||
/* < 1e7 */
|
||||
1572869, 3145721, 6291449,
|
||||
/* < 1e8 */
|
||||
12582917, 25165813, 50331653,
|
||||
/* < 1e9 */
|
||||
100663291, 201326611, 402653189, 805306357,
|
||||
/* < 1e10 */
|
||||
1610612741, 3221225473, 6442450939,
|
||||
/* < 1e11 */
|
||||
12884901893, 25769803799, 51539607551,
|
||||
/* < 1e12 */
|
||||
103079215111, 206158430209, 412316860441, 824633720831,
|
||||
/* < 1e13 */
|
||||
1649267441651, 3298534883309, 6597069766657,
|
||||
/* < 1e14 */
|
||||
13194139533299, 26388279066623, 52776558133303,
|
||||
/* < 1e15 */
|
||||
105553116266489, 211106232532969, 422212465066001, 844424930131963,
|
||||
/* < 1e16 */
|
||||
1688849860263953, 3377699720527861, 6755399441055731,
|
||||
/* < 1e17 */
|
||||
13510798882111483, 27021597764222939, 54043195528445957,
|
||||
/* < 1e18 */
|
||||
108086391056891903, 216172782113783773, 432345564227567621, 864691128455135207,
|
||||
/* < 1e19 */
|
||||
1729382256910270481, 3458764513820540933, 6917529027641081903,
|
||||
/* < 1e20 */
|
||||
13835058055282163729llu,
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
#endif /* CMC_IMPL_HASHTABLE_H */
|
||||
@@ -55,7 +55,7 @@ CMC_CREATE_UNIT(bidimap_test, true, {
|
||||
.calloc = calloc,
|
||||
.realloc = realloc,
|
||||
.free = free },
|
||||
&(struct cmc_callbacks_bidimap){ 0 });
|
||||
&(struct cmc_callbacks){ 0 });
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
@@ -69,7 +69,7 @@ CMC_CREATE_UNIT(bidimap_test, true, {
|
||||
.calloc = calloc,
|
||||
.realloc = realloc,
|
||||
.free = free },
|
||||
&(struct cmc_callbacks_bidimap){ 0 });
|
||||
&(struct cmc_callbacks){ 0 });
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
@@ -238,7 +238,7 @@ CMC_CREATE_UNIT(bidimap_test, true, {
|
||||
.calloc = calloc,
|
||||
.realloc = realloc,
|
||||
.free = free },
|
||||
&(struct cmc_callbacks_bidimap){ 0 });
|
||||
&(struct cmc_callbacks){ 0 });
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
@@ -271,7 +271,7 @@ CMC_CREATE_UNIT(bidimap_test, true, {
|
||||
.calloc = calloc,
|
||||
.realloc = realloc,
|
||||
.free = free },
|
||||
&(struct cmc_callbacks_bidimap){ 0 });
|
||||
&(struct cmc_callbacks){ 0 });
|
||||
|
||||
cmc_assert_not_equals(ptr, NULL, map);
|
||||
|
||||
@@ -366,7 +366,7 @@ CMC_CREATE_UNIT(bidimap_test, true, {
|
||||
|
||||
// customize
|
||||
bm_customize(map, &cmc_alloc_node_default,
|
||||
&(struct cmc_callbacks_bidimap){ 0 });
|
||||
&(struct cmc_callbacks){ 0 });
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, bm_flag(map));
|
||||
|
||||
// Insert
|
||||
|
||||
@@ -661,7 +661,7 @@ CMC_CREATE_UNIT(deque_test, true, {
|
||||
// customize
|
||||
d->flag = cmc_flags.ERROR;
|
||||
d_customize(d, &cmc_alloc_node_default,
|
||||
&(struct cmc_callbacks_deque){ 0 });
|
||||
&(struct cmc_callbacks){ 0 });
|
||||
cmc_assert_equals(int32_t, cmc_flags.OK, d_flag(d));
|
||||
|
||||
// push_front
|
||||
|
||||
Reference in new issue
Block a user