-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashtab_popitem.c
executable file
·100 lines (92 loc) · 2.88 KB
/
hashtab_popitem.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
92
93
94
95
96
97
98
99
100
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hashtab_popitem.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akharrou <akharrou@student.42.us.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/07 11:17:09 by akharrou #+# #+# */
/* Updated: 2019/04/17 06:37:42 by akharrou ### ########.fr */
/* */
/* ************************************************************************** */
/*
** NAME
** hashtab_popitem -- pop an item from a hashtable.
**
** SYNOPSIS
** #include "stdlib_42.h"
** #include "string_42.h"
** #include "hashtable.h"
**
** void *
** hashtab_popitem(t_hashtable **table, char *key);
**
** PARAMETERS
**
** t_hashtable **table Pointer to a pointer to a
** hashtable.
**
** char *key Key corresponding to an
** entry.
**
** DESCRIPTION
** Looks for an entry based on the 'key', if found, the entry's
** item is saved, the entry is then free'd and finally, a pointer
** to the saved item is returned.
**
** RETURN VALUES
** If successful returns a pointer to the item; otherwise NULL.
*/
#include "../Includes/stdlib_42.h"
#include "../Includes/string_42.h"
#include "../Includes/hashtable.h"
/*
** Helper function used to free an entry and that which it contains
** except for its item.
*/
static void free_entry_except_item(t_entry **entry)
{
if (entry && *entry)
{
if ((*entry)->key)
free((*entry)->key);
free(*entry);
(*entry) = NULL;
}
}
static void *popitem_util(t_hashtable **table, t_entry *first_entry,
unsigned int index, char *key)
{
t_entry *prev_entry;
t_entry *cur_entry;
void *item;
cur_entry = first_entry;
while (cur_entry)
{
if (ft_strcmp(cur_entry->key, key) == 0)
{
if (cur_entry == ((*table)->buckets)[index])
((*table)->buckets)[index] = cur_entry->next;
else
prev_entry->next = cur_entry->next;
item = cur_entry->item;
(*table)->entries -= 1;
free_entry_except_item(&cur_entry);
return (item);
}
prev_entry = cur_entry;
cur_entry = cur_entry->next;
}
return (NULL);
}
void *hashtab_popitem(t_hashtable **table, char *key)
{
unsigned int index;
if (table && *table && key)
{
index = HASHCODE(key, (*table)->num_buckets);
return (
popitem_util(table, ((*table)->buckets)[index], index, key));
}
return (NULL);
}