data_structures
hashset_remove.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* hashset_remove.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/21 22:59:47 by unite #+# #+# */
10 /* Updated: 2020/09/07 21:57:00 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "hashset.h"
15 #include "hashset_utils.h"
16 
17 static void hashset_rehash_cluster(t_hashset *hs, size_t start)
18 {
19  void *val;
20 
21  while (hs->vals[start] != NULL)
22  {
23  val = hs->vals[start];
24  hs->vals[start] = NULL;
25  hs->size--;
26  hashset_put(hs, val);
27  hs->type->del(val);
28  start = (start + 1) % hs->capacity;
29  }
30 }
31 
32 void hashset_remove(t_hashset *hs, const void *val)
33 {
34  size_t i;
35 
36  i = hs->type->hash(val, hs->capacity);
37  while (hs->type->cmp(hs->vals[i], val) != 0)
38  {
39  if (hs->vals[i] == NULL)
40  return ;
41  i = (i + 1) % hs->capacity;
42  }
43  hs->type->del(hs->vals[i]);
44  hs->vals[i] = NULL;
45  hs->size--;
46  if (hs->capacity > HASHSET_INIT_CAPACITY &&
47  hs->size <= hs->capacity / 8)
48  hashset_shrink(hs);
49  else
50  hashset_rehash_cluster(hs, i + 1);
51 }
52 
s_hashset::capacity
size_t capacity
The current capacity of the hashset.
Definition: hashset.h:47
hashset_put
void hashset_put(t_hashset *hs, const void *val)
Adds a copy of the specified element to the hashset.
Definition: hashset_put.c:17
hashset_utils.h
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
HASHSET_INIT_CAPACITY
#define HASHSET_INIT_CAPACITY
The default initial capacity of a newly initialized hashset.
Definition: hashset.h:28
hashset.h
s_hashset::vals
void ** vals
The data.
Definition: hashset.h:45
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
hashset_remove
void hashset_remove(t_hashset *hs, const void *val)
Removed the specified element from the set.
Definition: hashset_remove.c:32
s_hashset::size
size_t size
The number of elements in the hashset.
Definition: hashset.h:46
s_type::del
void(* del)(void *)
A function pointer used to free the memory taken by the data type.
Definition: types.h:51
s_hashset
A dynamically resizing linear-probing hashset.
Definition: hashset.h:43
s_hashset::type
const t_type * type
The type of elements in the hashset.
Definition: hashset.h:48