data_structures
Data Structures | Macros | Typedefs | Functions
hashmap.h File Reference
#include <errno.h>
#include <sys/types.h>
#include "types.h"
#include "queue.h"
#include "utils.h"
+ Include dependency graph for hashmap.h:

Go to the source code of this file.

Data Structures

struct  s_hashmap
 A symbol table of generic key-value pairs, implemented as a dynamically resizing linear-probing hashmap. More...
 

Macros

#define HASHMAP_INIT_CAPACITY   51
 The default initial capacity of a newly initialized hashmap. More...
 

Typedefs

typedef struct s_hashmap t_hashmap
 

Functions

t_hashmaphashmap_new (const t_type *key_type, const t_type *val_type)
 Initializes a new empty map. More...
 
void hashmap_put (t_hashmap *hm, const void *key, const void *val)
 Adds a key-value pair to the symbol table. More...
 
void * hashmap_get (const t_hashmap *hm, const void *key)
 Fetches the value associated with the given key. More...
 
t_queuehashmap_keys (const t_hashmap *hm)
 Returns a queue with all the keys in the map. More...
 
t_queuehashmap_vals (const t_hashmap *hm)
 Returns a queue with all the values in the map. More...
 
size_t hashmap_size (const t_hashmap *hm)
 Returns the number of elements in this map. More...
 
void hashmap_delete (t_hashmap *hm)
 Deletes this hashmap and free all its items and the associated data. More...
 
void hashmap_remove (t_hashmap *hm, const void *key)
 Removed the element associated with the specified key from the map. More...
 
int hashmap_contains (const t_hashmap *hm, const void *key)
 Is the specified key contained in the map? More...
 

Macro Definition Documentation

◆ HASHMAP_INIT_CAPACITY

#define HASHMAP_INIT_CAPACITY   51

The default initial capacity of a newly initialized hashmap.

Definition at line 28 of file hashmap.h.

Function Documentation

◆ hashmap_contains()

int hashmap_contains ( const t_hashmap hm,
const void *  key 
)

Is the specified key contained in the map?

Parameters
keyThe key
Returns
1 if the key is in the map, 0 otherwise

Definition at line 16 of file hashmap_contains.c.

◆ hashmap_delete()

void hashmap_delete ( t_hashmap hm)

Deletes this hashmap and free all its items and the associated data.

Does nothing if the argument is NULL.

Definition at line 16 of file hashmap_delete.c.

◆ hashmap_get()

void* hashmap_get ( const t_hashmap hm,
const void *  key 
)

Fetches the value associated with the given key.

Parameters
keyThe key
Returns
The value, or NULL if the key is not in the map.

Definition at line 17 of file hashmap_get.c.

◆ hashmap_keys()

t_queue* hashmap_keys ( const t_hashmap hm)

Returns a queue with all the keys in the map.

Returns
A queue with all the keys in the map.

Definition at line 16 of file hashmap_keys.c.

◆ hashmap_new()

t_hashmap* hashmap_new ( const t_type key_type,
const t_type val_type 
)

Initializes a new empty map.

Parameters
key_typeThe type of keys that this map can hold
val_typeThe type of values that this map can hold
Returns
The new hashmap
Note
For this function to work, the key_type must be hashable (i.e. implement the hash function).

Definition at line 16 of file hashmap_new.c.

◆ hashmap_put()

void hashmap_put ( t_hashmap hm,
const void *  key,
const void *  val 
)

Adds a key-value pair to the symbol table.

Parameters
keyThe key
valThe value

Definition at line 17 of file hashmap_put.c.

◆ hashmap_remove()

void hashmap_remove ( t_hashmap hm,
const void *  key 
)

Removed the element associated with the specified key from the map.

Parameters
keyThe key

Definition at line 36 of file hashmap_remove.c.

◆ hashmap_size()

size_t hashmap_size ( const t_hashmap hm)

Returns the number of elements in this map.

Returns
The number of elements in this map.

Definition at line 16 of file hashmap_size.c.

◆ hashmap_vals()

t_queue* hashmap_vals ( const t_hashmap hm)

Returns a queue with all the values in the map.

Returns
A queue with all the values in the map.

Definition at line 16 of file hashmap_vals.c.