-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtab_init.c
executable file
·62 lines (58 loc) · 2 KB
/
hashtab_init.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hashtab_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/07 17:26:31 by akharrou #+# #+# */
/* Updated: 2019/03/09 11:27:58 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
/*
** NAME
** hashtab_init -- create a hashtable of size INIT_HASHTABLE_SIZE.
**
** SYNOPSIS
** #include "stdlib_42.h"
** #include "hashtable.h"
**
** t_hashtable *
** hashtab_init(void);
**
** PARAMETERS
** None.
**
** DESCRIPTION
** Allocates an empty hash table of size INIT_HASHTABLE_SIZE (rounded
** up to the closest prime number).
**
** RETURN VALUES
** If successful, returns a pointer to the newly created hashtable;
** otherwise a NULL pointer is returned.
*/
#include "../Includes/stdlib_42.h"
#include "../Includes/hashtable.h"
t_hashtable *hashtab_init(void)
{
t_hashtable *table;
unsigned int size;
unsigned int i;
if (INIT_HASHTABLE_SIZE < 1)
return (NULL);
if (!(table = (t_hashtable *)malloc(sizeof(t_hashtable))))
return (NULL);
size = (unsigned int)ft_find_next_prime(INIT_HASHTABLE_SIZE);
if (!(table->buckets =
(t_entry **)malloc(sizeof(t_entry *) * size)))
{
free(table);
return (NULL);
}
table->num_buckets = size;
table->entries = 0;
i = -1;
while (size > ++i)
(table->buckets)[i] = NULL;
return (table);
}