naming convention change for dlst, btree functions
This commit is contained in:
parent
3eef8e3f7b
commit
50f9e3d0d0
21 changed files with 292 additions and 16 deletions
30
libftasm/includes/btree.h
Normal file
30
libftasm/includes/btree.h
Normal file
|
|
@ -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
|
||||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -16,6 +16,7 @@
|
|||
# include "ft_xattr.h"
|
||||
# include "lst.h"
|
||||
# include "dlst.h"
|
||||
# include "btree.h"
|
||||
|
||||
# include <string.h>
|
||||
# include <unistd.h>
|
||||
|
|
@ -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
|
||||
|
|
|
|||
46
libftasm/src/btree/btree_apply_by_level.c
Normal file
46
libftasm/src/btree/btree_apply_by_level.c
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* btree_apply_by_level.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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++;
|
||||
}
|
||||
}
|
||||
23
libftasm/src/btree/btree_apply_infix.c
Normal file
23
libftasm/src/btree/btree_apply_infix.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* btree_create_node.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 ;
|
||||
}
|
||||
22
libftasm/src/btree/btree_apply_prefix.c
Normal file
22
libftasm/src/btree/btree_apply_prefix.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* btree_create_node.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
23
libftasm/src/btree/btree_apply_suffix.c
Normal file
23
libftasm/src/btree/btree_apply_suffix.c
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* btree_create_node.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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 ;
|
||||
}
|
||||
24
libftasm/src/btree/btree_create_node.c
Normal file
24
libftasm/src/btree/btree_create_node.c
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* btree_create_node.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
42
libftasm/src/btree/btree_insert_data.c
Normal file
42
libftasm/src/btree/btree_insert_data.c
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* btree_create_node.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
}
|
||||
21
libftasm/src/btree/btree_level_count.c
Normal file
21
libftasm/src/btree/btree_level_count.c
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* btree_create_node.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
30
libftasm/src/btree/btree_search_item.c
Normal file
30
libftasm/src/btree/btree_search_item.c
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* btree_create_node.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
|
@ -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)
|
||||
{
|
||||
|
|
@ -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)
|
||||
{
|
||||
11
libftasm/src/dlst/ft_dlstdel.c
Normal file
11
libftasm/src/dlst/ft_dlstdel.c
Normal file
|
|
@ -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);
|
||||
}
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
@ -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;
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
@ -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;
|
||||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
|
|
|
|||
0
libftasm/src/path/ft_path_findfile.c
Normal file
0
libftasm/src/path/ft_path_findfile.c
Normal file
Loading…
Reference in a new issue