LibCDS
hash_map.h File Reference

The unordered map to store key value pairs. More...

Go to the source code of this file.

Data Structures

struct  HashMap
 The implementation for hash map. More...
 

Typedefs

typedef struct _HashMapData HashMapData
 HashMapData is the data type for the container private information. More...
 

Functions

int32_t HashMapInit (HashMap **ppObj)
 The constructor for HashMap. More...
 
void HashMapDeinit (HashMap **ppObj)
 The destructor for HashMap. More...
 
int32_t HashMapPut (HashMap *self, Pair *pPair, size_t size)
 Insert a key value pair into the map. More...
 
int32_t HashMapGet (HashMap *self, Key key, size_t size, Value *pValue)
 Retrieve the value corresponding to the designated key. More...
 
int32_t HashMapFind (HashMap *self, Key key, size_t size)
 Check if the map contains the designated key. More...
 
int32_t HashMapRemove (HashMap *self, Key key, size_t size)
 Delete the key value pair corresponding to the designated key. More...
 
int32_t HashMapSize (HashMap *self)
 Return the number of stored key value pairs. More...
 
int32_t HashMapIterate (HashMap *self, bool bReset, Pair **ppPair)
 Iterate through the map to retrieve each key value pair. More...
 
int32_t HashMapSetDestroy (HashMap *self, void(*pFunc)(Pair *))
 Set the custom key value pair resource clean method. More...
 
int32_t HashMapSetHash (HashMap *self, uint32_t(*pFunc)(Key, size_t))
 Set the custom hash function. More...
 

Detailed Description

The unordered map to store key value pairs.

Definition in file hash_map.h.

Typedef Documentation

typedef struct _HashMapData HashMapData

HashMapData is the data type for the container private information.

Definition at line 11 of file hash_map.h.

Function Documentation

int32_t HashMapInit ( HashMap **  ppObj)

The constructor for HashMap.

Parameters
ppObjThe double pointer to the to be constructed map
Return values
SUCC
ERR_NOMEMInsufficient memory for map construction
void HashMapDeinit ( HashMap **  ppObj)

The destructor for HashMap.

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 map
int32_t HashMapPut ( HashMap self,
Pair *  pPair,
size_t  size 
)

Insert a key value pair into the map.

This function inserts a key value pair into the map. If the hash key of the designated pair is the same with a certain one stored in the map, that pair will be replaced. Also, if the custom resource clean method is set, it runs the resource clean method for the replaced pair.

Parameters
selfThe pointer to HashMap structure
pPairThe pointer to the designated pair
sizeKey size in bytes
Return values
SUCC
ERR_NOINITUninitialized container
ERR_NOMEMInsufficient memory for map extension
ERR_KEYSIZEInvalid key size
int32_t HashMapGet ( HashMap self,
Key  key,
size_t  size,
Value *  pValue 
)

Retrieve the value corresponding to the designated key.

This function retrieves the value corresponding to the designated key from the map. If the key can be found, the value will be returned by the fourth parameter. Otherwise, the error code is returned and the fourth parameter is updated with NULL.

Parameters
selfThe pointer to HashMap structure
keyThe designated key
sizeKey size in bytes
pValueThe pointer to the returned value
Return values
SUCC
ERR_NOINITUninitialized container
ERR_NODATANo map entry can be found
ERR_GETInvalid parameter to store returned value
ERR_KEYSIZEInvalid key size
Note
The key should be the pointer to the data you plan to hash for.
int32_t HashMapFind ( HashMap self,
Key  key,
size_t  size 
)

Check if the map contains the designated key.

Parameters
selfThe pointer to HashMap 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 HashMapRemove ( HashMap self,
Key  key,
size_t  size 
)

Delete the key value pair corresponding to the designated key.

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

Parameters
selfThe pointer to HashMap structure
keyThe designated key
sizeKey size in bytes
Return values
SUCC
ERR_NOINITUninitialized container
ERR_NODATANo map 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 HashMapSize ( HashMap self)

Return the number of stored key value pairs.

Parameters
selfThe pointer to HashMap structure
Returns
The number of stored pairs
Return values
ERR_NOINITUninitialized container
int32_t HashMapIterate ( HashMap self,
bool  bReset,
Pair **  ppPair 
)

Iterate through the map to retrieve each key value pair.

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

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

After initialization, you can pass:

  • bReset = false
  • pPair = the pointer to get the returned pair at each iteration.
Parameters
selfThe pointer to HashMap structure
bResetThe knob to restart the iteration
ppPairThe double pointer to the returned pair
Return values
SUCCIterator initialized successfully
CONTINUEIteration in progress
ENDIteration terminiated
ERR_NOINITUninitialized container
ERR_GETInvalid parameter to store returned pair
Note
Please do not insert or delete pairs during map traversal
int32_t HashMapSetDestroy ( HashMap self,
void(*)(Pair *)  pFunc 
)

Set the custom key value pair resource clean method.

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

Set the custom hash function.

The default hash function is HashMurMur32.

Parameters
selfThe pointer to HashMap structure
pFuncThe function pointer to the custom method
Return values
SUCC
ERR_NOINITUninitialized container