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 "bits/callback.h" // for Transformer, Comparator, Iterator
15 #include "tree.h" // for Tree
19 Tree tre; // search tree to manage data
20 pthread_rwlock_t tlk; // lock to protect tree
21 bool cnl; // cancel state
22 pthread_rwlock_t clk; // lock to protect cancel state
25 void map_ini(Map *map);
29 The map should be uninitialized before passed to this function.
30 This function should be called before any other function is called on the tree.
33 void map_dst(Map *map);
37 The map should not be used afterwards.
38 Make sure to cancel the map before destroying it, to avoid memory leaks.
41 void map_cnl(Map *map, Iterator iter, void *arg, Transformer trans, TreeTraversionOrder order);
44 Cancels and clears the map.
46 All subsequent calls to add, get and del have no effect and return NULL.
48 Traverses the map and deletes all elements.
49 Calls func on every element, with the extra argument arg.
51 The map is empty afterwards.
52 If no callback is given, the traversion order is irrelevant.
55 void *map_add(Map *map, void *dat, Comparator cmp, Transformer trans);
58 Add an element to the map.
60 If an equal element is already in the tree, return it and don't add anything.
61 Otherwise, return added element.
64 void *map_get(Map *map, void *key, Comparator cmp, Transformer trans);
67 Get an element from the map, or return NULL if none found.
70 void *map_del(Map *map, void *key, Comparator cmp, Transformer trans);
73 Delete an element from the map and return it, or NULL if none found.
76 void map_trv(Map *map, Iterator iter, void *arg, Transformer trans, TreeTraversionOrder order);
80 Calls iter on every element, with the extra argument arg.