let's go ! B)

added bfs WIP
This commit is contained in:
Tanguy MAZE 2019-03-27 18:04:14 +01:00
parent 4e87b2e587
commit 7a67b49802
6 changed files with 209 additions and 16 deletions

108
Makefile Normal file
View File

@ -0,0 +1,108 @@
#******************************************************************************#
# #
# ::: :::::::: #
# Makefile :+: :+: :+: #
# +:+ +:+ +:+ #
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# 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

View File

@ -6,11 +6,11 @@
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/23 17:31:19 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 # define LEM_IN_H
# include "libft.h" # include "libft.h"
@ -24,7 +24,7 @@ typedef struct s_node
int ind; int ind;
} t_node; } t_node;
typedef struct s_ind; typedef struct s_ind
{ {
int index; int index;
struct s_ind *next; struct s_ind *next;
@ -33,6 +33,7 @@ typedef struct s_ind;
typedef struct s_lmdata typedef struct s_lmdata
{ {
int nbants; int nbants;
int nb_nodes;
t_list *nodes; t_list *nodes;
t_ind **adj; t_ind **adj;
} t_lmdata; } t_lmdata;
@ -40,4 +41,7 @@ typedef struct s_lmdata
void lm_initdata(t_lmdata *data); void lm_initdata(t_lmdata *data);
int lm_getparams(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 #endif

2
libft

@ -1 +1 @@
Subproject commit 05a6363b5d450a1c7cfc320257ed17e5678a7dfa Subproject commit 51295179f30451f2c2d6cbc8653c5accd9fb8a8a

72
srcs/bfs.c Normal file
View File

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* bfs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View File

@ -1 +0,0 @@
#include "lem_in.h"

View File

@ -1,28 +1,38 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* get_path.c :+: :+: :+: */ /* lst_ind.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */ /* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2019/03/25 16:15:08 by tmaze #+# #+# */ /* Created: 2019/03/27 14:56:55 by tmaze #+# #+# */
/* Updated: 2019/03/26 17:09:03 by tmaze ### ########.fr */ /* Updated: 2019/03/27 17:45:17 by tmaze ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "lem_in.h" #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]; t_ind *tmp;
char visited[nb_nodes];
t_list *queue;
queue = NULL; while (*lst != NULL)
ft_bzero(visited); {
tmp = *lst;
*lst = (*lst)->next;
ft_memdel((void**)&tmp);
}
} }