aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-04-03 07:19:25 +0200
committerCharles <sircharlesaze@gmail.com>2020-04-03 07:19:25 +0200
commitd2feec1f97e9f8f201e56ad33662bb663c328a0a (patch)
treef97690082352bb1752d867d66f21a7bee1ef126c /src
parent948c0953527fe3bef28904b38a16a9e4342e7e98 (diff)
downloadlibft-d2feec1f97e9f8f201e56ad33662bb663c328a0a.tar.gz
libft-d2feec1f97e9f8f201e56ad33662bb663c328a0a.tar.bz2
libft-d2feec1f97e9f8f201e56ad33662bb663c328a0a.zip
Changing hash table del function to regular one with a first order internal function, removing a few typedef to instead use standard types
Diffstat (limited to 'src')
-rw-r--r--src/algo/ft_is_set.c5
-rw-r--r--src/ht/ft_htdelone.c11
-rw-r--r--src/ht/ft_htdestroy.c10
-rw-r--r--src/ht/ft_htget.c6
-rw-r--r--src/ht/ft_hthash.c4
-rw-r--r--src/ht/ft_htnew.c8
-rw-r--r--src/ht/ft_htset.c37
-rw-r--r--src/ht/ft_inter_htdel_first_order.c33
-rw-r--r--src/str/ft_strcasecmp.c2
-rw-r--r--src/str/ft_strncasecmp.c2
-rw-r--r--src/str/ft_strncmp.c2
11 files changed, 79 insertions, 41 deletions
diff --git a/src/algo/ft_is_set.c b/src/algo/ft_is_set.c
index 3e7ae31..79a5e7c 100644
--- a/src/algo/ft_is_set.c
+++ b/src/algo/ft_is_set.c
@@ -6,14 +6,13 @@
/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/01/19 07:17:15 by cacharle #+# #+# */
-/* Updated: 2020/02/10 02:51:41 by cacharle ### ########.fr */
+/* Updated: 2020/04/03 07:05:19 by charles ### ########.fr */
/* */
/* ************************************************************************** */
-#include "libft.h"
#include "libft_algo.h"
-t_ftbool ft_is_set(void *base, size_t nel, size_t width,
+bool ft_is_set(void *base, size_t nel, size_t width,
t_ftcompar_func compar)
{
size_t i;
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;
+}
diff --git a/src/str/ft_strcasecmp.c b/src/str/ft_strcasecmp.c
index 044e6de..6dd86eb 100644
--- a/src/str/ft_strcasecmp.c
+++ b/src/str/ft_strcasecmp.c
@@ -11,7 +11,7 @@
/* ************************************************************************** */
#include "libft_str.h"
-#include "libft_types.h"
+#include "libft_def.h"
int ft_strcasecmp(const char *s1, const char *s2)
{
diff --git a/src/str/ft_strncasecmp.c b/src/str/ft_strncasecmp.c
index aafdc8c..7153237 100644
--- a/src/str/ft_strncasecmp.c
+++ b/src/str/ft_strncasecmp.c
@@ -11,7 +11,7 @@
/* ************************************************************************** */
#include "libft.h"
-#include "libft_types.h"
+#include "libft_def.h"
int ft_strncasecmp(const char *s1, const char *s2, size_t n)
{
diff --git a/src/str/ft_strncmp.c b/src/str/ft_strncmp.c
index caa052b..d810e8c 100644
--- a/src/str/ft_strncmp.c
+++ b/src/str/ft_strncmp.c
@@ -11,7 +11,7 @@
/* ************************************************************************** */
#include "libft.h"
-#include "libft_types.h"
+#include "libft_def.h"
int ft_strncmp(const char *s1, const char *s2, size_t n)
{