-
Notifications
You must be signed in to change notification settings - Fork 2
/
parser.c
86 lines (73 loc) · 1.95 KB
/
parser.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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "vector.h"
#include "dag.h"
#include "parser.h"
int parse_patterson(const char *fp, dag **ret_g) {
FILE *f = fopen(fp, "r");
if (f == NULL) {
return -1;
}
int n_nodes;
int n_resources;
fscanf(f, "%d %d", &n_nodes, &n_resources);
if (n_resources != 0) {
fprintf(stderr, "Resource constrained problems not supported\n");
fclose(f);
return -1;
}
if (n_nodes < 0) {
fprintf(stderr, "Illegal number of vertices specified\n");
fclose(f);
return -1;
}
unsigned node_lens[n_nodes];
idx_vec node_preds[n_nodes];
for (size_t i = 0; i < n_nodes; i++) {
if (idx_vec_init(&node_preds[i], 0) != 0) {
return -1;
}
}
// read data lines
for (size_t i = 0; i < n_nodes; i++) {
int n_succs;
fscanf(f, "%d %d", &node_lens[i], &n_succs);
for (size_t j = 0; j < n_succs; j++) {
unsigned succ_id;
fscanf(f, "%d", &succ_id);
succ_id--;
if (succ_id > 0) {
idx_vec_push(&node_preds[succ_id], i);
}
}
}
fclose(f);
dag *g = dag_create();
if (g == NULL) {
return -1;
}
for (size_t i = 1; i < n_nodes - 1; i++) {
dag_vertex(g, node_lens[i], node_preds[i].size, node_preds[i].data);
}
for (size_t i = 0; i < n_nodes; i++) {
idx_vec_destroy(&node_preds[i]);
}
dag_build(g);
*ret_g = g;
return 0;
}
void print_dot(dag *g, const char *name) {
assert(g != NULL);
printf("digraph %s {\n", name);
for (size_t i = 0, size = dag_size(g); i < size; i++) {
size_t nsuccs = dag_nsuccs(g, i);
unsigned succs[nsuccs];
dag_succs(g, i, succs);
for (size_t j = 0; j < nsuccs; j++) {
printf("\t%zu -> %u;\n", i, succs[j]);
}
}
printf("}\n");
}