Compare commits
18 Commits
ea0eda89d0
...
dd255666f1
Author | SHA1 | Date | |
---|---|---|---|
|
dd255666f1 | ||
|
4a5c798b25 | ||
|
9b75432c2e | ||
|
d85e0873cb | ||
|
0495ba9e55 | ||
|
eb85b914a4 | ||
|
86b0530bf7 | ||
|
8c4a6aac25 | ||
|
5d094ebcaf | ||
|
fb66233265 | ||
|
3b893147e5 | ||
|
434b629055 | ||
|
08e622a4f0 | ||
|
a2fd180398 | ||
|
ae93873452 | ||
|
c36f501b08 | ||
|
0a9d7b816d | ||
|
1abb3fc566 |
3
.gitmodules
vendored
3
.gitmodules
vendored
@@ -1,3 +0,0 @@
|
||||
[submodule "libft"]
|
||||
path = libft
|
||||
url = https://github.com/tvdu29/libft
|
24
Makefile
24
Makefile
@@ -6,11 +6,11 @@
|
||||
# By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ #
|
||||
# +#+#+#+#+#+ +#+ #
|
||||
# Created: 2019/03/27 16:51:02 by tmaze #+# #+# #
|
||||
# Updated: 2019/04/01 14:49:54 by tmaze ### ########.fr #
|
||||
# Updated: 2019/04/15 17:03:17 by tmaze ### ########.fr #
|
||||
# #
|
||||
#******************************************************************************#
|
||||
|
||||
NAME = lem_in
|
||||
NAME = lem-in
|
||||
|
||||
# Make options
|
||||
MAKEFLAGS += --no-print-directory
|
||||
@@ -36,9 +36,23 @@ OBJDIR = objs
|
||||
INCDIR = includes libft/includes
|
||||
|
||||
# Source files
|
||||
SRC = bfs.c lm_parser.c lm_mem_utils.c lm_graph_utils.c \
|
||||
lst_ind.c \
|
||||
edmunds_karp.c
|
||||
SRC = lm_parser.c \
|
||||
lm_mem_utils.c \
|
||||
lm_check_errors.c \
|
||||
lm_data_utils.c \
|
||||
lm_initdata.c \
|
||||
lm_mem_utils.c \
|
||||
lm_graph_utils.c \
|
||||
lm_parser_error_check.c \
|
||||
lm_utils_parser.c \
|
||||
bfs.c \
|
||||
lst_ind.c \
|
||||
edmunds_karp.c \
|
||||
push_ants.c \
|
||||
push_ants_utils.c \
|
||||
score_utils.c \
|
||||
lem_in.c
|
||||
|
||||
|
||||
OBJ = $(SRC:.c=.o)
|
||||
|
||||
|
@@ -6,15 +6,14 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/23 17:31:19 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/03 15:57:39 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/04/15 17:44:39 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#ifndef LEM_IN_H
|
||||
# define LEM_IN_H
|
||||
|
||||
# include "libft.h"
|
||||
# include <fcntl.h>
|
||||
# include "limits.h"
|
||||
|
||||
typedef struct s_node
|
||||
{
|
||||
@@ -33,11 +32,18 @@ typedef struct s_ind
|
||||
struct s_ind *next;
|
||||
} t_ind;
|
||||
|
||||
typedef struct s_path
|
||||
{
|
||||
int nb_ants;
|
||||
int score;
|
||||
t_ind *nodes;
|
||||
} t_path;
|
||||
|
||||
typedef struct s_lmdata
|
||||
{
|
||||
int nbants;
|
||||
int nb_nodes;
|
||||
t_list *nodes;
|
||||
int nb_paths_max;
|
||||
t_node *nodes_data;
|
||||
t_ind **adj;
|
||||
} t_lmdata;
|
||||
@@ -49,72 +55,96 @@ typedef struct s_bfs
|
||||
int queue;
|
||||
} t_bfs;
|
||||
|
||||
//LEM_IN MT STRUCTS
|
||||
typedef struct s_ants
|
||||
{
|
||||
int nb_ant;
|
||||
int nb_path;
|
||||
int nb_node;
|
||||
char end;
|
||||
struct s_ants *next;
|
||||
} t_ants;
|
||||
|
||||
typedef struct s_syntax
|
||||
{
|
||||
int nb_state;
|
||||
int s_cmd;
|
||||
int s_pos;
|
||||
int error;
|
||||
int s_error;
|
||||
int e_error;
|
||||
int v_error;
|
||||
int l_error;
|
||||
int gr_status;
|
||||
int s_vert;
|
||||
int e_vert;
|
||||
int gr_status;
|
||||
int e_cmd;
|
||||
int e_pos;
|
||||
int v_flag;
|
||||
} t_syntax;
|
||||
|
||||
typedef struct s_adjnode
|
||||
typedef struct s_temp
|
||||
{
|
||||
int dest;
|
||||
struct s_adjnode *next;
|
||||
} t_adjnode;
|
||||
int src_ind;
|
||||
int dest_ind;
|
||||
struct s_temp *next;
|
||||
} t_temp;
|
||||
|
||||
typedef struct s_adjlist
|
||||
typedef struct s_holder
|
||||
{
|
||||
t_adjnode *head;
|
||||
} t_adjlist;
|
||||
int count;
|
||||
t_temp *data;
|
||||
} t_holder;
|
||||
|
||||
typedef struct s_graph
|
||||
typedef struct s_rdata
|
||||
{
|
||||
int vert;
|
||||
t_adjlist *array;
|
||||
} t_graph;
|
||||
|
||||
typedef struct s_neighbour
|
||||
{
|
||||
int vertnum;
|
||||
struct s_neighbor *next;
|
||||
} t_neighbor;
|
||||
|
||||
|
||||
|
||||
//MT-FUNCTIONS
|
||||
int lm_error_exit(int flag);
|
||||
int lm_parser(int fd, t_syntax *synt,
|
||||
t_lmdata *ldata, t_graph *graph);
|
||||
void lm_init_data(t_syntax *synt, t_lmdata *ldata);
|
||||
void lm_add_vertex(t_lmdata *ldata, char *raw, char flag);
|
||||
t_adjnode *lm_new_node(int dest);
|
||||
t_graph *lm_creategraph(int v);
|
||||
void lm_add_edge(t_graph *graph, int src, int dest);
|
||||
void lm_ext_conn(t_graph *graph, t_lmdata *data, char *raw, t_syntax *synt);
|
||||
int lm_find_index(t_lmdata *data, char *str);
|
||||
|
||||
|
||||
|
||||
char *line;
|
||||
struct s_rdata *next;
|
||||
} t_rdata;
|
||||
|
||||
int lm_error_exit(void);
|
||||
int lm_validate(t_syntax *synt, t_lmdata *lmdata);
|
||||
int lm_parser(t_syntax *synt, t_lmdata *ldata
|
||||
, t_holder *holder);
|
||||
int lm_check_room_before(char **tab, t_syntax *synt);
|
||||
void lm_clear_unv(t_holder *holder);
|
||||
int lm_add_vertex(t_lmdata *ldata, char *raw, char flag
|
||||
, t_syntax *synt);
|
||||
int lm_ext_conn(t_holder *holder, t_lmdata *data
|
||||
, char *raw);
|
||||
int lm_find_index(t_lmdata *data, char *str);
|
||||
|
||||
int lm_get_value(char *line);
|
||||
void lm_get_cmd_vert(int count, t_syntax *synt
|
||||
, t_lmdata *ldata, char *line);
|
||||
void lm_get_vert_link(int count, t_syntax *synt
|
||||
, t_lmdata *ldata, t_holder *holder
|
||||
, char *line);
|
||||
int lm_validate_rooms(char *name, char *x, char *y);
|
||||
int lm_adj_parser(t_lmdata *lmdata, t_holder *holder);
|
||||
void lm_init_data(t_syntax *synt, t_holder *holder
|
||||
, t_lmdata *ldata);
|
||||
int lm_verify_cmd(t_syntax *synt, t_holder *holder
|
||||
, t_lmdata *data);
|
||||
void lm_initdata(t_lmdata *data);
|
||||
int lm_getparams(t_lmdata *data);
|
||||
void lm_clean_data(t_lmdata *data);
|
||||
|
||||
|
||||
|
||||
t_ind **lst_indinit(t_lmdata *data);
|
||||
int lst_indadd_link(t_lmdata *data, int n1, int n2);
|
||||
t_ind *lst_indadd(t_ind **lst, int ind);
|
||||
void lst_inddel(t_ind **lst);
|
||||
void tablst_inddel(t_ind **tab);
|
||||
void clean_path(t_path *tab);
|
||||
|
||||
void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind);
|
||||
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind);
|
||||
t_ants *add_ant(t_ants **ants, int nb_ant, int nb_path
|
||||
, t_path *paths);
|
||||
void del_ants(t_ants **ants);
|
||||
t_node *get_node(t_lmdata *data, int index);
|
||||
t_ind *get_node_path(t_ind *lst, int index);
|
||||
|
||||
int get_score(t_lmdata *data, t_path *ret);
|
||||
void bfs(t_lmdata *data, t_bfs *tab, int start_ind
|
||||
, int end_ind);
|
||||
t_path *edmunds_karp(t_lmdata *data, int start_ind, int end_ind);
|
||||
int push_ants(t_lmdata *data, t_path *paths);
|
||||
|
||||
#endif
|
||||
|
12
srcs/bfs.c
12
srcs/bfs.c
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/27 14:41:49 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/03 17:19:07 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/04/14 11:39:52 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -37,19 +37,21 @@ static void bfs_checkadj(t_lmdata *data, t_bfs *tab, int i)
|
||||
{
|
||||
if (tab[it->index].visited == 0 && it->weight == 2)
|
||||
used = 1;
|
||||
if (tab[it->index].visited == 0 && it->weight == 2)
|
||||
if (used)
|
||||
break ;
|
||||
it = it->next;
|
||||
}
|
||||
it = data->adj[tab[i].queue];
|
||||
while (it != NULL)
|
||||
{
|
||||
if (tab[it->index].visited == 0 && ((!used && it->weight > 0)
|
||||
if (tab[it->index].visited == 0 && ((!used && it->weight == 1)
|
||||
|| (used && it->weight == 2)))
|
||||
bfs_addtoqueue(tab, it->index, data->nb_nodes);
|
||||
if (tab[it->index].visited == 0 && ((!used && it->weight > 0)
|
||||
if (tab[it->index].visited == 0 && ((!used && it->weight == 1)
|
||||
|| (used && it->weight == 2)))
|
||||
tab[it->index].parent = tab[i].queue;
|
||||
if (it->weight == 2)
|
||||
break ;
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
@@ -72,11 +74,11 @@ void bfs(t_lmdata *data, t_bfs *tab, int start_ind, int end_ind)
|
||||
{
|
||||
int i;
|
||||
|
||||
ft_printf("New bfs\n");
|
||||
bfs_init(tab, data);
|
||||
bfs_addtoqueue(tab, start_ind, data->nb_nodes);
|
||||
i = 0;
|
||||
while (i < data->nb_nodes && tab[i].queue != -1
|
||||
&& tab[end_ind].parent == -1 && (tab[tab[i].queue].visited = 1))
|
||||
bfs_checkadj(data, tab, i++);
|
||||
bfs_print(tab, data->nb_nodes);
|
||||
}
|
||||
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/28 16:21:19 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/03 17:18:35 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/04/15 17:37:09 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -20,84 +20,122 @@ static void update_weights(t_lmdata *data, t_bfs *tab, int end_ind)
|
||||
i = end_ind;
|
||||
while (i != -1)
|
||||
{
|
||||
it = data->adj[tab[i].parent];
|
||||
while (it && it->index != i)
|
||||
it = it->next;
|
||||
if (it && it->index == i)
|
||||
it->weight--;
|
||||
it = data->adj[i];
|
||||
while (it && it->index != tab[i].parent)
|
||||
it = it->next;
|
||||
if (it && it->index == tab[i].parent)
|
||||
it->weight++;
|
||||
if (tab[i].parent != -1)
|
||||
{
|
||||
it = data->adj[tab[i].parent];
|
||||
while (it && it->index != i)
|
||||
it = it->next;
|
||||
if (it && it->index == i)
|
||||
it->weight--;
|
||||
it = data->adj[i];
|
||||
while (it && it->index != tab[i].parent)
|
||||
it = it->next;
|
||||
if (it && it->index == tab[i].parent)
|
||||
it->weight++;
|
||||
}
|
||||
i = tab[i].parent;
|
||||
}
|
||||
}
|
||||
|
||||
static int reset_weights(t_lmdata *data, int s_ind, int node)
|
||||
{
|
||||
t_ind *it;
|
||||
t_ind *it2;
|
||||
/* static int reset_weights(t_lmdata *data, int node) */
|
||||
/* { */
|
||||
/* t_ind *it; */
|
||||
/* t_ind *it2; */
|
||||
|
||||
it = data->adj[node];
|
||||
while (it && it->weight != 2)
|
||||
it = it->next;
|
||||
if (it && it->weight == 2)
|
||||
it->weight--;
|
||||
it2 = data->adj[it->index];
|
||||
while (it2 && it2->index != node)
|
||||
it2 = it2->next;
|
||||
if (it2 && it2->index == node)
|
||||
it2->weight++;
|
||||
return ((node == s_ind) ? -1 : it->index);
|
||||
}
|
||||
/* it = data->adj[node]; */
|
||||
/* it2 = NULL; */
|
||||
/* while (it && it->weight != 2) */
|
||||
/* it = it->next; */
|
||||
/* if (it && it->weight == 2) */
|
||||
/* it->weight = 1; */
|
||||
/* if (it && it->index) */
|
||||
/* it2 = data->adj[it->index]; */
|
||||
/* while (it2 && it2->index != node) */
|
||||
/* it2 = it2->next; */
|
||||
/* if (it2 && it2->index == node) */
|
||||
/* it2->weight = 1; */
|
||||
/* return ((it != NULL) ? it->index : -1); */
|
||||
/* } */
|
||||
|
||||
static t_ind **clean_ret(t_ind **ret)
|
||||
static t_path *clean_ret(t_path *ret)
|
||||
{
|
||||
tablst_inddel(ret);
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (ret[i].nodes)
|
||||
lst_inddel(&(ret[i++].nodes));
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
t_ind **resolve_path(t_lmdata *data, int s_ind, int e_ind
|
||||
, int nb_path)
|
||||
static t_path *resolve_path(t_lmdata *data, int s_ind, int e_ind, t_bfs *tab)
|
||||
{
|
||||
t_ind **ret;
|
||||
t_path *ret;
|
||||
int i;
|
||||
int j;
|
||||
t_ind *it;
|
||||
|
||||
ret = NULL;
|
||||
i = 0;
|
||||
if (nb_path > 0 && (ret = (t_ind**)ft_memalloc(sizeof(t_ind*)
|
||||
* (nb_path + 1))) != NULL)
|
||||
ret = NULL;
|
||||
if ((ret = (t_path*)ft_memalloc(sizeof(t_path) * (data->nb_paths_max))) != NULL)
|
||||
{
|
||||
while (i < nb_path && (j = e_ind) == e_ind)
|
||||
it = data->adj[e_ind];
|
||||
while (it && i < data->nb_paths_max && (j = e_ind) == e_ind)
|
||||
{
|
||||
while (j != s_ind)
|
||||
if (it->weight == 2 && (j = it->index))
|
||||
{
|
||||
if (lst_indadd(&(ret[i]), j) == NULL)
|
||||
if (lst_indadd(&(ret[i].nodes), e_ind) == NULL)
|
||||
return (clean_ret(ret));
|
||||
j = reset_weights(data, s_ind, j);
|
||||
while (j != s_ind)
|
||||
{
|
||||
if (lst_indadd(&(ret[i].nodes), j) == NULL)
|
||||
return (clean_ret(ret));
|
||||
j = tab[j].parent;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
if (j != -1 && lst_indadd(&(ret[i]), j) == NULL)
|
||||
return (clean_ret(ret));
|
||||
i++;
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
return (ret);
|
||||
}
|
||||
|
||||
t_ind **edmunds_karp(t_lmdata *data, int start_ind, int end_ind)
|
||||
t_path *edmunds_karp(t_lmdata *data, int start_ind, int end_ind)
|
||||
{
|
||||
t_bfs tab[data->nb_nodes];
|
||||
int nb_path;
|
||||
t_path *ret[2];
|
||||
int score[2];
|
||||
|
||||
nb_path = 0;
|
||||
bfs(data, tab, start_ind, end_ind);
|
||||
while (tab[end_ind].parent != -1)
|
||||
ret[0] = NULL;
|
||||
ret[1] = NULL;
|
||||
score[0] = 0;
|
||||
score[1] = 0;
|
||||
ft_printf("nb paths max: %d\n", data->nb_paths_max);
|
||||
if (data && data->adj)
|
||||
{
|
||||
nb_path++;
|
||||
update_weights(data, tab, end_ind);
|
||||
bfs(data, tab, start_ind, end_ind);
|
||||
while (tab[end_ind].parent != -1)
|
||||
{
|
||||
update_weights(data, tab, end_ind);
|
||||
if (tab[end_ind].parent != -1)
|
||||
ret[1] = resolve_path(data, start_ind, end_ind, tab);
|
||||
ft_printf("ret 0: %p\n", ret[0]);
|
||||
ft_printf("score 0: %d\nscore 1: %d\n", score[0], get_score(data, ret[1]));
|
||||
score[1] = get_score(data, ret[1]);
|
||||
if (ret[0] == NULL || score[0] > score[1])
|
||||
{
|
||||
score[0] = score[1];
|
||||
if (ret[0] != NULL)
|
||||
clean_ret(ret[0]);
|
||||
ret[0] = ret[1];
|
||||
bfs(data, tab, start_ind, end_ind);
|
||||
}
|
||||
else if (score[0] <= score[1])
|
||||
{
|
||||
clean_ret(ret[1]);
|
||||
break;
|
||||
}
|
||||
}
|
||||
return (ret[0]);
|
||||
}
|
||||
return (resolve_path(data, start_ind, end_ind, nb_path));
|
||||
return (NULL);
|
||||
}
|
||||
|
104
srcs/lem_in.c
104
srcs/lem_in.c
@@ -6,43 +6,87 @@
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/25 06:31:05 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/03/25 06:31:09 by mndhlovu ### ########.fr */
|
||||
/* Updated: 2019/04/15 17:55:43 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
int lm_error_exit(int flag)
|
||||
static t_node *get_node_role(t_lmdata *data, char role)
|
||||
{
|
||||
if (flag == 0)
|
||||
ft_printf("Invalid Map\n");
|
||||
if (flag == 1)
|
||||
ft_printf("Usage ./lem_in < map-file\n");
|
||||
if (flag == 2)
|
||||
ft_printf("Error opening the Map file\n");
|
||||
return (1);
|
||||
t_node *it;
|
||||
|
||||
it = data->nodes_data;
|
||||
while (it)
|
||||
{
|
||||
if (it->role == role)
|
||||
return (it);
|
||||
it = it->next;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
static void get_nb_paths_max(t_lmdata *data, int start, int end)
|
||||
{
|
||||
t_syntax synt;
|
||||
t_graph graph;
|
||||
t_lmdata ldata;
|
||||
int fd;
|
||||
int i;
|
||||
int j;
|
||||
t_ind *it;
|
||||
|
||||
lm_init_data(&synt, &ldata);
|
||||
if (ac > 1)
|
||||
{
|
||||
if ((fd = open (av[1], O_RDONLY)) < -1)
|
||||
return (lm_error_exit(2));
|
||||
if (lm_parser(fd, &synt, &ldata, &graph))
|
||||
{
|
||||
//the parser was successful in extracting data from the map
|
||||
//here goes the function for making the adjacency list
|
||||
ft_printf("number of ants %d\n", ldata.nbants);
|
||||
ft_printf("start flags status %d, location %d \n", synt.s_cmd, synt.s_pos);
|
||||
ft_printf("end flag status %d, location %d \n", synt.e_cmd, synt.e_pos);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
i = 0;
|
||||
j = 0;
|
||||
it = data->adj[start];
|
||||
while (it && ++i)
|
||||
it = it->next;
|
||||
it = data->adj[end];
|
||||
while (it && ++j)
|
||||
it= it->next;
|
||||
data->nb_paths_max = (i > j) ? j : i;
|
||||
}
|
||||
|
||||
static int lem_in(t_syntax *synt, t_holder *holder,
|
||||
t_lmdata *lmdata, t_path **ret)
|
||||
{
|
||||
if (!(lm_parser(synt, lmdata, holder)))
|
||||
return (0);
|
||||
if (!(lm_verify_cmd(synt, holder, lmdata)))
|
||||
return (0);
|
||||
if (!lst_indinit(lmdata))
|
||||
return (0);
|
||||
if (!(lm_adj_parser(lmdata, holder)))
|
||||
return (0);
|
||||
lm_clear_unv(holder);
|
||||
get_nb_paths_max(lmdata, get_node_role(lmdata, 's')->ind
|
||||
, get_node_role(lmdata, 'e')->ind);
|
||||
if ((*ret = edmunds_karp(lmdata, get_node_role(lmdata, 's')->ind
|
||||
, get_node_role(lmdata, 'e')->ind)) == NULL)
|
||||
return (0);
|
||||
if (!push_ants(lmdata, *ret))
|
||||
return (0);
|
||||
clean_path(*ret);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int lm_error_exit(void)
|
||||
{
|
||||
ft_putendl_fd("ERROR", 2);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
t_syntax synt;
|
||||
t_lmdata ldata;
|
||||
t_holder holder;
|
||||
t_path *ret;
|
||||
|
||||
(void)av;
|
||||
if (ac == 1)
|
||||
{
|
||||
lm_init_data(&synt, &holder, &ldata);
|
||||
if (!(lem_in(&synt, &holder, &ldata, &ret)))
|
||||
return (lm_error_exit());
|
||||
lm_clean_data(&ldata);
|
||||
return (0);
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
53
srcs/lm_check_errors.c
Normal file
53
srcs/lm_check_errors.c
Normal file
@@ -0,0 +1,53 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_check_errors.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/02 08:39:09 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/04/11 12:48:13 by mndhlovu ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
int lm_check_room_before(char **tab, t_syntax *synt)
|
||||
{
|
||||
if (tab[0] != NULL && tab[1] != NULL && tab[2] != NULL)
|
||||
{
|
||||
if (!lm_validate_rooms(tab[0], tab[1], tab[2]))
|
||||
{
|
||||
synt->v_error = 1;
|
||||
return (0);
|
||||
}
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
void lm_clear_unv(t_holder *holder)
|
||||
{
|
||||
t_temp *data;
|
||||
t_temp *flush;
|
||||
|
||||
data = holder->data;
|
||||
while (data)
|
||||
{
|
||||
flush = data;
|
||||
data = data->next;
|
||||
free(flush);
|
||||
flush = NULL;
|
||||
}
|
||||
holder->data = NULL;
|
||||
}
|
||||
|
||||
int lm_verify_cmd(t_syntax *synt, t_holder *holder, t_lmdata *data)
|
||||
{
|
||||
if (synt->s_cmd && synt->e_cmd && !synt->v_error
|
||||
&& !synt->l_error && !synt->e_error && !synt->s_error
|
||||
&& synt->nb_state && (holder->count > 0)
|
||||
&& (data->nb_nodes > 0))
|
||||
return (1);
|
||||
lm_clear_unv(holder);
|
||||
return (0);
|
||||
}
|
58
srcs/lm_data_utils.c
Normal file
58
srcs/lm_data_utils.c
Normal file
@@ -0,0 +1,58 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_data_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/05 06:35:40 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/04/11 11:32:32 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
t_rdata *lm_new_raw_line(char *raw)
|
||||
{
|
||||
t_rdata *line;
|
||||
|
||||
if (!(line = (t_rdata *)malloc(sizeof(t_rdata))))
|
||||
return (NULL);
|
||||
line->line = ft_strdup(raw);
|
||||
line->next = NULL;
|
||||
return (line);
|
||||
}
|
||||
|
||||
void lm_append_line(t_rdata **line, char *raw)
|
||||
{
|
||||
if (*line)
|
||||
{
|
||||
if((*line)->next)
|
||||
lm_append_line(&(*line)->next, raw);
|
||||
else
|
||||
(*line)->next = lm_new_raw_line(raw);
|
||||
}
|
||||
else
|
||||
*line = lm_new_raw_line(raw);
|
||||
}
|
||||
|
||||
void lm_clean_data(t_lmdata *data)
|
||||
{
|
||||
t_node *tmp;
|
||||
int i;
|
||||
|
||||
while (data->nodes_data)
|
||||
{
|
||||
tmp = data->nodes_data;
|
||||
data->nodes_data = data->nodes_data->next;
|
||||
ft_strdel(&(tmp->name));
|
||||
ft_memdel((void**)&tmp);
|
||||
}
|
||||
i = 0;
|
||||
while (i < data->nb_nodes)
|
||||
{
|
||||
lst_inddel(&(data->adj[i]));
|
||||
i++;
|
||||
}
|
||||
ft_memdel((void**)&(data->adj));
|
||||
}
|
@@ -6,63 +6,45 @@
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/29 07:17:06 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/03/29 07:17:26 by mndhlovu ### ########.fr */
|
||||
/* Updated: 2019/04/11 20:34:58 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
t_adjnode *lm_new_node(int dest)
|
||||
static int is_link_in(t_lmdata *data, int src, int dest)
|
||||
{
|
||||
t_adjnode *new;
|
||||
|
||||
if (!(new = (t_adjnode *)malloc(sizeof(t_adjnode))))
|
||||
return (NULL);
|
||||
new->dest = dest;
|
||||
new->next = NULL;
|
||||
return (new);
|
||||
t_ind *it;
|
||||
if (src < data->nb_nodes)
|
||||
{
|
||||
it = data->adj[src];
|
||||
while (it)
|
||||
{
|
||||
if (it->index == dest)
|
||||
return (1);
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
t_graph *lm_creategraph(int v)
|
||||
int lm_adj_parser(t_lmdata *lmdata, t_holder *holder)
|
||||
{
|
||||
t_graph *new_graph;
|
||||
int index;
|
||||
t_temp *data;
|
||||
|
||||
index = -1;
|
||||
if (!(new_graph = (t_graph *)malloc(sizeof(t_graph))))
|
||||
return (NULL);
|
||||
new_graph->vert = v;
|
||||
if (!(new_graph->array = (t_adjlist *)malloc(sizeof(t_adjlist) * v)))
|
||||
return (NULL);
|
||||
while (++index < v)
|
||||
new_graph->array[index].head = NULL;
|
||||
return (new_graph);
|
||||
if (holder != NULL)
|
||||
{
|
||||
data = holder->data;
|
||||
if (data != NULL)
|
||||
{
|
||||
while (data)
|
||||
{
|
||||
if (!is_link_in(lmdata, data->src_ind, data->dest_ind))
|
||||
lst_indadd_link(lmdata, data->src_ind, data->dest_ind);
|
||||
data = data->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
void lm_ext_conn(t_graph *graph, t_lmdata *data,
|
||||
char *raw, t_syntax *synt)
|
||||
{
|
||||
char **tab;
|
||||
|
||||
if (synt->gr_status == 5)
|
||||
{
|
||||
graph = lm_creategraph(data->nb_nodes);
|
||||
synt->gr_status = 1;
|
||||
}
|
||||
tab = ft_strsplit(raw, '-');
|
||||
if (tab != NULL)
|
||||
lm_add_edge(graph, lm_find_index(data, tab[0]), lm_find_index(data, tab[1]));
|
||||
}
|
||||
|
||||
//function to add the edges and create a proper adjacency list
|
||||
void lm_add_edge(t_graph *graph, int src, int dest)
|
||||
{
|
||||
t_adjnode *newnode;
|
||||
|
||||
newnode = lm_new_node(dest);
|
||||
newnode->next = graph->array[src].head;
|
||||
graph->array[src].head = newnode;
|
||||
newnode = lm_new_node(src);
|
||||
newnode->next = graph->array[dest].head;
|
||||
graph->array[dest].head = newnode;
|
||||
}
|
@@ -6,7 +6,7 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/23 17:43:34 by tmaze #+# #+# */
|
||||
/* Updated: 2019/03/23 17:45:09 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/04/08 18:26:13 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@@ -15,6 +15,6 @@
|
||||
void lm_initdata(t_lmdata *data)
|
||||
{
|
||||
data->nbants = 0;
|
||||
data->nodes = NULL;
|
||||
data->nodes_data = NULL;
|
||||
data->adj = NULL;
|
||||
}
|
||||
|
@@ -6,72 +6,121 @@
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/25 06:31:37 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/03/25 06:31:45 by mndhlovu ### ########.fr */
|
||||
/* Updated: 2019/04/11 11:39:06 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
void lm_init_data(t_syntax *synt, t_lmdata *ldata)
|
||||
void lm_init_data(t_syntax *synt, t_holder *holder, t_lmdata *ldata)
|
||||
{
|
||||
synt->nb_state = 0;
|
||||
synt->s_cmd = 0;
|
||||
synt->s_pos = 0;
|
||||
synt->e_cmd = 0;
|
||||
synt->e_pos = 0;
|
||||
synt->gr_status = 0;
|
||||
synt->e_vert = 0;
|
||||
synt->s_error = 0;
|
||||
synt->e_error = 0;
|
||||
synt->v_error = 0;
|
||||
synt->l_error = 0;
|
||||
synt->s_vert = 0;
|
||||
synt->e_vert = 0;
|
||||
synt->gr_status = 0;
|
||||
synt->s_pos = 0;
|
||||
synt->e_pos = 0;
|
||||
synt->v_flag = 0;
|
||||
holder->count = 0;
|
||||
holder->data = NULL;
|
||||
ldata->nbants = 0;
|
||||
ldata->nb_nodes = 0;
|
||||
ldata->nodes_data = NULL;
|
||||
ldata->nodes = NULL;
|
||||
ldata->adj = NULL;
|
||||
ldata->adj = NULL;
|
||||
}
|
||||
|
||||
void lm_add_vertex(t_lmdata *ldata, char *raw, char flag)
|
||||
static void lm_add_vertex_sub(t_lmdata *ldata, t_node *new)
|
||||
{
|
||||
t_node *tmp;
|
||||
|
||||
if (ldata->nodes_data == NULL)
|
||||
ldata->nodes_data = new;
|
||||
else
|
||||
{
|
||||
tmp = ldata->nodes_data;
|
||||
while (tmp->next)
|
||||
tmp = tmp->next;
|
||||
tmp->next = new;
|
||||
}
|
||||
(ldata->nb_nodes)++;
|
||||
}
|
||||
|
||||
int lm_add_vertex(t_lmdata *ldata, char *raw, char flag,
|
||||
t_syntax *synt)
|
||||
{
|
||||
char **tab;
|
||||
t_node *room;
|
||||
t_node *tmp;
|
||||
t_node *new;
|
||||
|
||||
if (!(room = (t_node *)malloc(sizeof(t_node))))
|
||||
return ;
|
||||
if (!(new = (t_node *)ft_memalloc(sizeof(t_node))))
|
||||
return (0);
|
||||
tab = ft_strsplit(raw, ' ');
|
||||
if (tab != NULL)
|
||||
{
|
||||
room->name = tab[0];
|
||||
room->x = ft_atoi(tab[1]);
|
||||
room->y = ft_atoi(tab[2]);
|
||||
room->role = flag;
|
||||
room->ind = ldata->nb_nodes;
|
||||
room->next = NULL;
|
||||
if (ldata->nodes_data == NULL)
|
||||
ldata->nodes_data = room;
|
||||
else
|
||||
if (lm_check_room_before(tab, synt))
|
||||
{
|
||||
tmp = ldata->nodes_data;
|
||||
while (tmp->next)
|
||||
tmp = tmp->next;
|
||||
tmp->next = room;
|
||||
if ((new->name = ft_strdup(tab[0])) == NULL)
|
||||
ft_del_words_tables(&tab);
|
||||
if (new->name == NULL)
|
||||
return (0);
|
||||
new->x = ft_atoi(tab[1]);
|
||||
new->y = ft_atoi(tab[2]);
|
||||
new->role = flag;
|
||||
new->ind = ldata->nb_nodes;
|
||||
new->next = NULL;
|
||||
lm_add_vertex_sub(ldata, new);
|
||||
ft_del_words_tables(&tab);
|
||||
return (1);
|
||||
}
|
||||
(ldata->nb_nodes)++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int lm_find_index(t_lmdata *data, char *str)
|
||||
int lm_find_index(t_lmdata *data, char *str)
|
||||
{
|
||||
t_node *nodes;
|
||||
t_node *nodes;
|
||||
|
||||
nodes = data->nodes_data;
|
||||
if (nodes != NULL)
|
||||
while (nodes)
|
||||
{
|
||||
while (nodes->next)
|
||||
{
|
||||
if (ft_strcmp(nodes->name, str))
|
||||
return (nodes->ind);
|
||||
nodes = nodes->next;
|
||||
}
|
||||
if (ft_strcmp(nodes->name, str) == 0)
|
||||
return (nodes->ind);
|
||||
nodes = nodes->next;
|
||||
}
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
|
||||
int lm_ext_conn(t_holder *holder, t_lmdata *data, char *raw)
|
||||
{
|
||||
char **tab;
|
||||
t_temp *temp;
|
||||
t_temp *new;
|
||||
|
||||
if (!(new = (t_temp *)ft_memalloc(sizeof(t_temp))))
|
||||
return (0);
|
||||
tab = ft_strsplit(raw, '-');
|
||||
if (tab != NULL)
|
||||
{
|
||||
new->src_ind = lm_find_index(data, tab[0]);
|
||||
new->dest_ind = lm_find_index(data, tab[1]);
|
||||
new->next = NULL;
|
||||
if (holder->data == NULL)
|
||||
holder->data = new;
|
||||
else
|
||||
{
|
||||
temp = holder->data;
|
||||
while (temp->next)
|
||||
temp = temp->next;
|
||||
temp->next = new;
|
||||
}
|
||||
(holder->count)++;
|
||||
ft_del_words_tables(&tab);
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
125
srcs/lm_parser.c
125
srcs/lm_parser.c
@@ -6,121 +6,94 @@
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/25 06:30:51 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/03/25 06:30:57 by mndhlovu ### ########.fr */
|
||||
/* Updated: 2019/04/11 09:51:29 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
static void lm_locate_cmd(int count, char *raw, t_syntax *synt)
|
||||
void lm_locate_cd(int index, t_syntax *synt, char *line)
|
||||
{
|
||||
int s_state;
|
||||
int e_state;
|
||||
char *tmp;
|
||||
int index;
|
||||
|
||||
index = 0;
|
||||
s_state = synt->s_cmd;
|
||||
e_state = synt->e_cmd;
|
||||
tmp = ft_strchr(raw, '#');
|
||||
if ((!s_state || !e_state) && (tmp != NULL && raw[index + 1] == '#'))
|
||||
tmp = ft_strchr(line, '#');
|
||||
if ((!synt->s_cmd || !synt->e_cmd) && (tmp != NULL && line[1] == '#'))
|
||||
{
|
||||
if (!s_state)
|
||||
if (!synt->s_cmd)
|
||||
{
|
||||
if (ft_strcmp(tmp, "##start"))
|
||||
if (ft_strcmp(tmp, "##start") == 0)
|
||||
{
|
||||
synt->s_cmd = 1;
|
||||
synt->e_pos = count;
|
||||
synt->s_pos = index;
|
||||
}
|
||||
}
|
||||
if (!e_state)
|
||||
if (!synt->e_cmd)
|
||||
{
|
||||
if (ft_strcmp(tmp, "##end"))
|
||||
if (ft_strcmp(tmp, "##end") == 0)
|
||||
{
|
||||
synt->e_cmd = 1;
|
||||
synt->s_pos = count;
|
||||
synt->e_pos = index;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void lm_get_vertices(int count, char *raw, t_syntax *synt,
|
||||
t_lmdata *data, t_graph *graph)
|
||||
int lm_get_ant_(int counter, t_lmdata *ldata, t_syntax *synt, char *line)
|
||||
{
|
||||
int index;
|
||||
char *tmp;
|
||||
char *tmp2;
|
||||
int value;
|
||||
|
||||
index = 0;
|
||||
value = 0;
|
||||
graph = NULL;
|
||||
if (synt->s_cmd > 0 && (synt->e_pos == count - 1))
|
||||
{
|
||||
synt->s_vert = count;
|
||||
lm_add_vertex(data, raw, 's');
|
||||
}
|
||||
if (synt->e_cmd > 0 && (synt->s_pos == count - 1))
|
||||
{
|
||||
synt->e_vert = count;
|
||||
lm_add_vertex(data, raw, 'e');
|
||||
}
|
||||
if (count > 0 && (count != synt->s_vert && count != synt->e_vert)
|
||||
&& (count != synt->s_pos && count != synt->e_pos))
|
||||
{
|
||||
tmp = ft_strchr(raw, '-');
|
||||
tmp2 = ft_strchr(raw, '#');
|
||||
if (tmp == NULL && tmp2 == NULL)
|
||||
lm_add_vertex(data, raw, 'v');
|
||||
else if (tmp2 == NULL && tmp != NULL)
|
||||
{
|
||||
lm_ext_conn(graph, data, raw, synt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void lm_get_ant_num(int count, char *raw, t_lmdata *ldata, t_syntax *synt)
|
||||
{
|
||||
int index;
|
||||
int value;
|
||||
|
||||
index = 0;
|
||||
if (count == 0)
|
||||
if (counter == 0)
|
||||
{
|
||||
if (!ft_isdigit(raw[index + 1]))
|
||||
value = lm_get_value(line);
|
||||
if (value != -1)
|
||||
{
|
||||
value = raw[index] - '0';
|
||||
ldata->nbants = (value > 0) ? value : 0;
|
||||
synt->nb_state = 1;
|
||||
}
|
||||
if (ft_isdigit(raw[index + 1]))
|
||||
{
|
||||
ldata->nbants = (ft_atoi(raw) > 0) ? ft_atoi(raw) : 0;
|
||||
ldata->nbants = value;
|
||||
synt->nb_state = 1;
|
||||
return (1);
|
||||
}
|
||||
free(line);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int lm_get_vertices(int count, t_syntax *synt,
|
||||
t_lmdata *data, t_holder *holder, char *line)
|
||||
{
|
||||
lm_get_cmd_vert(count, synt, data, line);
|
||||
if (!synt->s_error && !synt->e_error)
|
||||
{
|
||||
lm_get_vert_link(count, synt, data, holder, line);
|
||||
if (!synt->v_error && !synt->l_error)
|
||||
return (1);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int lm_parser(int fd, t_syntax *synt, t_lmdata *ldata,
|
||||
t_graph *graph)
|
||||
int lm_parser(t_syntax *synt, t_lmdata *ldata,
|
||||
t_holder *holder)
|
||||
{
|
||||
char *raw;
|
||||
int ret;
|
||||
int index;
|
||||
|
||||
index = 0;
|
||||
ft_printf("debug flags %d \n", index);
|
||||
while ((ret = get_next_line(fd, &raw)))
|
||||
while (ft_getline(&raw) > 0)
|
||||
{
|
||||
if (ret == -1)
|
||||
ft_printf("%s\n", raw);
|
||||
if (!(lm_get_ant_(index, ldata, synt, raw)) && index == 0)
|
||||
{
|
||||
ft_strdel(&raw);
|
||||
return (0);
|
||||
lm_get_ant_num(index, raw, ldata, synt);
|
||||
lm_locate_cmd(index, raw, synt);
|
||||
lm_get_vertices(index, raw, synt, ldata, graph);
|
||||
}
|
||||
lm_locate_cd(index, synt, raw);
|
||||
if (!(lm_get_vertices(index, synt, ldata, holder, raw)))
|
||||
{
|
||||
ft_strdel(&raw);
|
||||
return (0);
|
||||
}
|
||||
ft_strdel(&raw);
|
||||
index++;
|
||||
}
|
||||
if (index > 1)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
ft_strdel(&raw);
|
||||
ft_putchar('\n');
|
||||
return (1);
|
||||
}
|
||||
|
75
srcs/lm_parser_error_check.c
Normal file
75
srcs/lm_parser_error_check.c
Normal file
@@ -0,0 +1,75 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_parser_error_check.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/08 06:42:37 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/04/08 17:04:05 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
int lm_chk_format_nbr(char *raw)
|
||||
{
|
||||
while (*raw)
|
||||
{
|
||||
if (*raw == '-' || *raw == '+')
|
||||
raw++;
|
||||
if (!ft_isdigit(*raw))
|
||||
return (-1);
|
||||
raw++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int lm_check_max(char *raw)
|
||||
{
|
||||
if (ft_atoi(raw) < INT_MIN || ft_atoi(raw) > INT_MAX)
|
||||
return (-1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int lm_validate_name(char *name)
|
||||
{
|
||||
int index;
|
||||
|
||||
index = 0;
|
||||
if (name[0] == 'L')
|
||||
return (-1);
|
||||
while (name[index])
|
||||
{
|
||||
if (name[index] == '-')
|
||||
return (-1);
|
||||
index++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
int lm_validate_rooms(char *name, char *x, char *y)
|
||||
{
|
||||
if (lm_validate_name(name) == -1)
|
||||
return (-1);
|
||||
if (lm_chk_format_nbr(x) == -1 ||
|
||||
lm_check_max(x) == -1 || (ft_atoi(x) > 0 &&
|
||||
(ft_atoi(x) == 0)) ||
|
||||
ft_strlen(x) > 19)
|
||||
return (0);
|
||||
if (lm_chk_format_nbr(y) == -1 ||
|
||||
lm_check_max(y) == -1 || (ft_atoi(y) > 0 &&
|
||||
(ft_atoi(y) == 0)) ||
|
||||
ft_strlen(y) > 19)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
int lm_error_nbr(char *raw)
|
||||
{
|
||||
if (lm_chk_format_nbr(raw) == -1 || lm_check_max(raw) == -1 ||
|
||||
(ft_atoi(raw) > 0 && (ft_atoi(raw) == 0)) ||
|
||||
ft_strlen(raw) > 19)
|
||||
return (-1);
|
||||
return (0);
|
||||
}
|
90
srcs/lm_utils_parser.c
Normal file
90
srcs/lm_utils_parser.c
Normal file
@@ -0,0 +1,90 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* lm_utils_parser.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: mndhlovu <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/04 09:24:45 by mndhlovu #+# #+# */
|
||||
/* Updated: 2019/04/08 16:51:51 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
int lm_get_value(char *line)
|
||||
{
|
||||
int index;
|
||||
|
||||
if (line != NULL)
|
||||
{
|
||||
index = ft_atoi(line);
|
||||
if (index > INT_MIN && index < INT_MAX)
|
||||
{
|
||||
if (index > 0)
|
||||
return (index);
|
||||
else
|
||||
{
|
||||
return (-1);
|
||||
}
|
||||
}
|
||||
}
|
||||
return (-1);
|
||||
}
|
||||
|
||||
void lm_get_cmd_vert(int count, t_syntax *synt
|
||||
, t_lmdata *ldata, char *line)
|
||||
{
|
||||
char *tmp;
|
||||
|
||||
if (synt->s_pos == count - 1)
|
||||
{
|
||||
tmp = ft_strchr(line, '#');
|
||||
if (tmp == NULL && count != synt->e_vert)
|
||||
{
|
||||
synt->s_vert = count;
|
||||
if (!(lm_add_vertex(ldata, line, 's', synt)))
|
||||
synt->e_error = 1;
|
||||
}
|
||||
}
|
||||
if (synt->e_pos == count - 1)
|
||||
{
|
||||
tmp = ft_strchr(line, '#');
|
||||
if (tmp == NULL && count != synt->s_vert)
|
||||
{
|
||||
synt->e_vert = count;
|
||||
if (!(lm_add_vertex(ldata, line, 'e', synt)))
|
||||
synt->s_error = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void lm_get_vert_link(int count, t_syntax *synt, t_lmdata *ldata
|
||||
, t_holder *holder, char *line)
|
||||
{
|
||||
char *link_sign;
|
||||
char *hash;
|
||||
|
||||
|
||||
if (count > 0 && (count != synt->s_vert && count != synt->e_vert)
|
||||
&& (count != synt->s_pos && count != synt->e_pos))
|
||||
{
|
||||
link_sign = ft_strchr(line, '-');
|
||||
hash = ft_strchr(line, '#');
|
||||
if (link_sign == NULL && hash == NULL)
|
||||
{
|
||||
if (!(lm_add_vertex(ldata, line, 'v', synt)))
|
||||
{
|
||||
synt->v_error = 1;
|
||||
}
|
||||
}
|
||||
if (link_sign != NULL && hash == NULL)
|
||||
{
|
||||
if (!(lm_ext_conn(holder, ldata, line)))
|
||||
{
|
||||
synt->l_error = 1;
|
||||
return ;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@@ -6,12 +6,24 @@
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/27 14:56:55 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/03 15:57:26 by tmaze ### ########.fr */
|
||||
/* Updated: 2019/04/15 17:45:54 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
t_ind **lst_indinit(t_lmdata *data)
|
||||
{
|
||||
data->adj = (t_ind**)ft_memalloc(sizeof(t_ind*) * data->nb_nodes);
|
||||
return (data->adj);
|
||||
}
|
||||
|
||||
int lst_indadd_link(t_lmdata *data, int n1, int n2)
|
||||
{
|
||||
return (lst_indadd(&(data->adj[n1]), n2) && lst_indadd(&(data->adj[n2])
|
||||
, n1));
|
||||
}
|
||||
|
||||
t_ind *lst_indadd(t_ind **lst, int ind)
|
||||
{
|
||||
t_ind *new;
|
||||
@@ -38,12 +50,12 @@ void lst_inddel(t_ind **lst)
|
||||
}
|
||||
}
|
||||
|
||||
void tablst_inddel(t_ind **tab)
|
||||
void clean_path(t_path *tab)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (tab[i])
|
||||
lst_inddel(&(tab[i++]));
|
||||
while (tab[i].nodes)
|
||||
lst_inddel(&(tab[i++].nodes));
|
||||
ft_memdel((void**)&tab);
|
||||
}
|
||||
|
96
srcs/push_ants.c
Normal file
96
srcs/push_ants.c
Normal file
@@ -0,0 +1,96 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* push_ants.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/06 11:37:56 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/15 17:48:01 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
static void clean_ants(t_ants **ants)
|
||||
{
|
||||
t_ants *it;
|
||||
t_ants *tmp;
|
||||
|
||||
while (*ants && (*ants)->end)
|
||||
{
|
||||
tmp = *ants;
|
||||
*ants = (*ants)->next;
|
||||
ft_memdel((void**)&tmp);
|
||||
}
|
||||
it = *ants;
|
||||
while (it && it->next)
|
||||
{
|
||||
if (it->next->end)
|
||||
{
|
||||
tmp = it->next;
|
||||
it->next = tmp->next;
|
||||
ft_memdel((void**)&tmp);
|
||||
}
|
||||
else
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
|
||||
static void print_ants(t_lmdata *data, t_ants **ants, t_path *paths)
|
||||
{
|
||||
t_ants *it;
|
||||
|
||||
it = *ants;
|
||||
while (it)
|
||||
{
|
||||
ft_printf("L%d-%s", it->nb_ant, get_node(data, it->nb_node)->name);
|
||||
if (get_node_path(paths[it->nb_path].nodes, it->nb_node)
|
||||
&& get_node_path(paths[it->nb_path].nodes, it->nb_node)->next)
|
||||
it->nb_node = get_node_path(paths[it->nb_path].nodes
|
||||
, it->nb_node)->next->index;
|
||||
else
|
||||
it->end = 1;
|
||||
if (it->next)
|
||||
ft_putchar(' ');
|
||||
else
|
||||
ft_putchar('\n');
|
||||
it = it->next;
|
||||
}
|
||||
clean_ants(ants);
|
||||
}
|
||||
|
||||
int push_ants(t_lmdata *data, t_path *paths)
|
||||
{
|
||||
t_ants *ants;
|
||||
t_ants *it;
|
||||
t_node *node;
|
||||
int i;
|
||||
int ant_c;
|
||||
|
||||
ant_c = 1;
|
||||
ants = NULL;
|
||||
while ((ants || ant_c <= data->nbants) && (i = -1))
|
||||
{
|
||||
it = ants;
|
||||
while (it)
|
||||
{
|
||||
node = get_node(data, it->nb_node);
|
||||
if (node && node->next)
|
||||
it->nb_node = node->next->ind;
|
||||
it = it->next;
|
||||
}
|
||||
while (paths[++i].nodes)
|
||||
if (ant_c <= data->nbants && paths[i].nb_ants)
|
||||
{
|
||||
if (!add_ant(&ants, ant_c, i, paths))
|
||||
del_ants(&ants);
|
||||
if (!ants)
|
||||
return (0);
|
||||
ant_c++;
|
||||
paths[i].nb_ants--;
|
||||
}
|
||||
print_ants(data, &ants, paths);
|
||||
}
|
||||
return (1);
|
||||
}
|
70
srcs/push_ants_utils.c
Normal file
70
srcs/push_ants_utils.c
Normal file
@@ -0,0 +1,70 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* push_ants_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/10 11:23:36 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/15 17:44:11 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
t_ants *add_ant(t_ants **ants, int nb_ant, int nb_path, t_path *paths)
|
||||
{
|
||||
t_ants *new;
|
||||
t_ants *it;
|
||||
|
||||
if ((new = (t_ants*)ft_memalloc(sizeof(t_ants))) != NULL)
|
||||
{
|
||||
new->nb_ant = nb_ant;
|
||||
new->nb_path = nb_path;
|
||||
new->nb_node = paths[nb_path].nodes->index;
|
||||
new->end = 0;
|
||||
new->next = NULL;
|
||||
if (*ants == NULL)
|
||||
*ants = new;
|
||||
else
|
||||
{
|
||||
it = *ants;
|
||||
while (it->next)
|
||||
it = it->next;
|
||||
it->next = new;
|
||||
}
|
||||
}
|
||||
return (new);
|
||||
}
|
||||
|
||||
void del_ants(t_ants **ants)
|
||||
{
|
||||
t_ants *tmp;
|
||||
|
||||
while (*ants)
|
||||
{
|
||||
tmp = *ants;
|
||||
*ants = (*ants)->next;
|
||||
ft_memdel((void**)&tmp);
|
||||
}
|
||||
}
|
||||
|
||||
t_node *get_node(t_lmdata *data, int index)
|
||||
{
|
||||
t_node *it;
|
||||
|
||||
it = data->nodes_data;
|
||||
while (it && it->ind != index)
|
||||
it = it->next;
|
||||
return (it);
|
||||
}
|
||||
|
||||
t_ind *get_node_path(t_ind *lst, int index)
|
||||
{
|
||||
t_ind *it;
|
||||
|
||||
it = lst;
|
||||
while (it && it->index != index)
|
||||
it = it->next;
|
||||
return (it);
|
||||
}
|
73
srcs/score_utils.c
Normal file
73
srcs/score_utils.c
Normal file
@@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* score_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/04/15 14:24:15 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/15 17:50:56 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
void init_paths(t_path *ret)
|
||||
{
|
||||
int nb_paths;
|
||||
int i;
|
||||
int nb_nodes;
|
||||
t_ind *it;
|
||||
|
||||
nb_paths = 0;
|
||||
while (ret[nb_paths].nodes)
|
||||
{
|
||||
ret[nb_paths].nb_ants = 0;
|
||||
ret[nb_paths].score = 0;
|
||||
nb_paths++;
|
||||
}
|
||||
i = 0;
|
||||
while (ret[i].nodes && !(nb_nodes = 0))
|
||||
{
|
||||
it = ret[i].nodes;
|
||||
while (it && ++nb_nodes)
|
||||
it = it->next;
|
||||
ret[i].score = nb_nodes;
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
int get_score(t_lmdata *data, t_path *ret)
|
||||
{
|
||||
int i;
|
||||
int ant_c;
|
||||
int max;
|
||||
|
||||
init_paths(ret);
|
||||
i = -1;
|
||||
max = 0;
|
||||
while (ret[++i].nodes)
|
||||
{
|
||||
if (ret[max].score < ret[i].score)
|
||||
max = i;
|
||||
}
|
||||
ant_c = 0;
|
||||
while (ant_c < data->nbants && !(i = 0))
|
||||
{
|
||||
while (ret[i].nodes && ((ret[i].nb_ants > 0) * (ret[i].score + ret[i].nb_ants - 1)) < ret[max].score && ant_c < data->nbants)
|
||||
{
|
||||
ret[i].nb_ants++;
|
||||
ant_c++;
|
||||
ret[i].score = (ret[i].nb_ants > 0) * (ret[i].score + ret[i].nb_ants - 1);
|
||||
i++;
|
||||
}
|
||||
i = -1;
|
||||
max = 0;
|
||||
while (ret[++i].nodes)
|
||||
{
|
||||
if (ret[max].score < ret[i].score)
|
||||
max = i;
|
||||
}
|
||||
}
|
||||
return (ret[max].score);
|
||||
}
|
142
srcs/test.c
142
srcs/test.c
@@ -1,142 +0,0 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* test.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tmaze <tmaze@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2019/03/28 11:37:06 by tmaze #+# #+# */
|
||||
/* Updated: 2019/04/03 15:58:22 by tmaze ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "lem_in.h"
|
||||
|
||||
#define NB_NODES 8
|
||||
|
||||
int add_link(t_lmdata *data, int n1, int n2)
|
||||
{
|
||||
return (lst_indadd(&(data->adj[n1]), n2) && lst_indadd(&(data->adj[n2]), n1));
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
t_lmdata data;
|
||||
t_ind **path;
|
||||
t_ind *it;
|
||||
int i;
|
||||
|
||||
data.nb_nodes = NB_NODES;
|
||||
if ((data.adj = (t_ind**)ft_memalloc(sizeof(t_ind*) * NB_NODES)) == NULL)
|
||||
return (1);
|
||||
if (!add_link(&data, 0, 1) || !add_link(&data, 0, 2))
|
||||
{
|
||||
lst_inddel(&(data.adj[1]));
|
||||
lst_inddel(&(data.adj[0]));
|
||||
ft_memdel((void**)&(data.adj));
|
||||
return (1);
|
||||
}
|
||||
if (!add_link(&data, 1, 4))
|
||||
{
|
||||
lst_inddel(&(data.adj[2]));
|
||||
lst_inddel(&(data.adj[1]));
|
||||
lst_inddel(&(data.adj[0]));
|
||||
ft_memdel((void**)&(data.adj));
|
||||
return (1);
|
||||
}
|
||||
if (!add_link(&data, 2, 3) || !add_link(&data, 2, 5))
|
||||
{
|
||||
lst_inddel(&(data.adj[3]));
|
||||
lst_inddel(&(data.adj[2]));
|
||||
lst_inddel(&(data.adj[1]));
|
||||
lst_inddel(&(data.adj[0]));
|
||||
ft_memdel((void**)&(data.adj));
|
||||
return (1);
|
||||
}
|
||||
if (!add_link(&data, 3, 6))
|
||||
{
|
||||
lst_inddel(&(data.adj[5]));
|
||||
lst_inddel(&(data.adj[3]));
|
||||
lst_inddel(&(data.adj[2]));
|
||||
lst_inddel(&(data.adj[1]));
|
||||
lst_inddel(&(data.adj[0]));
|
||||
ft_memdel((void**)&(data.adj));
|
||||
return (1);
|
||||
}
|
||||
if (!add_link(&data, 4, 5))
|
||||
{
|
||||
lst_inddel(&(data.adj[6]));
|
||||
lst_inddel(&(data.adj[5]));
|
||||
lst_inddel(&(data.adj[3]));
|
||||
lst_inddel(&(data.adj[2]));
|
||||
lst_inddel(&(data.adj[1]));
|
||||
lst_inddel(&(data.adj[0]));
|
||||
ft_memdel((void**)&(data.adj));
|
||||
return (1);
|
||||
}
|
||||
if (!add_link(&data, 5, 6) || !add_link(&data, 5, 7))
|
||||
{
|
||||
lst_inddel(&(data.adj[6]));
|
||||
lst_inddel(&(data.adj[5]));
|
||||
lst_inddel(&(data.adj[4]));
|
||||
lst_inddel(&(data.adj[3]));
|
||||
lst_inddel(&(data.adj[2]));
|
||||
lst_inddel(&(data.adj[1]));
|
||||
lst_inddel(&(data.adj[0]));
|
||||
ft_memdel((void**)&(data.adj));
|
||||
return (1);
|
||||
}
|
||||
if (!add_link(&data, 6, 7))
|
||||
{
|
||||
lst_inddel(&(data.adj[7]));
|
||||
lst_inddel(&(data.adj[6]));
|
||||
lst_inddel(&(data.adj[5]));
|
||||
lst_inddel(&(data.adj[4]));
|
||||
lst_inddel(&(data.adj[3]));
|
||||
lst_inddel(&(data.adj[2]));
|
||||
lst_inddel(&(data.adj[1]));
|
||||
lst_inddel(&(data.adj[0]));
|
||||
ft_memdel((void**)&(data.adj));
|
||||
return (1);
|
||||
}
|
||||
ft_printf("===== list of adj =====\n");
|
||||
i = 0;
|
||||
while (i < NB_NODES)
|
||||
{
|
||||
ft_printf("===== adj of %d =====\n", i);
|
||||
it = data.adj[i];
|
||||
while (it)
|
||||
{
|
||||
ft_printf("index: %d\nweight: %d\n\n", it->index, it->weight);
|
||||
it = it->next;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
path = edmunds_karp(&data, 0, 7);
|
||||
if (path != NULL)
|
||||
{
|
||||
i = 0;
|
||||
while (path[i])
|
||||
{
|
||||
it = path[i];
|
||||
while (it)
|
||||
{
|
||||
ft_printf(" %d -> ", it->index);
|
||||
it = it->next;
|
||||
}
|
||||
ft_putchar('\n');
|
||||
i++;
|
||||
}
|
||||
}
|
||||
tablst_inddel(path);
|
||||
lst_inddel(&(data.adj[7]));
|
||||
lst_inddel(&(data.adj[6]));
|
||||
lst_inddel(&(data.adj[5]));
|
||||
lst_inddel(&(data.adj[4]));
|
||||
lst_inddel(&(data.adj[3]));
|
||||
lst_inddel(&(data.adj[2]));
|
||||
lst_inddel(&(data.adj[1]));
|
||||
lst_inddel(&(data.adj[0]));
|
||||
ft_memdel((void**)&(data.adj));
|
||||
return (0);
|
||||
}
|
Reference in New Issue
Block a user