data_structures
hashmap_put.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* hashmap_put.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/21 18:47:57 by unite #+# #+# */
10 /* Updated: 2020/09/07 21:54:15 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "hashmap.h"
15 #include "hashmap_utils.h"
16 
17 void hashmap_put(t_hashmap *hm, const void *key, const void *val)
18 {
19  size_t i;
20 
21  i = hm->key_type->hash(key, hm->capacity);
22  while (hm->keys[i] != NULL)
23  {
24  if (hm->key_type->cmp(hm->keys[i], key) == 0)
25  {
26  hm->val_type->del(hm->vals[i]);
27  hm->vals[i] = hm->val_type->copy(val);
28  return ;
29  }
30  i = (i + 1) % hm->capacity;
31  }
32  hm->keys[i] = hm->key_type->copy(key);
33  hm->vals[i] = hm->val_type->copy(val);
34  hm->size++;
35  if (hm->size >= hm->capacity / 2)
36  hashmap_grow(hm);
37 }
hashmap_utils.h
s_type::copy
void *(* copy)(const void *)
A function pointer used to copy the data type.
Definition: types.h:50
s_hashmap::key_type
const t_type * key_type
The type of keys in the hashmap.
Definition: hashmap.h:54
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_hashmap::size
size_t size
The number of elements in the hashmap.
Definition: hashmap.h:52
s_type::cmp
int(* cmp)(const void *, const void *)
(optional) A function ponter used to compare members of this data type
Definition: types.h:52
hashmap.h
s_hashmap::keys
void ** keys
The keys.
Definition: hashmap.h:50
s_type::hash
size_t(* hash)(const void *, size_t)
(optional) A function pointer used to get a hash value of this data type
Definition: types.h:53
s_hashmap
A symbol table of generic key-value pairs, implemented as a dynamically resizing linear-probing hashm...
Definition: hashmap.h:48
s_type::del
void(* del)(void *)
A function pointer used to free the memory taken by the data type.
Definition: types.h:51
s_hashmap::val_type
const t_type * val_type
The type of values in the hashmap.
Definition: hashmap.h:55
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