-
Notifications
You must be signed in to change notification settings - Fork 9
/
assembleg.c
368 lines (304 loc) · 9.93 KB
/
assembleg.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/* assembleg.c version 1.1; B D McKay, Sep 2018. */
#define USAGE "assembleg -n#|-n#:# [-i#|i#:#] [-L] [-q] [infile [outfile]]"
#define HELPTEXT \
" Assemble input graphs as components of output graphs.\n\
\n\
The output file has no header.\n\
If the input has any directed graphs, all outputs are directed.\n\
Otherwise, the output format is determined by the header\n\
or first input.\n\
The input graphs had better all fit into memory at once,\n\
unless -L is given, in which case only the graphs of at\n\
most half the output size are stored at once.\n\
The output graphs will be non-isomorphic if the input\n\
graphs are connected and non-isomorphic.\n\
\n\
-n# -n#:# Give range of output sizes (compulsory)\n\
-i# -i#:# Give range of input sizes to use\n\
-L Assume all input graphs strictly larger than maxn/2\n\
vertices follow any smaller graphs in the input,\n\
where maxn is the largest size specified by -n.\n\
This can greatly reduce memory consumption.\n\
-c Also write graphs consisting of a single input\n\
-q Suppress auxiliary information.\n"
/*************************************************************************/
#include "gtools.h"
static int ninputs; /* Number of inputs */
static unsigned long nout; /* Number of outputs */
typedef graph *graphptr;
static graphptr *gin; /* Their contents */
static int *size; /* Their sizes */
static int outcode;
/**************************************************************************/
static void
insertg(graph *g, int ng, graph *h, int nh, int n)
/* Insert nh-vertex graph starting at vertex ng in graph g.
n is the total size available.
It is assumed that g is empty apart from 0..ng-1. */
{
int i,j,m,mh;
set *gi,*hi;
m = SETWORDSNEEDED(n);
mh = SETWORDSNEEDED(nh);
for (i = 0, hi = h, gi = g + ng*m; i < nh;
++i, hi += mh, gi += m)
{
for (j = -1; (j = nextelement(hi,mh,j)) >= 0; )
ADDELEMENT(gi,ng+j);
}
}
static void
removeg(graph *g, int ng, int nh, int n)
/* Remove a subgraph that was in position ng..ng+nh-1. */
{
set *gi;
int i,j,m;
m = SETWORDSNEEDED(n);
for (i = ng, gi = g + ng*m; i < ng+nh; ++i, gi += m)
{
for (j = ng; j < ng+nh; ++j)
DELELEMENT(gi,j);
}
}
/**************************************************************************/
#define SORT_NAME sortbysize
#define SORT_OF_SORT 2
#define SORT_TYPE1 int
#define SORT_TYPE2 graphptr
#include "sorttemplates.c"
static void
readinputs(FILE *f, int imin, int imax)
/* Read inputs and sort by size */
{
size_t tablesize;
graph *g;
int m,n;
boolean digraph;
if ((gin = malloc(sizeof(graphptr)*10000)) == NULL ||
(size = malloc(sizeof(int)*10000)) == NULL)
gt_abort(">E malloc failed in readinputs()\n");
tablesize = 10000;
ninputs = 0;
while (TRUE)
{
if ((g = readgg(f,NULL,0,&m,&n,&digraph)) == NULL) break;
if (digraph) outcode = DIGRAPH6;
if (n < imin || n > imax) continue;
if (ninputs == tablesize)
{
tablesize += 10000;
if ((gin = realloc(gin,sizeof(graphptr)*tablesize)) == NULL ||
(size = realloc(size,sizeof(int)*tablesize)) == NULL)
gt_abort(">E realloc failed in readinputs()\n");
}
gin[ninputs] = g;
size[ninputs] = n;
++ninputs;
}
if (ninputs < 0 || ninputs > tablesize)
gt_abort(">E Some overflow problem in readinputs()\n");
sortbysize(size,gin,ninputs);
}
static void
readsomeinputs(FILE *f, int imin, int imax, int maxsize,
graph **g, int *n)
/* Read inputs and sort by size. Stop when either EOF or a
graph bigger than maxsize is read. */
{
size_t tablesize;
int m;
boolean digraph;
if ((gin = malloc(sizeof(graphptr)*10000)) == NULL ||
(size = malloc(sizeof(int)*10000)) == NULL)
gt_abort(">E malloc failed in readsomeinputs()\n");
tablesize = 10000;
ninputs = 0;
while (TRUE)
{
if ((*g = readgg(f,NULL,0,&m,n,&digraph)) == NULL) break;
if (digraph) outcode = DIGRAPH6;
if (*n > maxsize) break;
if (*n < imin || *n > imax) continue;
if (ninputs == tablesize)
{
tablesize += 10000;
if ((gin = realloc(gin,sizeof(graphptr)*tablesize)) == NULL ||
(size = realloc(size,sizeof(int)*tablesize)) == NULL)
gt_abort(">E realloc failed in readsomeinputs()\n");
}
gin[ninputs] = *g;
size[ninputs] = *n;
++ninputs;
}
if (ninputs < 0 || ninputs > tablesize)
gt_abort(">E Some overflow problem in readinputs()\n");
sortbysize(size,gin,ninputs);
}
/**************************************************************************/
static void
assemble(graph *g, int nmin, int nmax, int sofar, int lastpos,
boolean writeconn, FILE *outfile)
/* Recursively add one more graph.
sofar = how many vertices so far. */
{
int pos,newsize;
for (pos = lastpos; pos < ninputs; ++pos)
{
newsize = sofar + size[pos];
if (newsize > nmax) break;
insertg(g,sofar,gin[pos],size[pos],nmax);
if (newsize >= nmin && (sofar > 0 || writeconn))
{
if (outcode == DIGRAPH6)
writed6(outfile,g,SETWORDSNEEDED(nmax),newsize);
else if (outcode == GRAPH6)
writeg6(outfile,g,SETWORDSNEEDED(nmax),newsize);
else
writes6(outfile,g,SETWORDSNEEDED(nmax),newsize);
++nout;
}
assemble(g,nmin,nmax,newsize,pos,writeconn,outfile);
removeg(g,sofar,size[pos],nmax);
}
}
/**************************************************************************/
int
main(int argc, char *argv[])
{
char *infilename,*outfilename;
FILE *infile,*outfile;
boolean badargs,quiet;
boolean nswitch,cswitch,iswitch,Lswitch;
boolean digraph;
int j,m,n,argnum;
int codetype;
graph *gread,*gout;
nauty_counter nin;
char *arg,sw;
double t;
long nmin,nmax,mmax,imin,imax;
HELP; PUTVERSION;
infilename = outfilename = NULL;
badargs = FALSE;
iswitch = nswitch = cswitch = quiet = Lswitch = FALSE;
argnum = 0;
badargs = FALSE;
for (j = 1; !badargs && j < argc; ++j)
{
arg = argv[j];
if (arg[0] == '-' && arg[1] != '\0')
{
++arg;
while (*arg != '\0')
{
sw = *arg++;
SWBOOLEAN('q',quiet)
else SWBOOLEAN('c',cswitch)
else SWBOOLEAN('L',Lswitch)
else SWRANGE('n',":-",nswitch,nmin,nmax,"assembleg -n")
else SWRANGE('i',":-",iswitch,imin,imax,"assembleg -i")
else badargs = TRUE;
}
}
else
{
++argnum;
if (argnum == 1) infilename = arg;
else if (argnum == 2) outfilename = arg;
else badargs = TRUE;
}
}
if (!nswitch) gt_abort(">E assembleg: -n is compulsory\n");
if (badargs)
{
fprintf(stderr,">E Usage: %s\n",USAGE);
GETHELP;
exit(1);
}
if (nmin <= 0) nmin = 1;
if (nmin > NAUTY_INFINITY-2) nmin = NAUTY_INFINITY-2;
if (!quiet)
{
fprintf(stderr,">A assembleg -");
if (nmin == nmax) fprintf(stderr,"n%ld",nmin);
else fprintf(stderr,"n%ld:%ld",nmin,nmax);
if (iswitch)
{
if (imin == imax) fprintf(stderr,"i%ld",imin);
else fprintf(stderr,"i%ld:%ld",imin,imax);
}
if (cswitch) fprintf(stderr,"c");
if (Lswitch) fprintf(stderr,"L");
if (argnum > 0) fprintf(stderr," %s",infilename);
if (argnum > 1) fprintf(stderr," %s",outfilename);
fprintf(stderr,"\n");
fflush(stderr);
}
if (!iswitch || imin <= 0) imin = 1;
if (!iswitch || imax > nmax) imax = nmax;
if (!cswitch && imax == nmax) --imax;
if (infilename && infilename[0] == '-') infilename = NULL;
infile = opengraphfile(infilename,&codetype,FALSE,1);
if (!infile) exit(1);
if (!infilename) infilename = "stdin";
if (!outfilename || outfilename[0] == '-')
{
outfilename = "stdout";
outfile = stdout;
}
else if ((outfile = fopen(outfilename,"w")) == NULL)
{
fprintf(stderr,"Can't open output file %s\n",outfilename);
gt_abort(NULL);
}
if (codetype&SPARSE6) outcode = SPARSE6;
else if (codetype&DIGRAPH6) outcode = DIGRAPH6;
else outcode = GRAPH6;
gtools_check(WORDSIZE,1,1,NAUTYVERSIONID);
t = CPUTIME;
mmax = SETWORDSNEEDED(nmax);
if ((gout = malloc(mmax*sizeof(graph)*nmax)) == NULL)
gt_abort(">E assembleg: malloc() failed in main()\n");
nout = 0;
if (Lswitch)
{
readsomeinputs(infile,(int)imin,(int)imax,(int)nmax/2,&gread,&n);
EMPTYSET(gout,m*(size_t)nmax);
assemble(gout,(int)nmin,(int)nmax,0,0,cswitch,outfile);
while (gread)
{
if (n >= imin && n <= imax)
{
EMPTYSET(gout,mmax*(size_t)nmax);
insertg(gout,0,gread,n,(int)nmax);
if (n >= nmin && n <= nmax && cswitch)
{
if (outcode == DIGRAPH6)
writed6(outfile,gout,mmax,n);
else if (outcode == GRAPH6)
writeg6(outfile,gout,mmax,n);
else
writes6(outfile,gout,mmax,n);
++nout;
}
assemble(gout,(int)nmin,(int)nmax,n,0,cswitch,outfile);
}
FREES(gread);
if ((gread = readgg(infile,NULL,0,&m,&n,&digraph)) == NULL) break;
if (digraph) outcode = DIGRAPH6;
if (n <= nmax/2) gt_abort(">E assembleg -L : inputs in bad order\n");
}
}
else
{
readinputs(infile,(int)imin,(int)imax);
EMPTYSET(gout,m*(size_t)nmax);
assemble(gout,(int)nmin,(int)nmax,0,0,cswitch,outfile);
}
t = CPUTIME - t;
if (!quiet)
fprintf(stderr,">Z %d graphs read from %s; " COUNTER_FMT
" graphs written to %s in %3.2f sec.\n",
ninputs,infilename,nout,outfilename,t);
exit(0);
}