data_structures
hashmap.h
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* hashmap.h :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/21 18:15:39 by unite #+# #+# */
10 /* Updated: 2020/09/07 22:21:01 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #ifndef HASHMAP_H
15 
16 # define HASHMAP_H
17 
18 # include <errno.h>
19 # include <sys/types.h>
20 # include "types.h"
21 # include "queue.h"
22 # include "utils.h"
23 
28 # define HASHMAP_INIT_CAPACITY 51
29 
48 typedef struct s_hashmap
49 {
50  void **keys;
51  void **vals;
52  size_t size;
53  size_t capacity;
54  const t_type *key_type;
55  const t_type *val_type;
56 } t_hashmap;
57 
67 t_hashmap *hashmap_new(const t_type *key_type, const t_type *val_type);
68 
75 void hashmap_put(t_hashmap *hm, const void *key, const void *val);
76 
83 void *hashmap_get(const t_hashmap *hm, const void *key);
84 
90 t_queue *hashmap_keys(const t_hashmap *hm);
91 
97 t_queue *hashmap_vals(const t_hashmap *hm);
98 
104 size_t hashmap_size(const t_hashmap *hm);
105 
111 void hashmap_delete(t_hashmap *hm);
112 
118 void hashmap_remove(t_hashmap *hm, const void *key);
119 
126 int hashmap_contains(const t_hashmap *hm, const void *key);
127 
128 #endif
hashmap_new
t_hashmap * hashmap_new(const t_type *key_type, const t_type *val_type)
Initializes a new empty map.
Definition: hashmap_new.c:16
hashmap_keys
t_queue * hashmap_keys(const t_hashmap *hm)
Returns a queue with all the keys in the map.
Definition: hashmap_keys.c:16
s_hashmap::key_type
const t_type * key_type
The type of keys in the hashmap.
Definition: hashmap.h:54
hashmap_delete
void hashmap_delete(t_hashmap *hm)
Deletes this hashmap and free all its items and the associated data.
Definition: hashmap_delete.c:16
types.h
hashmap_vals
t_queue * hashmap_vals(const t_hashmap *hm)
Returns a queue with all the values in the map.
Definition: hashmap_vals.c:16
s_hashmap::size
size_t size
The number of elements in the hashmap.
Definition: hashmap.h:52
utils.h
s_hashmap::keys
void ** keys
The keys.
Definition: hashmap.h:50
hashmap_put
void hashmap_put(t_hashmap *hm, const void *key, const void *val)
Adds a key-value pair to the symbol table.
Definition: hashmap_put.c:17
s_list
Doubly-linked list of generic items.
Definition: list.h:54
queue.h
s_type
A full representation of a data type, used to achieve polymorphism in the implementation of data stru...
Definition: types.h:47
s_hashmap
A symbol table of generic key-value pairs, implemented as a dynamically resizing linear-probing hashm...
Definition: hashmap.h:48
hashmap_contains
int hashmap_contains(const t_hashmap *hm, const void *key)
Is the specified key contained in the map?
Definition: hashmap_contains.c:16
s_hashmap::val_type
const t_type * val_type
The type of values in the hashmap.
Definition: hashmap.h:55
hashmap_remove
void hashmap_remove(t_hashmap *hm, const void *key)
Removed the element associated with the specified key from the map.
Definition: hashmap_remove.c:36
hashmap_size
size_t hashmap_size(const t_hashmap *hm)
Returns the number of elements in this map.
Definition: hashmap_size.c:16
s_hashmap::capacity
size_t capacity
The current capacity of the hashmap.
Definition: hashmap.h:53
s_hashmap::vals
void ** vals
The values.
Definition: hashmap.h:51
hashmap_get
void * hashmap_get(const t_hashmap *hm, const void *key)
Fetches the value associated with the given key.
Definition: hashmap_get.c:17