The unordered map to store key value pairs.
More...
Go to the source code of this file.
|
typedef struct _HashMapData | HashMapData |
| HashMapData is the data type for the container private information. More...
|
|
|
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...
|
|
The unordered map to store key value pairs.
Definition in file hash_map.h.
HashMapData is the data type for the container private information.
Definition at line 11 of file hash_map.h.
int32_t HashMapInit |
( |
HashMap ** |
ppObj | ) |
|
The constructor for HashMap.
- Parameters
-
ppObj | The double pointer to the to be constructed map |
- Return values
-
SUCC | |
ERR_NOMEM | Insufficient 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
-
ppObj | The 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
-
self | The pointer to HashMap structure |
pPair | The pointer to the designated pair |
size | Key size in bytes |
- Return values
-
SUCC | |
ERR_NOINIT | Uninitialized container |
ERR_NOMEM | Insufficient memory for map extension |
ERR_KEYSIZE | Invalid 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
-
self | The pointer to HashMap structure |
key | The designated key |
size | Key size in bytes |
pValue | The pointer to the returned value |
- Return values
-
SUCC | |
ERR_NOINIT | Uninitialized container |
ERR_NODATA | No map entry can be found |
ERR_GET | Invalid parameter to store returned value |
ERR_KEYSIZE | Invalid 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
-
self | The pointer to HashMap structure |
key | The designated key |
size | Key size in bytes |
- Return values
-
SUCC | The key can be found |
NOKEY | The key cannot be found |
ERR_NOINIT | Uninitialized container |
ERR_KEYSIZE | Invalid 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
-
self | The pointer to HashMap structure |
key | The designated key |
size | Key size in bytes |
- Return values
-
SUCC | |
ERR_NOINIT | Uninitialized container |
ERR_NODATA | No map entry can be found |
ERR_KEYSIZE | Invalid 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
-
self | The pointer to HashMap structure |
- Returns
- The number of stored pairs
- Return values
-
ERR_NOINIT | Uninitialized 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
-
self | The pointer to HashMap structure |
bReset | The knob to restart the iteration |
ppPair | The double pointer to the returned pair |
- Return values
-
SUCC | Iterator initialized successfully |
CONTINUE | Iteration in progress |
END | Iteration terminiated |
ERR_NOINIT | Uninitialized container |
ERR_GET | Invalid 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
-
self | The pointer to HashMap structure |
pFunc | The function pointer to the custom method |
- Return values
-
SUCC | |
ERR_NOINIT | Uninitialized container |
int32_t HashMapSetHash |
( |
HashMap * |
self, |
|
|
uint32_t(*)(Key, size_t) |
pFunc |
|
) |
| |
Set the custom hash function.
The default hash function is HashMurMur32.
- Parameters
-
self | The pointer to HashMap structure |
pFunc | The function pointer to the custom method |
- Return values
-
SUCC | |
ERR_NOINIT | Uninitialized container |