5 Thread safe map/set, using a Tree.
6 Useful for managing big amounts of objects with add, get and del access from multiple threads.
9 #ifndef _DRAGONSTD_MAP_H_ // include guard
10 #define _DRAGONSTD_MAP_H_
12 #include <stdbool.h> // for bool
13 #include <pthread.h> // for pthread_rwlock_t
14 #include "tree.h" // for Tree
18 Tree tre; // search tree to manage data
19 pthread_rwlock_t tlk; // lock to protect tree
20 bool cnl; // cancel state
21 pthread_rwlock_t clk; // lock to protect cancel state
24 void map_ini(Map *map);
28 The map should be uninitialized before passed to this function.
29 This function should be called before any other function is called on the tree.
32 void map_dst(Map *map);
36 The map should not be used afterwards.
37 Make sure to cancel the map before destroying it, to avoid memory leaks.
40 void map_cnl(Map *map, void *iter, void *arg, void *trans, TreeTraversionOrder order);
43 Cancels and clears the map.
45 All subsequent calls to add, get and del have no effect and return NULL.
47 Traverses the map and deletes all elements.
48 Calls func on every element, with the extra argument arg.
50 The map is empty afterwards.
51 If no callback is given, the traversion order is irrelevant.
54 bool map_add(Map *map, void *key, void *dat, void *cmp, void *trans);
57 Add an element to the map.
59 If an equal element is already in the map, don't add anything.
60 Return whether an element has been added.
63 void *map_get(Map *map, void *key, void *cmp, void *trans);
66 Get an element from the map, or return NULL if none found.
69 bool map_del(Map *map, void *key, void *cmp, void *call, void *arg, void *trans);
72 Delete an element from the map if it is found.
73 Return whether an element has been deleted.
76 void map_trv(Map *map, void *iter, void *arg, void *trans, TreeTraversionOrder order);
80 Calls iter on every element, with the extra argument arg.