diff options
Diffstat (limited to 'src/ht')
| -rw-r--r-- | src/ht/ft_htcontent_new.c | 31 | ||||
| -rw-r--r-- | src/ht/ft_htdelone.c | 21 | ||||
| -rw-r--r-- | src/ht/ft_htdelone_key.c | 19 | ||||
| -rw-r--r-- | src/ht/ft_htdestroy.c | 24 | ||||
| -rw-r--r-- | src/ht/ft_htdestroy_all.c | 26 | ||||
| -rw-r--r-- | src/ht/ft_htdestroy_key.c | 26 | ||||
| -rw-r--r-- | src/ht/ft_htget.c | 28 | ||||
| -rw-r--r-- | src/ht/ft_hthash.c | 28 | ||||
| -rw-r--r-- | src/ht/ft_htnew.c | 32 | ||||
| -rw-r--r-- | src/ht/ft_htset.c | 34 | ||||
| -rw-r--r-- | src/ht/ft_inter_htkey_equal.c | 21 |
11 files changed, 290 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..d502bf2 --- /dev/null +++ b/src/ht/ft_htdelone.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htdelone.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:27:18 by cacharle #+# #+# */ +/* Updated: 2020/01/31 10:40:40 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(ht, key), + (t_ftbool (*)(void*, void*))ft_inter_htkey_equal, key, + (void (*)(void*))del); +} diff --git a/src/ht/ft_htdelone_key.c b/src/ht/ft_htdelone_key.c new file mode 100644 index 0000000..96d55ec --- /dev/null +++ b/src/ht/ft_htdelone_key.c @@ -0,0 +1,19 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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" +#include "libft_ht.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..e0442c6 --- /dev/null +++ b/src/ht/ft_htdestroy.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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" +#include "libft_ht.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, (void (*)(void*))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..6f98a43 --- /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/31 10:43:13 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_ht.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_htdelcontent_all); +} diff --git a/src/ht/ft_htdestroy_key.c b/src/ht/ft_htdestroy_key.c new file mode 100644 index 0000000..a704314 --- /dev/null +++ b/src/ht/ft_htdestroy_key.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htdestroy_key.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:31:02 by cacharle #+# #+# */ +/* Updated: 2020/01/31 10:43:45 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_ht.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, *ft_inter_htdelcontent_key); +} diff --git a/src/ht/ft_htget.c b/src/ht/ft_htget.c new file mode 100644 index 0000000..0002249 --- /dev/null +++ b/src/ht/ft_htget.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htget.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:33:21 by cacharle #+# #+# */ +/* Updated: 2020/01/31 10:40:57 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_ht.h" + +t_ftht_content *ft_htget(t_ftht *ht, char *key) +{ + + t_ftht_digest digest; + + return (NULL); // lstbsearch breaking + if (ht == NULL || key == NULL) + return (NULL); + digest = ft_hthash(ht, key); + return (ft_lstbsearch(ht->entries[digest], + (t_ftbool (*)(void*, void*))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..e7e696c --- /dev/null +++ b/src/ht/ft_hthash.c @@ -0,0 +1,28 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* 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 */ +/* */ +/* ************************************************************************** */ + +#include "libft_ht.h" + +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 << 16); + key++; + } + return (digest % ht->size); +} diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c new file mode 100644 index 0000000..d98a724 --- /dev/null +++ b/src/ht/ft_htnew.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htnew.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:19:16 by cacharle #+# #+# */ +/* Updated: 2020/02/10 02:16:20 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_ht.h" + +t_ftht *ft_htnew(t_ftsize size) +{ + t_ftht *ht; + + if (size == 0) + return (NULL); + if ((ht = (t_ftht*)malloc(sizeof(t_ftht))) == NULL) + return (NULL); + ht->entries = (t_ftht_entry*)ft_calloc(size, sizeof(t_ftht_entry)); + if (ht->entries == 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..5ace788 --- /dev/null +++ b/src/ht/ft_htset.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_htset.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 08:41:52 by cacharle #+# #+# */ +/* Updated: 2020/01/31 10:33:39 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_ht.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..7714c84 --- /dev/null +++ b/src/ht/ft_inter_htkey_equal.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_internal_htkey_equal.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:24:39 by cacharle #+# #+# */ +/* Updated: 2020/01/31 09:51:03 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" +#include "libft_ht.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); +} |
