diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ea32f2c --- /dev/null +++ b/Makefile @@ -0,0 +1,108 @@ +#******************************************************************************# +# # +# ::: :::::::: # +# Makefile :+: :+: :+: # +# +:+ +:+ +:+ # +# By: tmaze +#+ +:+ +#+ # +# +#+#+#+#+#+ +#+ # +# Created: 2019/03/27 16:51:02 by tmaze #+# #+# # +# Updated: 2019/03/27 18:00:05 by tmaze ### ########.fr # +# # +#******************************************************************************# + +NAME = lem_in + +# Make options +MAKEFLAGS += --no-print-directory + +# Output +UNAME := $(shell uname) +ifeq ($(UNAME), Darwin) +ECHO = @echo +endif + +ifeq ($(UNAME), Linux) +ECHO = @echo -e +endif + +# Compilator +CC = gcc +FLAGS = -Wall -Wextra -Werror + +# Folders +LIBDIR = libft +SRCDIR = srcs +OBJDIR = objs +INCDIR = includes libft/includes + +# Source files +SRC = bfs.c \ + lst_ind.c + +OBJ = $(SRC:.c=.o) + +LIB = ft +LIBFILE = libft.a + +# Prefixes +OBJP = $(addprefix $(OBJDIR)/, $(SRC:.c=.o)) +INCP = $(foreach dir, $(INCDIR), -I$(dir)) + +# Default Rule +DRULE = all + +# Main rules +default : + @$(ECHO) -e "$(PUR)===> $(GRE)$(NAME) : $(PUR) START RULE : $(DRULE) <===$(DEF)" + @make $(DRULE) + @$(ECHO) -e "$(PUR)===> $(GRE)$(NAME) : $(PUR) END RULE : $(DRULE) <===$(DEF)" + +all : $(NAME) + +# Compilation rules + +$(OBJDIR)/%.o : $(SRCDIR)/%.c includes/lem_in.h + @mkdir -p $(OBJDIR) + $(CC) $(FLAGS) -c -o $@ $< $(INCP) + +$(LIBDIR)/$(LIBFILE) : + @$(ECHO) "$(YEL)===> $(GRE)$(NAME) : $(YEL) Librairy Compilation <===$(DEF)" + $(MAKE) -C $(LIBDIR) all + +$(NAME) : $(OBJP) $(LIBDIR)/$(LIBFILE) + @$(ECHO) "$(YEL)===> $(GRE)$(NAME) : $(YEL) Binary Compilation <===$(DEF)" + $(CC) $(FLAGS) -o $@ $^ $(INCP) -L$(LIBDIR) -l$(LIB) + +test : $(OBJP) $(OBJT) $(LIBDIR)/$(LIBFILE) + @$(ECHO) "$(YEL)===> $(GRE)$(NAME) : $(YEL) Binary Compilation <===$(DEF)" + $(CC) $(FLAGS) -o $@ $^ $(INCP) -L$(LIBDIR) -l$(LIB) + +# Cleaner rules +clean : + @$(ECHO) "$(RED)===> $(GRE)$(NAME) : $(RED) Delete Object Files <===$(DEF)" + @$(RM) -rf $(OBJDIR) + @$(MAKE) -C $(LIBDIR) clean + +fclean : clean + @$(ECHO) "$(RED)===> $(GRE)$(NAME) : $(RED) Delete Binary File <===$(DEF)" + @$(RM) -f $(NAME1) $(NAME2) + @$(RM) -rf *.dSYM + @$(MAKE) -C $(LIBDIR) fclean + +re : fclean default + +# Phony +.PHONY = default all re clean fclean +# Color +DEF = \033[0m +BLA = \033[30m +BLI = \033[5m +BLU = \033[34m +CYA = \033[36m +GRA = \033[1m +GRE = \033[32m +PUR = \033[35m +RED = \033[31m +SOU = \033[4m +WHI = \033[37m +YEL = \033[33m diff --git a/includes/lem_in.h b/includes/lem_in.h index dbdfc79..be77939 100644 --- a/includes/lem_in.h +++ b/includes/lem_in.h @@ -6,11 +6,11 @@ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2019/03/23 17:31:19 by tmaze #+# #+# */ -/* Updated: 2019/03/26 17:08:38 by tmaze ### ########.fr */ +/* Updated: 2019/03/27 17:44:40 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ -#ifndef LEM_IN_N +#ifndef LEM_IN_H # define LEM_IN_H # include "libft.h" @@ -24,7 +24,7 @@ typedef struct s_node int ind; } t_node; -typedef struct s_ind; +typedef struct s_ind { int index; struct s_ind *next; @@ -33,6 +33,7 @@ typedef struct s_ind; typedef struct s_lmdata { int nbants; + int nb_nodes; t_list *nodes; t_ind **adj; } t_lmdata; @@ -40,4 +41,7 @@ typedef struct s_lmdata void lm_initdata(t_lmdata *data); int lm_getparams(t_lmdata *data); +t_ind *lst_indadd(t_ind **lst, int ind); +void lst_inddel(t_ind **lst); + #endif diff --git a/libft b/libft index 05a6363..5129517 160000 --- a/libft +++ b/libft @@ -1 +1 @@ -Subproject commit 05a6363b5d450a1c7cfc320257ed17e5678a7dfa +Subproject commit 51295179f30451f2c2d6cbc8653c5accd9fb8a8a diff --git a/srcs/bfs.c b/srcs/bfs.c new file mode 100644 index 0000000..b5042e9 --- /dev/null +++ b/srcs/bfs.c @@ -0,0 +1,72 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* bfs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: tmaze +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2019/03/27 14:41:49 by tmaze #+# #+# */ +/* Updated: 2019/03/27 17:06:33 by tmaze ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "lem_in.h" + +static void bfs_addtoqueue(int *queue, int node, int nb_nodes) +{ + int i; + + i = 0; + while (i < nb_nodes && (queue[i] == -1 || queue[i] != node)) + i++; + if (i < nb_nodes && queue[i] == -1) + queue[i] = node; +} + +static void bfs_checkadj(t_lmdata *data, int *queue, int *parent, int i) +{ + t_ind *it; + + it = data->adj[queue[i]]; + while (it != NULL) + { + bfs_addtoqueue(queue, it->index, data->nb_nodes); + parent[it->index] = queue[i]; + it = it->next; + } +} + +static void bfs_init(int *queue, char *visited, int *parent, t_lmdata *data) +{ + ft_bzero(queue, data->nb_nodes * sizeof(int)); + ft_bzero(visited, data->nb_nodes); + ft_memset(parent, -1, data->nb_nodes * sizeof(int)); +} + +t_ind *bfs(t_lmdata *data, int start_ind, int end_ind) +{ + int parent[data->nb_nodes]; + char visited[data->nb_nodes]; + int queue[data->nb_nodes]; + int i; + t_ind *ret; + + bfs_init(queue, visited, parent, data); + bfs_addtoqueue(queue, start_ind, data->nb_nodes); + i = 0; + while (i < data->nb_nodes && queue[i] != -1 && parent[end_ind] == -1 + && (visited[queue[i]] = 1)) + bfs_checkadj(data, queue, parent, i++); + ret = NULL; + i = end_ind; + while (i != -1) + { + if (lst_indadd(&ret, i) == NULL) + { + lst_inddel(&ret); + return (NULL); + } + i = parent[i]; + } + return (ret); +} diff --git a/srcs/ft_parser.c b/srcs/ft_parser.c deleted file mode 100644 index c855bc6..0000000 --- a/srcs/ft_parser.c +++ /dev/null @@ -1 +0,0 @@ -#include "lem_in.h" diff --git a/srcs/get_path.c b/srcs/lst_ind.c similarity index 58% rename from srcs/get_path.c rename to srcs/lst_ind.c index ed7619f..9656efe 100644 --- a/srcs/get_path.c +++ b/srcs/lst_ind.c @@ -1,28 +1,38 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* get_path.c :+: :+: :+: */ +/* lst_ind.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: tmaze +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ -/* Created: 2019/03/25 16:15:08 by tmaze #+# #+# */ -/* Updated: 2019/03/26 17:09:03 by tmaze ### ########.fr */ +/* Created: 2019/03/27 14:56:55 by tmaze #+# #+# */ +/* Updated: 2019/03/27 17:45:17 by tmaze ### ########.fr */ /* */ /* ************************************************************************** */ #include "lem_in.h" -int get_nb_nodes(t_lmdata *data) +t_ind *lst_indadd(t_ind **lst, int ind) { - + t_ind *new; + + if ((new = (t_ind*)ft_memalloc(sizeof(t_ind))) != NULL) + { + new->index = ind; + new->next = *lst; + *lst = new; + } + return (new); } -int get_path(t_lmdata *data, int nb_nodes) +void lst_inddel(t_ind **lst) { - int parent[nb_nodes]; - char visited[nb_nodes]; - t_list *queue; + t_ind *tmp; - queue = NULL; - ft_bzero(visited); + while (*lst != NULL) + { + tmp = *lst; + *lst = (*lst)->next; + ft_memdel((void**)&tmp); + } }