cdcontainers  0.1.1
Library of data containers and collections for C programming language.
map.h
Go to the documentation of this file.
1 // The MIT License (MIT)
2 // Copyright (c) 2018 Maksim Andrianov
3 //
4 // Permission is hereby granted, free of charge, to any person obtaining a copy
5 // of this software and associated documentation files (the "Software"), to
6 // deal in the Software without restriction, including without limitation the
7 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8 // sell copies of the Software, and to permit persons to whom the Software is
9 // furnished to do so, subject to the following conditions:
10 //
11 // The above copyright notice and this permission notice shall be included in
12 // all copies or substantial portions of the Software.
13 //
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20 // IN THE SOFTWARE.
30 #ifndef CDCONTAINERS_INCLUDE_CDCONTAINERS_MAP_H
31 #define CDCONTAINERS_INCLUDE_CDCONTAINERS_MAP_H
32 
33 #include <cdcontainers/common.h>
34 #include <cdcontainers/status.h>
36 
37 #include <assert.h>
38 #include <stdarg.h>
39 #include <stdbool.h>
40 
51 struct cdc_map {
52  void *container;
53  const struct cdc_map_table *table;
54 };
55 
61 struct cdc_map_iter {
62  void *iter;
63  const struct cdc_map_iter_table *table;
64 };
65 
66 // Base
89 enum cdc_stat cdc_map_ctor(const struct cdc_map_table *table,
90  struct cdc_map **m, struct cdc_data_info *info);
91 
114 enum cdc_stat cdc_map_ctorl(const struct cdc_map_table *table,
115  struct cdc_map **m, struct cdc_data_info *info,
116  ...);
117 
128 enum cdc_stat cdc_map_ctorv(const struct cdc_map_table *table,
129  struct cdc_map **m, struct cdc_data_info *info,
130  va_list args);
135 void cdc_map_dtor(struct cdc_map *m);
138 // Lookup
151 static inline enum cdc_stat cdc_map_get(struct cdc_map *m, void *key,
152  void **value)
153 {
154  assert(m != NULL);
155 
156  return m->table->get(m->container, key, value);
157 }
158 
167 static inline size_t cdc_map_count(struct cdc_map *m, void *key)
168 {
169  assert(m != NULL);
170 
171  return m->table->count(m->container, key);
172 }
173 
182 static inline void cdc_map_find(struct cdc_map *m, void *key,
183  struct cdc_map_iter *it)
184 {
185  assert(m != NULL);
186 
187  m->table->find(m->container, key, it->iter);
188 }
191 // Capacity
201 static inline size_t cdc_map_size(struct cdc_map *m)
202 {
203  assert(m != NULL);
204 
205  return m->table->size(m->container);
206 }
207 
213 static inline bool cdc_map_empty(struct cdc_map *m)
214 {
215  assert(m != NULL);
216 
217  return m->table->empty(m->container);
218 }
221 // Modifiers
230 static inline void cdc_map_clear(struct cdc_map *m)
231 {
232  assert(m != NULL);
233 
234  m->table->clear(m->container);
235 }
236 
250 static inline enum cdc_stat cdc_map_insert(struct cdc_map *m, void *key,
251  void *value, struct cdc_map_iter *it,
252  bool *inserted)
253 {
254  assert(m != NULL);
255 
256  void *iter = it ? it->iter : NULL;
257  return m->table->insert(m->container, key, value, iter, inserted);
258 }
259 
273 static inline enum cdc_stat cdc_map_insert_or_assign(struct cdc_map *m,
274  void *key, void *value,
275  struct cdc_map_iter *it,
276  bool *inserted)
277 {
278  assert(m != NULL);
279 
280  void *iter = it ? it->iter : NULL;
281  return m->table->insert_or_assign(m->container, key, value, iter, inserted);
282 }
283 
290 static inline size_t cdc_map_erase(struct cdc_map *m, void *key)
291 {
292  assert(m != NULL);
293 
294  return m->table->erase(m->container, key);
295 }
296 
302 static inline void cdc_map_swap(struct cdc_map *a, struct cdc_map *b)
303 {
304  assert(a != NULL);
305  assert(b != NULL);
306  assert(a->table == b->table);
307 
308  CDC_SWAP(void *, a->container, b->container);
309 }
312 // Iterators
322 static inline void cdc_map_begin(struct cdc_map *m, struct cdc_map_iter *it)
323 {
324  assert(m != NULL);
325  assert(it != NULL);
326 
327  m->table->begin(m->container, it->iter);
328 }
329 
335 static inline void cdc_map_end(struct cdc_map *m, struct cdc_map_iter *it)
336 {
337  assert(m != NULL);
338  assert(it != NULL);
339 
340  m->table->end(m->container, it->iter);
341 }
344 // Iterators
373 enum cdc_stat cdc_map_iter_ctor(struct cdc_map *m, struct cdc_map_iter *it);
374 
380 static inline void cdc_map_iter_dtor(struct cdc_map_iter *it)
381 {
382  assert(it != NULL);
383 
384  it->table->dtor(it->iter);
385 }
386 
392 static inline enum cdc_iterator_type cdc_map_iter_type(struct cdc_map_iter *it)
393 {
394  assert(it != NULL);
395 
396  return it->table->type();
397 }
398 
403 static inline void cdc_map_iter_next(struct cdc_map_iter *it)
404 {
405  assert(it != NULL);
406 
407  it->table->next(it->iter);
408 }
409 
414 static inline void cdc_map_iter_prev(struct cdc_map_iter *it)
415 {
416  assert(it != NULL);
417 
418  it->table->prev(it->iter);
419 }
420 
428 static inline bool cdc_map_iter_has_next(struct cdc_map_iter *it)
429 {
430  assert(it != NULL);
431 
432  return it->table->has_next(it->iter);
433 }
434 
442 static inline bool cdc_map_iter_has_prev(struct cdc_map_iter *it)
443 {
444  assert(it != NULL);
445 
446  return it->table->has_prev(it->iter);
447 }
448 
454 static inline void *cdc_map_iter_key(struct cdc_map_iter *it)
455 {
456  assert(it != NULL);
457 
458  return it->table->key(it->iter);
459 }
460 
466 static inline void *cdc_map_iter_value(struct cdc_map_iter *it)
467 {
468  assert(it != NULL);
469 
470  return it->table->value(it->iter);
471 }
472 
478 static inline struct cdc_pair cdc_map_iter_key_value(struct cdc_map_iter *it)
479 {
480  assert(it != NULL);
481 
482  return it->table->key_value(it->iter);
483 }
484 
493 static inline bool cdc_map_iter_is_eq(struct cdc_map_iter *it1,
494  struct cdc_map_iter *it2)
495 {
496  assert(it1 != NULL);
497  assert(it2 != NULL);
498  assert(it1->table == it2->table);
499 
500  return it1->table->eq(it1->iter, it2->iter);
501 }
504 // Short names
505 #ifdef CDC_USE_SHORT_NAMES
506 typedef struct cdc_map map_t;
507 typedef struct cdc_map_iter map_iter_t;
508 
509 // Base
510 #define map_ctor(...) cdc_map_ctor(__VA_ARGS__)
511 #define map_ctorv(...) cdc_map_ctorv(__VA_ARGS__)
512 #define map_ctorl(...) cdc_map_ctorl(__VA_ARGS__)
513 #define map_dtor(...) cdc_map_dtor(__VA_ARGS__)
514 
515 // Lookup
516 #define map_get(...) cdc_map_get(__VA_ARGS__)
517 #define map_count(...) cdc_map_count(__VA_ARGS__)
518 #define map_find(...) cdc_map_find(__VA_ARGS__)
519 
520 // Capacity
521 #define map_size(...) cdc_map_size(__VA_ARGS__)
522 #define map_empty(...) cdc_map_empty(__VA_ARGS__)
523 
524 // Modifiers
525 #define map_clear(...) cdc_map_clear(__VA_ARGS__)
526 #define map_insert(...) cdc_map_insert(__VA_ARGS__)
527 #define map_insert_or_assign(...) cdc_map_insert_or_assign(__VA_ARGS__)
528 #define map_erase(...) cdc_map_erase(__VA_ARGS__)
529 #define map_swap(...) cdc_map_swap(__VA_ARGS__)
530 
531 // Iterators
532 #define map_begin(...) cdc_map_begin(__VA_ARGS__)
533 #define map_end(...) cdc_map_end(__VA_ARGS__)
534 
535 // Iterators
536 #define map_iter_ctor(...) cdc_map_iter_ctor(__VA_ARGS__)
537 #define map_iter_dtor(...) cdc_map_iter_dtor(__VA_ARGS__)
538 #define map_iter_next(...) cdc_map_iter_next(__VA_ARGS__)
539 #define map_iter_has_next(...) cdc_map_iter_has_next(__VA_ARGS__)
540 #define map_iter_key(...) cdc_map_iter_key(__VA_ARGS__)
541 #define map_iter_value(...) cdc_map_iter_value(__VA_ARGS__)
542 #define map_iter_key_value(...) cdc_map_iter_key_value(__VA_ARGS__)
543 #define map_iter_is_eq(...) cdc_map_iter_is_eq(__VA_ARGS__)
544 #endif
545 
546 #endif // CDCONTAINERS_INCLUDE_CDCONTAINERS_MAP_H
void(* find)(void *cntr, void *key, void *it)
Definition: imap.h:66
size_t(* count)(void *cntr, void *key)
Definition: imap.h:65
static void cdc_map_iter_next(struct cdc_map_iter *it)
Advances the iterator to the next element in the map.
Definition: map.h:403
static size_t cdc_map_erase(struct cdc_map *m, void *key)
Removes the element (if one exists) with the key equivalent to key.
Definition: map.h:290
bool(* has_next)(void *it)
Definition: imap.h:47
static void cdc_map_iter_prev(struct cdc_map_iter *it)
Advances the iterator to the previous element in the map.
Definition: map.h:414
enum cdc_stat cdc_map_ctor(const struct cdc_map_table *table, struct cdc_map **m, struct cdc_data_info *info)
Constructs an empty map.
enum cdc_stat(* insert)(void *cntr, void *key, void *value, void *it, bool *inserted)
Definition: imap.h:70
void * iter
Definition: map.h:62
enum cdc_stat(* get)(void *cntr, void *key, void **value)
Definition: imap.h:64
bool(* has_prev)(void *it)
Definition: imap.h:48
void(* end)(void *cntr, void *it)
Definition: imap.h:77
static void cdc_map_end(struct cdc_map *m, struct cdc_map_iter *it)
Initializes the iterator to the end.
Definition: map.h:335
void *(* value)(void *it)
Definition: imap.h:50
static bool cdc_map_iter_has_prev(struct cdc_map_iter *it)
Returns true if there is at least one element behind the iterator, i.e. the iterator is not at the fr...
Definition: map.h:442
The cdc_map is service struct.
Definition: map.h:51
void(* begin)(void *cntr, void *it)
Definition: imap.h:76
static void cdc_map_iter_dtor(struct cdc_map_iter *it)
Destroys the iterator. It should be called after the iterator is no longer needed. Releases resources.
Definition: map.h:380
static bool cdc_map_iter_is_eq(struct cdc_map_iter *it1, struct cdc_map_iter *it2)
Returns false if the iterator |it1| equal to the iterator |it2|, otherwise returns false...
Definition: map.h:493
void(* next)(void *it)
Definition: imap.h:45
The cdc_map_table is a map interface.
enum cdc_stat(* insert_or_assign)(void *cntr, void *key, void *value, void *it, bool *inserted)
Definition: imap.h:72
The cdc_map_table struct.
Definition: imap.h:60
static void * cdc_map_iter_value(struct cdc_map_iter *it)
Returns an item&#39;s value.
Definition: map.h:466
static void cdc_map_find(struct cdc_map *m, void *key, struct cdc_map_iter *it)
Finds an element with key equivalent to key.
Definition: map.h:182
static size_t cdc_map_count(struct cdc_map *m, void *key)
Returns the number of elements with key that compares equal to the specified argument key...
Definition: map.h:167
const struct cdc_map_iter_table * table
Definition: map.h:63
static void cdc_map_clear(struct cdc_map *m)
Removes all the elements from the map.
Definition: map.h:230
enum cdc_stat cdc_map_iter_ctor(struct cdc_map *m, struct cdc_map_iter *it)
Constructs a map iterator. Should be called for each new iterator.
cdc_iterator_type
Definition: common.h:115
static struct cdc_pair cdc_map_iter_key_value(struct cdc_map_iter *it)
Returns a pair, where first - key, second - value.
Definition: map.h:478
Definition: common.h:60
const struct cdc_map_table * table
Definition: map.h:53
enum cdc_stat cdc_map_ctorv(const struct cdc_map_table *table, struct cdc_map **m, struct cdc_data_info *info, va_list args)
Constructs a map, initialized by args. The last item must be CDC_END.
#define CDC_SWAP(T, x, y)
Definition: common.h:40
enum cdc_stat cdc_map_ctorl(const struct cdc_map_table *table, struct cdc_map **m, struct cdc_data_info *info,...)
Constructs a map, initialized by an variable number of pointers on cdc_pair&#39;s(first - key...
The cdc_map_iter_table struct.
Definition: imap.h:41
static bool cdc_map_iter_has_next(struct cdc_map_iter *it)
Returns true if there is at least one element ahead of the iterator, i.e. the iterator is not at the ...
Definition: map.h:428
void * container
Definition: map.h:52
bool(* eq)(void *it1, void *it2)
Definition: imap.h:52
static void * cdc_map_iter_key(struct cdc_map_iter *it)
Returns an item&#39;s key.
Definition: map.h:454
cdc_stat
Definition: status.h:24
void(* dtor)(void *it)
Definition: imap.h:43
static void cdc_map_swap(struct cdc_map *a, struct cdc_map *b)
Swaps maps a and b. This operation is very fast and never fails.
Definition: map.h:302
static enum cdc_stat cdc_map_insert(struct cdc_map *m, void *key, void *value, struct cdc_map_iter *it, bool *inserted)
Inserts an element into the container, if the container doesn&#39;t already contain an element with an eq...
Definition: map.h:250
void(* prev)(void *it)
Definition: imap.h:46
The cdc_map_iter is service struct.
Definition: map.h:61
void cdc_map_dtor(struct cdc_map *m)
Destroys the map.
bool(* empty)(void *cntr)
Definition: imap.h:68
static enum cdc_stat cdc_map_get(struct cdc_map *m, void *key, void **value)
Returns a value that is mapped to a key. If the key does not exist, then NULL will return...
Definition: map.h:151
void *(* key)(void *it)
Definition: imap.h:49
static enum cdc_stat cdc_map_insert_or_assign(struct cdc_map *m, void *key, void *value, struct cdc_map_iter *it, bool *inserted)
Inserts an element or assigns to the current element if the key already exists.
Definition: map.h:273
static enum cdc_iterator_type cdc_map_iter_type(struct cdc_map_iter *it)
Returns a type of iterator.
Definition: map.h:392
enum cdc_iterator_type(* type)()
Definition: imap.h:44
size_t(* size)(void *cntr)
Definition: imap.h:67
struct cdc_pair(* key_value)(void *it)
Definition: imap.h:51
size_t(* erase)(void *cntr, void *key)
Definition: imap.h:74
static size_t cdc_map_size(struct cdc_map *m)
Returns the number of items in the map.
Definition: map.h:201
The cdc_data_info struct used to initialize contaners.
Definition: common.h:71
void(* clear)(void *cntr)
Definition: imap.h:69
static void cdc_map_begin(struct cdc_map *m, struct cdc_map_iter *it)
Initializes the iterator to the beginning.
Definition: map.h:322
static bool cdc_map_empty(struct cdc_map *m)
Checks if the map has no elements.
Definition: map.h:213