aboutsummaryrefslogtreecommitdiff
path: root/libft/src/ht
diff options
context:
space:
mode:
authorCharles Cabergs <me@cacharle.xyz>2020-10-11 15:52:52 +0200
committerCharles Cabergs <me@cacharle.xyz>2020-10-11 15:52:52 +0200
commitc98de126d2252fe47dc2a9094a5f9a8fa6b4b60a (patch)
tree12f1c827ee063ed3e7038f6a704014e611e4f388 /libft/src/ht
parenta4ceb5974d1b7dcdd12cc81b7eb07893ea16c8ad (diff)
downloadminishell-c98de126d2252fe47dc2a9094a5f9a8fa6b4b60a.tar.gz
minishell-c98de126d2252fe47dc2a9094a5f9a8fa6b4b60a.tar.bz2
minishell-c98de126d2252fe47dc2a9094a5f9a8fa6b4b60a.zip
Removing libft/minishell_test submodules, Removing subject/README/etc
Diffstat (limited to 'libft/src/ht')
-rw-r--r--libft/src/ht/ft_htdelone.c30
-rw-r--r--libft/src/ht/ft_htdestroy.c32
-rw-r--r--libft/src/ht/ft_htentry_new.c37
-rw-r--r--libft/src/ht/ft_htget.c35
-rw-r--r--libft/src/ht/ft_hthash.c35
-rw-r--r--libft/src/ht/ft_htiter.c31
-rw-r--r--libft/src/ht/ft_htnew.c37
-rw-r--r--libft/src/ht/ft_htset.c59
-rw-r--r--libft/src/ht/ft_htset_safe.c25
-rw-r--r--libft/src/ht/ft_inter_htdel_first_order.c33
-rw-r--r--libft/src/ht/ft_inter_htkey_cmp.c25
11 files changed, 379 insertions, 0 deletions
diff --git a/libft/src/ht/ft_htdelone.c b/libft/src/ht/ft_htdelone.c
new file mode 100644
index 0000000..bc2e047
--- /dev/null
+++ b/libft/src/ht/ft_htdelone.c
@@ -0,0 +1,30 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_htdelone.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/30 09:27:18 by cacharle #+# #+# */
+/* Updated: 2020/04/03 07:14:28 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include "libft_ht.h"
+
+/*
+** \brief Delete one hash table entry
+** \param key Key of entry to delete
+** \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)(void*))
+{
+ ft_inter_htdel_first_order_setup(del);
+ ft_lstremove_if(ht->buckets + ft_hthash(ht, key),
+ ft_inter_htkey_cmp, key,
+ (void (*)(void*))ft_inter_htdel_first_order);
+ ft_inter_htdel_first_order_teardown();
+}
diff --git a/libft/src/ht/ft_htdestroy.c b/libft/src/ht/ft_htdestroy.c
new file mode 100644
index 0000000..c43754e
--- /dev/null
+++ b/libft/src/ht/ft_htdestroy.c
@@ -0,0 +1,32 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_htdestroy.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/30 08:31:02 by cacharle #+# #+# */
+/* Updated: 2020/04/03 07:01:30 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_ht.h"
+
+/*
+** \brief Destroy an hash table.
+** \param del Function to delete each entry
+** \warning The del function HAS to free the key
+*/
+
+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*))ft_inter_htdel_first_order);
+ ft_inter_htdel_first_order_teardown();
+ free(ht->buckets);
+ free(ht);
+}
diff --git a/libft/src/ht/ft_htentry_new.c b/libft/src/ht/ft_htentry_new.c
new file mode 100644
index 0000000..12a1159
--- /dev/null
+++ b/libft/src/ht/ft_htentry_new.c
@@ -0,0 +1,37 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_htentry_new.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/30 08:45:36 by cacharle #+# #+# */
+/* Updated: 2020/02/17 04:09:50 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include "libft_ht.h"
+
+/*
+** \brief Create a new hash table key/value pair.
+** \param key Hash entry string key (always duplicated)
+** \return Content or NULL if an allocation failed.
+*/
+
+t_ftht_entry *ft_htentry_new(char *key, void *value)
+{
+ t_ftht_entry *content;
+
+ if (key == NULL)
+ return (NULL);
+ if ((content = (t_ftht_entry*)malloc(sizeof(t_ftht_entry))) == NULL)
+ return (NULL);
+ if ((content->key = ft_strdup(key)) == NULL)
+ {
+ free(content);
+ return (NULL);
+ }
+ content->value = value;
+ return (content);
+}
diff --git a/libft/src/ht/ft_htget.c b/libft/src/ht/ft_htget.c
new file mode 100644
index 0000000..75da785
--- /dev/null
+++ b/libft/src/ht/ft_htget.c
@@ -0,0 +1,35 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_htget.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/30 08:33:21 by cacharle #+# #+# */
+/* Updated: 2020/04/03 07:12:58 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include "libft_ht.h"
+
+/*
+** \brief Retrieve a value with a key
+** \param ht Hash table where key is searched
+** \param key Searched key
+** \return Value void pointer at key or NULL if not found
+*/
+
+void *ft_htget(t_ftht *ht, char *key)
+{
+ size_t digest;
+ t_ftlst *found;
+
+ if (ht == NULL || key == NULL)
+ return (NULL);
+ digest = ft_hthash(ht, key);
+ found = ft_lstlfind(ht->buckets[digest], ft_inter_htkey_cmp, key);
+ if (found == NULL)
+ return (NULL);
+ return (((t_ftht_entry*)found->data)->value);
+}
diff --git a/libft/src/ht/ft_hthash.c b/libft/src/ht/ft_hthash.c
new file mode 100644
index 0000000..200252f
--- /dev/null
+++ b/libft/src/ht/ft_hthash.c
@@ -0,0 +1,35 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_hthash.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/30 09:56:01 by cacharle #+# #+# */
+/* Updated: 2020/10/11 14:00:00 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_ht.h"
+
+/*
+** \brief Hash a string
+** \param ht So that the index is in the hash table bound
+** \param key String to hash
+** \return Hash
+*/
+
+size_t ft_hthash(t_ftht *ht, char *key)
+{
+ size_t digest;
+
+ if (*key == '\0')
+ return (0);
+ digest = *key++ << 7;
+ while (*key != '\0')
+ {
+ digest = ((1000003 * digest) ^ *key) & (1 << 16);
+ key++;
+ }
+ return (digest % ht->size);
+}
diff --git a/libft/src/ht/ft_htiter.c b/libft/src/ht/ft_htiter.c
new file mode 100644
index 0000000..b854993
--- /dev/null
+++ b/libft/src/ht/ft_htiter.c
@@ -0,0 +1,31 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_htiter.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <charles.cabergs@gmail.com> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/04/01 18:02:24 by charles #+# #+# */
+/* Updated: 2020/04/01 18:02:32 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_ht.h"
+
+/*
+** \brief Iterate over entry of hash table
+** \param ht Iterated hash table
+** \param f Function applied to each entry
+*/
+
+void ft_htiter(t_ftht *ht, void (*f)(t_ftht_entry*))
+{
+ size_t i;
+
+ i = 0;
+ while (i < ht->size)
+ {
+ ft_lstiter(ht->buckets[i], (void (*)(void*))f);
+ i++;
+ }
+}
diff --git a/libft/src/ht/ft_htnew.c b/libft/src/ht/ft_htnew.c
new file mode 100644
index 0000000..585c211
--- /dev/null
+++ b/libft/src/ht/ft_htnew.c
@@ -0,0 +1,37 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_htnew.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/30 08:19:16 by cacharle #+# #+# */
+/* Updated: 2020/04/04 22:34:55 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_ht.h"
+
+/*
+** \brief Create a new hash table.
+** \param size Size of the underlying array of linked list (buckets)
+** \return Created hash table or NULL is an allocation failed
+*/
+
+t_ftht *ft_htnew(size_t size)
+{
+ t_ftht *ht;
+
+ if (size == 0)
+ return (NULL);
+ if ((ht = (t_ftht*)malloc(sizeof(t_ftht))) == NULL)
+ return (NULL);
+ ht->buckets = (t_ftlst**)ft_calloc(size, sizeof(t_ftlst*));
+ if (ht->buckets == NULL)
+ {
+ free(ht);
+ return (NULL);
+ }
+ ht->size = size;
+ return (ht);
+}
diff --git a/libft/src/ht/ft_htset.c b/libft/src/ht/ft_htset.c
new file mode 100644
index 0000000..b3a44ee
--- /dev/null
+++ b/libft/src/ht/ft_htset.c
@@ -0,0 +1,59 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_htset.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/30 08:41:52 by cacharle #+# #+# */
+/* Updated: 2020/04/03 07:13:17 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_ht.h"
+
+/*
+** \brief Create/Update a entry in hash table.
+** \note If `key` already exist in `ht`
+** 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
+** \param del Destroy function in case the entry is modified.
+** \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)(void*)
+)
+{
+ size_t digest;
+ t_ftht_entry *entry;
+ t_ftlst *tmp;
+
+ if (ht == NULL || key == 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(((t_ftht_entry*)tmp->data)->value);
+ ((t_ftht_entry*)tmp->data)->value = value;
+ return ((t_ftht_entry*)tmp->data);
+ }
+ if ((entry = ft_htentry_new(key, value)) == NULL)
+ return (NULL);
+ if ((tmp = ft_lstnew(entry)) == NULL)
+ {
+ free(entry->key);
+ free(entry);
+ return (NULL);
+ }
+ ft_lstpush_front(ht->buckets + digest, tmp);
+ return (entry);
+}
diff --git a/libft/src/ht/ft_htset_safe.c b/libft/src/ht/ft_htset_safe.c
new file mode 100644
index 0000000..fd132d2
--- /dev/null
+++ b/libft/src/ht/ft_htset_safe.c
@@ -0,0 +1,25 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_htset_safe.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: charles <me@cacharle.xyz> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/09/09 15:46:10 by charles #+# #+# */
+/* Updated: 2020/09/09 15:46:24 by charles ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft_ht.h"
+
+t_ftht_entry *ft_htset_safe(
+ t_ftht *ht,
+ char *key,
+ void *value,
+ void (*del)(void*)
+)
+{
+ if (value == NULL)
+ return (NULL);
+ return (ft_htset(ht, key, value, del));
+}
diff --git a/libft/src/ht/ft_inter_htdel_first_order.c b/libft/src/ht/ft_inter_htdel_first_order.c
new file mode 100644
index 0000000..b6fd770
--- /dev/null
+++ b/libft/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/libft/src/ht/ft_inter_htkey_cmp.c b/libft/src/ht/ft_inter_htkey_cmp.c
new file mode 100644
index 0000000..e8a0375
--- /dev/null
+++ b/libft/src/ht/ft_inter_htkey_cmp.c
@@ -0,0 +1,25 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_internal_htkey_equal.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2020/01/30 09:24:39 by cacharle #+# #+# */
+/* Updated: 2020/02/28 12:20:43 by cacharle ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "libft.h"
+#include "libft_ht.h"
+
+/*
+** Hash table internal function to compare key string in linked list.
+*/
+
+int ft_inter_htkey_cmp(const void *ref_key, const void *content)
+{
+ if (ref_key == NULL || content == NULL)
+ return (-1);
+ return (ft_strcmp((char*)ref_key, ((t_ftht_entry*)content)->key));
+}