-
Notifications
You must be signed in to change notification settings - Fork 0
/
graph.c
234 lines (214 loc) · 6.28 KB
/
graph.c
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#ifndef GRAPH_C_ODEVSYS_IS_PROBLEMATIC_621
#define GRAPH_C_ODEVSYS_IS_PROBLEMATIC_621
#include "graph.h"
#include <string.h>
//#include "node.h"
//#include "edge.h"
//#include "arrays.h"
FILE* file_from_string(const char* str, const char* mode) {
FILE* file = NULL;
if(strcmp(str, ":stdin") == 0) {
if(mode[0]=='w')
chyba("Cannot write in stdin!%s", 12,"");
return stdin;
}
else if(strcmp(str, ":stdout") == 0) {
if(mode[0]=='r')
chyba("Cannot read from stdout!%s", 12,"");
return stdout;
}
else {
file = fopen(str, mode);
}
if(file == NULL) {
chyba("File \"%s\" cannot be open.", 6+6+6, str);
}
else {
return file;
}
}
Graph* allocate_graph()
{
Graph* g = NEW(Graph);
g->nodes = array_create(sizeof(Node), 10);
g->sorted = true;
g->forceSorting = true;
return g;
}
void free_graph(Graph** graph)
{
Graph* g = *graph;
graph_clear(g);
array_destroy(g->nodes);
g->nodes = NULL;
free(g);
*graph = NULL;
}
void graph_clear(Graph* const g)
{
Node* currentNode;
AR_FOREACH_PTR(currentNode, g->nodes, Node) {
if(currentNode->edges != NULL) {
array_destroy(currentNode->edges);
}
}
array_clear(g->nodes);
//return NULL;
}
void load_txt(const char* fname, Graph* g)
{
FILE* stream = file_from_string(fname, "r");
Node* node = NULL;
graph_clear(g);
NodeName max_node_name = 0;
while(!feof(stream)) {
int name, target, cost;
if(fscanf(stream, "%d %d %d", &name, &target, &cost)!=3)
break;
if(target > max_node_name) {
max_node_name = target;
}
if(name > max_node_name) {
max_node_name = name;
}
// add new node
if(node==NULL || node->name != name) {
// Expand array as much as possible to predict
array_reserve(g->nodes, max_node_name-1);
// grab new node
node = graph_add_node(g, name, NULL);
}
node_add_edge(node, target, cost);
}
fclose(stream);
}
void save_txt(const Graph* const g, const char* fname)
{
FILE* stream = file_from_string(fname, "w");
Node* currentNode;
AR_FOREACH_PTR(currentNode, g->nodes, Node) {
if(currentNode->edges != NULL) {
Array* edges = currentNode->edges;
const NodeName name = currentNode->name;
for (size_t i = 0, l = edges->length; i < l; ++i) {
Edge* e = array_get_fast(edges, i);
fprintf(stream, "%u %u %u\n", name, e->B, e->cost);
}
}
}
fclose(stream);
}
const unsigned char header[] = {0xDE, 0xAD};
void save_bin(const Graph* const g, const char* fname)
{
FILE* stream = file_from_string(fname, "wb");
fwrite(header, sizeof(*header), 2, stream);
Node* n;
AR_FOREACH_PTR(n, g->nodes, Node) {
node_write_binary(n, stream);
}
fclose(stream);
}
void load_bin(const char* fname, Graph* g)
{
FILE* stream = file_from_string(fname, "rb");
graph_clear(g);
unsigned char read_header[2];
size_t read = fread(read_header, sizeof(*header), 2, stream);
if(read<2)
chyba("Corrupted binary file %s!", 22, fname);
if(read_header[0]!=header[0] || read_header[1]!=header[1])
chyba("Invalid header in file %s!", 23, fname);
Node n;
n.edges = NULL;
while(node_read_binary(&n, stream)) {
array_push(g->nodes, &n);
n.edges = NULL;
}
// If last element failed to initialize fully
if(n.edges != NULL)
array_destroy(n.edges);
fclose(stream);
}
Node* graph_add_node(Graph * const g, const NodeName name, void *value) {
Node* node = NULL;
if((node=graph_find_node(g, name))!=NULL) {
node->value = value;
return node;
}
return graph_add_node_unsafe(g, name, value);
}
Node* graph_find_node(const Graph* const g, const NodeName name)
{
if(g->sorted) {
if(name < (int)g->nodes->length && name>=0) {
return array_get_fast(g->nodes, name);
}
}
else {
Node* currentNode;
AR_FOREACH_PTR(currentNode, g->nodes, Node) {
if(currentNode->name == name) {
return currentNode;
}
}
}
return NULL;
}
Node* graph_add_node_unsafe(Graph* const g, const NodeName name, void* value)
{
Node node;
node.costToStart = -1;
node.parent = -1;
node.edges = NULL;
node.name = name;
node.value = value;
// Automatically mark graph as unsorted
// this makes search slower
if(g->sorted) {
if(g->nodes->length > 0) {
Node* const prev_node = array_get_fast(g->nodes, g->nodes->length-1);
if(prev_node->name != name-1) {
// sorting can only be forced when appending after the end
if(g->forceSorting && name>prev_node->name) {
graph_fill_before(g, name);
}
else {
log_info("Graph is no longer ordered, nodes %d -> %d!", prev_node->name, name);
g->sorted = false;
g->forceSorting = false;
}
}
}
else if(name!=0) {
if(g->forceSorting) {
graph_fill_before(g, name);
}
else {
log_info("Graph is no longer ordered, first node: %d!", name);
g->sorted = false;
}
}
}
array_push(g->nodes, &node);
return array_get_fast(g->nodes, g->nodes->length-1);
}
void graph_fill_before(Graph* const g, const NodeName addedNode)
{
const Node* const lastNode = array_get_fast(g->nodes, g->nodes->length-1);
NodeName nextName = lastNode->name;
if(nextName+1>=addedNode) {
return;
}
// reserve space in array for new nodes
// that's rather optimal thing to do
array_reserve(g->nodes, g->nodes->length+(size_t)(addedNode - nextName));
log_info("Reserving space for %d new nodes starting from %d to %d",
(int)(addedNode - nextName), (int)lastNode->name, (int)addedNode);
while((++nextName) < addedNode) {
graph_add_node_unsafe(g, nextName, NULL);
}
}
void graph_destroy(Graph** g){free_graph(g);}
Graph* graph_create() {return allocate_graph();}
#endif