data_structures
hashmap_new.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* hashmap_new.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/21 18:35:49 by unite #+# #+# */
10 /* Updated: 2020/09/07 21:54:55 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "hashmap.h"
15 
16 t_hashmap *hashmap_new(const t_type *key_type, const t_type *val_type)
17 {
18  t_hashmap *hm;
19 
20  if (key_type->hash == NULL)
21  ds_exit_set(EINVAL);
22  hm = ds_xcalloc(sizeof(t_hashmap), 1);
23  hm->keys = ds_xcalloc(sizeof(void *), HASHMAP_INIT_CAPACITY);
24  hm->vals = ds_xcalloc(sizeof(void *), HASHMAP_INIT_CAPACITY);
25  hm->val_type = val_type;
26  hm->key_type = key_type;
28  return (hm);
29 }
HASHMAP_INIT_CAPACITY
#define HASHMAP_INIT_CAPACITY
The default initial capacity of a newly initialized hashmap.
Definition: hashmap.h:28
s_hashmap::key_type
const t_type * key_type
The type of keys in the hashmap.
Definition: hashmap.h:54
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
ds_xcalloc
void * ds_xcalloc(size_t count, size_t size)
Replicates behaviour of calloc from libc, but fails on memory allocation errors.
Definition: ds_xcalloc.c:22
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_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
s_hashmap::val_type
const t_type * val_type
The type of values in the hashmap.
Definition: hashmap.h:55
ds_exit_set
void ds_exit_set(int err)
Set errno to the specified value, print the error message, and exit the process.
Definition: ds_exit_set.c:22
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