ucx
UAP Common Extensions
Loading...
Searching...
No Matches
hash_map.h File Reference

Hash map implementation. More...

#include "map.h"

Go to the source code of this file.

Data Structures

struct  cx_hash_map_s
 Internal structure for a hash map. More...
 

Functions

CxMapcxHashMapCreate (const CxAllocator *allocator, size_t itemsize, size_t buckets)
 Creates a new hash map with the specified number of buckets.
 
int cxMapRehash (CxMap *map)
 Increases the number of buckets, if necessary.
 

Detailed Description

Hash map implementation.

Author
Mike Becker
Olaf Wintermann

Function Documentation

◆ cxHashMapCreate()

CxMap * cxHashMapCreate ( const CxAllocator * allocator,
size_t itemsize,
size_t buckets )

Creates a new hash map with the specified number of buckets.

If buckets is zero, an implementation defined default will be used.

If elem_size is CX_STORE_POINTERS, the created map stores pointers instead of copies of the added elements.

Note
Iterators provided by this hash map implementation provide the remove operation. The index value of an iterator is incremented when the iterator advanced without removing an entry. In other words, when the iterator is finished, index==size .
Parameters
allocatorthe allocator to use (if NULL, the cxDefaultAllocator will be used)
itemsizethe size of one element
bucketsthe initial number of buckets in this hash map
Returns
a pointer to the new hash map

◆ cxMapRehash()

int cxMapRehash ( CxMap * map)

Increases the number of buckets, if necessary.

The load threshold is 0.75*buckets. If the element count exceeds the load threshold, the map will be rehashed. Otherwise, no action is performed, and this function simply returns 0.

The rehashing process ensures that the number of buckets is at least 2.5 times the element count. So there is enough room for additional elements without the need of another soon rehashing.

You can use this function after filling a map to increase access performance.

Note
If the specified map is not a hash map, the behavior is undefined.
Parameters
mapthe map to rehash
Return values
zerosuccess
non-zeroif a memory allocation error occurred