-
Notifications
You must be signed in to change notification settings - Fork 9
/
gtools.c
2875 lines (2522 loc) · 68.6 KB
/
gtools.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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* gtools.c : Common routines for gtools programs. */
/* Version 4.2, Oct 2017. */
/* Todo: size check if MAXN>0; option to free memory */
#include "gtools.h"
#ifndef SEEK_SET
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#endif
TLS_ATTR size_t ogf_linelen;
TLS_ATTR boolean is_pipe;
#if HAVE_FSEEKO
#define FSEEK_VER fseeko
#define FTELL_VER ftello
#define OFF_T_VER off_t
#else
#if !FTELL_DEC
extern long ftell(FILE*);
extern int fseek(FILE*,long,int);
#endif
#define FSEEK_VER fseek
#define FTELL_VER ftell
#define OFF_T_VER long
#endif
#if !POPEN_DEC
extern FILE *popen(const char*,const char*);
#endif
/*
Version 1.1: Fixed sparse6 input for powers of 2. May 9, 1998
Version 1.2: Added "cmd: ..." option for opengraphfile().
Fixed readg() bug (could not be invisible). Oct 5, 1998
Version 1.3: Added "is_pipe". June 20, 2002
Version 1.4: Stuff for autoconf. August 30, 2002
Version 1.5: Unlocked stdio for efficiency. October 31, 2004
Also fwrite() in place of fputs() for writeline().
Version 1.6: i/o for sparsegraph; use of s6len; improve allocations
Version 1.7: Add stringcounts()
Add very long size code (see formats.txt)
Version 1.8: Add gtools_check()
Version 1.9: Add writepc_sg(), readpc_sg() and readpcle_sg()
Add planar_code options to opengraphfile()
Version 2.4: Add writeec_sg(), readec_sg() (MISSING!)
Add edge_code options to opengraphfile()
Version 2.5: Remove sortints(), not used
Version 2.6: Add sgtog6() and writeg6_sg()
Version 2.7: Add lots of explicit casts.
Fix planar code output for n > 255.
Version 3.0: Procedures for incremental sparse6 format.
Add checkgline()
Version 4.0: Procedures for digraph6 format.
Version 4.1: Made encodegraphsize() external.
Version 4.2: Fixes for null graphs; thanks to Kevin Ryde.
*/
#define B(i) (1 << ((i)-1))
#define M(i) ((1 << (i))-1)
/*********************************************************************
opengraphfile(filename,codetype,assumefixed,position)
opens and positions a file for reading graphs.
filename = the name of the file to open
(NULL means stdin, assumed already open)
If filename starts with "cmd:", the remainder is taken
to be a command to open a subshell for, using a pipe.
codetype = returns a code for the format.
This is a combination of SPARSE6, GRAPH6, PLANARCODE,
PLANARCODELE, PLANARCODEBE, EDGECODE, DIGRAPH6,
UNKNOWN_TYPE and HAS_HEADER.
If a header is present, that overrides the data.
If there is no header, the first graph is examined.
assumefixed = nonzero if files other than stdin or pipes should be
assumed to be seekable and have equal record sizes.
Ignored if there is a sparse6 header or the first
graph has sparse6 format. The most common example
is that graph6 and digraph6 records have lengths
that depend only on the number of vertices.
position = the number of the record to position to
(the first is number 1; 0 and -NOLIMIT also mean
to position at start). planar_code files can only
be positioned at the start.
If the file starts with ">", there must be a header.
Otherwise opengraphfile() fails.
The value returned is a file pointer or NULL.
If assumedfixed is not zero and position > 1, the global variable
ogf_linelen is set to the length (including \n) of the length of the
first record other than the header.
The global variable is_pipe is set to whether the input file is a pipe.
**********************************************************************/
FILE*
opengraphfile(char *filename, int *codetype, int assumefixed, long position)
{
FILE *f;
int c,bl,firstc;
long i,l;
OFF_T_VER pos,pos1,pos2;
boolean bad_header;
is_pipe = FALSE;
if (filename == NULL)
{
f = stdin;
assumefixed = FALSE;
}
else
{
if (filename[0] == 'c' && filename[1] == 'm'
&& filename[2] == 'd' && filename[3] == ':')
{
#if !HAVE_POPEN
gt_abort
(">E The \"cmd:\" option is not available in this version.\n");
#else
filename += 4;
while (*filename == ' ') ++filename;
f = popen(filename,"r");
#endif
assumefixed = FALSE;
is_pipe = TRUE;
}
else
f = fopen(filename,"r");
if (f == NULL)
{
fprintf(stderr,">E opengraphfile: can't open %s\n",filename);
return NULL;
}
}
FLOCKFILE(f);
firstc = c = GETC(f);
if (c == EOF)
{
*codetype = GRAPH6;
FUNLOCKFILE(f);
return f;
}
if (c != '>')
{
*codetype = firstc == ':' ? SPARSE6 : firstc == '&' ? DIGRAPH6 : GRAPH6;
ungetc(c,f);
}
else
{
bad_header = FALSE;
if ((c = GETC(f)) == EOF || c != '>')
bad_header = TRUE;
if (!bad_header && ((c = GETC(f)) == EOF ||
(c != 'g' && c != 's' && c != 'p' && c != 'd' && c != 'e')))
bad_header = TRUE;
if (!bad_header && c == 'g')
{
if ((c = GETC(f)) == EOF || c != 'r' ||
(c = GETC(f)) == EOF || c != 'a' ||
(c = GETC(f)) == EOF || c != 'p' ||
(c = GETC(f)) == EOF || c != 'h' ||
(c = GETC(f)) == EOF || c != '6' ||
(c = GETC(f)) == EOF || c != '<' ||
(c = GETC(f)) == EOF || c != '<')
bad_header = TRUE;
else
*codetype = GRAPH6 | HAS_HEADER;
}
else if (!bad_header && c == 'd')
{
if ((c = GETC(f)) == EOF || c != 'i' ||
(c = GETC(f)) == EOF || c != 'g' ||
(c = GETC(f)) == EOF || c != 'r' ||
(c = GETC(f)) == EOF || c != 'a' ||
(c = GETC(f)) == EOF || c != 'p' ||
(c = GETC(f)) == EOF || c != 'h' ||
(c = GETC(f)) == EOF || c != '6' ||
(c = GETC(f)) == EOF || c != '<' ||
(c = GETC(f)) == EOF || c != '<')
bad_header = TRUE;
else
*codetype = DIGRAPH6 | HAS_HEADER;
}
else if (!bad_header && c == 'e')
{
if ((c = GETC(f)) == EOF || c != 'd' ||
(c = GETC(f)) == EOF || c != 'g' ||
(c = GETC(f)) == EOF || c != 'e' ||
(c = GETC(f)) == EOF || c != '_' ||
(c = GETC(f)) == EOF || c != 'c' ||
(c = GETC(f)) == EOF || c != 'o' ||
(c = GETC(f)) == EOF || c != 'd' ||
(c = GETC(f)) == EOF || c != 'e' ||
(c = GETC(f)) == EOF || c != '<' ||
(c = GETC(f)) == EOF || c != '<')
bad_header = TRUE;
else
*codetype = EDGECODE | HAS_HEADER;
}
else if (!bad_header && c == 's')
{
if ((c = GETC(f)) == EOF || c != 'p' ||
(c = GETC(f)) == EOF || c != 'a' ||
(c = GETC(f)) == EOF || c != 'r' ||
(c = GETC(f)) == EOF || c != 's' ||
(c = GETC(f)) == EOF || c != 'e' ||
(c = GETC(f)) == EOF || c != '6' ||
(c = GETC(f)) == EOF || c != '<' ||
(c = GETC(f)) == EOF || c != '<')
bad_header = TRUE;
else
*codetype = SPARSE6 | HAS_HEADER;
}
else if (!bad_header && c == 'p')
{
if ((c = GETC(f)) == EOF || c != 'l' ||
(c = GETC(f)) == EOF || c != 'a' ||
(c = GETC(f)) == EOF || c != 'n' ||
(c = GETC(f)) == EOF || c != 'a' ||
(c = GETC(f)) == EOF || c != 'r' ||
(c = GETC(f)) == EOF || c != '_' ||
(c = GETC(f)) == EOF || c != 'c' ||
(c = GETC(f)) == EOF || c != 'o' ||
(c = GETC(f)) == EOF || c != 'd' ||
(c = GETC(f)) == EOF || c != 'e')
bad_header = TRUE;
else
{
if ((c = GETC(f)) == EOF)
bad_header = TRUE;
else if (c == ' ')
{
if ((bl = GETC(f)) == EOF || (bl != 'l' && bl != 'b') ||
(c = GETC(f)) == EOF || c != 'e' ||
(c = GETC(f)) == EOF || c != '<' ||
(c = GETC(f)) == EOF || c != '<')
bad_header = TRUE;
else if (bl == 'l')
*codetype = PLANARCODELE | HAS_HEADER;
else
*codetype = PLANARCODEBE | HAS_HEADER;
}
else if (c == '<')
{
if ((c = GETC(f)) == EOF || c != '<')
bad_header = TRUE;
else
*codetype = PLANARCODE | HAS_HEADER;
}
else
bad_header = TRUE;
}
}
if (bad_header)
{
fprintf(stderr,">E opengraphfile: illegal header in %s\n",
filename == NULL ? "stdin" : filename);
*codetype = UNKNOWN_TYPE | HAS_HEADER;
FUNLOCKFILE(f);
return NULL;
}
}
if (position <= 1) return f;
if (*codetype&PLANARCODEANY)
{
fprintf(stderr,
">E opengraphfile: planar_code files can only be opened at the start\n");
*codetype = UNKNOWN_TYPE | HAS_HEADER;
FUNLOCKFILE(f);
fclose(f);
return NULL;
}
if (*codetype&EDGECODE)
{
fprintf(stderr,
">E opengraphfile: edge_code files can only be opened at the start\n");
*codetype = UNKNOWN_TYPE | HAS_HEADER;
FUNLOCKFILE(f);
fclose(f);
return NULL;
}
if (!assumefixed || (*codetype&SPARSE6) || firstc == ':')
{
l = 1;
while ((c = GETC(f)) != EOF)
{
if (c == '\n')
{
++l;
if (l == position) break;
}
}
if (l == position) return f;
fprintf(stderr,
">E opengraphfile: can't find line %ld in %s\n",position,
filename == NULL ? "stdin" : filename);
return NULL;
}
else
{
pos1 = FTELL_VER(f);
if (pos1 < 0)
{
fprintf(stderr,">E opengraphfile: error on first ftell\n");
return NULL;
}
for (i = 1; (c = GETC(f)) != EOF && c != '\n'; ++i) {}
ogf_linelen = i;
if (c == EOF)
{
fprintf(stderr,
">E opengraphfile: required record no present\n");
FUNLOCKFILE(f);
return NULL;
}
pos2 = FTELL_VER(f);
if (pos2 < 0)
{
fprintf(stderr,">E opengraphfile: error on second ftell\n");
return NULL;
}
pos = pos1 + (position-1)*(pos2-pos1);
if (FSEEK_VER(f,pos,SEEK_SET) < 0)
{
fprintf(stderr,">E opengraphfile: seek failed\n");
return NULL;
}
}
FUNLOCKFILE(f);
return f;
}
/*********************************************************************/
void
writeline(FILE *f, char *s)
/* write a line with error checking */
/* \n is not appended automatically */
{
size_t slen;
slen = strlen(s);
if (fwrite(s,1,slen,f) != slen || ferror(f))
gt_abort(">E writeline : error on writing\n");
}
/*********************************************************************/
/* This function used to be called getline(), but this was changed due
to too much confusion with the GNU function of that name.
*/
char*
gtools_getline(FILE *f) /* read a line with error checking */
/* includes \n (if present) and \0. Immediate EOF causes NULL return. */
{
DYNALLSTAT(char,s,s_sz);
int c;
long i;
DYNALLOC1(char,s,s_sz,5000,"gtools_getline");
FLOCKFILE(f);
i = 0;
while ((c = GETC(f)) != EOF && c != '\n')
{
if (i == s_sz-3)
DYNREALLOC(char,s,s_sz,3*(s_sz/2)+10000,"gtools_getline");
s[i++] = (char)c;
}
FUNLOCKFILE(f);
if (i == 0 && c == EOF) return NULL;
if (c == '\n') s[i++] = '\n';
s[i] = '\0';
return s;
}
/****************************************************************************/
char*
getecline(FILE *f) /* read an edge_code line */
/* No trailing \n or \0 is added. Immediate EOF causes NULL return. */
{
size_t headsize,bodysize;
int sizesize,edgesize;
int c1,c,i;
DYNALLSTAT(unsigned char,s,s_sz);
FLOCKFILE(f);
if ((c1 = GETC(f)) == EOF) return NULL;
if (c1 > 0)
{
bodysize = c1;
edgesize = 1;
headsize = 1;
}
else
{
if ((c = GETC(f)) == EOF)
gt_abort(">E Incomplete edge_code line\n");
else
{
sizesize = c >> 4;
edgesize = c & 0xF;
bodysize = 0;
for (i = 0; i < sizesize; ++i)
{
if ((c = GETC(f)) == EOF)
gt_abort(">E Incomplete edge_code line\n");
else
bodysize = (bodysize << 8) + c;
}
headsize = 2 + sizesize;
}
}
DYNALLOC1(unsigned char,s,s_sz,headsize+bodysize,"getecline");
s[0] = (unsigned char)c1;
if (c1 == 0)
{
s[1] = (char)((sizesize << 4) + edgesize);
for (i = 0; i < sizesize; ++i)
s[headsize-1-i] = (bodysize >> 8*i) & 0xFF;
}
if (bodysize > 0 && fread(s+headsize,bodysize,1,f) != bodysize)
gt_abort(">E Incomplete edge_code line\n");
FUNLOCKFILE(f);
return (char*)s;
}
int
graphsize(char *s)
/* Get size of graph out of graph6, digraph6 or sparse6 string. */
{
char *p;
int n;
if (s[0] == ':' || s[0] == '&') p = s+1;
else p = s;
n = *p++ - BIAS6;
if (n > SMALLN)
{
n = *p++ - BIAS6;
if (n > SMALLN)
{
n = *p++ - BIAS6;
n = (n << 6) | (*p++ - BIAS6);
n = (n << 6) | (*p++ - BIAS6);
n = (n << 6) | (*p++ - BIAS6);
n = (n << 6) | (*p++ - BIAS6);
n = (n << 6) | (*p++ - BIAS6);
}
else
{
n = (n << 6) | (*p++ - BIAS6);
n = (n << 6) | (*p++ - BIAS6);
}
}
return n;
}
/****************************************************************************/
void
encodegraphsize(int n, char **pp)
/* Encode the size n in a string starting at **p, and reset **p
to point to the character after the size */
{
char *p;
p = *pp;
if (n <= SMALLN)
*p++ = (char)(BIAS6 + n);
else if (n <= SMALLISHN)
{
*p++ = MAXBYTE;
*p++ = (char)(BIAS6 + (n >> 12));
*p++ = (char)(BIAS6 + ((n >> 6) & C6MASK));
*p++ = (char)(BIAS6 + (n & C6MASK));
}
else
{
*p++ = MAXBYTE;
*p++ = MAXBYTE;
*p++ = (char)(BIAS6 + (n >> 30));
*p++ = (char)(BIAS6 + ((n >> 24) & C6MASK));
*p++ = (char)(BIAS6 + ((n >> 18) & C6MASK));
*p++ = (char)(BIAS6 + ((n >> 12) & C6MASK));
*p++ = (char)(BIAS6 + ((n >> 6) & C6MASK));
*p++ = (char)(BIAS6 + (n & C6MASK));
}
*pp = p;
}
/****************************************************************************/
void
stringcounts(char *s, int *pn, size_t *pe)
/* Determine number of edges of graph6, digraph6 or sparse6 string */
{
char *p;
int i,j,k,x,nb,v,n,need;
size_t count;
boolean done;
n = graphsize(s);
*pn = n;
p = s + (s[0] == ':' || s[0] == '&') + SIZELEN(n);
if (s[0] == ':') /* sparse6 */
{
count = 0;
for (i = n-1, nb = 0; i > 0 ; i >>= 1, ++nb) {}
k = 0;
v = 0;
done = FALSE;
while (!done)
{
if (k == 0)
{
x = *(p++);
if (x == '\n' || x == '\0')
{
done = TRUE; continue;
}
else
{
x -= BIAS6; k = 6;
}
}
if ((x & B(k))) ++v;
--k;
need = nb;
j = 0;
while (need > 0 && !done)
{
if (k == 0)
{
x = *(p++);
if (x == '\n' || x == '\0')
{
done = TRUE; continue;
}
else
{
x -= BIAS6; k = 6;
}
}
if (need >= k)
{
j = (j << k) | (x & M(k));
need -= k; k = 0;
}
else
{
k -= need;
j = (j << need) | ((x >> k) & M(need));
need = 0;
}
}
if (done) continue;
if (j > v)
v = j;
else if (v < n)
++count;
}
}
else /* graph6 or digraph6 */
{
count = 0;
for (; *p != '\n' && *p != '\0'; ++p)
count += bytecount[*p - BIAS6];
}
*pe = count;
}
/****************************************************************************/
void
stringtograph(char *s, graph *g, int m)
/* Convert string (graph6, digraph6 or sparse6 format) to graph. */
/* Assumes g is big enough to hold it. */
{
char *p;
int n,i,j,k,v,x,nb,need;
size_t ii;
set *gi,*gj;
boolean done;
n = graphsize(s);
if (n == 0) return;
p = s + (s[0] == ':' || s[0] == '&') + SIZELEN(n);
if (TIMESWORDSIZE(m) < n)
gt_abort(">E stringtograph: impossible m value\n");
for (ii = m*(size_t)n; --ii > 0;) g[ii] = 0;
g[0] = 0;
if (s[0] != ':' && s[0] != '&') /* graph6 format */
{
k = 1;
for (j = 1; j < n; ++j)
{
gj = GRAPHROW(g,j,m);
for (i = 0; i < j; ++i)
{
if (--k == 0)
{
k = 6;
x = *(p++) - BIAS6;
}
if ((x & TOPBIT6))
{
gi = GRAPHROW(g,i,m);
ADDELEMENT(gi,j);
ADDELEMENT(gj,i);
}
x <<= 1;
}
}
}
else if (s[0] == '&')
{
k = 1;
for (i = 0; i < n; ++i)
{
gi = GRAPHROW(g,i,m);
for (j = 0; j < n; ++j)
{
if (--k == 0)
{
k = 6;
x = *(p++) - BIAS6;
}
if ((x & TOPBIT6))
{
ADDELEMENT(gi,j);
}
x <<= 1;
}
}
}
else /* sparse6 format */
{
for (i = n-1, nb = 0; i > 0 ; i >>= 1, ++nb) {}
k = 0;
v = 0;
done = FALSE;
while (!done)
{
if (k == 0)
{
x = *(p++);
if (x == '\n' || x == '\0')
{
done = TRUE; continue;
}
else
{
x -= BIAS6; k = 6;
}
}
if ((x & B(k))) ++v;
--k;
need = nb;
j = 0;
while (need > 0 && !done)
{
if (k == 0)
{
x = *(p++);
if (x == '\n' || x == '\0')
{
done = TRUE; continue;
}
else
{
x -= BIAS6; k = 6;
}
}
if (need >= k)
{
j = (j << k) | (x & M(k));
need -= k; k = 0;
}
else
{
k -= need;
j = (j << need) | ((x >> k) & M(need));
need = 0;
}
}
if (done) continue;
if (j > v)
v = j;
else if (v < n)
{
ADDELEMENT(GRAPHROW(g,v,m),j);
ADDELEMENT(GRAPHROW(g,j,m),v);
}
}
}
}
/****************************************************************************/
void
stringtograph_inc(char *s, graph *g, int m,
graph *prevg, int prevn)
/* Convert string (graph6, digraph6 or sparse6 format) to graph,
allowing incremental sparse6 format with a prior graph assumed
to have matching m,n values.
If prevg != NULL and type is is6, use prevg as prior graph.
Assumes g is big enough to hold it.
*digraph is set according to the graph type.
*/
{
char *p;
int n,i,j,k,v,x,nb,need;
size_t ii;
set *gi,*gj;
boolean done;
if (s[0] == ';' && !prevg)
gt_abort(">E stringtograph_inc missing prior graph\n");
if (s[0] == ';')
{
n = prevn;
if (n == 0) return;
p = s + 1;
for (ii = m*(size_t)n; --ii > 0;) g[ii] = prevg[ii];
g[0] = prevg[0];
}
else
{
n = graphsize(s);
if (n == 0) return;
p = s + (s[0] == ':' || s[0] == '&') + SIZELEN(n);
for (ii = m*(size_t)n; --ii > 0;) g[ii] = 0;
g[0] = 0;
}
if (TIMESWORDSIZE(m) < n)
gt_abort(">E stringtograph_inc: impossible m value\n");
if (s[0] != ':' && s[0] != ';' && s[0] != '&') /* graph6 format */
{
k = 1;
for (j = 1; j < n; ++j)
{
gj = GRAPHROW(g,j,m);
for (i = 0; i < j; ++i)
{
if (--k == 0)
{
k = 6;
x = *(p++) - BIAS6;
}
if ((x & TOPBIT6))
{
gi = GRAPHROW(g,i,m);
FLIPELEMENT(gi,j);
if (i != j) FLIPELEMENT(gj,i);
}
x <<= 1;
}
}
}
else if (s[0] == '&') /* digraph6 format */
{
k = 1;
for (j = 0; j < n; ++j)
{
gj = GRAPHROW(g,j,m);
for (i = 0; i < n; ++i)
{
if (--k == 0)
{
k = 6;
x = *(p++) - BIAS6;
}
if ((x & TOPBIT6))
{
FLIPELEMENT(gj,i);
}
x <<= 1;
}
}
}
else /* sparse6 format */
{
for (i = n-1, nb = 0; i != 0 ; i >>= 1, ++nb) {}
k = 0;
v = 0;
done = FALSE;
while (!done)
{
if (k == 0)
{
x = *(p++);
if (x == '\n' || x == '\0')
{
done = TRUE; continue;
}
else
{
x -= BIAS6; k = 6;
}
}
if ((x & B(k))) ++v;
--k;
need = nb;
j = 0;
while (need > 0 && !done)
{
if (k == 0)
{
x = *(p++);
if (x == '\n' || x == '\0')
{
done = TRUE; continue;
}
else
{
x -= BIAS6; k = 6;
}
}
if (need >= k)
{
j = (j << k) | (x & M(k));
need -= k; k = 0;
}
else
{
k -= need;
j = (j << need) | ((x >> k) & M(need));
need = 0;
}
}
if (done) continue;
if (j > v)
v = j;
else if (v < n)
{
FLIPELEMENT(GRAPHROW(g,v,m),j);
if (j != v) FLIPELEMENT(GRAPHROW(g,j,m),v);
}
}
}
}
/***********************************************************************/
graph* /* read graph into nauty format */
readgg(FILE *f, graph *g, int reqm, int *pm, int *pn, boolean *digraph)
/* graph6, digraph6 and sparse6 formats are supported
f = an open file
g = place to put the answer (NULL for dynamic allocation)
reqm = the requested value of m (0 => compute from n)
*pm = the actual value of m
*pn = the value of n
*digraph = whether the input is a digraph
*/
{
char *s,*p;
int m,n;
if ((readg_line = gtools_getline(f)) == NULL) return NULL;
s = readg_line;
if (s[0] == ':')
{
readg_code = SPARSE6;
*digraph = FALSE;
p = s + 1;
}
else if (s[0] == '&')
{
readg_code = DIGRAPH6;
*digraph = TRUE;
p = s + 1;
}
else
{
readg_code = GRAPH6;
*digraph = FALSE;
p = s;
}
while (*p >= BIAS6 && *p <= MAXBYTE)
++p;
if (*p == '\0')
gt_abort(">E readgg: missing newline\n");
else if (*p != '\n')
gt_abort(">E readgg: illegal character\n");
n = graphsize(s);
if (readg_code == GRAPH6 && p - s != G6LEN(n))
gt_abort(">E readgg: truncated graph6 line\n");
if (readg_code == DIGRAPH6 && p - s != D6LEN(n))
gt_abort(">E readgg: truncated digraph6 line\n");
if (reqm > 0 && TIMESWORDSIZE(reqm) < n)
gt_abort(">E readgg: reqm too small\n");
else if (reqm > 0)
m = reqm;
else
m = (n + WORDSIZE - 1) / WORDSIZE;
if (g == NULL)
{
if ((g = (graph*)ALLOCS(n,m*sizeof(graph))) == NULL)
gt_abort(">E readgg: malloc failed\n");
}
*pn = n;
*pm = m;
stringtograph(s,g,m);
return g;
}
/***********************************************************************/
graph* /* read undirected graph into nauty format */
readg(FILE *f, graph *g, int reqm, int *pm, int *pn)
/* graph6 and sparse6 formats are supported
f = an open file
g = place to put the answer (NULL for dynamic allocation)
reqm = the requested value of m (0 => compute from n)
*pm = the actual value of m
*pn = the value of n
Only allows undirected graphs.
*/
{
boolean digraph;
graph *gg;
gg = readgg(f,g,reqm,pm,pn,&digraph);
if (!gg) return NULL;
if (digraph)
gt_abort(">E readg() doesn't know digraphs; use readgg()\n");
return gg;
}
/***********************************************************************/