CMagic  0.5.0
Portable C library of utilities and data structures
map.h
Go to the documentation of this file.
1 
8 #ifndef CMAGIC_MAP_H
9 #define CMAGIC_MAP_H
10 
11 #include <assert.h>
12 #include <stdbool.h>
13 #include <stddef.h>
14 #include "cmagic/memory.h"
15 #include "cmagic/utils.h"
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
36 typedef int (*cmagic_map_key_comparator_t)(const void *key1, const void *key2);
37 
45 typedef void (*cmagic_map_erase_destructor_t)(void *key, void *value);
46 
47 void *
48 cmagic_map_new(size_t key_size, size_t value_size, cmagic_map_key_comparator_t key_comparator,
49  const cmagic_memory_alloc_packet_t *alloc_packet);
50 
51 void
52 cmagic_map_free(void *map_ptr);
53 
54 typedef struct {
55  const void *key;
56  void *value;
58 
62 typedef struct {
63 
69 
75 
77 
79 cmagic_map_allocate(void *map_ptr, const void *key);
80 
82 cmagic_map_insert(void *map_ptr, const void *key, const void *value);
83 
84 void
85 cmagic_map_erase(void *map_ptr, const void *key, cmagic_map_erase_destructor_t destructor);
86 
87 void
88 cmagic_map_clear(void *map_ptr);
89 
90 size_t
91 cmagic_map_size(void *map_ptr);
92 
94 cmagic_map_first(void *map_ptr);
95 
97 cmagic_map_last(void *map_ptr);
98 
100 cmagic_map_iterator_next(cmagic_map_iterator_t iterator);
101 
103 cmagic_map_iterator_prev(cmagic_map_iterator_t iterator);
104 
106 cmagic_map_find(void *map_ptr, const void *key);
107 
109 cmagic_map_get_alloc_packet(void *map_ptr);
110 
118 #define CMAGIC_MAP(key_type) key_type*
119 
130 #define CMAGIC_MAP_NEW(key_type, value_type, key_comparator, alloc_packet) ((CMAGIC_MAP(key_type)) \
131  cmagic_map_new(sizeof(key_type), sizeof(value_type), (key_comparator), (alloc_packet)))
132 
138 #define CMAGIC_MAP_FREE(cmagic_map) cmagic_map_free((void*)(cmagic_map))
139 
151 #define CMAGIC_MAP_ALLOCATE(cmagic_map, key) \
152  (CMAGIC_UTILS_ASSERT_SAME_TYPE(*(cmagic_map), *(key)), \
153  cmagic_map_allocate((void*)(cmagic_map), (key)))
154 
162 #define CMAGIC_MAP_INSERT(cmagic_map, key, value) \
163  (CMAGIC_UTILS_ASSERT_SAME_TYPE(*(cmagic_map), *(key)), \
164  cmagic_map_insert((void*)(cmagic_map), (key), (value)))
165 
175 #define CMAGIC_MAP_ERASE_EXT(cmagic_map, key, destructor) \
176  (CMAGIC_UTILS_ASSERT_SAME_TYPE(*(cmagic_map), *(key)), \
177  cmagic_map_erase((void*)(cmagic_map), (key), (destructor)))
178 
186 #define CMAGIC_MAP_ERASE(cmagic_map, key) CMAGIC_MAP_ERASE_EXT(cmagic_map, key, NULL)
187 
192 #define CMAGIC_MAP_CLEAR(cmagic_map) cmagic_map_clear((void*)(cmagic_map))
193 
199 #define CMAGIC_MAP_SIZE(cmagic_map) cmagic_map_size((void*)(cmagic_map))
200 
208 #define CMAGIC_MAP_FIRST(cmagic_map) cmagic_map_first((void*)(cmagic_map))
209 
217 #define CMAGIC_MAP_LAST(cmagic_map) cmagic_map_last((void*)(cmagic_map))
218 
224 #define CMAGIC_MAP_ITERATOR_NEXT(iterator) cmagic_map_iterator_next(iterator)
225 
232 #define CMAGIC_MAP_ITERATOR_PREV(iterator) cmagic_map_iterator_prev(iterator)
233 
240 #define CMAGIC_MAP_FIND(cmagic_map, key) (CMAGIC_UTILS_ASSERT_SAME_TYPE(*(cmagic_map), *(key)), \
241  cmagic_map_find((void*)(cmagic_map), (key)))
242 
250 #define CMAGIC_MAP_GET_KEY(key_type, iterator) \
251  (assert(iterator), assert((iterator)->key), *((const key_type*)(iterator)->key))
252 
260 #define CMAGIC_MAP_GET_VALUE(value_type, iterator) \
261  (assert(iterator), assert((iterator)->value), *((value_type*)(iterator)->value))
262 
268 #define CMAGIC_MAP_GET_ALLOC_PACKET(cmagic_map) \
269  cmagic_map_get_alloc_packet((void*)(cmagic_map))
270 
271 #ifdef __cplusplus
272 } // extern "C"
273 #endif
274 
275 #endif /* CMAGIC_MAP_H */
int(* cmagic_map_key_comparator_t)(const void *key1, const void *key2)
Pointer to a function that compares two keys.
Definition: map.h:36
Portable substitutes of standard malloc() and free() functions.
Set of allocation functions. Used in some CMagic structures to specify a desired memory pool...
Definition: memory.h:187
General purpose utilities.
bool already_exists
true if the element already exists in the map and the map was not modified, false if a new element ha...
Definition: map.h:74
cmagic_map_iterator_t inserted_or_existing
iterator pointing to a new or already existing element or NULL if the allocation of a new element has...
Definition: map.h:68
Definition: map.h:54
Map insertion result.
Definition: map.h:62
void(* cmagic_map_erase_destructor_t)(void *key, void *value)
User defined additional tasks to be executed right before map element deletion.
Definition: map.h:45