LibCDS
hash_set.h
Go to the documentation of this file.
1 /**
2  * @file hash_set.h The unordered set to store unique keys.
3  */
4 
5 #ifndef _HASH_SET_H_
6 #define _HASH_SET_H_
7 
8 #include "../util.h"
9 
10 /** HashSetData is the data type for the container private information. */
11 typedef struct _HashSetData HashSetData;
12 
13 /** The implementation for hash set. */
14 typedef struct _HashSet {
15  /** The container private information */
17 
18  /** Insert a key into the set.
19  @see HashSetAdd */
20  int32_t (*add) (struct _HashSet*, Key, size_t);
21 
22  /** Check if the set contains the designated key.
23  @see HashSetFind */
24  int32_t (*find) (struct _HashSet*, Key, size_t);
25 
26  /** Delete the designated key from the set.
27  @see HashSetRemove */
28  int32_t (*remove) (struct _HashSet*, Key, size_t);
29 
30  /** Return the number of stored unique keys.
31  @see HashSetSize */
32  int32_t (*size) (struct _HashSet*);
33 
34  /** Iterate through the set to retrieve each key.
35  @see HashSetIterate */
36  int32_t (*iterate) (struct _HashSet*, bool, Key*);
37 
38  /** Set the custom key resource clean method.
39  @see HashSetSetDestroy */
40  int32_t (*set_destroy) (struct _HashSet*, void (*) (Key));
41 
42  /** Set the custom hash function.
43  @see HashSetSetHash */
44  int32_t (*set_hash) (struct _HashSet*, uint32_t (*) (Key, size_t));
45 } HashSet;
46 
47 
48 /*===========================================================================*
49  * Definition for the exported member operations *
50  *===========================================================================*/
51 /**
52  * @brief The constructor for HashSet.
53  *
54  * @param ppObj The double pointer to the to be constructed set
55  *
56  * @retval SUCC
57  * @retval ERR_NOMEM Insufficient memory for set construction
58  */
59 int32_t HashSetInit(HashSet **ppObj);
60 
61 /**
62  * @brief The destructor for HashSet.
63  *
64  * If the custom resource clean method is set, it also runs the clean method
65  * for each pair.
66  *
67  * @param ppObj The double pointer to the to be destructed set
68  */
69 void HashSetDeinit(HashSet **ppObj);
70 
71 /**
72  * @brief Insert a key into the set.
73  *
74  * This function inserts a key into the set. If the designated key is the same
75  * with a certain one stored in the set, that key will be replaced to maintain
76  * the element uniqueness. Also, if the custom resource clean method is set, it
77  * runs the resource clean method for the replaced key.
78  *
79  * @param self The pointer to HashSet structure
80  * @param key The designated key
81  * @param size Key size in bytes
82  *
83  * @retval SUCC
84  * @retval ERR_NOINIT Uninitialized container
85  * @retval ERR_NOMEM Insufficient memory for set extension
86  * @retval ERR_KEYSIZE Invalid key size
87  *
88  * @note The key should be the pointer to the data you plan to hash for.
89  */
90 int32_t HashSetAdd(HashSet *self, Key key, size_t size);
91 
92 /**
93  * @brief Check if the set contains the designated key.
94  *
95  * @param self The pointer to HashSet structure
96  * @param key The designated key
97  * @param size Key size in bytes
98  *
99  * @retval SUCC The key can be found
100  * @retval NOKEY The key cannot be found
101  * @retval ERR_NOINIT Uninitialized container
102  * @retval ERR_KEYSIZE Invalid key size
103  *
104  * @note The key should be the pointer to the data you plan to hash for.
105  */
106 int32_t HashSetFind(HashSet *self, Key key, size_t size);
107 
108 /**
109  * @brief Delete the designated key from the set.
110  *
111  * This function deletes the designated key from the set. If the custom resource
112  * clean method is set, it also runs the clean method for the deleted key.
113  *
114  * @param self The pointer to HashSet structure
115  * @param key The designated key
116  * @param size Key size in bytes
117  *
118  * @retval SUCC
119  * @retval ERR_NOINIT Uninitialized container
120  * @retval ERR_NODATA No set entry can be found
121  * @retval ERR_KEYSIZE Invalid key size
122  *
123  * @note The key should be the pointer to the data you plan to hash for.
124  */
125 int32_t HashSetRemove(HashSet *self, Key key, size_t size);
126 
127 /**
128  * @brief Return the number of stored unique keys.
129  *
130  * @param self The pointer to HashSet structure
131  *
132  * @return The number of stored pairs
133  * @retval ERR_NOINIT Uninitialized container
134  */
135 int32_t HashSetSize(HashSet *self);
136 
137 /**
138  * @brief Iterate through the set to retrieve each key.
139  *
140  * Before iterating through the set, it is necessary to pass:
141  * - bReset = true
142  * - pKey = NULL
143  * for iterator initialization.
144  *
145  * After initialization, you can pass:
146  * - bReset = false
147  * - pKey = the pointer to get the returned key at each iteration.
148  *
149  * @param self The pointer to HashSet structure
150  * @param bReset The knob to restart the iteration
151  * @param pKey The pointer to the returned key
152  *
153  * @retval SUCC Iterator initialized successfully
154  * @retval CONTINUE Iteration in progress
155  * @retval END Iteration terminated
156  * @retval ERR_NOINIT Uninitialized container
157  * @retval ERR_GET Invalid parameter to store returned key
158  *
159  * @note Please do not insert or delete keys during set traversal
160  */
161 int32_t HashSetIterate(HashSet *self, bool bReset, Key *pKey);
162 
163 /**
164  * @brief Set the custom key resource clean method.
165  *
166  * @param self The pointer to HashSet structure
167  * @param pFunc The function pointer to the custom method
168  *
169  * @retval SUCC
170  * @retval ERR_NOINIT Uninitialized container
171  */
172 int32_t HashSetSetDestroy(HashSet *self, void (*pFunc) (Key));
173 
174 /**
175  * @brief Set the custom hash function.
176  *
177  * The default hash function is HashMurMur32.
178  *
179  * @param self The pointer to HashSet structure
180  * @param pFunc The function pointer to the custom method
181  *
182  * @retval SUCC
183  * @retval ERR_NOINIT Uninitialized container
184  */
185 int32_t HashSetSetHash(HashSet *self, uint32_t (*pFunc) (Key, size_t));
186 
187 /**
188  * @brief Perform union operation for the designated two sets and create the
189  * result set.
190  *
191  * @param pFst The pointer to the first source set
192  * @param pSnd The pointer to the second source set
193  * @param ppDst The double pointer to the to be created result set
194  *
195  * @retval SUCC
196  * @retval ERR_NOINIT Uninitialized source sets
197  * @retval ERR_NOMEM Insufficient memory for new set creation
198  *
199  * @note The newly created set will not delegate any key resource clean methods
200  * from two source sets. You can still set the clean method for it. However, to
201  * avoid the "double-free" problem, it is better to let the source sets handle
202  * the resource clean issue and treat the new set as the pure result from the
203  * union operation.
204  */
205 int32_t HashSetUnion(HashSet *pFst, HashSet *pSnd, HashSet **ppDst);
206 
207 /**
208  * @brief Perform intersection operation for the designated two sets and create
209  * the result set.
210  *
211  * @param pFst The pointer to the first source set
212  * @param pSnd The pointer to the second source set
213  * @param ppDst The double pointer to the to be created result set
214  *
215  * @retval SUCC
216  * @retval ERR_NOINIT Uninitialized source sets
217  * @retval ERR_NOMEM Insufficient memory for new set creation
218  *
219  * @note The newly created set will not delegate any key resource clean methods
220  * from two source sets. You can still set the clean method for it. However, to
221  * avoid the "double-free" problem, it is better to let the source sets handle
222  * the resource clean issue and treat the new set as the pure result from the
223  * intersection operation.
224  */
225 int32_t HashSetIntersect(HashSet *pFst, HashSet *pSnd, HashSet **ppDst);
226 
227 /**
228  * @brief Perform difference operation for the first source set against the
229  * second source set and create the result set.
230  *
231  * @param pFst The pointer to the first source set
232  * @param pSnd The pointer to the second source set
233  * @param ppDst The double pointer to the to be created result set
234  *
235  * @retval SUCC
236  * @retval ERR_NOINIT Uninitialized source sets
237  * @retval ERR_NOMEM Insufficient memory for new set creation
238  *
239  * @note The newly created set will not delegate any key resource clean methods
240  * from two source sets. You can still set the clean method for it. However, to
241  * avoid the "double-free" problem, it is better to let the source sets handle
242  * the resource clean issue and treat the new set as the pure result from the
243  * difference operation.
244  */
245 int32_t HashSetDifference(HashSet *pFst, HashSet *pSnd, HashSet **ppDst);
246 
247 #endif
struct _HashSetData HashSetData
HashSetData is the data type for the container private information.
Definition: hash_set.h:11
int32_t HashSetSetHash(HashSet *self, uint32_t(*pFunc)(Key, size_t))
Set the custom hash function.
HashSetData * pData
The container private information.
Definition: hash_set.h:16
int32_t HashSetAdd(HashSet *self, Key key, size_t size)
Insert a key into the set.
int32_t HashSetIntersect(HashSet *pFst, HashSet *pSnd, HashSet **ppDst)
Perform intersection operation for the designated two sets and create the result set.
int32_t HashSetSize(HashSet *self)
Return the number of stored unique keys.
void HashSetDeinit(HashSet **ppObj)
The destructor for HashSet.
int32_t HashSetSetDestroy(HashSet *self, void(*pFunc)(Key))
Set the custom key resource clean method.
int32_t HashSetRemove(HashSet *self, Key key, size_t size)
Delete the designated key from the set.
The implementation for hash set.
Definition: hash_set.h:14
int32_t HashSetFind(HashSet *self, Key key, size_t size)
Check if the set contains the designated key.
int32_t HashSetIterate(HashSet *self, bool bReset, Key *pKey)
Iterate through the set to retrieve each key.
int32_t HashSetDifference(HashSet *pFst, HashSet *pSnd, HashSet **ppDst)
Perform difference operation for the first source set against the second source set and create the re...
int32_t HashSetUnion(HashSet *pFst, HashSet *pSnd, HashSet **ppDst)
Perform union operation for the designated two sets and create the result set.
int32_t HashSetInit(HashSet **ppObj)
The constructor for HashSet.