-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtab_new.c
executable file
·69 lines (65 loc) · 2.5 KB
/
hashtab_new.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
63
64
65
66
67
68
69
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hashtab_new.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <akharrou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/07 17:26:31 by akharrou #+# #+# */
/* Updated: 2019/03/09 11:22:47 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
/*
** NAME
** hashtab_new -- create a hashtable of size 'num_entries'.
**
** SYNOPSIS
** #include "stdlib_42.h"
** #include "hashtable.h"
**
** t_hashtable *
** hashtab_new(unsigned int num_entries);
**
** PARAMETERS
**
** unsigned int num_entries Number of entries that the newly
** created hashtable will be capable
** of holding.
**
** Note: this integer will be rounded
** up to the closest prime number; this
** this reduces the chances of colisions
** even less likely.
**
** DESCRIPTION
** Allocates an empty hash table of size 'num_entries' (rounded up
** to the closest prime number).
**
** RETURN VALUES
** If successful, returns a pointer to the newly allocated hashtable;
** otherwise a NULL pointer is returned.
*/
#include "../Includes/stdlib_42.h"
#include "../Includes/hashtable.h"
t_hashtable *hashtab_new(unsigned int num_entries)
{
t_hashtable *table;
unsigned int i;
if (num_entries < 1)
return (NULL);
if (!(table = (t_hashtable *)malloc(sizeof(t_hashtable))))
return (NULL);
num_entries = (unsigned int)ft_find_next_prime(num_entries);
if (!(table->buckets =
(t_entry **)malloc(sizeof(t_entry *) * num_entries)))
{
free(table);
return (NULL);
}
table->num_buckets = num_entries;
table->entries = 0;
i = -1;
while (num_entries > ++i)
(table->buckets)[i] = NULL;
return (table);
}