diff options
Diffstat (limited to 'src/ht')
| -rw-r--r-- | src/ht/ft_htdelone.c | 11 | ||||
| -rw-r--r-- | src/ht/ft_htdestroy.c | 10 | ||||
| -rw-r--r-- | src/ht/ft_htget.c | 6 | ||||
| -rw-r--r-- | src/ht/ft_hthash.c | 4 | ||||
| -rw-r--r-- | src/ht/ft_htnew.c | 8 | ||||
| -rw-r--r-- | src/ht/ft_htset.c | 37 | ||||
| -rw-r--r-- | src/ht/ft_inter_htdel_first_order.c | 33 |
7 files changed, 74 insertions, 35 deletions
diff --git a/src/ht/ft_htdelone.c b/src/ht/ft_htdelone.c index 7374a44..bc2e047 100644 --- a/src/ht/ft_htdelone.c +++ b/src/ht/ft_htdelone.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 09:27:18 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:10:16 by cacharle ### ########.fr */ +/* Updated: 2020/04/03 07:14:28 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,14 +16,15 @@ /* ** \brief Delete one hash table entry ** \param key Key of entry to delete -** \param del Function to destroy the entry -** \warning The del function HAS to free the key +** \param del Function to destroy the entry value ** \note Do nothing if their is to entry which correspond to key */ -void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_entry*)) +void ft_htdelone(t_ftht *ht, char *key, void (*del)(void*)) { + ft_inter_htdel_first_order_setup(del); ft_lstremove_if(ht->buckets + ft_hthash(ht, key), ft_inter_htkey_cmp, key, - (void (*)(void*))del); + (void (*)(void*))ft_inter_htdel_first_order); + ft_inter_htdel_first_order_teardown(); } diff --git a/src/ht/ft_htdestroy.c b/src/ht/ft_htdestroy.c index ff362d2..c43754e 100644 --- a/src/ht/ft_htdestroy.c +++ b/src/ht/ft_htdestroy.c @@ -6,11 +6,10 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:31:02 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:10:31 by cacharle ### ########.fr */ +/* Updated: 2020/04/03 07:01:30 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_ht.h" /* @@ -19,12 +18,15 @@ ** \warning The del function HAS to free the key */ -void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_entry*)) +void ft_htdestroy(t_ftht *ht, t_ftdel_func del) { if (ht == NULL) return ; + ft_inter_htdel_first_order_setup(del); while (ht->size-- > 0) - ft_lstdestroy(ht->buckets + ht->size, (void (*)(void*))del); + ft_lstdestroy(ht->buckets + ht->size, + (void (*)(void*))ft_inter_htdel_first_order); + ft_inter_htdel_first_order_teardown(); free(ht->buckets); free(ht); } diff --git a/src/ht/ft_htget.c b/src/ht/ft_htget.c index a6383fe..75da785 100644 --- a/src/ht/ft_htget.c +++ b/src/ht/ft_htget.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:33:21 by cacharle #+# #+# */ -/* Updated: 2020/04/01 18:02:57 by charles ### ########.fr */ +/* Updated: 2020/04/03 07:12:58 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,8 +22,8 @@ void *ft_htget(t_ftht *ht, char *key) { - t_ftht_digest digest; - t_ftlst *found; + size_t digest; + t_ftlst *found; if (ht == NULL || key == NULL) return (NULL); diff --git a/src/ht/ft_hthash.c b/src/ht/ft_hthash.c index 3369d24..c5d42ea 100644 --- a/src/ht/ft_hthash.c +++ b/src/ht/ft_hthash.c @@ -20,9 +20,9 @@ */ // maybe use a less efficient but understandable function -t_ftht_digest ft_hthash(t_ftht *ht, char *key) +size_t ft_hthash(t_ftht *ht, char *key) { - t_ftht_digest digest; + size_t digest; if (*key == '\0') return (0); diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c index e5335d2..b6d1cc3 100644 --- a/src/ht/ft_htnew.c +++ b/src/ht/ft_htnew.c @@ -6,12 +6,12 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:19:16 by cacharle #+# #+# */ -/* Updated: 2020/02/28 12:23:43 by cacharle ### ########.fr */ +/* Updated: 2020/04/03 06:53:51 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_ht.h" +#include "libft_mem.h" /* ** \brief Create a new hash table. @@ -19,7 +19,7 @@ ** \return Created hash table or NULL is an allocation failed */ -t_ftht *ft_htnew(t_ftsize size) +t_ftht *ft_htnew(size_t size) { t_ftht *ht; @@ -27,7 +27,7 @@ t_ftht *ft_htnew(t_ftsize size) return (NULL); if ((ht = (t_ftht*)malloc(sizeof(t_ftht))) == NULL) return (NULL); - ht->buckets = (t_ftht_bucket*)ft_calloc(size, sizeof(t_ftht_entry)); + ht->buckets = (t_ftlst**)ft_calloc(size, sizeof(t_ftlst*)); if (ht->buckets == NULL) { free(ht); diff --git a/src/ht/ft_htset.c b/src/ht/ft_htset.c index 68d3752..b3a44ee 100644 --- a/src/ht/ft_htset.c +++ b/src/ht/ft_htset.c @@ -6,18 +6,17 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/01/30 08:41:52 by cacharle #+# #+# */ -/* Updated: 2020/04/01 18:02:12 by charles ### ########.fr */ +/* Updated: 2020/04/03 07:13:17 by charles ### ########.fr */ /* */ /* ************************************************************************** */ -#include "libft.h" #include "libft_ht.h" /* ** \brief Create/Update a entry in hash table. ** \note If `key` already exist in `ht` -** only updates the list node content. -** Else create a new list node in addition the list content. +** only updates the list node entry. +** Else create a new list node in addition the list entry. ** \param ht Hash table where the entry is modified ** \param key Key of the new entry ** \param value Value of the new entry @@ -25,32 +24,36 @@ ** \return Pointer to the created entry, NULL if an allocation failed. */ -t_ftht_entry *ft_htset(t_ftht *ht, char *key, void *value, - void (*del)(t_ftht_entry*)) +t_ftht_entry *ft_htset( + t_ftht *ht, + char *key, + void *value, + void (*del)(void*) +) { - t_ftht_digest digest; - t_ftht_entry *content; - t_ftht_bucket bucket; + size_t digest; + t_ftht_entry *entry; t_ftlst *tmp; if (ht == NULL || key == NULL) return (NULL); - if ((content = ft_htentry_new(key, value)) == NULL) - return (NULL); digest = ft_hthash(ht, key); tmp = ft_lstlfind(ht->buckets[digest], ft_inter_htkey_cmp, key); if (tmp != NULL) { if (del != NULL) - del(tmp->data); - tmp->data = content; + del(((t_ftht_entry*)tmp->data)->value); + ((t_ftht_entry*)tmp->data)->value = value; return ((t_ftht_entry*)tmp->data); } - if ((bucket = ft_lstnew(content)) == NULL) + if ((entry = ft_htentry_new(key, value)) == NULL) + return (NULL); + if ((tmp = ft_lstnew(entry)) == NULL) { - del(content); + free(entry->key); + free(entry); return (NULL); } - ft_lstpush_front(ht->buckets + digest, bucket); - return (content); + ft_lstpush_front(ht->buckets + digest, tmp); + return (entry); } diff --git a/src/ht/ft_inter_htdel_first_order.c b/src/ht/ft_inter_htdel_first_order.c new file mode 100644 index 0000000..b6fd770 --- /dev/null +++ b/src/ht/ft_inter_htdel_first_order.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_inter_htdel_first_order.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/04/03 06:56:54 by charles #+# #+# */ +/* Updated: 2020/04/03 06:58:36 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_ht.h" + +static t_ftdel_func g_htdelone_value_del_func = NULL; + +void ft_inter_htdel_first_order(t_ftht_entry *entry) +{ + if (g_htdelone_value_del_func != NULL) + g_htdelone_value_del_func(entry->value); + free(entry->key); + free(entry); +} + +void ft_inter_htdel_first_order_setup(t_ftdel_func del) +{ + g_htdelone_value_del_func = del; +} + +void ft_inter_htdel_first_order_teardown(void) +{ + g_htdelone_value_del_func = NULL; +} |
