data_structures
hashset_put.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* hashset_put.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/22 01:58:58 by unite #+# #+# */
10 /* Updated: 2020/09/07 21:57:15 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "hashset.h"
15 #include "hashset_utils.h"
16 
17 void hashset_put(t_hashset *hs, const void *val)
18 {
19  size_t i;
20 
21  i = hs->type->hash(val, hs->capacity);
22  while (hs->vals[i] != NULL)
23  {
24  if (hs->type->cmp(hs->vals[i], val) == 0)
25  return ;
26  i = (i + 1) % hs->capacity;
27  }
28  hs->vals[i] = hs->type->copy(val);
29  hs->size++;
30  if (hs->size >= hs->capacity / 2)
31  hashset_grow(hs);
32 }
s_type::copy
void *(* copy)(const void *)
A function pointer used to copy the data type.
Definition: types.h:50
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.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
s_hashset::size
size_t size
The number of elements in the hashset.
Definition: hashset.h:46
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