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

Macros

#define BKTHOMPS_U_SET_STARTING_BUCKETS   16
 
#define BKTHOMPS_U_SET_RESIZE_AT   0.75
 
#define BKTHOMPS_U_SET_RESIZE_RATIO   2
 

Functions

unordered_set unordered_set_init (const size_t key_size, unsigned long(*hash)(const void *const), int(*comparator)(const void *const, const void *const))
 
bk_err unordered_set_rehash (unordered_set me)
 
size_t unordered_set_size (unordered_set me)
 
bk_bool unordered_set_is_empty (unordered_set me)
 
int unordered_set_put (unordered_set me, void *const key)
 
int unordered_set_contains (unordered_set me, void *const key)
 
int unordered_set_remove (unordered_set me, void *const key)
 
int unordered_set_clear (unordered_set me)
 
unordered_set unordered_set_destroy (unordered_set me)
 

Macro Definition Documentation

◆ BKTHOMPS_U_SET_RESIZE_AT

#define BKTHOMPS_U_SET_RESIZE_AT   0.75

◆ BKTHOMPS_U_SET_RESIZE_RATIO

#define BKTHOMPS_U_SET_RESIZE_RATIO   2

◆ BKTHOMPS_U_SET_STARTING_BUCKETS

#define BKTHOMPS_U_SET_STARTING_BUCKETS   16

Function Documentation

◆ unordered_set_clear()

int unordered_set_clear ( unordered_set  me)

Clears the keys from the unordered set.

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

◆ unordered_set_contains()

int unordered_set_contains ( unordered_set  me,
void *const  key 
)

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

◆ unordered_set_destroy()

unordered_set unordered_set_destroy ( unordered_set  me)

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

◆ unordered_set_init()

unordered_set unordered_set_init ( const size_t  key_size,
unsigned long(*)(const void *const)  hash,
int(*)(const void *const, const void *const)  comparator 
)

Initializes an unordered set.

Parameters
key_sizethe size of each key in the unordered set; 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 set, or NULL if it was not successfully initialized due to either invalid input arguments or memory allocation error

◆ unordered_set_is_empty()

bk_bool unordered_set_is_empty ( unordered_set  me)

Determines whether or not the unordered set is empty.

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

◆ unordered_set_put()

int unordered_set_put ( unordered_set  me,
void *const  key 
)

Adds an element to the unordered set if the unordered set does not already contain it. The pointer to the key being passed in should point to the key type which this unordered set holds. For example, if this unordered set 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 set to add to
keythe element to add
Returns
BK_OK if no error
-BK_ENOMEM if out of memory

◆ unordered_set_rehash()

bk_err unordered_set_rehash ( unordered_set  me)

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

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

◆ unordered_set_remove()

int unordered_set_remove ( unordered_set  me,
void *const  key 
)

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

◆ unordered_set_size()

size_t unordered_set_size ( unordered_set  me)

Gets the size of the unordered set.

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