HTLIB
C Hash Table Library
|
A simple hashmap library in C. More...
#include <stdbool.h>
#include <stddef.h>
Go to the source code of this file.
Typedefs | |
typedef struct ht_hashmap | ht_hashmap_t |
Opaque type for the hashmap. | |
Enumerations | |
enum | ht_key_type_t { HT_KEY_TYPE_INT , HT_KEY_TYPE_UINT , HT_KEY_TYPE_DOUBLE , HT_KEY_TYPE_STRING } |
Type of the hashmap key. More... | |
enum | ht_value_type_t { HT_VALUE_TYPE_INT , HT_VALUE_TYPE_UINT , HT_VALUE_TYPE_DOUBLE , HT_VALUE_TYPE_STRING , HT_VALUE_TYPE_PTR } |
Type of the hashmap value. More... | |
Functions | |
ht_hashmap_t * | ht_new (ht_key_type_t key_type, ht_value_type_t value_type, size_t initial_capacity) |
Creates a new hashmap. | |
bool | ht_insert (ht_hashmap_t *ht, const void *key, const void *value) |
Inserts a key-value pair into the hashmap. | |
bool | ht_get (ht_hashmap_t *ht, const void *key, void *value_out) |
Gets the value associated with a key. | |
bool | ht_remove (ht_hashmap_t *ht, const void *key) |
Removes a key-value pair from the hashmap. | |
size_t | ht_size (ht_hashmap_t *ht) |
Gets the number of elements in the hashmap. | |
bool | ht_is_empty (ht_hashmap_t *ht) |
Checks if the hashmap is empty. | |
void | ht_clear (ht_hashmap_t *ht) |
Clears all elements from the hashmap. | |
void | ht_destroy (ht_hashmap_t *ht) |
Destroys the hashmap. | |
A simple hashmap library in C.
Definition in file htlib.h.
typedef struct ht_hashmap ht_hashmap_t |
enum ht_key_type_t |
Type of the hashmap key.
The key type determines how the keys are hashed and compared.
HT_KEY_TYPE_INT
: The key is an integer.HT_KEY_TYPE_UINT
: The key is an unsigned integer.HT_KEY_TYPE_DOUBLE
: The key is a double.HT_KEY_TYPE_STRING
: The key is a string. Definition at line 22 of file htlib.h.
enum ht_value_type_t |
Type of the hashmap value.
The value type determines how the values are stored and compared.
HT_VALUE_TYPE_INT
: The value is an integer.HT_VALUE_TYPE_UINT
: The value is an unsigned integer.HT_VALUE_TYPE_DOUBLE
: The value is a double.HT_VALUE_TYPE_STRING
: The value is a string.HT_VALUE_TYPE_PTR
: The value is a pointer.Note: The HT_VALUE_TYPE_PTR
type is useful for storing arbitrary data types, but it requires the user to manage the memory of the stored values. The library does not handle memory management for pointer values.
Definition at line 44 of file htlib.h.
void ht_clear | ( | ht_hashmap_t * | ht | ) |
Clears all elements from the hashmap.
ht | The hashmap to clear. |
void ht_destroy | ( | ht_hashmap_t * | ht | ) |
Destroys the hashmap.
ht | The hashmap to destroy. |
bool ht_get | ( | ht_hashmap_t * | ht, |
const void * | key, | ||
void * | value_out ) |
Gets the value associated with a key.
ht | The hashmap to search. |
key | The key to search for. |
value_out | A pointer to a variable to store the value in. |
bool ht_insert | ( | ht_hashmap_t * | ht, |
const void * | key, | ||
const void * | value ) |
Inserts a key-value pair into the hashmap.
ht | The hashmap to insert into. |
key | The key to insert. |
value | The value to insert. |
bool ht_is_empty | ( | ht_hashmap_t * | ht | ) |
Checks if the hashmap is empty.
ht | The hashmap to check. |
ht_hashmap_t * ht_new | ( | ht_key_type_t | key_type, |
ht_value_type_t | value_type, | ||
size_t | initial_capacity ) |
Creates a new hashmap.
key_type | The type of the keys in the hashmap. |
value_type | The type of the values in the hashmap. |
initial_capacity | The initial capacity of the hashmap. |
bool ht_remove | ( | ht_hashmap_t * | ht, |
const void * | key ) |
Removes a key-value pair from the hashmap.
ht | The hashmap to remove from. |
key | The key to remove. |
size_t ht_size | ( | ht_hashmap_t * | ht | ) |
Gets the number of elements in the hashmap.
ht | The hashmap to get the size of. |