CMagic
0.5.0
Portable C library of utilities and data structures
|
Implementation of a map 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_map_iterator_t |
struct | cmagic_map_insert_result_t |
Map insertion result. More... | |
Macros | |
#define | CMAGIC_MAP(key_type) key_type* |
Convenient alias for type* . Returned type of CMAGIC_MAP_NEW. More... | |
#define | CMAGIC_MAP_NEW(key_type, value_type, key_comparator, alloc_packet) |
Allocates and returns an address of a newly created empty map. More... | |
#define | CMAGIC_MAP_FREE(cmagic_map) cmagic_map_free((void*)(cmagic_map)) |
Frees the resources allocated by the map before. More... | |
#define | CMAGIC_MAP_ALLOCATE(cmagic_map, key) |
Allocates space for a new element (key-value pair) but does not initialize it. More... | |
#define | CMAGIC_MAP_INSERT(cmagic_map, key, value) |
Allocates space for a new element and initializes it with data under key and value . More... | |
#define | CMAGIC_MAP_ERASE_EXT(cmagic_map, key, destructor) |
Extended version of CMAGIC_MAP_ERASE. More... | |
#define | CMAGIC_MAP_ERASE(cmagic_map, key) CMAGIC_MAP_ERASE_EXT(cmagic_map, key, NULL) |
Removes a single element (key-value pair) from the map. More... | |
#define | CMAGIC_MAP_CLEAR(cmagic_map) cmagic_map_clear((void*)(cmagic_map)) |
Removes all elements from the map. More... | |
#define | CMAGIC_MAP_SIZE(cmagic_map) cmagic_map_size((void*)(cmagic_map)) |
Returns the number of elements in the map. More... | |
#define | CMAGIC_MAP_FIRST(cmagic_map) cmagic_map_first((void*)(cmagic_map)) |
Return iterator to the first element in map. More... | |
#define | CMAGIC_MAP_LAST(cmagic_map) cmagic_map_last((void*)(cmagic_map)) |
Return iterator to the last element in map. More... | |
#define | CMAGIC_MAP_ITERATOR_NEXT(iterator) cmagic_map_iterator_next(iterator) |
Returns iterator to the next element in container. More... | |
#define | CMAGIC_MAP_ITERATOR_PREV(iterator) cmagic_map_iterator_prev(iterator) |
Returns iterator to the previous element in container. More... | |
#define | CMAGIC_MAP_FIND(cmagic_map, key) |
Searches the container for an element with a key equivalent to key and returns an iterator to it if found, otherwise it returns NULL . More... | |
#define | CMAGIC_MAP_GET_KEY(key_type, iterator) (assert(iterator), assert((iterator)->key), *((const key_type*)(iterator)->key)) |
Helper macro for retrieving the key from the iterator. More... | |
#define | CMAGIC_MAP_GET_VALUE(value_type, iterator) (assert(iterator), assert((iterator)->value), *((value_type*)(iterator)->value)) |
Helper macro for retrieving the value from the iterator. More... | |
#define | CMAGIC_MAP_GET_ALLOC_PACKET(cmagic_map) cmagic_map_get_alloc_packet((void*)(cmagic_map)) |
Retrieves cmagic_memory_alloc_packet_t associated with the map. More... | |
Typedefs | |
typedef int(* | cmagic_map_key_comparator_t) (const void *key1, const void *key2) |
Pointer to a function that compares two keys. More... | |
typedef void(* | cmagic_map_erase_destructor_t) (void *key, void *value) |
User defined additional tasks to be executed right before map element deletion. More... | |
Implementation of a map container.
Please use provided macros instead of raw functions to gain additional type checks.
#define CMAGIC_MAP | ( | key_type | ) | key_type* |
Convenient alias for type*
. Returned type of CMAGIC_MAP_NEW.
type | type of map keys |
#define CMAGIC_MAP_ALLOCATE | ( | cmagic_map, | |
key | |||
) |
Allocates space for a new element (key-value pair) but does not initialize it.
New element is allocated only if key
doesn't already exist in the map.
key
. Otherwise the map will be in an undefined state. cmagic_map | a map allocated before with CMAGIC_MAP_NEW |
key | pointer to the key value, needed to place a new element in the right place in the internal binary tree |
#define CMAGIC_MAP_CLEAR | ( | cmagic_map | ) | cmagic_map_clear((void*)(cmagic_map)) |
Removes all elements from the map.
cmagic_map | a map allocated before with CMAGIC_MAP_NEW |
#define CMAGIC_MAP_ERASE | ( | cmagic_map, | |
key | |||
) | CMAGIC_MAP_ERASE_EXT(cmagic_map, key, NULL) |
Removes a single element (key-value pair) from the map.
cmagic_map | a map allocated before with CMAGIC_MAP_NEW |
key | pointer to a key to be removed from the map. 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 map. |
#define CMAGIC_MAP_ERASE_EXT | ( | cmagic_map, | |
key, | |||
destructor | |||
) |
Extended version of CMAGIC_MAP_ERASE.
cmagic_map | a map allocated before with CMAGIC_MAP_NEW |
key | pointer to a key to be removed from the map. 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 map. |
destructor | function of type cmagic_map_erase_destructor_t to be called on the key and value right before deleting them |
#define CMAGIC_MAP_FIND | ( | cmagic_map, | |
key | |||
) |
Searches the container for an element with a key equivalent to key
and returns an iterator to it if found, otherwise it returns NULL
.
key | pointer to a key to be searched for |
key
is found, or NULL
otherwise #define CMAGIC_MAP_FIRST | ( | cmagic_map | ) | cmagic_map_first((void*)(cmagic_map)) |
Return iterator to the first element in map.
Returns an iterator pointing to the first element in the map according to the order defined by cmagic_map_key_comparator_t.
cmagic_map | a map allocated before with CMAGIC_MAP_NEW |
NULL
if the map is empty #define CMAGIC_MAP_FREE | ( | cmagic_map | ) | cmagic_map_free((void*)(cmagic_map)) |
Frees the resources allocated by the map before.
Must not use cmagic_map
after free.
cmagic_map | a map allocated before with CMAGIC_MAP_NEW |
#define CMAGIC_MAP_GET_ALLOC_PACKET | ( | cmagic_map | ) | cmagic_map_get_alloc_packet((void*)(cmagic_map)) |
Retrieves cmagic_memory_alloc_packet_t associated with the map.
cmagic_map | a map allocated before with CMAGIC_MAP_NEW |
#define CMAGIC_MAP_GET_KEY | ( | key_type, | |
iterator | |||
) | (assert(iterator), assert((iterator)->key), *((const key_type*)(iterator)->key)) |
Helper macro for retrieving the key from the iterator.
iterator
must not be NULL
key_type | type of the keys of the map which the iterator is associated with |
iterator | cmagic_map_iterator_t object |
#define CMAGIC_MAP_GET_VALUE | ( | value_type, | |
iterator | |||
) | (assert(iterator), assert((iterator)->value), *((value_type*)(iterator)->value)) |
Helper macro for retrieving the value from the iterator.
iterator
must not be NULL
value_type | type of the values of the map which the iterator is associated with |
iterator | cmagic_map_iterator_t object |
#define CMAGIC_MAP_INSERT | ( | cmagic_map, | |
key, | |||
value | |||
) |
Allocates space for a new element and initializes it with data under key
and value
.
cmagic_map | a map allocated before with CMAGIC_MAP_NEW |
key | pointer to the key value |
value | pointer to the value value |
#define CMAGIC_MAP_ITERATOR_NEXT | ( | iterator | ) | cmagic_map_iterator_next(iterator) |
Returns iterator to the next element in container.
iterator | cmagic_map_iterator_t object |
NULL
if iterator
was the last element in container #define CMAGIC_MAP_ITERATOR_PREV | ( | iterator | ) | cmagic_map_iterator_prev(iterator) |
Returns iterator to the previous element in container.
iterator | cmagic_map_iterator_t object |
NULL
if iterator
was the first element in container #define CMAGIC_MAP_LAST | ( | cmagic_map | ) | cmagic_map_last((void*)(cmagic_map)) |
Return iterator to the last element in map.
Returns an iterator pointing to the last element in the map according to the order defined by cmagic_map_key_comparator_t.
cmagic_map | a map allocated before with CMAGIC_MAP_NEW |
NULL
if the map is empty #define CMAGIC_MAP_NEW | ( | key_type, | |
value_type, | |||
key_comparator, | |||
alloc_packet | |||
) |
Allocates and returns an address of a newly created empty map.
key_type | type of map elements |
value_type | type of map values |
key_comparator | function of type cmagic_map_key_comparator_t determining the order of the elements |
alloc_packet | cmagic_memory_alloc_packet_t suite of dynamic memory managing functions |
#define CMAGIC_MAP_SIZE | ( | cmagic_map | ) | cmagic_map_size((void*)(cmagic_map)) |
Returns the number of elements in the map.
cmagic_map | a map allocated before with CMAGIC_MAP_NEW |
typedef void(* cmagic_map_erase_destructor_t) (void *key, void *value) |
User defined additional tasks to be executed right before map element deletion.
free
function on the key
or value
. It's called later internally by map implementation key | pointer to key to be deleted |
value | pointer to the value to be deleted |
typedef int(* cmagic_map_key_comparator_t) (const void *key1, const void *key2) |
Pointer to a function that compares two keys.
This function is called by the map implementation to compare two kes, 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 map key. 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 |