diff --git a/libft/includes/btree.h b/libft/includes/btree.h new file mode 100644 index 00000000..ab5b587e --- /dev/null +++ b/libft/includes/btree.h @@ -0,0 +1,30 @@ +#ifndef BTREE_H +# define BTREE_H + +#include "libft.h" + +typedef struct s_btree t_btree; + +struct s_btree +{ + struct s_btree *left; + struct s_btree *right; + void *item; +}; + +t_btree *btree_create_node(void *item); + +void btree_insert_data( + t_btree **root, + void *item, + int (*cmpf)(void *, void *)); + +void *btree_search_item(t_btree *root, + void *data_ref, int (*cmpf)(void *, void *)); + +int btree_level_count(t_btree *root); +void btree_apply_prefix(t_btree *root, void (*applyf)(void *)); +void btree_apply_infix(t_btree *root, void (*applyf)(void *)); +void btree_apply_suffix(t_btree *root, void (*applyf)(void *)); + +#endif diff --git a/libft/includes/dlst.h b/libft/includes/dlst.h index ffcf518f..42d73729 100644 --- a/libft/includes/dlst.h +++ b/libft/includes/dlst.h @@ -23,12 +23,13 @@ struct s_dlist typedef struct s_dlist t_dlist; -void ft_dlst_add_after(t_dlist **alst, t_dlist *new); -void ft_dlst_add_before(t_dlist **alst, t_dlist *new); -void ft_dlst_delone(t_dlist **alst, void (*del)(void *, size_t)); -int ft_dlst_size(t_dlist *list); -t_dlist *ft_dlst_new(void const *content, size_t content_size); -t_dlist *ft_dlst_last(t_dlist *list); +void ft_dlstadd_after(t_dlist **alst, t_dlist *new); +void ft_dlstadd_before(t_dlist **alst, t_dlist *new); +void ft_dlstdel(t_dlist **alst, void (*del)(void *, size_t)); +void ft_dlstdelone(t_dlist **alst, void (*del)(void *, size_t)); +int ft_dlstsize(t_dlist *list); +t_dlist *ft_dlstnew(void const *content, size_t content_size); +t_dlist *ft_dlstlast(t_dlist *list); char *ft_dlsttostr(t_dlist *list); #endif diff --git a/libft/includes/libft.h b/libft/includes/libft.h index 311a94f2..39f21ec7 100644 --- a/libft/includes/libft.h +++ b/libft/includes/libft.h @@ -16,6 +16,7 @@ # include "ft_xattr.h" # include "lst.h" # include "dlst.h" +# include "btree.h" # include # include @@ -129,5 +130,5 @@ int ft_printf(const char *format, ...); char *ft_getenv(char **env, char *key); -void *ft_realloc(void *data, int size) +void *ft_realloc(void *data, int size); #endif diff --git a/libft/src/btree/btree_apply_by_level.c b/libft/src/btree/btree_apply_by_level.c new file mode 100644 index 00000000..0cb2c4bd --- /dev/null +++ b/libft/src/btree/btree_apply_by_level.c @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_apply_by_level.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/19 12:06:15 by jhalford #+# #+# */ +/* Updated: 2016/08/23 17:49:17 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +int g_level = 0; + +static void btree_apply_to_level( + t_btree *root, + int level, + int is_first_elem, + void (*applyf)(void *item, int current_level, int is_first_elem)) +{ + if (level == g_level) + { + (*applyf)(root->item, level, is_first_elem); + return ; + } + if (root->left) + btree_apply_to_level(root->left, level + 1, is_first_elem, applyf); + if (root->right) + btree_apply_to_level(root->right, level + 1, 0, applyf); +} + +void btree_apply_by_level( + t_btree *root, + void (*applyf)(void *item, int current_level, int is_first_elem)) +{ + int height; + + height = btree_level_count(root); + while (g_level < height) + { + btree_apply_to_level(root, 0, 1, applyf); + g_level++; + } +} diff --git a/libft/src/btree/btree_apply_infix.c b/libft/src/btree/btree_apply_infix.c new file mode 100644 index 00000000..65d2b5d6 --- /dev/null +++ b/libft/src/btree/btree_apply_infix.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_create_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */ +/* Updated: 2016/08/18 21:10:05 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +void btree_apply_infix(t_btree *root, void (*applyf)(void *)) +{ + if (root->left) + btree_apply_infix(root->left, applyf); + (*applyf)(root->item); + if (root->right) + btree_apply_infix(root->right, applyf); + return ; +} diff --git a/libft/src/btree/btree_apply_prefix.c b/libft/src/btree/btree_apply_prefix.c new file mode 100644 index 00000000..e7e4332c --- /dev/null +++ b/libft/src/btree/btree_apply_prefix.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_create_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */ +/* Updated: 2016/08/18 21:06:44 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +void btree_apply_prefix(t_btree *root, void (*applyf)(void *)) +{ + (*applyf)(root->item); + if (root->left) + btree_apply_prefix(root->left, applyf); + if (root->right) + btree_apply_prefix(root->right, applyf); +} diff --git a/libft/src/btree/btree_apply_suffix.c b/libft/src/btree/btree_apply_suffix.c new file mode 100644 index 00000000..9cf8b4ea --- /dev/null +++ b/libft/src/btree/btree_apply_suffix.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_create_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */ +/* Updated: 2016/08/18 21:11:19 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +void btree_apply_suffix(t_btree *root, void (*applyf)(void *)) +{ + if (root->left) + btree_apply_suffix(root->left, applyf); + if (root->right) + btree_apply_suffix(root->right, applyf); + (*applyf)(root->item); + return ; +} diff --git a/libft/src/btree/btree_create_node.c b/libft/src/btree/btree_create_node.c new file mode 100644 index 00000000..b50c4be4 --- /dev/null +++ b/libft/src/btree/btree_create_node.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_create_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */ +/* Updated: 2016/08/19 23:32:10 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +t_btree *btree_create_node(void *item) +{ + t_btree *node; + + node = (t_btree *)malloc(sizeof(t_btree)); + node->left = 0; + node->right = 0; + node->item = item; + return (node); +} diff --git a/libft/src/btree/btree_insert_data.c b/libft/src/btree/btree_insert_data.c new file mode 100644 index 00000000..1c88672e --- /dev/null +++ b/libft/src/btree/btree_insert_data.c @@ -0,0 +1,42 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_create_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */ +/* Updated: 2016/08/19 14:12:59 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +void btree_insert_data( + t_btree **root, + void *item, + int (*cmpf)(void *, void *)) +{ + t_btree *node; + + if (!*root) + { + *root = btree_create_node(item); + return ; + } + node = *root; + if ((*cmpf)(item, node->item) < 0) + { + if (node->left) + btree_insert_data(&node->left, item, cmpf); + else + node->left = btree_create_node(item); + } + else + { + if (node->right) + btree_insert_data(&node->right, item, cmpf); + else + node->right = btree_create_node(item); + } +} diff --git a/libft/src/btree/btree_level_count.c b/libft/src/btree/btree_level_count.c new file mode 100644 index 00000000..41248654 --- /dev/null +++ b/libft/src/btree/btree_level_count.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_create_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */ +/* Updated: 2016/08/25 17:46:00 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +int btree_level_count(t_btree *root) +{ + return (root + ? 1 + FT_MAX(btree_level_count(root->left), + btree_level_count(root->right)) + : 0); +} diff --git a/libft/src/btree/btree_search_item.c b/libft/src/btree/btree_search_item.c new file mode 100644 index 00000000..a7a1eb68 --- /dev/null +++ b/libft/src/btree/btree_search_item.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* btree_create_node.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: jhalford +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2016/08/16 13:43:51 by jhalford #+# #+# */ +/* Updated: 2016/08/23 19:04:56 by jhalford ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "btree.h" + +void *btree_search_item(t_btree *root, + void *data_ref, int (*cmpf)(void *, void *)) +{ + void *out; + + out = NULL; + if (root) + { + out = btree_search_item(root->left, data_ref, cmpf); + if (!out && ((*cmpf)(root->item, data_ref) == 0)) + out = root->item; + if (!out) + out = btree_search_item(root->right, data_ref, cmpf); + } + return (out); +} diff --git a/libft/src/dlst/ft_dlst_add_after.c b/libft/src/dlst/ft_dlstadd_after.c similarity index 95% rename from libft/src/dlst/ft_dlst_add_after.c rename to libft/src/dlst/ft_dlstadd_after.c index c1fa36eb..caec607d 100644 --- a/libft/src/dlst/ft_dlst_add_after.c +++ b/libft/src/dlst/ft_dlstadd_after.c @@ -12,7 +12,7 @@ #include "libft.h" -void ft_dlst_add_after(t_dlist **alst, t_dlist *new) +void ft_dlstadd_after(t_dlist **alst, t_dlist *new) { if (new) { diff --git a/libft/src/dlst/ft_dlst_add_before.c b/libft/src/dlst/ft_dlstadd_before.c similarity index 95% rename from libft/src/dlst/ft_dlst_add_before.c rename to libft/src/dlst/ft_dlstadd_before.c index 5025986c..e2d6b2e8 100644 --- a/libft/src/dlst/ft_dlst_add_before.c +++ b/libft/src/dlst/ft_dlstadd_before.c @@ -12,7 +12,7 @@ #include "libft.h" -void ft_dlst_add_before(t_dlist **alst, t_dlist *new) +void ft_dlstadd_before(t_dlist **alst, t_dlist *new) { if (new) { diff --git a/libft/src/dlst/ft_dlstdel.c b/libft/src/dlst/ft_dlstdel.c new file mode 100644 index 00000000..43131fb7 --- /dev/null +++ b/libft/src/dlst/ft_dlstdel.c @@ -0,0 +1,11 @@ +#include "libft.h" + +void ft_dlstdel(t_dlist **alst, void (*del)(void *, size_t)) +{ + if (alst && *alst) + { + ft_dlstdel(&(*alst)->next, del); + ft_dlstdel(&(*alst)->prev, del); + } + ft_dlstdelone(alst, del); +} diff --git a/libft/src/dlst/ft_dlst_delone.c b/libft/src/dlst/ft_dlstdelone.c similarity index 94% rename from libft/src/dlst/ft_dlst_delone.c rename to libft/src/dlst/ft_dlstdelone.c index ff83b296..a83bf62e 100644 --- a/libft/src/dlst/ft_dlst_delone.c +++ b/libft/src/dlst/ft_dlstdelone.c @@ -12,7 +12,7 @@ #include "libft.h" -void ft_dlst_delone(t_dlist **alst, void (*del)(void *, size_t)) +void ft_dlstdelone(t_dlist **alst, void (*del)(void *, size_t)) { t_dlist *tmp; diff --git a/libft/src/dlst/ft_dlst_last.c b/libft/src/dlst/ft_dlstlast.c similarity index 96% rename from libft/src/dlst/ft_dlst_last.c rename to libft/src/dlst/ft_dlstlast.c index 9772cc0c..1286c32c 100644 --- a/libft/src/dlst/ft_dlst_last.c +++ b/libft/src/dlst/ft_dlstlast.c @@ -12,7 +12,7 @@ #include "libft.h" -t_dlist *ft_dlst_last(t_dlist *list) +t_dlist *ft_dlstlast(t_dlist *list) { while (list && list->next) list = list->next; diff --git a/libft/src/dlst/ft_dlst_new.c b/libft/src/dlst/ft_dlstnew.c similarity index 95% rename from libft/src/dlst/ft_dlst_new.c rename to libft/src/dlst/ft_dlstnew.c index 8725382f..dfe32bdc 100644 --- a/libft/src/dlst/ft_dlst_new.c +++ b/libft/src/dlst/ft_dlstnew.c @@ -12,7 +12,7 @@ #include "libft.h" -t_dlist *ft_dlst_new(void const *content, size_t content_size) +t_dlist *ft_dlstnew(void const *content, size_t content_size) { t_dlist *new; diff --git a/libft/src/dlst/ft_dlst_size.c b/libft/src/dlst/ft_dlstsize.c similarity index 97% rename from libft/src/dlst/ft_dlst_size.c rename to libft/src/dlst/ft_dlstsize.c index 9777bb11..b536efa2 100644 --- a/libft/src/dlst/ft_dlst_size.c +++ b/libft/src/dlst/ft_dlstsize.c @@ -12,7 +12,7 @@ #include "libft.h" -int ft_dlst_size(t_dlist *list) +int ft_dlstsize(t_dlist *list) { int size; t_dlist *tmp; diff --git a/libft/src/get_next_line/get_next_line.c b/libft/src/get_next_line/get_next_line.c index 3263c520..cc8d1cdf 100644 --- a/libft/src/get_next_line/get_next_line.c +++ b/libft/src/get_next_line/get_next_line.c @@ -13,7 +13,7 @@ #include "libft.h" #define BUFF_SIZE 32 -static char *ft_realloc(char *line, int size) +static char *ft_strrealloc(char *line, int size) { char *str; @@ -41,7 +41,7 @@ static int ft_loop_read(int fd, char **line, char (*save)[]) ft_strcat(*line, buf); return (1); } - if ((*line = ft_realloc(*line, ret)) == NULL) + if ((*line = ft_strrealloc(*line, ret)) == NULL) return (-1); ft_strcat(*line, buf); } diff --git a/libft/src/mem/ft_realloc.c b/libft/src/mem/ft_realloc.c index 33f6c10c..f64a50d0 100644 --- a/libft/src/mem/ft_realloc.c +++ b/libft/src/mem/ft_realloc.c @@ -16,8 +16,10 @@ void *ft_realloc(void *data, int size) { void *new; + ft_printf("realloc befor: '%s'\n", data); new = ft_memalloc(size); - ft_memcpy(new, data, size); + ft_memcpy(new, data, ft_strlen(data)); ft_memdel(&data); + ft_printf("realloc after: '%s'\n", new); return (new); } diff --git a/libft/src/path/ft_path_findfile.c b/libft/src/path/ft_path_findfile.c new file mode 100644 index 00000000..e69de29b