libstd v0.1.0
Loading...
Searching...
No Matches
Classes | Typedefs | Functions
hashmap.h File Reference
#include <stdbool.h>
#include <stddef.h>
Include dependency graph for hashmap.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  HashNode
 Node structure representing a key-value pair in a hash bucket's collision chain. More...
 
struct  HashMap
 Structure representing a generic Hash Map. More...
 

Typedefs

typedef struct HashNode HashNode
 Node structure representing a key-value pair in a hash bucket's collision chain.
 

Functions

void hashmap_init (HashMap *map, size_t bucket_count, size_t key_size, size_t value_size, unsigned long(*hash_fn)(void *), int(*cmp_fn)(void *, void *))
 Initializes a Hash Map structure.
 
void hashmap_destroy (HashMap *map)
 Destroys the Hash Map and frees all allocated buckets, nodes, and stored data.
 
bool hashmap_insert (HashMap *map, void *key, const void *data)
 Inserts a new key-value pair into the Hash Map.
 
bool hashmap_remove (HashMap *map, void *key)
 Removes a key-value pair from the Hash Map by key.
 
bool hashmap_update (HashMap *map, void *key, const void *new_data)
 Updates the value associated with an existing key in the Hash Map.
 
void * hashmap_get (HashMap *map, void *key)
 Retrieves the value associated with a given key.
 
int hashmap_size (const HashMap *map)
 Retrieves the total number of key-value pairs stored in the Hash Map.
 

Typedef Documentation

◆ HashNode

typedef struct HashNode HashNode

Node structure representing a key-value pair in a hash bucket's collision chain.

Function Documentation

◆ hashmap_destroy()

void hashmap_destroy ( HashMap map)

Destroys the Hash Map and frees all allocated buckets, nodes, and stored data.

Parameters
mapPointer to the HashMap to destroy.

◆ hashmap_get()

void * hashmap_get ( HashMap map,
void *  key 
)

Retrieves the value associated with a given key.

Parameters
mapPointer to the HashMap.
keyPointer to the key to search for.
Returns
Pointer to the stored value data if found, or NULL if the key does not exist.

◆ hashmap_init()

void hashmap_init ( HashMap map,
size_t  bucket_count,
size_t  key_size,
size_t  value_size,
unsigned long(*)(void *)  hash_fn,
int(*)(void *, void *)  cmp_fn 
)

Initializes a Hash Map structure.

Parameters
mapPointer to the HashMap structure to initialize.
bucket_countInitial number of buckets to allocate.
key_sizeSize of the key data in bytes.
value_sizeSize of the value data in bytes.
hash_fnPointer to the hash function for keys.
cmp_fnPointer to the key comparison function (should return 0 when keys match).

◆ hashmap_insert()

bool hashmap_insert ( HashMap map,
void *  key,
const void *  data 
)

Inserts a new key-value pair into the Hash Map.

Parameters
mapPointer to the HashMap.
keyPointer to the key to insert.
dataPointer to the value associated with the key.
Returns
true if the insertion was successful, or false on memory allocation failure or duplicate entry depending on implementation behavior.

◆ hashmap_remove()

bool hashmap_remove ( HashMap map,
void *  key 
)

Removes a key-value pair from the Hash Map by key.

Parameters
mapPointer to the HashMap.
keyPointer to the key to remove.
Returns
true if the key was found and removed, false otherwise.

◆ hashmap_size()

int hashmap_size ( const HashMap map)

Retrieves the total number of key-value pairs stored in the Hash Map.

Parameters
mapPointer to the HashMap.
Returns
Total number of items stored.

◆ hashmap_update()

bool hashmap_update ( HashMap map,
void *  key,
const void *  new_data 
)

Updates the value associated with an existing key in the Hash Map.

Parameters
mapPointer to the HashMap.
keyPointer to the key whose value should be updated.
new_dataPointer to the new value data.
Returns
true if the key was found and updated, false if the key does not exist.