LibCDS
hash_set.h File Reference

The unordered set to store unique keys. More...

Go to the source code of this file.

Data Structures

struct  HashSet
 The implementation for hash set. More...
 

Typedefs

typedef struct _HashSetData HashSetData
 HashSetData is the data type for the container private information. More...
 

Functions

int32_t HashSetInit (HashSet **ppObj)
 The constructor for HashSet. More...
 
void HashSetDeinit (HashSet **ppObj)
 The destructor for HashSet. More...
 
int32_t HashSetAdd (HashSet *self, Key key, size_t size)
 Insert a key into the set. More...
 
int32_t HashSetFind (HashSet *self, Key key, size_t size)
 Check if the set contains the designated key. More...
 
int32_t HashSetRemove (HashSet *self, Key key, size_t size)
 Delete the designated key from the set. More...
 
int32_t HashSetSize (HashSet *self)
 Return the number of stored unique keys. More...
 
int32_t HashSetIterate (HashSet *self, bool bReset, Key *pKey)
 Iterate through the set to retrieve each key. More...
 
int32_t HashSetSetDestroy (HashSet *self, void(*pFunc)(Key))
 Set the custom key resource clean method. More...
 
int32_t HashSetSetHash (HashSet *self, uint32_t(*pFunc)(Key, size_t))
 Set the custom hash function. More...
 
int32_t HashSetUnion (HashSet *pFst, HashSet *pSnd, HashSet **ppDst)
 Perform union operation for the designated two sets and create the result set. More...
 
int32_t HashSetIntersect (HashSet *pFst, HashSet *pSnd, HashSet **ppDst)
 Perform intersection operation for the designated two sets and create the result set. More...
 
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 result set. More...
 

Detailed Description

The unordered set to store unique keys.

Definition in file hash_set.h.

Typedef Documentation

typedef struct _HashSetData HashSetData

HashSetData is the data type for the container private information.

Definition at line 11 of file hash_set.h.

Function Documentation

int32_t HashSetInit ( HashSet **  ppObj)

The constructor for HashSet.

Parameters
ppObjThe double pointer to the to be constructed set
Return values
SUCC
ERR_NOMEMInsufficient memory for set construction
void HashSetDeinit ( HashSet **  ppObj)

The destructor for HashSet.

If the custom resource clean method is set, it also runs the clean method for each pair.

Parameters
ppObjThe double pointer to the to be destructed set
int32_t HashSetAdd ( HashSet self,
Key  key,
size_t  size 
)

Insert a key into the set.

This function inserts a key into the set. If the designated key is the same with a certain one stored in the set, that key will be replaced to maintain the element uniqueness. Also, if the custom resource clean method is set, it runs the resource clean method for the replaced key.

Parameters
selfThe pointer to HashSet structure
keyThe designated key
sizeKey size in bytes
Return values
SUCC
ERR_NOINITUninitialized container
ERR_NOMEMInsufficient memory for set extension
ERR_KEYSIZEInvalid key size
Note
The key should be the pointer to the data you plan to hash for.
int32_t HashSetFind ( HashSet self,
Key  key,
size_t  size 
)

Check if the set contains the designated key.

Parameters
selfThe pointer to HashSet structure
keyThe designated key
sizeKey size in bytes
Return values
SUCCThe key can be found
NOKEYThe key cannot be found
ERR_NOINITUninitialized container
ERR_KEYSIZEInvalid key size
Note
The key should be the pointer to the data you plan to hash for.
int32_t HashSetRemove ( HashSet self,
Key  key,
size_t  size 
)

Delete the designated key from the set.

This function deletes the designated key from the set. If the custom resource clean method is set, it also runs the clean method for the deleted key.

Parameters
selfThe pointer to HashSet structure
keyThe designated key
sizeKey size in bytes
Return values
SUCC
ERR_NOINITUninitialized container
ERR_NODATANo set entry can be found
ERR_KEYSIZEInvalid key size
Note
The key should be the pointer to the data you plan to hash for.
int32_t HashSetSize ( HashSet self)

Return the number of stored unique keys.

Parameters
selfThe pointer to HashSet structure
Returns
The number of stored pairs
Return values
ERR_NOINITUninitialized container
int32_t HashSetIterate ( HashSet self,
bool  bReset,
Key *  pKey 
)

Iterate through the set to retrieve each key.

Before iterating through the set, it is necessary to pass:

  • bReset = true
  • pKey = NULL for iterator initialization.

After initialization, you can pass:

  • bReset = false
  • pKey = the pointer to get the returned key at each iteration.
Parameters
selfThe pointer to HashSet structure
bResetThe knob to restart the iteration
pKeyThe pointer to the returned key
Return values
SUCCIterator initialized successfully
CONTINUEIteration in progress
ENDIteration terminated
ERR_NOINITUninitialized container
ERR_GETInvalid parameter to store returned key
Note
Please do not insert or delete keys during set traversal
int32_t HashSetSetDestroy ( HashSet self,
void(*)(Key)  pFunc 
)

Set the custom key resource clean method.

Parameters
selfThe pointer to HashSet structure
pFuncThe function pointer to the custom method
Return values
SUCC
ERR_NOINITUninitialized container
int32_t HashSetSetHash ( HashSet self,
uint32_t(*)(Key, size_t)  pFunc 
)

Set the custom hash function.

The default hash function is HashMurMur32.

Parameters
selfThe pointer to HashSet structure
pFuncThe function pointer to the custom method
Return values
SUCC
ERR_NOINITUninitialized container
int32_t HashSetUnion ( HashSet pFst,
HashSet pSnd,
HashSet **  ppDst 
)

Perform union operation for the designated two sets and create the result set.

Parameters
pFstThe pointer to the first source set
pSndThe pointer to the second source set
ppDstThe double pointer to the to be created result set
Return values
SUCC
ERR_NOINITUninitialized source sets
ERR_NOMEMInsufficient memory for new set creation
Note
The newly created set will not delegate any key resource clean methods from two source sets. You can still set the clean method for it. However, to avoid the "double-free" problem, it is better to let the source sets handle the resource clean issue and treat the new set as the pure result from the union operation.
int32_t HashSetIntersect ( HashSet pFst,
HashSet pSnd,
HashSet **  ppDst 
)

Perform intersection operation for the designated two sets and create the result set.

Parameters
pFstThe pointer to the first source set
pSndThe pointer to the second source set
ppDstThe double pointer to the to be created result set
Return values
SUCC
ERR_NOINITUninitialized source sets
ERR_NOMEMInsufficient memory for new set creation
Note
The newly created set will not delegate any key resource clean methods from two source sets. You can still set the clean method for it. However, to avoid the "double-free" problem, it is better to let the source sets handle the resource clean issue and treat the new set as the pure result from the intersection operation.
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 result set.

Parameters
pFstThe pointer to the first source set
pSndThe pointer to the second source set
ppDstThe double pointer to the to be created result set
Return values
SUCC
ERR_NOINITUninitialized source sets
ERR_NOMEMInsufficient memory for new set creation
Note
The newly created set will not delegate any key resource clean methods from two source sets. You can still set the clean method for it. However, to avoid the "double-free" problem, it is better to let the source sets handle the resource clean issue and treat the new set as the pure result from the difference operation.