osurs  0.0.1
Data Structures | Typedefs | Functions
hashmap.h File Reference

Hashmap data structure. More...

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

Data Structures

struct  HashMapEntry
 Entry of the hashmap. More...
 
struct  HashMap
 A hashmap. More...
 

Typedefs

typedef struct HashMapEntry HashMapEntry
 Entry of the hashmap. More...
 
typedef struct HashMap HashMap
 A hashmap. More...
 

Functions

void hash_map_init (HashMap *map)
 Initialize the hashmap. More...
 
HashMaphash_map_create ()
 Create a hashmap on the heap. More...
 
void hash_map_put (HashMap *map, const char *key, void *value)
 Put a new entry into the hashmap. More...
 
void * hash_map_get (HashMap *map, const char *key)
 Get a value from a key. More...
 
void * hash_map_get_random (HashMap *map)
 Get a random value. More...
 
void hash_map_remove (HashMap *map, const char *key)
 Remove an entry from the hashmap. More...
 
void hash_map_print (HashMap *map)
 Print the hashmap content. More...
 
void hash_map_clear (HashMap *map)
 Clear all values in the hashmap. More...
 
void hash_map_free (HashMap *map)
 Free hashmap and entries. More...
 

Detailed Description

Hashmap data structure.

Map abstract data type (ADT) implementation as a hashmap. The implementation uses closed-addressing (separate chaining) to handle duplicate index values. A bucket of the hashmap contains an entry, which can hold a reference to the next entry in the same bucket.

Note: This is not a multimap, entries on existing keys are replaced. This implementation is not thread-safe.

Date
: 2022-12-19
Author
: Merlin Unterfinger

Typedef Documentation

◆ HashMap

typedef struct HashMap HashMap

A hashmap.

Abstract data type (ADT) map implementation as a hashmap.

◆ HashMapEntry

typedef struct HashMapEntry HashMapEntry

Entry of the hashmap.

An entry in the hashmap, which knows the next entry (if there are any) in the same bucket (same index).

Function Documentation

◆ hash_map_clear()

void hash_map_clear ( HashMap map)

Clear all values in the hashmap.

Frees all entries in the hashmap. It is important to note, that the values itself have to be freed manually if needed, by iterating over all entries before clearing.

Parameters
mapA hashmap struct.

◆ hash_map_create()

HashMap* hash_map_create ( )

Create a hashmap on the heap.

Returns
HashMap*

◆ hash_map_free()

void hash_map_free ( HashMap map)

Free hashmap and entries.

It is important to note, that the values itself have to be freed manually if needed, by iterating over all entries before clearing and freeing the hashmap.

Parameters
mapA hashmap struct.

◆ hash_map_get()

void* hash_map_get ( HashMap map,
const char *  key 
)

Get a value from a key.

Returns a void pointer to the value or NULL if the key is not existing in the hashmap.

Parameters
mapA hashmap.
keyThe key.
Returns
void* The pointer pointing to the value.

◆ hash_map_get_random()

void* hash_map_get_random ( HashMap map)

Get a random value.

Returns a random void pointer to a value or NULL if the hashmap is empty.

Parameters
mapA hashmap.
Returns
void*

◆ hash_map_init()

void hash_map_init ( HashMap map)

Initialize the hashmap.

Parameters
mapA hashmap.

◆ hash_map_print()

void hash_map_print ( HashMap map)

Print the hashmap content.

Prints the key, its index (hash value) and the address (void pointer) of the value.

Parameters
mapA hashmap struct.

◆ hash_map_put()

void hash_map_put ( HashMap map,
const char *  key,
void *  value 
)

Put a new entry into the hashmap.

Puts the entry into the bucket at the index (hash value) of the key. If the bucket is not empty, new entry is created and placed to the beginning of the chain. Then the old entry in the array is replaced with the new entry. If a key already exists, the value is replaced.

Parameters
mapA hashmap.
keyThe key.
valueA void pointer to the value.

◆ hash_map_remove()

void hash_map_remove ( HashMap map,
const char *  key 
)

Remove an entry from the hashmap.

Parameters
mapA hashmap structure.
keyThe key.