LibCDS
tree_map.h
Go to the documentation of this file.
1 /**
2  * @file tree_map.h The ordered map to store key value pairs.
3  */
4 
5 #ifndef _TREE_MAP_H_
6 #define _TREE_MAP_H_
7 
8 #include "../util.h"
9 
10 /** TreeMapData is the data type for the container private information. */
11 typedef struct _TreeMapData TreeMapData;
12 
13 /** The implementation for ordered map. */
14 typedef struct _TreeMap {
15  /** The container private information */
17 
18  /** Insert a key value pair into the map.
19  @see TreeMapPut */
20  int32_t (*put) (struct _TreeMap*, Pair*);
21 
22  /** Retrieve the value corresponding to the designated key.
23  @see TreeMapGet */
24  int32_t (*get) (struct _TreeMap*, Key, Value*);
25 
26  /** Check if the map contains the designated key.
27  @see TreeMapFind */
28  int32_t (*find) (struct _TreeMap*, Key);
29 
30  /** Delete the key value pair corresponding to the designated key.
31  @see TreeMapRemove */
32  int32_t (*remove) (struct _TreeMap*, Key);
33 
34  /** Return the number of stored key value pairs.
35  @see TreeMapSize */
36  int32_t (*size) (struct _TreeMap*);
37 
38  /** Retrieve the key value pair with the minimum order from the map.
39  @see TreeMapMinimum */
40  int32_t (*minimum) (struct _TreeMap*, Pair**);
41 
42  /** Retrieve the key value pair with the maximum order from the map.
43  @see TreeMapMaximum */
44  int32_t (*maximum) (struct _TreeMap*, Pair**);
45 
46  /** Retrieve the key value pair which is the predecessor of the given key.
47  @see TreeMapPredecessor */
48  int32_t (*predecessor) (struct _TreeMap*, Key, Pair**);
49 
50  /** Retrieve the key value pair which is the successor of the given key.
51  @see TreeMapSuccessor */
52  int32_t (*successor) (struct _TreeMap*, Key, Pair**);
53 
54  /** Iterate through the map from the minimum order to the maximum order.
55  @see TreeMapIterate */
56  int32_t (*iterate) (struct _TreeMap*, bool, Pair**);
57 
58  /** Iterate through the map from the maximum order to the minimum order.
59  @see TreeMapReverseIterate */
60  int32_t (*reverse_iterate) (struct _TreeMap*, bool, Pair**);
61 
62  /** Set the custom key comparison method.
63  @see TreeMapSetCompare */
64  int32_t (*set_compare) (struct _TreeMap*, int32_t (*) (Key, Key));
65 
66  /** Set the custom key value pair resource clean method.
67  @see TreeMapSetDestroy */
68  int32_t (*set_destroy) (struct _TreeMap*, void (*) (Pair*));
69 } TreeMap;
70 
71 
72 /*===========================================================================*
73  * Definition for the exported member operations *
74  *===========================================================================*/
75 /**
76  * @brief The constructor for TreeMap.
77  *
78  * @param ppObj The double pointer to the to be constructed map
79  *
80  * @retval SUCC
81  * @retval ERR_NOMEM Insufficient memory for map construction
82  */
83 int32_t TreeMapInit(TreeMap **ppObj);
84 
85 /**
86  * @brief The destructor for TreeMap.
87  *
88  * If the custom resource clean method is set, it also runs the clean method
89  * for each pair.
90  *
91  * @param ppObj The double pointer to the to be destructed map
92  */
93 void TreeMapDeinit(TreeMap **ppObj);
94 
95 /**
96  * @brief Insert a key value pair into the map.
97  *
98  * This function inserts a key value pair into the map. If the order of the
99  * designated pair is the same with a certain one stored in the map, that pair
100  * will be replaced. Also, if the custom resource clean method is set, it runs
101  * the clean method for the replaced pair.
102  *
103  * @param self The pointer to TreeMap structure
104  * @param pPair The pointer to the designated pair
105  *
106  * @retval SUCC
107  * @retval ERR_NOINIT Uninitialized container
108  * @retval ERR_NOMEM Insufficient memory for map extension
109  */
110 int32_t TreeMapPut(TreeMap *self, Pair *pPair);
111 
112 /**
113  * @brief Retrieve the value corresponding to the designated key.
114  *
115  * This function retrieves the value corresponding to the designated key from
116  * the map. If the key can be found, the value will be returned by the third
117  * parameter. Otherwise, the error code in returned and the third parameter is
118  * updated with NULL.
119  *
120  * @param self The pointer to TreeMap structure
121  * @param key The designated key
122  * @param pValue The pointer to the returned value
123  *
124  * @retval SUCC
125  * @retval ERR_NOINIT Uninitialized container
126  * @retval ERR_NODATA No map entry can be found
127  * @retval ERR_GET Invalid parameter to store returned value
128  */
129 int32_t TreeMapGet(TreeMap *self, Key key, Value *pValue);
130 
131 /**
132  * @brief Check if the map contains the designated key.
133  *
134  * @param self The pointer to TreeMap structure
135  * @param key The designated key
136  *
137  * @retval SUCC The key can be found
138  * @retval NOKEY The key cannot be found
139  * @retval ERR_NOINIT Uninitialized container
140  */
141 int32_t TreeMapFind(TreeMap *self, Key key);
142 
143 /**
144  * @brief Delete the key value pair corresponding to the designated key.
145  *
146  * This function deletes the key value pair corresponding to the designated key.
147  * If the custom resource clean method is set, it runs the clean methods for
148  * the deleted pair.
149  *
150  * @param self The pointer to TreeMap structure
151  * @param key The designated key
152  *
153  * @retval SUCC
154  * @retval ERR_NOINIT Uninitialized container
155  * @retval ERR_NODATA No map entry can be found
156  */
157 int32_t TreeMapRemove(TreeMap *self, Key key);
158 
159 /**
160  * @brief Return the number of stored key value pairs.
161  *
162  * @param self The pointer to TreeMap structure
163  *
164  * @return The number of stored pairs
165  * @retval ERR_NOINIT Uninitialized container
166  */
167 int32_t TreeMapSize(TreeMap *self);
168 
169 /**
170  * @brief Retrieve the key value pair with the minimum order from the map.
171  *
172  * @param self The pointer to TreeMap structure
173  * @param ppPair The double pointer to the returned pair
174  *
175  * @retval SUCC
176  * @retval ERR_NOINIT Uninitialized container
177  * @retval ERR_IDX Empty map
178  * @retval ERR_GET Invalid parameter to store returned pair
179  */
180 int32_t TreeMapMinimum(TreeMap *self, Pair **ppPair);
181 
182 /**
183  * @brief Retrieve the key value pair with the maximum order from the map.
184  *
185  * @param self The pointer to TreeMap structure
186  * @param ppPair The double pointer to the returned pair
187  *
188  * @retval SUCC
189  * @retval ERR_NOINIT Uninitialized container
190  * @retval ERR_IDX Empty map
191  * @retval ERR_GET Invalid parameter to store returned pair
192  */
193 int32_t TreeMapMaximum(TreeMap *self, Pair **ppPair);
194 
195 /**
196  * @brief Retrieve the key value pair which is the predecessor of the given key.
197  *
198  * @param self The pointer to TreeMap structure
199  * @param key The designated key
200  * @param ppPair The double pointer to the returned pair
201  *
202  * @retval SUCC
203  * @retval ERR_NOINIT Uninitialized container
204  * @retval ERR_NODATA Non-existent immediate predecessor
205  * @retval ERR_GET Invalid parameter to store returned pair
206  */
207 int32_t TreeMapPredecessor(TreeMap *self, Key key, Pair **ppPair);
208 
209 /**
210  * @brief Retrieve the key value pair which is the successor of the given key.
211  *
212  * @param self The pointer to TreeMap structure
213  * @param key The designated key
214  * @param ppPair The double pointer to the returned pair
215  *
216  * @retval SUCC
217  * @retval ERR_NOINIT Uninitialized container
218  * @retval ERR_NODATA Non-existent immediate successor
219  * @retval ERR_GET Invalid parameter to store returned pair
220  */
221 int32_t TreeMapSuccessor(TreeMap *self, Key key, Pair **ppPair);
222 
223 /**
224  * @brief Iterate through the map from the minimum order to the maximum order.
225  *
226  * Before iterating through the map, it is necessary to pass:
227  * - bReset = true
228  * - pPair = NULL
229  * for iterator initialization.
230  *
231  * After initialization, you can pass:
232  * - bReset = false
233  * - pPair = the pointer to get the returned pair at each iteration.
234  *
235  * @param self The pointer to TreeMap structure
236  * @param bReset The knob to restart the iteration
237  * @param ppPair The double pointer to the returned pair
238  *
239  * @retval SUCC Iterator initialized successfully
240  * @retval CONTINUE Iteration in progress
241  * @retval END Iteration terminiated
242  * @retval ERR_NOINIT Uninitialized container
243  * @retval ERR_GET Invalid parameter to store returned pair
244  */
245 int32_t TreeMapIterate(TreeMap *self, bool bReset, Pair **ppPair);
246 
247 /**
248  * @brief Reversely iterate through the map from the minimum order to the
249  * maximum order.
250  *
251  * Before iterating through the map, it is necessary to pass:
252  * - bReset = true
253  * - pPair = NULL
254  * for iterator initialization.
255  *
256  * After initialization, you can pass:
257  * - bReset = false
258  * - pPair = the pointer to get the returned pair at each iteration.
259  *
260  * @param self The pointer to TreeMap structure
261  * @param bReset The knob to restart the iteration
262  * @param ppPair The double pointer to the returned pair
263  *
264  * @retval SUCC Iterator initialized successfully
265  * @retval CONTINUE Iteration in progress
266  * @retval END Iteration terminiated
267  * @retval ERR_NOINIT Uninitialized container
268  * @retval ERR_GET Invalid parameter to store returned pair
269  */
270 int32_t TreeMapReverseIterate(TreeMap *self, bool bReset, Pair **ppPair);
271 
272 /**
273  * @brief Set the custom key comparison method.
274  *
275  * @param self The pointer to TreeMap structure
276  * @param pFunc The function pointer to the custom method
277  *
278  * @retval SUCC
279  * @retval ERR_NOINIT Uninitialized container
280  */
281 int32_t TreeMapSetCompare(TreeMap *self, int32_t (*pFunc) (Key, Key));
282 
283 /**
284  * @brief Set the custom key value pair resource clean method.
285  *
286  * @param self The pointer to TreeMap structure
287  * @param pFunc The function pointer to the custom method
288  *
289  * @retval SUCC
290  * @retval ERR_NOINIT Uninitialized container
291  */
292 int32_t TreeMapSetDestroy(TreeMap *self, void (*pFunc) (Pair*));
293 
294 #endif
The implementation for ordered map.
Definition: tree_map.h:14
int32_t TreeMapPredecessor(TreeMap *self, Key key, Pair **ppPair)
Retrieve the key value pair which is the predecessor of the given key.
int32_t TreeMapPut(TreeMap *self, Pair *pPair)
Insert a key value pair into the map.
int32_t TreeMapSuccessor(TreeMap *self, Key key, Pair **ppPair)
Retrieve the key value pair which is the successor of the given key.
int32_t TreeMapMinimum(TreeMap *self, Pair **ppPair)
Retrieve the key value pair with the minimum order from the map.
int32_t TreeMapSetCompare(TreeMap *self, int32_t(*pFunc)(Key, Key))
Set the custom key comparison method.
int32_t TreeMapIterate(TreeMap *self, bool bReset, Pair **ppPair)
Iterate through the map from the minimum order to the maximum order.
int32_t TreeMapFind(TreeMap *self, Key key)
Check if the map contains the designated key.
TreeMapData * pData
The container private information.
Definition: tree_map.h:16
int32_t TreeMapRemove(TreeMap *self, Key key)
Delete the key value pair corresponding to the designated key.
struct _TreeMapData TreeMapData
TreeMapData is the data type for the container private information.
Definition: tree_map.h:11
int32_t TreeMapMaximum(TreeMap *self, Pair **ppPair)
Retrieve the key value pair with the maximum order from the map.
int32_t TreeMapGet(TreeMap *self, Key key, Value *pValue)
Retrieve the value corresponding to the designated key.
int32_t TreeMapSetDestroy(TreeMap *self, void(*pFunc)(Pair *))
Set the custom key value pair resource clean method.
int32_t TreeMapSize(TreeMap *self)
Return the number of stored key value pairs.
int32_t TreeMapReverseIterate(TreeMap *self, bool bReset, Pair **ppPair)
Reversely iterate through the map from the minimum order to the maximum order.
void TreeMapDeinit(TreeMap **ppObj)
The destructor for TreeMap.
int32_t TreeMapInit(TreeMap **ppObj)
The constructor for TreeMap.