LibCDS
hash_map.h
Go to the documentation of this file.
1 /**
2  * @file hash_map.h The unordered map to store key value pairs.
3  */
4 
5 #ifndef _HASH_MAP_H_
6 #define _HASH_MAP_H_
7 
8 #include "../util.h"
9 
10 /** HashMapData is the data type for the container private information. */
11 typedef struct _HashMapData HashMapData;
12 
13 /** The implementation for hash map. */
14 typedef struct _HashMap {
15  /** The container private information */
17 
18  /** Insert a key value pair into the map.
19  @see HashMapPut */
20  int32_t (*put) (struct _HashMap*, Pair*, size_t);
21 
22  /** Retrieve the value corresponding to the designated key.
23  @see HashMapGet */
24  int32_t (*get) (struct _HashMap*, Key, size_t, Value*);
25 
26  /** Check if the map contains the designated key.
27  @see HashMapFind */
28  int32_t (*find) (struct _HashMap*, Key, size_t);
29 
30  /** Delete the key value pair corresponding to the designated key.
31  @see HashMapRemove */
32  int32_t (*remove) (struct _HashMap*, Key, size_t);
33 
34  /** Return the number of stored key value pairs.
35  @see HashMapSize */
36  int32_t (*size) (struct _HashMap*);
37 
38  /** Iterate through the map to retrieve each key value pair.
39  @see HashMapIterate */
40  int32_t (*iterate) (struct _HashMap*, bool, Pair**);
41 
42  /** Set the custom key value pair resource clean method.
43  @see HashMapSetDestroy */
44  int32_t (*set_destroy) (struct _HashMap*, void (*) (Pair*));
45 
46  /** Set the custom hash function.
47  @see HashMapSetHash */
48  int32_t (*set_hash) (struct _HashMap*, uint32_t (*) (Key, size_t));
49 } HashMap;
50 
51 
52 /*===========================================================================*
53  * Definition for the exported member operations *
54  *===========================================================================*/
55 /**
56  * @brief The constructor for HashMap.
57  *
58  * @param ppObj The double pointer to the to be constructed map
59  *
60  * @retval SUCC
61  * @retval ERR_NOMEM Insufficient memory for map construction
62  */
63 int32_t HashMapInit(HashMap **ppObj);
64 
65 /**
66  * @brief The destructor for HashMap.
67  *
68  * If the custom resource clean method is set, it also runs the clean method
69  * for each pair.
70  *
71  * @param ppObj The double pointer to the to be destructed map
72  */
73 void HashMapDeinit(HashMap **ppObj);
74 
75 /**
76  * @brief Insert a key value pair into the map.
77  *
78  * This function inserts a key value pair into the map. If the hash key of the
79  * designated pair is the same with a certain one stored in the map, that pair
80  * will be replaced. Also, if the custom resource clean method is set, it runs
81  * the resource clean method for the replaced pair.
82  *
83  * @param self The pointer to HashMap structure
84  * @param pPair The pointer to the designated pair
85  * @param size Key size in bytes
86  *
87  * @retval SUCC
88  * @retval ERR_NOINIT Uninitialized container
89  * @retval ERR_NOMEM Insufficient memory for map extension
90  * @retval ERR_KEYSIZE Invalid key size
91  */
92 int32_t HashMapPut(HashMap *self, Pair *pPair, size_t size);
93 
94 /**
95  * @brief Retrieve the value corresponding to the designated key.
96  *
97  * This function retrieves the value corresponding to the designated key from
98  * the map. If the key can be found, the value will be returned by the fourth
99  * parameter. Otherwise, the error code is returned and the fourth parameter is
100  * updated with NULL.
101  *
102  * @param self The pointer to HashMap structure
103  * @param key The designated key
104  * @param size Key size in bytes
105  * @param pValue The pointer to the returned value
106  *
107  * @retval SUCC
108  * @retval ERR_NOINIT Uninitialized container
109  * @retval ERR_NODATA No map entry can be found
110  * @retval ERR_GET Invalid parameter to store returned value
111  * @retval ERR_KEYSIZE Invalid key size
112  *
113  * @note The key should be the pointer to the data you plan to hash for.
114  */
115 int32_t HashMapGet(HashMap *self, Key key, size_t size, Value *pValue);
116 
117 /**
118  * @brief Check if the map contains the designated key.
119  *
120  * @param self The pointer to HashMap structure
121  * @param key The designated key
122  * @param size Key size in bytes
123  *
124  * @retval SUCC The key can be found
125  * @retval NOKEY The key cannot be found
126  * @retval ERR_NOINIT Uninitialized container
127  * @retval ERR_KEYSIZE Invalid key size
128  *
129  * @note The key should be the pointer to the data you plan to hash for.
130  */
131 int32_t HashMapFind(HashMap *self, Key key, size_t size);
132 
133 /**
134  * @brief Delete the key value pair corresponding to the designated key.
135  *
136  * This function deletes the key value pair corresponding to the designated key.
137  * If the custom resource clean method is set, it also runs the clean method for
138  * the deleted pair.
139  *
140  * @param self The pointer to HashMap structure
141  * @param key The designated key
142  * @param size Key size in bytes
143  *
144  * @retval SUCC
145  * @retval ERR_NOINIT Uninitialized container
146  * @retval ERR_NODATA No map entry can be found
147  * @retval ERR_KEYSIZE Invalid key size
148  *
149  * @note The key should be the pointer to the data you plan to hash for.
150  */
151 int32_t HashMapRemove(HashMap *self, Key key, size_t size);
152 
153 /**
154  * @brief Return the number of stored key value pairs.
155  *
156  * @param self The pointer to HashMap structure
157  *
158  * @return The number of stored pairs
159  * @retval ERR_NOINIT Uninitialized container
160  */
161 int32_t HashMapSize(HashMap *self);
162 
163 /**
164  * @brief Iterate through the map to retrieve each key value pair.
165  *
166  * Before iterating through the map, it is necessary to pass:
167  * - bReset = true
168  * - pPair = NULL
169  * for iterator initialization.
170  *
171  * After initialization, you can pass:
172  * - bReset = false
173  * - pPair = the pointer to get the returned pair at each iteration.
174  *
175  * @param self The pointer to HashMap structure
176  * @param bReset The knob to restart the iteration
177  * @param ppPair The double pointer to the returned pair
178  *
179  * @retval SUCC Iterator initialized successfully
180  * @retval CONTINUE Iteration in progress
181  * @retval END Iteration terminiated
182  * @retval ERR_NOINIT Uninitialized container
183  * @retval ERR_GET Invalid parameter to store returned pair
184  *
185  * @note Please do not insert or delete pairs during map traversal
186  */
187 int32_t HashMapIterate(HashMap *self, bool bReset, Pair **ppPair);
188 
189 /**
190  * @brief Set the custom key value pair resource clean method.
191  *
192  * @param self The pointer to HashMap structure
193  * @param pFunc The function pointer to the custom method
194  *
195  * @retval SUCC
196  * @retval ERR_NOINIT Uninitialized container
197  */
198 int32_t HashMapSetDestroy(HashMap *self, void (*pFunc) (Pair*));
199 
200 /**
201  * @brief Set the custom hash function.
202  *
203  * The default hash function is HashMurMur32.
204  *
205  * @param self The pointer to HashMap structure
206  * @param pFunc The function pointer to the custom method
207  *
208  * @retval SUCC
209  * @retval ERR_NOINIT Uninitialized container
210  */
211 int32_t HashMapSetHash(HashMap *self, uint32_t (*pFunc) (Key, size_t));
212 
213 #endif
int32_t HashMapRemove(HashMap *self, Key key, size_t size)
Delete the key value pair corresponding to the designated key.
int32_t HashMapPut(HashMap *self, Pair *pPair, size_t size)
Insert a key value pair into the map.
void HashMapDeinit(HashMap **ppObj)
The destructor for HashMap.
int32_t HashMapGet(HashMap *self, Key key, size_t size, Value *pValue)
Retrieve the value corresponding to the designated key.
int32_t HashMapSize(HashMap *self)
Return the number of stored key value pairs.
int32_t HashMapSetDestroy(HashMap *self, void(*pFunc)(Pair *))
Set the custom key value pair resource clean method.
struct _HashMapData HashMapData
HashMapData is the data type for the container private information.
Definition: hash_map.h:11
int32_t HashMapIterate(HashMap *self, bool bReset, Pair **ppPair)
Iterate through the map to retrieve each key value pair.
int32_t HashMapSetHash(HashMap *self, uint32_t(*pFunc)(Key, size_t))
Set the custom hash function.
int32_t HashMapFind(HashMap *self, Key key, size_t size)
Check if the map contains the designated key.
The implementation for hash map.
Definition: hash_map.h:14
HashMapData * pData
The container private information.
Definition: hash_map.h:16
int32_t HashMapInit(HashMap **ppObj)
The constructor for HashMap.