-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashtab_clear.c
executable file
·91 lines (84 loc) · 2.27 KB
/
hashtab_clear.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hashtab_clear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <akharrou@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/07 17:26:03 by akharrou #+# #+# */
/* Updated: 2019/03/09 11:07:29 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
/*
** NAME
** hashtab_clear -- remove and free all entries of a hashtable without
** free'ing the hashtable itself.
**
** SYNOPSIS
** #include "stdlib_42.h"
** #include "hashtable.h"
**
** int
** hashtab_clear(t_hashtable **table);
**
** PARAMETERS
**
** t_hashtable **table Pointer to a pointer to a hashtable.
**
** DESCRIPTION
** Removes and frees all entries contained in the hashtable pointed to
** by (*table) without free'ing the hashtable itself.
**
** RETURN VALUES
** If successful returns 0; otherwise -1.
*/
#include "../Includes/stdlib_42.h"
#include "../Includes/hashtable.h"
static void entry_free_(t_entry **entry)
{
if (entry && *entry)
{
if ((*entry)->key)
free((*entry)->key);
if ((*entry)->item)
free((*entry)->item);
free(*entry);
(*entry) = NULL;
}
}
static void bucket_free_(t_entry **head)
{
t_entry *temp;
if (head)
{
while (*head)
{
temp = (*head);
(*head) = (*head)->next;
entry_free_(&temp);
}
}
}
int hashtab_clear(t_hashtable **table)
{
unsigned int i;
if (table)
{
if (*table)
{
if ((*table)->buckets)
{
i = 0;
while ((*table)->num_buckets > i)
{
if (((*table)->buckets)[i])
bucket_free_(&((*table)->buckets)[i]);
i++;
}
(*table)->entries = 0;
return (0);
}
}
}
return (-1);
}