-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_tables.c
More file actions
121 lines (112 loc) · 3.29 KB
/
make_tables.c
File metadata and controls
121 lines (112 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* ************************************************************************** */
/* */
/* :::::::: */
/* make_tables.c :+: :+: */
/* +:+ */
/* By: rcorke <rcorke@student.codam.nl> +#+ */
/* +#+ */
/* Created: 2019/08/10 14:11:43 by rcorke #+# #+# */
/* Updated: 2019/09/28 16:28:29 by rcorke ######## odam.nl */
/* */
/* ************************************************************************** */
#include "lem_in.h"
static void check_start_end(t_store *iter, t_table *lem)
{
if (!ft_strcmp(iter->str, "##start"))
{
if (lem->start)
multiple_start_end_error(lem);
while (iter && iter->str[0] == '#')
iter = iter->next;
if (!iter)
wrong_input_error(lem, NULL);
lem->start = get_room_name(iter->str);
}
else if (!ft_strcmp(iter->str, "##end"))
{
if (lem->end)
multiple_start_end_error(lem);
while (iter && iter->str[0] == '#')
iter = iter->next;
if (!iter)
wrong_input_error(lem, NULL);
lem->end = get_room_name(iter->str);
}
}
static void add_to_link_table(char *str, t_table *lem, int index)
{
lem->link_table[index] = (t_links *)malloc(sizeof(t_links));
lem->link_table[index]->index = index;
lem->link_table[index]->str = get_room_name(str);
lem->link_table[index]->distance = -1;
lem->link_table[index]->bfs_visited = 0;
lem->link_table[index]->trav_visited = 0;
lem->link_table[index]->num_links = 0;
lem->link_table[index]->in = -1;
lem->link_table[index]->out = -1;
lem->link_table[index]->next = NULL;
}
static void add_room(t_table *lem, char *str, int index)
{
int hash_index;
t_hash *iter;
t_hash *new;
char *room_name;
room_name = get_room_name(str);
hash_index = lemin_hash(room_name, lem->rooms);
new = (t_hash *)malloc(sizeof(t_hash));
new->name = room_name;
new->index = index;
new->next = NULL;
if (!lem->hash_table[hash_index])
{
lem->hash_table[hash_index] = new;
lem->hash_table[hash_index]->next = NULL;
}
else
{
iter = lem->hash_table[hash_index];
while (iter->next)
iter = iter->next;
iter->next = new;
iter->next->next = NULL;
}
add_to_link_table(str, lem, index);
}
static void set_hash_to_null(t_table *lem, t_hash **hash_table)
{
int x;
x = 0;
while (x < lem->rooms)
{
hash_table[x] = NULL;
x++;
}
}
void make_tables(t_table *lem)
{
t_store *iter;
int input;
int index;
lem->hash_table = (t_hash **)malloc(sizeof(t_hash *) * lem->rooms);
set_hash_to_null(lem, lem->hash_table);
lem->link_table = (t_links **)malloc(sizeof(t_links *) * lem->rooms);
iter = lem->storage;
index = 0;
while (iter->next)
{
input = input_type(iter->str);
if (input == 2)
{
add_room(lem, iter->str, index);
index++;
}
else if (input == 3)
add_link(iter->str, lem);
else if (input == 4)
check_start_end(iter, lem);
iter = iter->next;
}
if (!lem->start || !lem->end || ft_strequ(lem->start, lem->end))
no_start_end_error(lem);
}