-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopulate.c
More file actions
177 lines (152 loc) · 3.9 KB
/
Copy pathpopulate.c
File metadata and controls
177 lines (152 loc) · 3.9 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
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
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "graph.h"
/* Usage: populate foo.g < edges */
/* Takes a list of edges on standard input; dumps the graph to foo.g */
struct edge {
vertex_t p, c;
};
struct edge *edges;
size_t num_edges, edges_alloced;
vertex_t max_vertex;
vertex_t num_vertices;
vertex_t *parent_count, *child_count;
void count_neighbors(void);
void populate_edges(FILE *in) {
unsigned int p, c;
while (fscanf(in, "%u %u\n", &p, &c) == 2) {
assert(num_edges <= edges_alloced);
if (num_edges == edges_alloced) {
/* Need more memory */
edges_alloced++; /* in case it was 0 */
edges_alloced *= 2;
edges = realloc(edges, edges_alloced * sizeof(struct edge));
if (!edges) {
perror("realloc of edges");
exit(2);
}
}
assert(num_edges < edges_alloced);
if (p > max_vertex)
max_vertex = p;
if (c > max_vertex)
max_vertex = c;
edges[num_edges].p = p;
edges[num_edges].c = c;
num_edges++;
}
if (ferror(in)) {
perror("reading input");
exit(1);
}
num_vertices = max_vertex + 1;
count_neighbors();
}
void count_neighbors(void) {
struct edge *e;
parent_count = calloc(num_vertices, sizeof(vertex_t));
child_count = calloc(num_vertices, sizeof(vertex_t));
if (!parent_count || !child_count) {
perror("calloc of counts");
exit(2);
}
for (e = edges; e < edges + num_edges; e++) {
parent_count[e->c]++;
child_count[e->p]++;
}
}
graph populate_graph(void) {
graph G;
size_t s = GRAPH_NEEDED_STRUCTURE_SIZE(num_vertices, num_edges);
G = calloc(s, sizeof(vertex_t));
if (!G) {
perror("calloc of graph");
exit(2);
}
G_MAGIC(G) = GRAPH_MAGIC_NUMBER;
G_VERSION(G) = GRAPH_VERSION;
G_HEADER_SIZE(G) = GRAPH_HEADER_SIZE;
G_STRUCTURE_SIZE(G) = s;
G_TOTAL_VERTICES(G) = num_vertices;
G_TOTAL_EDGES(G) = num_edges; /* every edge appears twice */
G_ENTRY_SIZE(G) = GRAPH_VERTEX_ENTRY_SIZE;
/* Allocate space for edge lists */
vertex_t v;
vertex_t i = 0;
for (v = 0; v < num_vertices; v++) {
G_NUM_PARENTS(G,v) = 0; /* will be filled in later */
G_PARENT_LIST_START(G,v) = i;
i += parent_count[v];
G_NUM_CHILDREN(G,v) = 0;
G_CHILD_LIST_START(G,v) = i;
i += child_count[v];
assert(&G_EDGE(G,i) <= G+s);
}
/* Fill in edge lists */
struct edge *e;
for (e = edges; e < edges + num_edges; e++) {
G_PARENT(G, e->c, G_NUM_PARENTS(G, e->c)++) = e->p;
G_CHILD(G, e->p, G_NUM_CHILDREN(G, e->p)++) = e->c;
}
/* Re-validate */
for (v = 0; v < num_vertices; v++) {
assert(G_NUM_PARENTS(G, v) == parent_count[v]);
assert(&G_PARENT(G, v, G_NUM_PARENTS(G,v)) <= G+s);
assert(G_NUM_CHILDREN(G,v) == child_count[v]);
assert(&G_CHILD(G, v, G_NUM_CHILDREN(G,v)) <= G+s);
}
return G;
}
void write_graph(const_graph G, const char *filename) {
int fd;
if ((fd = open(filename, O_CREAT | O_WRONLY, 0644)) < 0) {
perror(filename);
exit(1);
}
if (flock(fd, LOCK_EX) < 0) {
perror("flock");
exit(1);
}
if (ftruncate(fd, 0) < 0) {
perror("ftruncate");
exit(1);
}
/* Write everything out */
const char *p = (const char *)G;
size_t s = G_STRUCTURE_SIZE(G) * sizeof(vertex_t);
const char *end = p + s;
while (p < end) {
errno = 0;
ssize_t res = write(fd, p, end - p);
if (res > 0) {
p += res;
} else if (errno != EINTR) {
perror("write");
exit(1);
}
}
if (flock(fd, LOCK_UN) < 0) {
perror("flock(LOCK_UN)");
}
if (close(fd) < 0) {
perror("close");
}
}
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "Usage: %s foo.g < edgelist\n", argv[0]);
exit(3);
}
populate_edges(stdin);
write_graph(populate_graph(), argv[1]);
return 0;
}