-
Notifications
You must be signed in to change notification settings - Fork 0
/
naututil.c
3193 lines (2844 loc) · 102 KB
/
naututil.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
/*****************************************************************************
* *
* miscellaneous utilities for use with nauty 2.7. *
* None of these procedures are needed by nauty, but all are by dreadnaut. *
* *
* Copyright (1984-2017) Brendan McKay. All rights reserved. *
* Subject to waivers and disclaimers in nauty.h. *
* *
* CHANGE HISTORY *
* 10-Nov-87 : final changes for version 1.2 *
* 5-Dec-87 : changes made for version 1.3 : *
* - added procedures readinteger() and readstring() *
* - replaced all uses of fscanf() by appropriate uses *
* of readinteger() or readstring() *
* - "N:" is now illegal in readgraph() if N is too large *
* or too small *
* 28-Sep-88 : renamed to version 1.4 (no changes to this file) *
* 23-Mar-89 : changes for version 1.5 : *
* - declared key in hash() *
* - changed file name to naututil.c *
* 29-Mar-89 : - declared workperm[] and workset[], and modified *
* many routines to use them. *
* - added putmapping() *
* - reworked some code in mathon() and rangraph() *
* 3-Apr-89 : - changed putorbits() to use O(n) algorithm *
* 5-Apr-89 : - modifed readgraph() to not require fresh line *
* - changed MAKEEMPTY uses to EMPTYSET uses *
* 26-Apr-89 : - moved ptncode() and equitable() to nautaux.c *
* - added putquotient() *
* 18-Aug-89 : - modified putset() to use "i:j" syntax optionally *
* - modified putorbits() to use putset() *
* - modified calling sequence for mathon() *
* 19-Aug-90 : - changed delimeter arg of copycomment to int *
* 14-Oct-90 : renamed to version 1.6 (no changes to this file) *
* 23-Jan-91 : changes for version 1.7 : *
* - fixed bug in complement() *
* 27-Aug-92 : - made linelength <= 0 mean no line breaks *
* 5-Jun-93 : renamed to version 1.7+ (no changes to this file) *
* 18-Aug-93 : renamed to version 1.8 (no changes to this file) *
* 17-Sep-93 : renamed to version 1.9 (no changes to this file) *
* 13-Jul-96 : changes for version 2.0 : *
* - added dynamic allocation *
* - added limit parameter to readstring *
* - added readvperm() and sublabel() *
* 31-Aug-96 : - converted from RAN to KRAN *
* 6-Feb-97 : - corrected DYNALLOC1 call in putmapping *
* 10-Dec-97 : - KRAN now initialises automatically *
* 9-Jan-00 : - added naututil_check() *
* 12-Feb-00 : - some minor code formatting *
* 16-Nov-00 : - changes as listed in nauty.h *
* 23-Apr-01 : changes for version 2.1 : *
* - removed EXTDEFS *
* 2-Jun-01 : - added converse() *
* 21-Nov-01 : use NAUTYREQUIRED in naututil_check() *
* 11-Apr-03 : changes for version 2.2 : *
* - added rangraph2() *
* 17-Nov-03 : changed INFINITY to NAUTY_INFINITY *
* 10-Dec-06 : removed BIGNAUTY *
* 4-Nov-09 : added readgraph_sg, putgraph_sg, putcanon_sg *
* 10-Nov-09 : removed shortish and permutation types *
* 14-Nov-09 : added relabel_sg(), putdegs_sg(), sublabel_sg() *
* 19-Nov-09 : added individualise() *
* 20-Nov-09 : added hashgraph_sg(), listhash(), hashgraph() *
* 19-Dec-09 : added ranreg_sg(), rangraph2_sg() *
* 21-May-10 : conform to type changes in sparsegraph *
* 5-Jun-10 : add mathon_sg() *
* 10-Jun-10 : add putquotient_sg() and complement_sg() *
* 26-Jan-11 : fix nde error in sublabel_sg() *
* 15-Jan-12 : add TLS_ATTR attributes *
* 3-Mar-12 : add putorbitsplus() and putset_firstbold() *
* : write orbit sizes if not trivial *
* 17-Mar-12 : move seed definition to naututil.h *
* 20-Sep-12 : allow quoted strings in readstring() *
* 20-Sep-12 : the first argument of ungetc is int, not char *
* 4-Mar-13 : remove a side-effect issue in setinter() *
* 17-Dec-15 : add readgraph_swg() and update putgraph_sg() *
* 22-Jan-16 : add readintger_sl() and getint_sl() *
* 29-Feb-16 : add subpartition() *
* 6-Apr-16 : add countcells(), make subpartition return a count *
* *
*****************************************************************************/
#define ONE_WORD_SETS
#include "naututil.h" /* which includes nauty.h, nautinv.h and stdio.h */
#include "nausparse.h"
#if MAXM==1
#define M 1
#else
#define M m
#endif
#if !MAXN
DYNALLSTAT(int,workperm,workperm_sz);
DYNALLSTAT(set,workset,workset_sz);
#else
static TLS_ATTR int workperm[MAXN+2]; /* used for scratch purposes */
static TLS_ATTR set workset[MAXM]; /* used for scratch purposes */
#endif
#define ECHUNKSIZE 1000 /* should be a multiple of 2 */
typedef struct echunk {struct echunk *next; int edge[ECHUNKSIZE];} echunk;
static TLS_ATTR echunk first_echunk = {NULL,{0}};
typedef struct echunkw {struct echunkw *next; \
struct {int v1,v2; sg_weight wt;} edge[ECHUNKSIZE];} echunkw;
static TLS_ATTR echunkw first_echunkw = {NULL,{{0,0,0}}};
#ifdef NLMAP
#define GETNW(c,f) do c = getc(f); while (c==' '||c=='\t')
#define GETNWC(c,f) do c = getc(f); while (c==' '||c==','||c=='\t')
#define GETNWL(c,f) do c = getc(f); while (c==' '||c=='\n'||c=='\t')
#else
#define GETNW(c,f) do c = getc(f); while (c==' '||c=='\t'||c=='\r')
#define GETNWC(c,f) do c = getc(f); while (c==' '||c==','||c=='\t'||c=='\r')
#define GETNWL(c,f) do c = getc(f); while (c==' '||c=='\n'||c=='\t'||c=='\r')
#endif
#define ISDIGIT(c) ((c) >= '0' && (c) <= '9')
static const long fuzz1[] = {1984625421L, 971524688L,1175081625L, 377165387L};
static const long fuzz2[] = {2001381726L,1615243355L, 191176436L,1212176501L};
#define FUZZ1(x) ((x) ^ fuzz1[(x)&3L])
#define FUZZ2(x) ((x) ^ fuzz2[(x)&3L])
#define SORT_OF_SORT 1
#define SORT_NAME sort1int
#define SORT_TYPE1 int
#include "sorttemplates.c" /* define sort1int(a,n) */
/*****************************************************************************
* *
* setinter(set1,set2,m) = the number of elements in the intersection of *
* the sets set1 and set2. *
* *
*****************************************************************************/
int
setinter(set *set1, set *set2, int m)
{
setword x;
#if MAXM==1
if ((x = *set1 & *set2) != 0) return POPCOUNT(x);
else return 0;
#else
int count,i;
count = 0;
for (i = m; --i >= 0;)
{
if ((x = (*set1 & *set2)) != 0) count += POPCOUNT(x);
++set1;
++set2;
}
return count;
#endif
}
/*****************************************************************************
* *
* setsize(set1,m) = the number of elements in the set set1. *
* *
*****************************************************************************/
int
setsize(set *set1, int m)
{
#if MAXM==1
if (set1 != 0) return POPCOUNT(*set1);
else return 0;
#else
int count,i;
setword x;
count = 0;
for (i = m; --i >= 0;)
if ((x = *set1++) != 0) count += POPCOUNT(x);
return count;
#endif
}
/*****************************************************************************
* *
* flushline(f) reads from f until '\n' or EOF. *
* If non-trivial characters are skipped, the user is informed. *
* *
*****************************************************************************/
void
flushline(FILE *f)
{
boolean msg;
int c;
msg = FALSE;
while ((c = getc(f)) != EOF && c != '\n')
if (msg)
PUTC((char)c,ERRFILE);
else if (c != ' ' && c != '\t' && c != '\f' &&
c != '\r' && c != ',')
{
msg = TRUE;
fprintf(ERRFILE,"input skipped : '%c",(char)c);
}
if (msg) fprintf(ERRFILE,"'\n\n");
}
/*****************************************************************************
* *
* readinteger(f,&i) reads an optionally-signed integer from f, preceded by *
* any amount of white space. The function value returned is TRUE if an *
* integer was found, FALSE otherwise. *
* *
*****************************************************************************/
boolean
readinteger(FILE *f, int *p)
{
int c,ans,minus;
GETNWL(c,f);
if (!ISDIGIT(c) && c != '-' && c != '+')
{
if (c != EOF) ungetc(c,f);
return FALSE;
}
minus = c == '-';
ans = (c == '-' || c == '+' ? 0 : c - '0');
c = getc(f);
while (ISDIGIT(c))
{
ans *= 10;
ans += c - '0';
c = getc(f);
}
if (c != EOF) ungetc(c,f);
*p = (minus ? -ans : ans);
return TRUE;
}
/*****************************************************************************
* *
* readinteger_sl(f,&i) reads an optionally-signed integer from f, preceded *
* by any amount of white space except newlines. The function value *
* returned is TRUE if an integer was found, FALSE otherwise. *
* *
*****************************************************************************/
boolean
readinteger_sl(FILE *f, int *p)
{
int c,ans,minus;
GETNW(c,f);
if (!ISDIGIT(c) && c != '-' && c != '+')
{
if (c != EOF) ungetc(c,f);
return FALSE;
}
minus = c == '-';
ans = (c == '-' || c == '+' ? 0 : c - '0');
c = getc(f);
while (ISDIGIT(c))
{
ans *= 10;
ans += c - '0';
c = getc(f);
}
if (c != EOF) ungetc(c,f);
*p = (minus ? -ans : ans);
return TRUE;
}
/*****************************************************************************
* *
* readstring(f,s,slen) reads a string from f. First any amount of white *
* space is skipped (including newlines). If the next character is a *
* double-quote, everything after that before the next double-quote or *
* newline is put into s. If the next character is not a double-quote, *
* everything before the next white space is put into s. A nul is added, *
* but no more than slen characters are ever put into s. The function *
* value is TRUE if a string was found and FALSE otherwise. *
* *
*****************************************************************************/
boolean
readstring(FILE *f, char *s, int slen)
{
int c;
char *slim;
slim = s + slen - 1;
GETNWL(c,f);
if (c == EOF)
{
*s = '\0';
return FALSE;
}
if (c == '"')
{
c = getc(f);
while (c != '"' && c != '\n' && c != '\r' && c != EOF)
{
if (s <= slim) *s++ = (char)c;
c = getc(f);
}
if (c != '"' && c != EOF) ungetc(c,f);
}
else
{
if (s <= slim) *s++ = (char)c;
c = getc(f);
while (c != ' ' && c != '\n' && c != '\t' && c != '\r' && c != EOF)
{
if (s <= slim) *s++ = (char)c;
c = getc(f);
}
if (c != EOF) ungetc(c,f);
}
if (s <= slim) *s = '\0';
else *slim = '\0';
return TRUE;
}
/*****************************************************************************
* *
* getint(f) reads an integer from f, optionally preceded by '=' *
* and white space. -1 is returned if the attempt was unsuccessful. *
* *
*****************************************************************************/
int
getint(FILE *f)
{
int i,c;
GETNWL(c,f);
if (c != '=') ungetc(c,f);
if (readinteger(f,&i)) return i;
else return -1;
}
/*****************************************************************************
* *
* getint_sl(f) reads an integer from f, optionally preceded by '=' and *
* white space other than newlines. -1 is returned if the attempt was *
* unsuccessful. *
*****************************************************************************/
int
getint_sl(FILE *f)
{
int i,c;
GETNW(c,f);
if (c != '=') ungetc(c,f);
if (readinteger_sl(f,&i)) return i;
else return -1;
}
/*****************************************************************************
* *
* putset(f,set1,curlenp,linelength,m,compress) writes the set set1 to *
* file f using at most linelength characters per line (excluding '\n'). *
* A value of linelength <= 0 dictates no line breaks at all. *
* *curlenp is the number of characters on the line so far; it is updated. *
* A range j1,j1+1,...,j2 for j2-j1>=2 is written as "j1:j2" if compress *
* is nonzero (eg. TRUE); otherwise each element is written separately. *
* No final '\n' is written. labelorg is used. *
* *
* FUNCTIONS CALLED: nextelement(),itos() *
* *
*****************************************************************************/
void
putset(FILE *f, set *set1, int *curlenp, int linelength,
int m, boolean compress)
{
int slen,j1,j2;
char s[40];
j1 = -1;
while ((j1 = nextelement(set1,m,j1)) >= 0)
{
j2 = j1;
if (compress)
{
while (nextelement(set1,m,j2) == j2 + 1) ++j2;
if (j2 == j1+1) j2 = j1;
}
slen = itos(j1+labelorg,s);
if (j2 >= j1 + 2)
{
s[slen] = ':';
slen += 1 + itos(j2+labelorg,&s[slen+1]);
}
if (linelength > 0 && *curlenp + slen + 1 >= linelength)
{
fprintf(f,"\n ");
*curlenp = 3;
}
fprintf(f," %s",s);
*curlenp += slen + 1;
j1 = j2;
}
}
/*****************************************************************************
* *
* putset_firstbold(f,set1,curlenp,linelength,m,compress) is the same as *
* putset(f,set1,curlenp,linelength,m,compress) except that the first *
* element of the set is written bold. This is only useful when output is *
* to a device that interprets ANSI control sequences. *
* *
* FUNCTIONS CALLED: nextelement(),itos() *
* *
*****************************************************************************/
void
putset_firstbold(FILE *f, set *set1, int *curlenp, int linelength,
int m, boolean compress)
{
int slen,slen1,j1,j2;
char s[50],c;
boolean bold;
bold = TRUE;
j1 = -1;
while ((j1 = nextelement(set1,m,j1)) >= 0)
{
j2 = j1;
if (compress)
{
while (nextelement(set1,m,j2) == j2 + 1) ++j2;
if (j2 == j1+1) j2 = j1;
}
slen1 = slen = itos(j1+labelorg,s);
if (j2 >= j1 + 2)
{
s[slen] = ':';
slen += 1 + itos(j2+labelorg,&s[slen+1]);
}
c = s[slen1];
if (linelength > 0 && *curlenp + slen + 1 >= linelength)
{
fprintf(f,"\n ");
*curlenp = 3;
}
if (bold)
{
s[slen1] = '\0';
fprintf(f," \033[1m%s\033[0m",s);
s[slen1] = c;
fprintf(f,"%s",&s[slen1]);
bold = FALSE;
}
else
fprintf(f," %s",s);
*curlenp += slen + 1;
j1 = j2;
}
}
/*****************************************************************************
* *
* readgraph(f,g,digraph,prompt,edit,linelength,m,n) reads a graph g from f. *
* Commands: (There is always a "current vertex" v, initially labelorg; *
* n is an unsigned integer.) *
* n : add edge (v,n) *
* -n : delete edge (v,n) *
* n: : set v := n, and exit if v >= n. *
* ? : type neighbours of vertex v *
* ; : increment v, and exit if v >= n. *
* . : exit *
* ! : skip rest of input line *
* *
* If digraph==FALSE, loops are illegal and (x,y) => (y,x) *
* If edit==FALSE, the graph is initialized to empty. *
* If prompt==TRUE, prompts are written to PROMPTFILE. *
* linelength is a limit on the number of characters per line caused by '?' *
* A value of linelength <= 0 dictates no line breaks at all. *
* labelorg is used. *
* *
* FUNCTIONS CALLED : putset() *
* *
*****************************************************************************/
void
readgraph(FILE *f, graph *g, boolean digraph, boolean prompt,
boolean edit, int linelength, int m, int n)
{
int v,c;
int curlen,w;
graph *gv;
boolean neg;
if (!edit)
for (v = 0, gv = g; v < n; ++v, gv += M) EMPTYSET(gv,m);
v = 0;
gv = g;
neg = FALSE;
while (TRUE)
{
GETNWC(c,f);
if (ISDIGIT(c))
{
ungetc(c,f);
readinteger(f,&w);
w -= labelorg;
if (neg)
{
neg = FALSE;
if (w < 0 || w >= n || (!digraph && w == v))
fprintf(ERRFILE,"illegal edge (%d,%d) ignored\n\n",
v+labelorg,w+labelorg);
else
{
DELELEMENT(gv,w);
if (!digraph) DELELEMENT(GRAPHROW(g,w,M),v);
}
}
else
{
GETNWC(c,f);
if (c == ':')
if (w < 0 || w >= n)
fprintf(ERRFILE,
"illegal vertex number %d ignored\n\n",
w+labelorg);
else
{
v = w;
gv = GRAPHROW(g,v,M);
}
else
{
ungetc(c,f);
if (w < 0 || w >= n || (!digraph && w == v))
fprintf(ERRFILE,"illegal edge (%d,%d) ignored\n\n",
v+labelorg,w+labelorg);
else
{
ADDELEMENT(gv,w);
if (!digraph) ADDELEMENT(GRAPHROW(g,w,M),v);
}
}
}
}
else switch(c)
{
case ';':
neg = FALSE;
++v;
if (v >= n) return;
gv = GRAPHROW(g,v,M);
break;
case '?':
neg = FALSE;
fprintf(PROMPTFILE,"%2d : ",v+labelorg);
curlen = 5;
putset(PROMPTFILE,gv,&curlen,linelength,M,FALSE);
fprintf(PROMPTFILE,";\n");
break;
case '\n':
neg = FALSE;
if (prompt) fprintf(PROMPTFILE,"%2d : ",v+labelorg);
break;
case EOF:
case '.':
return;
case '-':
neg = TRUE;
break;
case '!':
do
c = getc(f);
while (c != '\n' && c != EOF);
if (c == '\n') ungetc(c,f);
break;
default :
fprintf(ERRFILE,"illegal char '%c' - use '.' to exit\n\n",
(char)c);
}
}
}
/**************************************************************************/
void
ranreg_sg(sparsegraph *sg, int degree, int n)
/* Make a random regular simple undirected graph.
* For MAXN!=0, the maximum degree is MAXREG.
* sg must be initialised
*/
{
long i,k,v,w;
boolean ok;
int *dd,*ee;
size_t *vv,nde,j;
#if MAXN
int p[MAXREG*MAXN];
#else
DYNALLSTAT(int,p,p_sz);
DYNALLOC2(int,p,p_sz,degree,n,"genrang");
#endif
nde = (size_t)n * (size_t)degree;
SG_ALLOC(*sg,n,nde,"ranreg_sg");
SG_VDE(sg,vv,dd,ee);
DYNFREE(sg->w,sg->wlen);
sg->nv = n;
sg->nde = nde;
for (i = j = 0; i < n; ++i)
for (k = 0; k < degree; ++k) p[j++] = i;
for (i = 0; i < n; ++i) vv[i] = i*(size_t)degree;
do
{
ok = TRUE;
for (j = nde; j > 0; j -= 2)
{
i = KRAN(j-1);
k = p[i];
if (k == p[j-1]) break;
p[i] = p[j-2]; p[j-2] = k;
}
if (j > 0) { ok = FALSE; continue; }
for (i = 0; i < n; ++i) dd[i] = 0;
for (j = nde; j > 0; )
{
v = p[--j];
w = p[--j];
if (v != w)
{
for (i = dd[w]; --i >= 0;) if (ee[vv[w]+i] == v) break;
if (i >= 0) { ok = FALSE; break; }
}
ee[vv[w]+(dd[w]++)] = v;
ee[vv[v]+(dd[v]++)] = w;
}
}
while (!ok);
}
/*****************************************************************************
* *
* readgraph_sg(f,sg,digraph,prompt,linelength,n) reads a graph g from f. *
* Commands: (There is always a "current vertex" v, initially labelorg; *
* n is an unsigned integer.) *
* n : add edge (v,n) *
* -n : delete edge (v,n) *
* n: : set v := n, and exit if v >= n. *
* ? : type neighbours of vertex v ** NOT IMPLEMENTED ** *
* ; : increment v, and exit if v >= n. *
* . : exit *
* ! : skip rest of input line *
* sg must be initialised *
* *
* If digraph==FALSE, loops are illegal and (x,y) => (y,x) *
* If prompt==TRUE, prompts are written to PROMPTFILE. *
* linelength is a limit on the number of characters per line caused by '?' *
* A value of linelength <= 0 dictates no line breaks at all. *
* labelorg is used. *
* *
*****************************************************************************/
void
readgraph_sg(FILE *f, sparsegraph *sg, boolean digraph, boolean prompt,
int linelength, int n)
{
int i,j,k,vv,ww,c;
boolean neg,done;
int *d,*e,*evi;
echunk *ec,*ecnext,*ec_end;
size_t ned,*v,iec,iec_end;
sg->nv = n;
DYNALLOC1(size_t,sg->v,sg->vlen,n,"malloc");
DYNALLOC1(int,sg->d,sg->dlen,n,"malloc");
DYNFREE(sg->w,sg->wlen);
v = sg->v;
d = sg->d;
for (i = 0; i < n; ++i) d[i] = 0;
ec = &first_echunk;
iec = 0;
vv = 0;
neg = done = FALSE;
while (!done)
{
GETNWC(c,f);
if (ISDIGIT(c))
{
ungetc(c,f);
readinteger(f,&ww);
ww -= labelorg;
if (neg)
{
neg = FALSE;
if (ww < 0 || ww >= n || (!digraph && ww == vv))
fprintf(ERRFILE,"illegal edge (%d,%d) ignored\n\n",
vv+labelorg,ww+labelorg);
else
{
if (iec == ECHUNKSIZE)
{
if (!ec->next)
{
ecnext = (echunk*)ALLOCS(1,sizeof(echunk));
if (!ecnext) alloc_error("malloc");
ecnext->next = NULL;
ec->next = ecnext;
}
ec = ec->next;
iec = 0;
}
ec->edge[iec++] = vv;
ec->edge[iec++] = -1 - ww;
++d[vv];
if (!digraph && ww != vv) ++d[ww];
}
}
else
{
GETNWC(c,f);
if (c == ':')
{
if (ww < 0 || ww >= n)
fprintf(ERRFILE,
"illegal vertex number %d ignored\n\n",
ww+labelorg);
else
vv = ww;
}
else
{
ungetc(c,f);
if (ww < 0 || ww >= n || (!digraph && ww == vv))
fprintf(ERRFILE,"illegal edge (%d,%d) ignored\n\n",
vv+labelorg,ww+labelorg);
else
{
if (iec == ECHUNKSIZE)
{
if (!ec->next)
{
ecnext = (echunk*)ALLOCS(1,sizeof(echunk));
if (!ecnext) alloc_error("malloc");
ecnext->next = NULL;
ec->next = ecnext;
}
ec = ec->next;
iec = 0;
}
ec->edge[iec++] = vv;
ec->edge[iec++] = ww;
++d[vv];
if (!digraph && ww != vv) ++d[ww];
}
}
}
}
else switch(c)
{
case ';':
neg = FALSE;
++vv;
if (vv >= n) done = TRUE;
break;
case '?':
neg = FALSE;
fprintf(ERRFILE,"Command \'?\' not implemented.\n\n");
break;
case '\n':
neg = FALSE;
if (prompt) fprintf(PROMPTFILE,"%2d : ",vv+labelorg);
break;
case EOF:
case '.':
done = TRUE;
break;
case '-':
neg = TRUE;
break;
case '!':
do
c = getc(f);
while (c != '\n' && c != EOF);
if (c == '\n') ungetc(c,f);
break;
default :
fprintf(ERRFILE,"illegal char '%c' - use '.' to exit\n\n",
(char)c);
}
}
ned = 0;
for (i = 0; i < n; ++i) ned += d[i];
DYNALLOC1(int,sg->e,sg->elen,ned,"malloc");
e = sg->e;
v[0] = 0;
for (i = 1; i < n; ++i) v[i] = v[i-1] + d[i-1];
for (i = 0; i < n; ++i) d[i] = 0;
iec_end = iec;
ec_end = ec;
iec = 0;
ec = &first_echunk;
if (ned != 0) while (TRUE)
{
vv = ec->edge[iec++];
ww = ec->edge[iec++];
if (ww >= 0)
{
e[v[vv]+(d[vv]++)] = ww;
if (!digraph && ww != vv) e[v[ww] +(d[ww]++)] = vv;
}
else
{
ww = -1 - ww;
for (i = 0; i < d[vv]; ++i)
if (e[v[vv]+i] == ww) break;
if (i < d[vv])
{
e[v[vv]+i] = e[v[vv]+d[vv]-1];
--d[vv];
}
if (!digraph && ww != vv)
{
for (i = 0; i < d[ww]; ++i)
if (e[v[ww]+i] == vv) break;
if (i < d[ww])
{
e[v[ww]+i] = e[v[ww]+d[ww]-1];
--d[ww];
}
}
}
if (iec == iec_end && ec == ec_end) break;
if (iec == ECHUNKSIZE) { iec = 0; ec = ec->next; }
}
sortlists_sg(sg);
ned = 0;
for (i = 0; i < n; ++i)
{
if (d[i] > 1)
{
evi = e + v[i];
j = 1;
for (k = 1; k < d[i]; ++k)
if (evi[k] != evi[j-1]) evi[j++] = evi[k];
d[i] = j;
}
ned += d[i];
}
sg->nde = ned;
}
/*****************************************************************************
* *
* readgraph_swg(f,sg,digraph,prompt,linelength,n) reads a sparse weighted *
* graph g from f. *
* Commands: (There is always a "current vertex" v, initially labelorg; *
* n is an unsigned integer, w is a weight.) *
* n : add edge (v,n) *
* -n : delete edge (v,n) *
* n: : set v := n, and exit if v >= n. *
* ? : type neighbours of vertex v ** NOT IMPLEMENTED ** *
* ; : increment v, and exit if v >= n. *
* . : exit *
* ! : skip rest of input line *
* w# : set the weight for the next edge only *
* W# : set the weight from now on *
* sg must be initialised *
* *
* If digraph==FALSE, loops are illegal and (x,y) => (y,x) *
* For digraphs, an unspecified opposite edge has weight SG_MINWEIGHT *
* If edges are inserted more than once, the largest weight counts. *
* If prompt==TRUE, prompts are written to PROMPTFILE. *
* linelength is a limit on the number of characters per line caused by '?' *
* A value of linelength <= 0 dictates no line breaks at all. *
* labelorg is used. *
* *
*****************************************************************************/
void
readgraph_swg(FILE *f, sparsegraph *sg, boolean digraph, boolean prompt,
int linelength, int n)
{
int i,j,k,vv,ww,c;
boolean neg,done;
int *d,*e,*evi;
echunkw *ec,*ecnext,*ec_end;
size_t ned,*v,iec,iec_end;
sg_weight *wt,currwt,defwt,*wvi;
sg->nv = n;
DYNALLOC1(size_t,sg->v,sg->vlen,n,"malloc");
DYNALLOC1(int,sg->d,sg->dlen,n,"malloc");
v = sg->v;
d = sg->d;
wt = sg->w;
for (i = 0; i < n; ++i) d[i] = 0;
defwt = currwt = 1;
ec = &first_echunkw;
iec = 0;
vv = 0;
neg = done = FALSE;
while (!done)
{
GETNWC(c,f);
if (ISDIGIT(c))
{
ungetc(c,f);
readinteger(f,&ww);
ww -= labelorg;
if (neg)
{
neg = FALSE;
if (ww < 0 || ww >= n || (!digraph && ww == vv))
fprintf(ERRFILE,"illegal edge (%d,%d) ignored\n\n",
vv+labelorg,ww+labelorg);
else
{
if (iec == ECHUNKSIZE)
{
if (!ec->next)
{
ecnext = (echunkw*)ALLOCS(1,sizeof(echunkw));
if (!ecnext) alloc_error("malloc");
ecnext->next = NULL;
ec->next = ecnext;
}
ec = ec->next;
iec = 0;
}
ec->edge[iec].v1 = vv;
ec->edge[iec].v2 = -1 - ww;
ec->edge[iec].wt = currwt;
++iec;
currwt = defwt;
++d[vv];
if (ww != vv) ++d[ww];
}
}
else
{
GETNWC(c,f);
if (c == ':')
{
if (ww < 0 || ww >= n)
fprintf(ERRFILE,
"illegal vertex number %d ignored\n\n",
ww+labelorg);
else
vv = ww;
}
else
{