HTLIB
C Hash Table Library
Loading...
Searching...
No Matches
htlib.h File Reference

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_tht_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.
 

Detailed Description

A simple hashmap library in C.

Definition in file htlib.h.

Typedef Documentation

◆ ht_hashmap_t

typedef struct ht_hashmap ht_hashmap_t

Opaque type for the hashmap.

The actual definition of the hashmap structure is hidden from the user. The user interacts with the hashmap using the provided functions.

Definition at line 59 of file htlib.h.

Enumeration Type Documentation

◆ 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.

22 {
23 HT_KEY_TYPE_INT,
24 HT_KEY_TYPE_UINT,
25 HT_KEY_TYPE_DOUBLE,
26 HT_KEY_TYPE_STRING,
ht_key_type_t
Type of the hashmap key.
Definition htlib.h:22

◆ 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.

44 {
45 HT_VALUE_TYPE_INT,
46 HT_VALUE_TYPE_UINT,
47 HT_VALUE_TYPE_DOUBLE,
48 HT_VALUE_TYPE_STRING,
49 HT_VALUE_TYPE_PTR
ht_value_type_t
Type of the hashmap value.
Definition htlib.h:44

Function Documentation

◆ ht_clear()

void ht_clear ( ht_hashmap_t * ht)

Clears all elements from the hashmap.

Parameters
htThe hashmap to clear.

◆ ht_destroy()

void ht_destroy ( ht_hashmap_t * ht)

Destroys the hashmap.

Parameters
htThe hashmap to destroy.

◆ ht_get()

bool ht_get ( ht_hashmap_t * ht,
const void * key,
void * value_out )

Gets the value associated with a key.

Parameters
htThe hashmap to search.
keyThe key to search for.
value_outA pointer to a variable to store the value in.
Returns
True if the key was found, false otherwise.

◆ ht_insert()

bool ht_insert ( ht_hashmap_t * ht,
const void * key,
const void * value )

Inserts a key-value pair into the hashmap.

Parameters
htThe hashmap to insert into.
keyThe key to insert.
valueThe value to insert.
Returns
True if the insertion was successful, false otherwise.

◆ ht_is_empty()

bool ht_is_empty ( ht_hashmap_t * ht)

Checks if the hashmap is empty.

Parameters
htThe hashmap to check.
Returns
True if the hashmap is empty, false otherwise.

◆ ht_new()

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.

Parameters
key_typeThe type of the keys in the hashmap.
value_typeThe type of the values in the hashmap.
initial_capacityThe initial capacity of the hashmap.
Returns
A pointer to the new hashmap, or NULL if an error occurred.

◆ ht_remove()

bool ht_remove ( ht_hashmap_t * ht,
const void * key )

Removes a key-value pair from the hashmap.

Parameters
htThe hashmap to remove from.
keyThe key to remove.
Returns
True if the removal was successful, false otherwise.

◆ ht_size()

size_t ht_size ( ht_hashmap_t * ht)

Gets the number of elements in the hashmap.

Parameters
htThe hashmap to get the size of.
Returns
The number of elements in the hashmap.