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.
map.c File Reference
#include <string.h>
#include "include/map.h"
Include dependency graph for map.c:

Functions

map map_init (const size_t key_size, const size_t value_size, int(*const comparator)(const void *const, const void *const))
 
size_t map_size (map me)
 
bk_bool map_is_empty (map me)
 
bk_err map_put (map me, void *const key, void *const value)
 
bk_bool map_get (void *const value, map me, void *const key)
 
bk_bool map_contains (map me, void *const key)
 
bk_bool map_remove (map me, void *const key)
 
void * map_first (map me)
 
void * map_last (map me)
 
void * map_lower (map me, void *const key)
 
void * map_higher (map me, void *const key)
 
void * map_floor (map me, void *const key)
 
void * map_ceiling (map me, void *const key)
 
void map_clear (map me)
 
map map_destroy (map me)
 

Function Documentation

◆ map_ceiling()

void* map_ceiling ( map  me,
void *const  key 
)

Returns the key which is the ceiling of the comparison key. Meaning that the the lowest key which is higher or equal to the key used for comparison is returned.

Parameters
methe map to get the ceiling key from
keythe key to use for comparison
Returns
the key which is the ceiling, or NULL if it does not exist

◆ map_clear()

void map_clear ( map  me)

Clears the key-value pairs from the map.

Parameters
methe map to clear

◆ map_contains()

bk_bool map_contains ( map  me,
void *const  key 
)

Determines if the map contains the specified key. The pointer to the key being passed in should point to the key type which this map holds. For example, if this 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 map to check for the element
keythe key to check
Returns
BK_TRUE if the map contained the element, otherwise BK_FALSE

◆ map_destroy()

map map_destroy ( map  me)

Frees the 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 map to free from memory
Returns
NULL

◆ map_first()

void* map_first ( map  me)

Returns the first (lowest) key in this map. The returned key is a pointer to the internally stored key, which should not be modified. Modifying it results in undefined behaviour.

Parameters
methe map to get the key from
Returns
the lowest key in this map, or NULL if it is empty

◆ map_floor()

void* map_floor ( map  me,
void *const  key 
)

Returns the key which is the floor of the comparison key. Meaning that the the highest key which is lower or equal to the key used for comparison is returned.

Parameters
methe map to get the floor key from
keythe key to use for comparison
Returns
the key which is the floor, or NULL if it does not exist

◆ map_get()

bk_bool map_get ( void *const  value,
map  me,
void *const  key 
)

Gets the value associated with a key in the map. The pointer to the key being passed in and the value being obtained should point to the key and value types which this map holds. For example, if this 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 map to get from
keythe key to search for
Returns
BK_TRUE if the map contained the key-value pair, otherwise BK_FALSE

◆ map_higher()

void* map_higher ( map  me,
void *const  key 
)

Returns the key which is strictly higher than the comparison key. Meaning that the lowest key which is higher than the key used for comparison is returned.

Parameters
methe map to get the higher key from
keythe key to use for comparison
Returns
the key which is strictly higher, or NULL if it does not exist

◆ map_init()

map map_init ( const size_t  key_size,
const size_t  value_size,
int(*)(const void *const, const void *const)  comparator 
)

Initializes a map.

Parameters
key_sizethe size of each key in the map; must be positive
value_sizethe size of each value in the map; must be positive
comparatorthe comparator function used for key ordering; must not be NULL
Returns
the newly-initialized map, or NULL if it was not successfully initialized due to either invalid input arguments or memory allocation error

◆ map_is_empty()

bk_bool map_is_empty ( map  me)

Determines whether or not the map is empty.

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

◆ map_last()

void* map_last ( map  me)

Returns the last (highest) key in this map. The returned key is a pointer to the internally stored key, which should not be modified. Modifying it results in undefined behaviour.

Parameters
methe map to get the key from
Returns
the highest key in this map, or NULL if it is empty

◆ map_lower()

void* map_lower ( map  me,
void *const  key 
)

Returns the key which is strictly lower than the comparison key. Meaning that the highest key which is lower than the key used for comparison is returned.

Parameters
methe map to get the lower key from
keythe key to use for comparison
Returns
the key which is strictly lower, or NULL if it does not exist

◆ map_put()

bk_err map_put ( map  me,
void *const  key,
void *const  value 
)

Adds a key-value pair to the map. If the 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 map holds. For example, if this 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 map to add to
keythe key to add
valuethe value to add
Returns
BK_OK if no error
-BK_ENOMEM if out of memory

◆ map_remove()

bk_bool map_remove ( map  me,
void *const  key 
)

Removes the key-value pair from the map if it contains it. The pointer to the key being passed in should point to the key type which this map holds. For example, if this 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 map to remove an element from
keythe key to remove
Returns
BK_TRUE if the map contained the key-value pair, otherwise BK_FALSE

◆ map_size()

size_t map_size ( map  me)

Gets the size of the map.

Parameters
methe map to check
Returns
the size of the map