Banjo API 0.0.1
Multi-purpose C99 API
Loading...
Searching...
No Matches
Hash Table

API related to the bj_htable object. More...

Collaboration diagram for Hash Table:

Detailed Description

bj_htable is an associative container that maps a key to a value. The elements are stored in an array of linked lists called buckets.

Macros

#define bj_htable_new_t(K, V)
 Creates a new bj_htable with types inferred for keys and values.
 

Typedefs

typedef uint32_t(* bj_hash_fn) (const void *p_data, size_t size)
 Function type for hashing keys.
 
typedef struct bj_htable_t bj_htable
 Typedef for the bj_htable_t struct.
 

Functions

bj_htablebj_htable_new (size_t bytes_key, size_t bytes_value)
 Creates a new bj_htable with the specified sizes for keys and values.
 
void bj_htable_del (bj_htable *p_table)
 Deletes a bj_htable object and releases associated memory.
 
void * bj_htable_set (bj_htable *table, void *p_key, void *p_value)
 Inserts a value into the hash table.
 
void * bj_htable_get (const bj_htable *table, const void *p_key, void *p_default)
 Returns the value associated with p_key, if it exists.
 
size_t bj_htable_len (const bj_htable *table)
 Returns the number of elements in the table.
 

Macro Definition Documentation

◆ bj_htable_new_t

#define bj_htable_new_t ( K,
V )
Value:
bj_htable_new(sizeof(K), sizeof(V))
bj_htable * bj_htable_new(size_t bytes_key, size_t bytes_value)
Creates a new bj_htable with the specified sizes for keys and values.
Parameters
KType of keys.
VType of values.
Returns
A pointer to the newly created bj_htable object.
Examples
htable.c.

Function Documentation

◆ bj_htable_del()

void bj_htable_del ( bj_htable * p_table)
Parameters
p_tablePointer to the bj_htable object to delete.
Examples
htable.c.

◆ bj_htable_get()

void * bj_htable_get ( const bj_htable * table,
const void * p_key,
void * p_default )
Parameters
tableThe table object.
p_keyThe search key.
p_defaultThe value returned if the key is not found.
Returns
A pointer to the found value or p_default.

If table does not contain a value for p_key, the function returns p_default.

Examples
htable.c.

◆ bj_htable_len()

size_t bj_htable_len ( const bj_htable * table)
Parameters
tableThe table object.
Returns
The number of elements in the table.

◆ bj_htable_new()

bj_htable * bj_htable_new ( size_t bytes_key,
size_t bytes_value )
Parameters
bytes_keySize of each key in bytes.
bytes_valueSize of each value in bytes.
Returns
A pointer to the newly created bj_htable object.

◆ bj_htable_set()

void * bj_htable_set ( bj_htable * table,
void * p_key,
void * p_value )
Parameters
tableThe table object.
p_keyThe key used to identify the value.
p_valueA pointer to the value to insert.
Returns
A pointer to the inserted value.

If the key already exists, the associated value is modified to store p_value instead. Otherwise, a new entry is created and the (key, value) pair is stored.

Weak / Strong ownership

When p_info->weak_owning is set to BJ_TRUE, the container does not allocate any new memory for storing the actual data. Memory allocation is still needed for the container structure itself. In this case, the caller is responsible for the lifetime of the inserted objects (key and value).

When set to BJ_FALSE, the inserted data is copied into the container's internal memory using bj_memcpy.

Examples
htable.c.