Containers
This library provides various containers. Each container has utility functions to manipulate the data it holds. This is an abstraction as to not have to manually manage and reallocate memory.
unordered_map.c File Reference
#include <string.h>
#include "include/unordered_map.h"
Include dependency graph for unordered_map.c:

Macros

#define BKTHOMPS_U_MAP_STARTING_BUCKETS   16
 
#define BKTHOMPS_U_MAP_RESIZE_AT   0.75
 
#define BKTHOMPS_U_MAP_RESIZE_RATIO   2
 

Functions

unordered_map unordered_map_init (const size_t key_size, const size_t value_size, unsigned long(*hash)(const void *const), int(*comparator)(const void *const, const void *const))
 
bk_err unordered_map_rehash (unordered_map me)
 
size_t unordered_map_size (unordered_map me)
 
bk_bool unordered_map_is_empty (unordered_map me)
 
bk_err unordered_map_put (unordered_map me, void *const key, void *const value)
 
bk_bool unordered_map_get (void *const value, unordered_map me, void *const key)
 
bk_bool unordered_map_contains (unordered_map me, void *const key)
 
bk_bool unordered_map_remove (unordered_map me, void *const key)
 
int unordered_map_clear (unordered_map me)
 
unordered_map unordered_map_destroy (unordered_map me)
 

Macro Definition Documentation

◆ BKTHOMPS_U_MAP_RESIZE_AT

#define BKTHOMPS_U_MAP_RESIZE_AT   0.75

◆ BKTHOMPS_U_MAP_RESIZE_RATIO

#define BKTHOMPS_U_MAP_RESIZE_RATIO   2

◆ BKTHOMPS_U_MAP_STARTING_BUCKETS

#define BKTHOMPS_U_MAP_STARTING_BUCKETS   16

Function Documentation

◆ unordered_map_clear()

int unordered_map_clear ( unordered_map  me)

Clears the key-value pairs from the unordered map.

Parameters
methe unordered map to clear
Returns
BK_OK if no error
-BK_ENOMEM if out of memory

◆ unordered_map_contains()

bk_bool unordered_map_contains ( unordered_map  me,
void *const  key 
)

Determines if the unordered map contains the specified key. The pointer to the key being passed in should point to the key type which this unordered map holds. For example, if this unordered map holds key integers, the key pointer should be a pointer to an integer. Since the key is being copied, the pointer only has to be valid when this function is called.

Parameters
methe unordered map to check for the key
keythe key to check
Returns
BK_TRUE if the unordered map contained the key, otherwise BK_FALSE

◆ unordered_map_destroy()

unordered_map unordered_map_destroy ( unordered_map  me)

Frees the unordered map memory. Performing further operations after calling this function results in undefined behavior. Freeing NULL is legal, and causes no operation to be performed.

Parameters
methe unordered map to free from memory
Returns
NULL

◆ unordered_map_get()

bk_bool unordered_map_get ( void *const  value,
unordered_map  me,
void *const  key 
)

Gets the value associated with a key in the unordered map. The pointer to the key being passed in and the value being obtained should point to the key and value types which this unordered map holds. For example, if this unordered map holds integer keys and values, the key and value pointers should be a pointer to an integer. Since the key and value are being copied, the pointer only has to be valid when this function is called.

Parameters
valuethe value to copy to
methe unordered map to get from
keythe key to search for
Returns
BK_TRUE if the unordered map contained the key-value pair, otherwise BK_FALSE

◆ unordered_map_init()

unordered_map unordered_map_init ( const size_t  key_size,
const size_t  value_size,
unsigned long(*)(const void *const)  hash,
int(*)(const void *const, const void *const)  comparator 
)

Initializes an unordered map.

Parameters
key_sizethe size of each key in the unordered map; must be positive
value_sizethe size of each value in the unordered map; must be positive
hashthe hash function which computes the hash from the key; must not be NULL
comparatorthe comparator function which compares two keys; must not be NULL
Returns
the newly-initialized unordered map, or NULL if it was not successfully initialized due to either invalid input arguments or memory allocation error

◆ unordered_map_is_empty()

bk_bool unordered_map_is_empty ( unordered_map  me)

Determines whether or not the unordered map is empty.

Parameters
methe unordered map to check
Returns
BK_TRUE if the unordered map is empty, otherwise BK_FALSE

◆ unordered_map_put()

bk_err unordered_map_put ( unordered_map  me,
void *const  key,
void *const  value 
)

Adds a key-value pair to the unordered map. If the unordered map already contains the key, the value is updated to the new value. The pointer to the key and value being passed in should point to the key and value type which this unordered map holds. For example, if this unordered map holds integer keys and values, the key and value pointer should be a pointer to an integer. Since the key and value are being copied, the pointer only has to be valid when this function is called.

Parameters
methe unordered map to add to
keythe key to add
valuethe value to add
Returns
BK_OK if no error
-BK_ENOMEM if out of memory

◆ unordered_map_rehash()

bk_err unordered_map_rehash ( unordered_map  me)

Rehashes all the keys in the unordered map. Used when storing references and changing the keys. This should rarely be used.

Parameters
methe unordered map to rehash
Returns
BK_OK if no error
-BK_ENOMEM if out of memory

◆ unordered_map_remove()

bk_bool unordered_map_remove ( unordered_map  me,
void *const  key 
)

Removes the key-value pair from the unordered map if it contains it. The pointer to the key being passed in should point to the key type which this unordered map holds. For example, if this unordered map holds key integers, the key pointer should be a pointer to an integer. Since the key is being copied, the pointer only has to be valid when this function is called.

Parameters
methe unordered map to remove a key from
keythe key to remove
Returns
BK_TRUE if the unordered map contained the key, otherwise BK_FALSE

◆ unordered_map_size()

size_t unordered_map_size ( unordered_map  me)

Gets the size of the unordered map.

Parameters
methe unordered map to check
Returns
the size of the unordered map