Banjo API 0.0.1
Multi-purpose C99 API
Loading...
Searching...
No Matches
htable.c

Hash Table example.

Hash Table example.

#include <banjo/htable.h>
#include <banjo/main.h>
#include <banjo/log.h>
int main(int argc, char* argv[]) {
(void)argc;
(void)argv;
// Create a new hash table
bj_htable* table = bj_htable_new_t(int, int); // No custom allocator
// Insert values into the table
int key1 = 123;
int value1 = 456;
int key2 = 789;
int value2 = 101112;
bj_htable_set(table, &key1, &value1);
bj_htable_set(table, &key2, &value2);
// Retrieve values from the table
int* retrieved_value1 = (int*)bj_htable_get(table, &key1, 0);
bj_info("{%d} = %d", key1, *retrieved_value1);
int* retrieved_value2 = (int*)bj_htable_get(table, &key2, 0);
bj_info("{%d} = %d", key2, *retrieved_value2);
// Delete the table
bj_htable_del(table);
return 0;
}
struct bj_htable_t bj_htable
Typedef for the bj_htable_t struct.
Definition htable.h:28
void * bj_htable_set(bj_htable *table, void *p_key, void *p_value)
Inserts a value into the hash table.
#define bj_htable_new_t(K, V)
Creates a new bj_htable with types inferred for keys and values.
Definition htable.h:49
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.
void bj_htable_del(bj_htable *p_table)
Deletes a bj_htable object and releases associated memory.
#define bj_info(...)
Log a message using the BJ_LOG_INFO level.
Definition log.h:103
Header file for Hash Table container type.
Logging utility functions.