data_structures
hashmap_remove.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* hashmap_remove.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/21 19:07:34 by unite #+# #+# */
10 /* Updated: 2020/09/07 21:53:55 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "hashmap.h"
15 #include "hashmap_utils.h"
16 
17 static void hashmap_rehash_cluster(t_hashmap *hm, size_t start)
18 {
19  void *key;
20  void *val;
21 
22  while (hm->keys[start] != NULL)
23  {
24  key = hm->keys[start];
25  val = hm->vals[start];
26  hm->keys[start] = NULL;
27  hm->vals[start] = NULL;
28  hm->size--;
29  hashmap_put(hm, key, val);
30  hm->key_type->del(key);
31  hm->val_type->del(val);
32  start = (start + 1) % hm->capacity;
33  }
34 }
35 
36 void hashmap_remove(t_hashmap *hm, const void *key)
37 {
38  size_t i;
39 
40  i = hm->key_type->hash(key, hm->capacity);
41  while (hm->key_type->cmp(hm->keys[i], key) != 0)
42  {
43  if (hm->keys[i] == NULL)
44  return ;
45  i = (i + 1) % hm->capacity;
46  }
47  hm->key_type->del(hm->keys[i]);
48  hm->val_type->del(hm->vals[i]);
49  hm->keys[i] = NULL;
50  hm->vals[i] = NULL;
51  hm->size--;
52  if (hm->capacity > HASHMAP_INIT_CAPACITY &&
53  hm->size <= hm->capacity / 8)
54  hashmap_shrink(hm);
55  else
56  hashmap_rehash_cluster(hm, i + 1);
57 }
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_INIT_CAPACITY
#define HASHMAP_INIT_CAPACITY
The default initial capacity of a newly initialized hashmap.
Definition: hashmap.h:28
hashmap_utils.h
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