CMagic
0.5.0
Portable C library of utilities and data structures
|
Implementation of a set container. More...
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include "cmagic/memory.h"
#include "cmagic/utils.h"
Go to the source code of this file.
Classes | |
struct | cmagic_set_iterator_t |
struct | cmagic_set_insert_result_t |
Set insertion result. More... | |
Macros | |
#define | CMAGIC_SET(key_type) key_type* |
Convenient alias for type* . Returned type of CMAGIC_SET_NEW. More... | |
#define | CMAGIC_SET_NEW(key_type, key_comparator, alloc_packet) ((CMAGIC_SET(key_type))cmagic_set_new(sizeof(key_type), (key_comparator), (alloc_packet))) |
Allocates and returns an address of a newly created empty set. More... | |
#define | CMAGIC_SET_FREE(cmagic_set) cmagic_set_free((void*)(cmagic_set)) |
Frees the resources allocated by the set before. More... | |
#define | CMAGIC_SET_ALLOCATE(cmagic_set, key) |
Allocates space for a new element but does not initialize it. More... | |
#define | CMAGIC_SET_INSERT(cmagic_set, key) |
Allocates space for a new element and initializes it with data under key . More... | |
#define | CMAGIC_SET_ERASE_EXT(cmagic_set, key, destructor) |
Extended version of CMAGIC_SET_ERASE. More... | |
#define | CMAGIC_SET_ERASE(cmagic_set, key) CMAGIC_SET_ERASE_EXT(cmagic_set, key, NULL) |
Removes a single element from the set. More... | |
#define | CMAGIC_SET_CLEAR(cmagic_set) cmagic_set_clear((void*)(cmagic_set)) |
Removes all elements from the set. More... | |
#define | CMAGIC_SET_SIZE(cmagic_set) cmagic_set_size((void*)(cmagic_set)) |
Returns the number of elements in the set. More... | |
#define | CMAGIC_SET_FIRST(cmagic_set) cmagic_set_first((void*)(cmagic_set)) |
Return iterator to the first element in set. More... | |
#define | CMAGIC_SET_LAST(cmagic_set) cmagic_set_last((void*)(cmagic_set)) |
Return iterator to the last element in set. More... | |
#define | CMAGIC_SET_ITERATOR_NEXT(iterator) cmagic_set_iterator_next(iterator) |
Returns iterator to the next element in container. More... | |
#define | CMAGIC_SET_ITERATOR_PREV(iterator) cmagic_set_iterator_prev(iterator) |
Returns iterator to the previous element in container. More... | |
#define | CMAGIC_SET_FIND(cmagic_set, key) |
Searches the container for an element equivalent to key and returns an iterator to it if found, otherwise it returns NULL . More... | |
#define | CMAGIC_SET_GET_KEY(key_type, iterator) (assert(iterator), assert((iterator)->key), *((const key_type*)(iterator)->key)) |
Helper macro for retrieving the key value from the iterator. More... | |
#define | CMAGIC_SET_GET_ALLOC_PACKET(cmagic_set) cmagic_set_get_alloc_packet((void*)(cmagic_set)) |
Retrieves cmagic_memory_alloc_packet_t associated with the set. More... | |
Typedefs | |
typedef int(* | cmagic_set_key_comparator_t) (const void *key1, const void *key2) |
Pointer to a function that compares two elements. More... | |
typedef void(* | cmagic_set_erase_destructor_t) (void *key) |
User defined additional tasks to be executed right before key deletion. More... | |
Implementation of a set container.
Please use provided macros instead of raw functions to gain additional type checks.
#define CMAGIC_SET | ( | key_type | ) | key_type* |
Convenient alias for type*
. Returned type of CMAGIC_SET_NEW.
type | type of set elements |
#define CMAGIC_SET_ALLOCATE | ( | cmagic_set, | |
key | |||
) |
Allocates space for a new element but does not initialize it.
New element is allocated only if it doesn't already exist in the set.
key
. Otherwise the set will be in an undefined state. cmagic_set | a set allocated before with CMAGIC_SET_NEW |
key | pointer to the key value, needed to place a new element in the right place in the internal binary tree |
#define CMAGIC_SET_CLEAR | ( | cmagic_set | ) | cmagic_set_clear((void*)(cmagic_set)) |
Removes all elements from the set.
cmagic_set | a set allocated before with CMAGIC_SET_NEW |
#define CMAGIC_SET_ERASE | ( | cmagic_set, | |
key | |||
) | CMAGIC_SET_ERASE_EXT(cmagic_set, key, NULL) |
Removes a single element from the set.
cmagic_set | a set allocated before with CMAGIC_SET_NEW |
key | pointer to a value to be removed from the set. This function compares by a value, the key doesn't have to be an address of the original key. Does nothing if the element doesn't exist in the set. |
#define CMAGIC_SET_ERASE_EXT | ( | cmagic_set, | |
key, | |||
destructor | |||
) |
Extended version of CMAGIC_SET_ERASE.
cmagic_set | a set allocated before with CMAGIC_SET_NEW |
key | pointer to a value to be removed from the set. This function compares by a value, the key doesn't have to be an address of the original key. Does nothing if the element doesn't exist in the set. |
destructor | function of type cmagic_set_erase_destructor_t to be called on the key right before deleting it |
#define CMAGIC_SET_FIND | ( | cmagic_set, | |
key | |||
) |
Searches the container for an element equivalent to key
and returns an iterator to it if found, otherwise it returns NULL
.
key | pointer to a value to be searched for |
key
is found, or NULL
otherwise #define CMAGIC_SET_FIRST | ( | cmagic_set | ) | cmagic_set_first((void*)(cmagic_set)) |
Return iterator to the first element in set.
Returns an iterator pointing to the first element in the set according to the order defined by cmagic_set_key_comparator_t.
cmagic_set | a set allocated before with CMAGIC_SET_NEW |
NULL
if the set is empty #define CMAGIC_SET_FREE | ( | cmagic_set | ) | cmagic_set_free((void*)(cmagic_set)) |
Frees the resources allocated by the set before.
Must not use cmagic_set
after free.
cmagic_set | a set allocated before with CMAGIC_SET_NEW |
#define CMAGIC_SET_GET_ALLOC_PACKET | ( | cmagic_set | ) | cmagic_set_get_alloc_packet((void*)(cmagic_set)) |
Retrieves cmagic_memory_alloc_packet_t associated with the set.
cmagic_set | a set allocated before with CMAGIC_SET_NEW |
#define CMAGIC_SET_GET_KEY | ( | key_type, | |
iterator | |||
) | (assert(iterator), assert((iterator)->key), *((const key_type*)(iterator)->key)) |
Helper macro for retrieving the key value from the iterator.
iterator
must not be NULL
key_type | type of set elements to which the iterator points to |
iterator | cmagic_set_iterator_t object |
#define CMAGIC_SET_INSERT | ( | cmagic_set, | |
key | |||
) |
Allocates space for a new element and initializes it with data under key
.
cmagic_set | a set allocated before with CMAGIC_SET_NEW |
key | pointer to the key value |
#define CMAGIC_SET_ITERATOR_NEXT | ( | iterator | ) | cmagic_set_iterator_next(iterator) |
Returns iterator to the next element in container.
iterator | cmagic_set_iterator_t object |
NULL
if iterator
was the last element in container #define CMAGIC_SET_ITERATOR_PREV | ( | iterator | ) | cmagic_set_iterator_prev(iterator) |
Returns iterator to the previous element in container.
iterator | cmagic_set_iterator_t object |
NULL
if iterator
was the first element in container #define CMAGIC_SET_LAST | ( | cmagic_set | ) | cmagic_set_last((void*)(cmagic_set)) |
Return iterator to the last element in set.
Returns an iterator pointing to the last element in the set according to the order defined by cmagic_set_key_comparator_t.
cmagic_set | a set allocated before with CMAGIC_SET_NEW |
NULL
if the set is empty #define CMAGIC_SET_NEW | ( | key_type, | |
key_comparator, | |||
alloc_packet | |||
) | ((CMAGIC_SET(key_type))cmagic_set_new(sizeof(key_type), (key_comparator), (alloc_packet))) |
Allocates and returns an address of a newly created empty set.
key_type | type of set elements |
key_comparator | function of type cmagic_set_key_comparator_t determining the order of the elements |
alloc_packet | cmagic_memory_alloc_packet_t suite of dynamic memory managing functions |
#define CMAGIC_SET_SIZE | ( | cmagic_set | ) | cmagic_set_size((void*)(cmagic_set)) |
Returns the number of elements in the set.
cmagic_set | a set allocated before with CMAGIC_SET_NEW |
typedef void(* cmagic_set_erase_destructor_t) (void *key) |
User defined additional tasks to be executed right before key deletion.
free
function on the key
. It's called later internally by set implementation key | pointer to key to be deleted |
typedef int(* cmagic_set_key_comparator_t) (const void *key1, const void *key2) |
Pointer to a function that compares two elements.
This function is called by the set implementation to compare two elements, which is necessary to properly construct and move inside its internal binary tree. Both arguments must be manually casted to a type corresponding to the type of the set. This function defines the order of the elements.
key1 | pointer to the first key value |
key2 | pointer to the second first key value |
value | meaning |
---|---|
<0 | The element pointed to by key1 goes before the element pointed to by key2 |
0 | The element pointed to by key1 is equivalent to the element pointed to by key2 |
>0 | The element pointed to by key1 goes after the element pointed to by key2 |