diff options
Diffstat (limited to 'src')
| -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 | ||||
| -rw-r--r-- | src/lst/ft_lstbsearch.c | 56 | ||||
| -rw-r--r-- | src/lst/ft_lstremove_if.c | 32 | ||||
| -rw-r--r-- | src/str/ft_strcpy.c | 2 | ||||
| -rw-r--r-- | src/str/ft_strlen.c | 2 |
16 files changed, 388 insertions, 2 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) +} diff --git a/src/lst/ft_lstbsearch.c b/src/lst/ft_lstbsearch.c new file mode 100644 index 0000000..6af9cae --- /dev/null +++ b/src/lst/ft_lstbsearch.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstbsearch.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:17:51 by cacharle #+# #+# */ +/* Updated: 2020/01/30 09:17:53 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +static t_list *st_lstmiddle(t_list *lst, t_list) +{ + t_list *slow; + t_list *fast; + + if (lst == NULL) + return (NULL); + slow = lst; + fast = lst; + while (fast != last) + { + fast = fast->next; + if (fast == NULL) + break; + slow = slow->next; + fast = fast->next; + } + return (slow); +} + +static t_list *st_lstbsearch_rec(t_list *lst, t_list *last, + t_ftbool (*equal)(void *ref, void *content), void *ref) +{ + t_list *mid; + t_list *left; + + if (lst == NULL) + return (NULL); + if ((*equal)(lst->content)) + return (lst); + mid = st_lstmiddle(lst, last); + left = ft_lstbsearch_rec(lst->next, mid, equal)); + if (left != NULL) + return (left); + return (ft_lstbsearch_rec(mid, NULL, equal)); +} + +t_list *ft_lstbsearch(t_list *lst, + t_ftbool (*equal)(void *ref, void *content), void *ref) +{ + return (ft_lstbsearch_rec(lst, NULL, equal)); +} diff --git a/src/lst/ft_lstremove_if.c b/src/lst/ft_lstremove_if.c new file mode 100644 index 0000000..2fa06a3 --- /dev/null +++ b/src/lst/ft_lstremove_if.c @@ -0,0 +1,32 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_lstremove_if.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/01/30 09:36:49 by cacharle #+# #+# */ +/* Updated: 2020/01/30 09:55:47 by cacharle ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void ft_lstremove_if(t_list **lst, + t_ftbool (*equal)(void *ref, void *content), void *ref, + void (*del)(void *content)) +{ + t_list *saved_next; + + if (lst == NULL || *lst == NULL) + return ; + if (!equal(ref, &(*lst)->val)) + { + ft_lstremove_if(&(*lst)->next, equal, ref, del); + return ; + } + saved_next = (*lst)->next; + ft_lstdelone(*lst, del); + *lst = saved_next; + ref_ft_list_remove_if(lst, equal, ref, del); +} diff --git a/src/str/ft_strcpy.c b/src/str/ft_strcpy.c index 9677b24..ee6ff0d 100644 --- a/src/str/ft_strcpy.c +++ b/src/str/ft_strcpy.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:38:36 by cacharle #+# #+# */ -/* Updated: 2019/11/20 23:25:57 by cacharle ### ########.fr */ +/* Updated: 2020/01/17 11:36:19 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/str/ft_strlen.c b/src/str/ft_strlen.c index 0e0a47c..0d593e1 100644 --- a/src/str/ft_strlen.c +++ b/src/str/ft_strlen.c @@ -6,7 +6,7 @@ /* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/10/07 10:32:48 by cacharle #+# #+# */ -/* Updated: 2019/11/21 01:45:42 by cacharle ### ########.fr */ +/* Updated: 2020/01/17 11:13:43 by cacharle ### ########.fr */ /* */ /* ************************************************************************** */ |
