diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-01-30 10:36:49 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-01-30 10:36:49 +0100 |
| commit | aa9613efb6fb39bd96fc4836b5d38c3746af1b15 (patch) | |
| tree | 0fac2b661a860b3ca2e3effa868384290064f708 /src/ht | |
| parent | fe37597119353ce183fc404417b81bd4702f64b7 (diff) | |
| download | libft-aa9613efb6fb39bd96fc4836b5d38c3746af1b15.tar.gz libft-aa9613efb6fb39bd96fc4836b5d38c3746af1b15.tar.bz2 libft-aa9613efb6fb39bd96fc4836b5d38c3746af1b15.zip | |
hash table draft
Diffstat (limited to 'src/ht')
| -rw-r--r-- | src/ht/ft_htcontent_new.c | 31 | ||||
| -rw-r--r-- | src/ht/ft_htdelone.c | 19 | ||||
| -rw-r--r-- | src/ht/ft_htdelone_key.c | 18 | ||||
| -rw-r--r-- | src/ht/ft_htdestroy.c | 23 | ||||
| -rw-r--r-- | src/ht/ft_htdestroy_all.c | 26 | ||||
| -rw-r--r-- | src/ht/ft_htdestroy_key.c | 25 | ||||
| -rw-r--r-- | src/ht/ft_htdestroy_value.c | 25 | ||||
| -rw-r--r-- | src/ht/ft_htget.c | 24 | ||||
| -rw-r--r-- | src/ht/ft_hthash.c | 26 | ||||
| -rw-r--r-- | src/ht/ft_htnew.c | 28 | ||||
| -rw-r--r-- | src/ht/ft_htset.c | 33 | ||||
| -rw-r--r-- | src/ht/ft_inter_htkey_equal.c | 20 |
12 files changed, 298 insertions, 0 deletions
diff --git a/src/ht/ft_htcontent_new.c b/src/ht/ft_htcontent_new.c new file mode 100644 index 0000000..4ffa9bf --- /dev/null +++ b/src/ht/ft_htcontent_new.c @@ -0,0 +1,31 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htcontent_new.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:45:36 by cacharle #+# #+# */ +/* Updated: 2020/01/30 09:52:28 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_ht.h" + +t_ftht_content *ft_htcontent_new(char *key, void *value) +{ + t_ftht_content *content; + + if (key == NULL) + return (NULL); + if ((content = (t_ftht_content*)malloc(sizeof(t_ftht_content))) == NULL) + return (NULL); + if ((content->key = ft_strdup(key)) == NULL) + { + free(content); + return (NULL); + } + content->value = value; + return (content); +} diff --git a/src/ht/ft_htdelone.c b/src/ht/ft_htdelone.c new file mode 100644 index 0000000..8d350ae --- /dev/null +++ b/src/ht/ft_htdelone.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:27:18 by cacharle #+# #+# */ +/* Updated: 2020/01/30 09:55:06 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_ht.h" + +void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_content*)) +{ + ft_lstremove_if(ht->entries + ft_hthash(key), ft_iter_htkey_equal, key, del); +} diff --git a/src/ht/ft_htdelone_key.c b/src/ht/ft_htdelone_key.c new file mode 100644 index 0000000..5dc0c16 --- /dev/null +++ b/src/ht/ft_htdelone_key.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htdelone_key.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:45:11 by cacharle #+# #+# */ +/* Updated: 2020/01/30 09:46:42 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_htdelone_key(t_ftht *ht, char *key) +{ + ft_htdelone(ht, key, ft_inter_htdelcontent_key); +} diff --git a/src/ht/ft_htdestroy.c b/src/ht/ft_htdestroy.c new file mode 100644 index 0000000..6e04386 --- /dev/null +++ b/src/ht/ft_htdestroy.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htdestroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:19:06 by cacharle #+# #+# */ +/* Updated: 2020/01/30 08:33:09 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_content*)) +{ + if (ht == NULL) + return ; + while (ht->size-- > 0) + ft_lstclear(ht->entries + ht->size, del); + free(ht->entries); + free(ht); +} diff --git a/src/ht/ft_htdestroy_all.c b/src/ht/ft_htdestroy_all.c new file mode 100644 index 0000000..761f577 --- /dev/null +++ b/src/ht/ft_htdestroy_all.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htdestroy_all.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:29:58 by cacharle #+# #+# */ +/* Updated: 2020/01/30 08:30:53 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static void st_htdelcontent_all(t_ftht_content *content) +{ + if (content == NULL) + return ; + free(content->key); + free(content->value); +} + +void ft_htdestroy_all(t_ftht *ht) +{ + ft_htdestroy(ht, *st_dtdelcontent_all); +} diff --git a/src/ht/ft_htdestroy_key.c b/src/ht/ft_htdestroy_key.c new file mode 100644 index 0000000..e3d562f --- /dev/null +++ b/src/ht/ft_htdestroy_key.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htdestroy_key.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:31:02 by cacharle #+# #+# */ +/* Updated: 2020/01/30 09:46:14 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_inter_htdelcontent_key(t_ftht_content *content) +{ + if (content == NULL) + return ; + free(content->key); +} + +void ft_htdestroy_key(t_ftht *ht) +{ + ft_htdestroy(ht, *st_dtdelcontent_key); +} diff --git a/src/ht/ft_htdestroy_value.c b/src/ht/ft_htdestroy_value.c new file mode 100644 index 0000000..b71b960 --- /dev/null +++ b/src/ht/ft_htdestroy_value.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htdestroy_value.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:31:29 by cacharle #+# #+# */ +/* Updated: 2020/01/30 08:31:49 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static void st_htdelcontent_value(t_ftht_content *content) +{ + if (content == NULL) + return ; + free(content->value); +} + +void ft_htdestroy_value(t_ftht *ht) +{ + ft_htdestroy(ht, *st_dtdelcontent_value); +} diff --git a/src/ht/ft_htget.c b/src/ht/ft_htget.c new file mode 100644 index 0000000..983fd74 --- /dev/null +++ b/src/ht/ft_htget.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htget.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:33:21 by cacharle #+# #+# */ +/* Updated: 2020/01/30 09:25:51 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_ftht_content *ft_htget(t_ftht *ht, char *key) +{ + t_ftht_digest digest; + + if (ht == NULL || key == NULL) + return (NULL); + digest = ft_hthash(ht, key); + return (ft_lstbsearch(ht->entries[digest], + &ft_inter_htkey_equal, key)->content); +} diff --git a/src/ht/ft_hthash.c b/src/ht/ft_hthash.c new file mode 100644 index 0000000..66f8efb --- /dev/null +++ b/src/ht/ft_hthash.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_hthash.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:56:01 by cacharle #+# #+# */ +/* Updated: 2020/01/30 10:34:27 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +t_ftht_digest ft_hthash(t_ftht *ht, char *key) +{ + t_ftht_digest digest; + + if (*key == '\0') + return (0); + digest = *key++ << 7; + while (*key != '\0') + { + digest = ((1000003 * digest) ^ *key) & (1<<32); + key++; + } + return (digest); +} diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c new file mode 100644 index 0000000..c3b7cc7 --- /dev/null +++ b/src/ht/ft_htnew.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:19:16 by cacharle #+# #+# */ +/* Updated: 2020/01/30 08:19:18 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_ftht *ft_htnew(t_ftsize size) +{ + t_ftht *ht; + + if ((ht = (t_ftht*)malloc(sizeof(t_ftht))) == NULL) + return (NULL); + if ((ht->entries = (t_ftht_entry*)ft_calloc(size, sizeof(t_ftht_entry))) == NULL) + { + free(ht); + return (NULL); + } + ht->size = size; + return (ht); +} diff --git a/src/ht/ft_htset.c b/src/ht/ft_htset.c new file mode 100644 index 0000000..32aa448 --- /dev/null +++ b/src/ht/ft_htset.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:41:52 by cacharle #+# #+# */ +/* Updated: 2020/01/30 08:50:48 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value) +{ + t_ftht_digest digest; + t_ftht_content *content; + t_ftht_entry entry; + + if (ht == NULL || key == NULL) + return (NULL) + if ((content = ft_htcontent_new(key, value)) == NULL) + return (NULL); + if ((entry = ft_lstnew(content)) = NULL) + { + free(content); + return (NULL); + } + digest = ft_hthash(ht, key); + ft_lstadd_front(ht->entries + digest, entry); + return (content); +} diff --git a/src/ht/ft_inter_htkey_equal.c b/src/ht/ft_inter_htkey_equal.c new file mode 100644 index 0000000..b652bba --- /dev/null +++ b/src/ht/ft_inter_htkey_equal.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_internal_htkey_equal.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:24:39 by cacharle #+# #+# */ +/* Updated: 2020/01/30 09:25:36 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_ftbool ft_inter_htkey_equal(char *ref_key, t_ftht_content *content) +{ + if (ref_key == NULL || content == NULL) + return (FALSE); + return (ft_strcmp(ref_key, content->key) == 0) +} |
