-
Notifications
You must be signed in to change notification settings - Fork 66
Expand file tree
/
Copy pathio.gi
More file actions
2604 lines (2334 loc) · 71.1 KB
/
io.gi
File metadata and controls
2604 lines (2334 loc) · 71.1 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
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
#############################################################################
##
## io.gi
## Copyright (C) 2014-19 James D. Mitchell
##
## Licensing information can be found in the README file of this package.
##
#############################################################################
##
#############################################################################
## This file contains methods for reading and writing digraphs from and to
## files.
##
## It is organized as follows:
##
## 0. Internal functions
##
## 1. Picklers for IO
##
## 2. Read/WriteDigraphs (the main functions)
##
## 3. Decoders
##
## 4. Encoders
##
#############################################################################
#############################################################################
# 0. Internal functions
#############################################################################
BindGlobal("DIGRAPHS_SplitStringBySubstring",
function(string, substring)
local m, n, i, j, out, nr;
if Length(string) = 0 then
return [];
elif Length(substring) = 1 then
return SplitString(string, substring);
fi;
m := Length(string);
n := Length(substring);
i := 1;
j := 1;
out := [];
nr := 0;
while m - i >= n do
if string{[i .. i + n - 1]} = substring then
if i <> 1 then
nr := nr + 1;
out[nr] := string{[j .. i - 1]};
j := i + n;
i := i + n;
fi;
else
i := i + 1;
fi;
od;
nr := nr + 1;
out[nr] := string{[j .. Length(string)]};
return out;
end);
BindGlobal("DIGRAPHS_Graph6Length",
function(n)
local list;
list := [];
if n < 0 then
return fail;
elif n < 63 then
Add(list, n);
elif n < 258248 then
Add(list, 63);
Add(list, Int(n / 64 ^ 2));
Add(list, Int(n / 64) mod 64);
Add(list, n mod 64);
elif n < 68719476736 then
Add(list, 63);
Add(list, 63);
Add(list, Int(n / 64 ^ 5));
Add(list, Int(n / 64 ^ 4) mod 64);
Add(list, Int(n / 64 ^ 3) mod 64);
Add(list, Int(n / 64 ^ 2) mod 64);
Add(list, Int(n / 64 ^ 1) mod 64);
Add(list, n mod 64);
else
return fail;
fi;
return list;
end);
################################################################################
# 1. Picklers
################################################################################
InstallMethod(IO_Pickle, "for a digraph with known digraph group",
[IsFile, IsDigraph and HasDigraphGroup],
function(file, D)
local g, out;
g := DigraphGroup(D);
if IsTrivial(g) then
TryNextMethod();
elif IO_Write(file, "DIGG") = fail then
return IO_Error;
fi;
out := [GeneratorsOfGroup(g),
RepresentativeOutNeighbours(D),
DigraphSchreierVector(D)];
return IO_Pickle(file, out);
end);
IO_Unpicklers.DIGG := function(file)
local list, gens, rep_out, sch, out, trace, word, D, i, w;
list := IO_Unpickle(file);
if list = IO_Error then
return IO_Error;
fi;
gens := list[1];
rep_out := list[2];
sch := list[3];
out := [];
for i in [1 .. Length(sch)] do
if sch[i] < 0 then
out[i] := rep_out[-sch[i]];
fi;
trace := DIGRAPHS_TraceSchreierVector(gens, sch, i);
out[i] := rep_out[trace.representative];
word := trace.word;
for w in word do
out[i] := OnTuples(out[i], gens[w]);
od;
od;
D := ConvertToImmutableDigraphNC(out);
SetDigraphGroup(D, Group(gens));
SetDigraphSchreierVector(D, sch);
SetRepresentativeOutNeighbours(D, rep_out);
return D;
end;
InstallMethod(IO_Pickle, "for a digraph",
[IsFile, IsDigraph],
function(file, D)
if IO_Write(file, "DIGT") = fail then
return IO_Error;
fi;
return IO_Pickle(file, OutNeighbours(D));
end);
IO_Unpicklers.DIGT := function(file)
local out;
out := IO_Unpickle(file);
if out = IO_Error then
return IO_Error;
fi;
return ConvertToImmutableDigraphNC(out);
end;
################################################################################
# 2. ReadDigraphs and WriteDigraphs
################################################################################
InstallGlobalFunction(IteratorFromDigraphFile,
function(arg...)
local filename, decoder, file, record;
if Length(arg) = 1 then
filename := arg[1];
decoder := fail;
elif Length(arg) = 2 then
filename := arg[1];
decoder := arg[2];
else
ErrorNoReturn("there must be 1 or 2 arguments,");
fi;
if not IsString(filename) then
ErrorNoReturn("the 1st argument must be a string,");
elif decoder <> fail and not IsFunction(decoder) then
ErrorNoReturn("the 2nd argument must be a function or fail,");
fi;
file := DigraphFile(UserHomeExpand(filename), decoder, "r");
record := rec(file := file, current := file!.coder(file));
record.NextIterator := function(iter)
local next;
next := iter!.current;
iter!.current := iter!.file!.coder(iter!.file);
return next;
end;
record.IsDoneIterator := function(iter)
if iter!.current = IO_Nothing then
if not iter!.file!.closed then
IO_Close(iter!.file);
fi;
return true;
else
return false;
fi;
end;
# TODO store filename, decoder in iter?
record.ShallowCopy := function(_)
local file;
file := DigraphFile(UserHomeExpand(filename), decoder, "r");
return rec(file := file, current := file!.coder(file));
end;
return IteratorByFunctions(record);
end);
BindGlobal("WholeFileDecoders", HashSet());
AddSet(WholeFileDecoders, "DigraphFromDreadnautString");
AddSet(WholeFileDecoders, "DigraphFromDIMACSString");
BindGlobal("WholeFileEncoders", HashSet());
AddSet(WholeFileEncoders, "DreadnautString");
AddSet(WholeFileEncoders, "DIMACSString");
InstallGlobalFunction(IsWholeFileDecoder,
decoder -> NameFunction(decoder) in WholeFileDecoders);
InstallGlobalFunction(IsWholeFileEncoder,
encoder -> NameFunction(encoder) in WholeFileEncoders);
# these functions wrap the various line encoders/decoders in this file so that
# they behave like IO_Pickle.
BindGlobal("DIGRAPHS_EncoderWrapper",
function(encoder)
if encoder = IO_Pickle or NameFunction(encoder) in WholeFileEncoders then
return encoder;
elif NameFunction(encoder) = "WriteDIMACSDigraph" then
return DIMACSString; # edge case (legacy function)
fi;
return {file, D} -> IO_WriteLine(file, encoder(D));
end);
BindGlobal("DIGRAPHS_DecoderWrapper",
function(decoder)
if decoder = IO_Unpickle or NameFunction(decoder) in WholeFileDecoders then
return decoder;
elif NameFunction(decoder) = "ReadDIMACSDigraph" then
return DigraphFromDIMACSString; # edge case (legacy function)
fi;
return
function(file)
local line;
line := IO_ReadLine(file);
if line = "" then
return IO_Nothing;
fi;
return decoder(line);
end;
end);
# if we are choosing the decoder, then the file extension is used.
BindGlobal("DIGRAPHS_ChooseFileDecoder",
function(filename)
local splitname, extension;
if not IsString(filename) then
ErrorNoReturn("the argument <filename> must be a string,");
fi;
splitname := SplitString(filename, ".");
extension := Remove(splitname);
if extension in ["gz", "bz2", "xz"] then
extension := Remove(splitname);
fi;
if extension = "txt" then
return DigraphPlainTextLineDecoder(" ", " ", 1);
elif extension = "g6" then
return DigraphFromGraph6String;
elif extension = "s6" then
return DigraphFromSparse6String;
elif extension = "d6" then
return DigraphFromDigraph6String;
elif extension = "ds6" then
return DigraphFromDiSparse6String;
elif extension = "p" or extension = "pickle" then
return IO_Unpickle;
elif extension = "dre" then
return DigraphFromDreadnautString;
elif extension = "dimacs" then
return DigraphFromDIMACSString;
fi;
return fail;
end);
# if we are choosing the decoder, then the file extension is used.
BindGlobal("DIGRAPHS_ChooseFileEncoder",
function(filename)
local splitname, extension;
if not IsString(filename) then
ErrorNoReturn("the argument <filename> must be a string,");
fi;
splitname := SplitString(filename, ".");
extension := Remove(splitname);
if extension in ["gz", "bz2", "xz"] then
extension := Remove(splitname);
fi;
if extension = "txt" then
return DigraphPlainTextLineEncoder(" ", " ", -1);
elif extension = "g6" then
return Graph6String;
elif extension = "s6" then
return Sparse6String;
elif extension = "d6" then
return Digraph6String;
elif extension = "ds6" then
return DiSparse6String;
elif extension = "p" or extension = "pickle" then
return IO_Pickle;
elif extension = "dre" then
return DreadnautString;
elif extension = "dimacs" then
return DIMACSString;
fi;
return fail;
end);
InstallGlobalFunction(DigraphFile,
function(arg...)
local coder, mode, name, file;
# defaults
coder := fail;
mode := "r";
if Length(arg) = 1 then
name := arg[1];
elif Length(arg) = 2 then
name := arg[1];
if IsString(arg[2]) then
mode := arg[2];
else
coder := arg[2];
fi;
elif Length(arg) = 3 then
name := arg[1];
coder := arg[2];
mode := arg[3];
else
ErrorNoReturn("there must be 1, 2, or 3 arguments,");
fi;
# TODO check that the mode and the coder are compatible
if not IsString(name) then
ErrorNoReturn("the 1st argument <name> must be a string,");
elif not (IsFunction(coder) or coder = fail) then
ErrorNoReturn("the 2nd argument <coder> must be a function or fail,");
elif not mode in ["a", "w", "r"] then
ErrorNoReturn("the 3rd argument <mode> must be one of \"a\", ",
"\"w\", or \"r\"");
fi;
if coder = fail then # <coder> not specified by the user
if mode = "r" then
coder := DIGRAPHS_ChooseFileDecoder(name);
else
coder := DIGRAPHS_ChooseFileEncoder(name);
fi;
fi;
if coder = fail then
ErrorNoReturn("cannot determine the file format,");
elif mode = "r" then
coder := DIGRAPHS_DecoderWrapper(coder);
else
coder := DIGRAPHS_EncoderWrapper(coder);
fi;
file := IO_CompressedFile(UserHomeExpand(name), mode);
if file = fail then
ErrorNoReturn("cannot open the file given as the 1st argument <name>,");
fi;
file!.coder := coder;
file!.mode := mode;
return file;
end);
InstallGlobalFunction(ReadDigraphs,
function(arg...)
local nr, decoder, name, file, i, next, out, line;
# defaults
nr := infinity;
decoder := fail;
if Length(arg) = 1 then
name := arg[1];
elif Length(arg) = 2 then
name := arg[1];
if IsInt(arg[2]) then
nr := arg[2];
else
decoder := arg[2];
fi;
elif Length(arg) = 3 then
name := arg[1];
decoder := arg[2];
nr := arg[3];
else
ErrorNoReturn("there must be 1, 2, or 3 arguments,");
fi;
if not (IsString(name) or IsFile(name)) then
ErrorNoReturn("the 1st argument <filename> must be a string or IO ",
"file object,");
elif not (IsFunction(decoder) or decoder = fail) then
ErrorNoReturn("the argument <decoder> must be a function or fail,");
elif not (IsPosInt(nr) or IsInfinity(nr)) then
ErrorNoReturn("the argument <nr> must be a positive integer or ",
"infinity");
fi;
if IsString(name) then
file := DigraphFile(name, decoder, "r");
else
file := name;
if file!.closed then
ErrorNoReturn("the 1st argument <filename> is a closed file,");
elif file!.rbufsize = false then
ErrorNoReturn("the mode of the 1st argument <filename> must be \"r\",");
fi;
fi;
decoder := file!.coder;
if nr < infinity then
if NameFunction(decoder) in WholeFileDecoders then
Info(InfoWarning, 1, NameFunction(decoder), " is a whole file decoder ",
" and so only one digraph should be specified. If possible, the ",
"final digraph will be returned, or else an error.");
else
i := 0;
next := fail;
while i < nr - 1 and next <> IO_Nothing do
i := i + 1;
next := IO_ReadLine(file);
od;
if next <> IO_Nothing then
out := decoder(file);
else
out := IO_Nothing;
fi;
if IsString(arg[1]) then
IO_Close(file);
fi;
return out;
fi;
fi;
out := [];
if NameFunction(decoder) in WholeFileDecoders then
line := IO_ReadUntilEOF(file);
if IsString(arg[1]) then
IO_Close(file);
fi;
Add(out, decoder(line));
return out;
else
next := decoder(file);
fi;
while next <> IO_Nothing do
Add(out, next);
next := decoder(file);
od;
if IsString(arg[1]) then
IO_Close(file);
fi;
return out;
end);
InstallGlobalFunction(WriteDigraphs,
function(arg...)
local name, digraphs, encoder, mode, splitname, compext, g6sum, s6sum, v, e,
dg6sum, ds6sum, file, i, D, oldOnBreak, CloseAndCleanup, OnBreakWithCleanup;
# defaults
encoder := fail;
mode := "a";
if Length(arg) = 2 then
name := arg[1];
digraphs := arg[2];
elif IsFile(arg[1]) then
ErrorNoReturn("the 1st argument <filename> is a file, and so there must ",
"only be 2 arguments,");
elif Length(arg) = 3 then
name := arg[1];
digraphs := arg[2];
if IsString(arg[3]) then
mode := arg[3];
else
encoder := arg[3];
fi;
elif Length(arg) = 4 then
name := arg[1];
digraphs := arg[2];
encoder := arg[3];
mode := arg[4];
else
ErrorNoReturn("there must be 2, 3, or 4 arguments,");
fi;
if not IsList(digraphs) then
digraphs := [digraphs];
fi;
if not (IsString(name) or IsFile(name)) then
ErrorNoReturn("the 1st argument <filename> must be a string or a file,");
elif not ForAll(digraphs, IsDigraph) then
ErrorNoReturn("the 2nd argument <digraphs> must be a digraph or list of ",
"digraphs,");
elif not (IsFunction(encoder) or encoder = fail) then
ErrorNoReturn("the argument <encoder> must be a function or fail,");
elif not mode in ["a", "w"] then
ErrorNoReturn("the argument <mode> must be \"a\" or \"w\",");
elif IsString(name) and not IsExistingFile(name) then
mode := "w";
fi;
if IsString(name) then
if encoder = fail and DIGRAPHS_ChooseFileEncoder(name) = fail then
# the file encoder was not specified and cannot be deduced from the
# filename, so we try to make a guess based on the digraphs themselves
splitname := SplitString(name, ".");
if Last(splitname) in ["xz", "gz", "bz2"] then
compext := Remove(splitname);
fi;
# Do we know all the graphs to be symmetric?
if ForAll(digraphs, g -> HasIsSymmetricDigraph(g)
and IsSymmetricDigraph(g)) then
if ForAny(digraphs, IsMultiDigraph) then
encoder := DiSparse6String;
Add(splitname, "ds6");
else
# Find the sum of length estimates using Graph6 and Sparse6
g6sum := 0;
s6sum := 0;
for D in digraphs do
v := DigraphNrVertices(D);
e := DigraphNrEdges(D);
g6sum := g6sum + (v * (v - 1) / 2);
s6sum := s6sum + (e / 2 * (Log2Int(v - 1) + 2) * 3 / 2);
od;
if g6sum < s6sum and not ForAny(digraphs, DigraphHasLoops) then
encoder := Graph6String;
Add(splitname, "g6");
else
encoder := Sparse6String;
Add(splitname, "s6");
fi;
fi;
else
if ForAny(digraphs, IsMultiDigraph) then
encoder := DiSparse6String;
Add(splitname, "ds6");
else
# Find the sum of length estimates using Digraph6 and DiSparse6
dg6sum := 0;
ds6sum := 0;
for D in digraphs do
v := DigraphNrVertices(D);
e := DigraphNrEdges(D);
dg6sum := dg6sum + v ^ 2;
ds6sum := ds6sum + (e * (Log2Int(v) + 2) * 3 / 2);
od;
if dg6sum < ds6sum then
encoder := Digraph6String;
Add(splitname, "d6");
else
encoder := DiSparse6String;
Add(splitname, "ds6");
fi;
fi;
fi;
name := JoinStringsWithSeparator(splitname, ".");
if IsBound(compext) then
Append(name, ".");
Append(name, compext);
fi;
Info(InfoWarning, 1, "Writing to ", name);
fi;
file := DigraphFile(name, encoder, mode);
if NameFunction(file!.coder) in WholeFileEncoders and file!.mode <> "w" then
if IsString(arg[1]) then
IO_Close(file);
fi;
ErrorNoReturn(NameFunction(file!.coder), " is a whole file ",
"encoder and so the argument <mode> must be \"w\".");
fi;
else
file := name;
if file!.closed then
ErrorNoReturn("the 1st argument <filename> is closed,");
elif file!.wbufsize = false then
ErrorNoReturn("the mode of the 1st argument <filename> must be ",
"\"w\" or \"a\",");
fi;
fi;
oldOnBreak := OnBreak;
CloseAndCleanup := function()
if IsString(arg[1]) then
IO_Close(file);
fi;
OnBreak := oldOnBreak;
end;
OnBreakWithCleanup := function()
CloseAndCleanup();
OnBreak();
end;
OnBreak := OnBreakWithCleanup;
encoder := file!.coder;
if NameFunction(encoder) in WholeFileEncoders then
if Length(digraphs) > 1 then
Info(InfoWarning, 1, "the encoder ", NameFunction(encoder),
" is a whole file encoder, and so only one digraph should be ",
"specified. Only the last digraph will be encoded.");
fi;
IO_Write(file, encoder(Last(digraphs)));
else
for i in [1 .. Length(digraphs)] do
encoder(file, digraphs[i]);
od;
fi;
CloseAndCleanup();
return IO_OK;
end);
################################################################################
# 3. Decoders
################################################################################
InstallMethod(DigraphFromGraph6StringCons, "for IsMutableDigraph and a string",
[IsMutableDigraph, IsString],
function(filt, s)
local FindCoord, list, n, start, maxedges, out, pos, nredges, i, bpos, edge,
j;
s := Chomp(s);
# find a position in the adj matrix from the vector
# knowing a lower bound for pos_y
FindCoord := function(pos, bound)
local i, sum;
i := bound;
sum := i * (i + 1) / 2;
while sum < pos do
i := i + 1;
sum := sum + i;
od;
return [pos - sum + i, i + 1];
end;
if Length(s) = 0 then
ErrorNoReturn("the 2nd argument <s> must be a non-empty string,");
fi;
# Convert ASCII chars to integers
list := List(s, i -> IntChar(i) - 63);
# Get n the number of vertices of the graph
if list[1] <> 63 then
n := list[1];
start := 2;
elif Length(list) > 300 then
if list[2] = 63 then
n := 0;
for i in [0 .. 5] do
n := n + 2 ^ (6 * i) * list[8 - i];
od;
start := 9;
else
n := 0;
for i in [0 .. 2] do
n := n + 2 ^ (6 * i) * list[4 - i];
od;
start := 5;
fi;
else
ErrorNoReturn("the 2nd argument <s> is not a valid graph6 string,");
fi;
maxedges := n * (n - 1) / 2;
if list <> [0] and list <> [1] and
not (Int((maxedges - 1) / 6) + start = Length(list) and
Last(list) mod 2 ^ ((0 - maxedges) mod 6) = 0) then
ErrorNoReturn("the 2nd argument <s> is not a valid graph6 string,");
fi;
out := List([1 .. n], x -> []);
# Obtaining the adjacency vector
pos := 1;
nredges := 0;
for j in [start .. Length(list)] do # Every integer corresponds to 6 bits
i := list[j];
bpos := 1;
while i > 0 do
if i mod 2 = 0 then
i := i / 2;
else
edge := FindCoord(pos + 6 - bpos, 0);
Add(out[edge[1]], edge[2]);
Add(out[edge[2]], edge[1]);
nredges := nredges + 1;
i := (i - 1) / 2;
fi;
bpos := bpos + 1;
od;
pos := pos + 6;
od;
return DigraphNC(filt, out);
end);
InstallMethod(DigraphFromGraph6StringCons,
"for IsImmutableDigraph and a string",
[IsImmutableDigraph, IsString],
function(_, s)
local D;
D := MakeImmutable(DigraphFromGraph6StringCons(IsMutableDigraph, s));
SetIsSymmetricDigraph(D, true);
SetIsMultiDigraph(D, false);
SetDigraphHasLoops(D, false);
return D;
end);
InstallMethod(DigraphFromGraph6String, "for a function and a string",
[IsFunction, IsString],
DigraphFromGraph6StringCons);
InstallMethod(DigraphFromGraph6String, "for a string", [IsString],
s -> DigraphFromGraph6String(IsImmutableDigraph, s));
InstallMethod(DigraphFromDigraph6StringCons,
"for IsMutableDigraph and a string",
[IsMutableDigraph, IsString],
function(filt, s)
local legacy, list, n, start, i, range, source, pos, len, j, bpos, tabpos;
# NOTE: this package originally used a version of digraph6 that reads down
# the columns of an adjacency matrix, and appends a '+' to the start. This
# has been replaced by the Nauty standard, which reads across the rows of the
# matrix, and appends a '&' to the start. For backwards compatibility, this
# now accepts both formats, sending an info warning if the old format is used.
s := Chomp(s);
# Check non-emptiness
if Length(s) = 0 then
ErrorNoReturn("the 2nd argument <s> must be a non-empty string,");
fi;
# Check for the special '&' character (or the deprecated '+')
if s[1] = '&' then
legacy := false;
elif s[1] = '+' then
legacy := true;
Info(InfoDigraphs, 1, "Digraph6 strings beginning with '+' use an old");
Info(InfoDigraphs, 1, "specification of the Digraph6 format that is");
Info(InfoDigraphs, 1, "incompatible with the present standard. They can");
Info(InfoDigraphs, 1, "still be read by the Digraphs package, but are");
Info(InfoDigraphs, 1, "unlikely to be recognised by other programs.");
Info(InfoDigraphs, 1, "Please consider re-encoding with the new format.");
else
ErrorNoReturn("the 2nd argument <s> is not a valid digraph6 string,");
fi;
# Convert ASCII chars to integers
list := List(s, i -> IntChar(i) - 63);
# Get n the number of vertices of the graph
if list[2] <> 63 then
n := list[2];
start := 3;
elif Length(list) > 300 then
if list[3] = 63 then
n := 0;
for i in [0 .. 5] do
n := n + 2 ^ (6 * i) * list[9 - i];
od;
start := 10;
else
n := 0;
for i in [0 .. 2] do
n := n + 2 ^ (6 * i) * list[5 - i];
od;
start := 6;
fi;
else
ErrorNoReturn("the 2nd argument <s> is not a valid digraph6 string,");
fi;
range := [];
source := [];
# Obtaining the adjacency vector
pos := 1;
len := 1;
for j in [start .. Length(list)] do # Every integer corresponds to 6 bits
i := list[j];
bpos := 1;
while i > 0 do
if i mod 2 = 0 then
i := i / 2;
else
tabpos := pos + 6 - bpos;
range[len] := (tabpos - 1) mod n + 1;
source[len] := (tabpos - range[len]) / n + 1;
len := len + 1;
i := (i - 1) / 2;
fi;
bpos := bpos + 1;
od;
pos := pos + 6;
od;
if legacy then # source and range are reversed
return DigraphNC(filt,
rec(DigraphNrVertices := n,
DigraphSource := range,
DigraphRange := source));
fi;
return DigraphNC(filt,
rec(DigraphNrVertices := n,
DigraphRange := range,
DigraphSource := source));
end);
InstallMethod(DigraphFromDigraph6StringCons,
"for IsImmutableDigraph and a string",
[IsImmutableDigraph, IsString],
{filt, s} -> MakeImmutable(DigraphFromDigraph6StringCons(IsMutableDigraph, s)));
InstallMethod(DigraphFromDigraph6String, "for a function and a string",
[IsFunction, IsString],
DigraphFromDigraph6StringCons);
InstallMethod(DigraphFromDigraph6String, "for a string",
[IsString], s -> DigraphFromDigraph6String(IsImmutableDigraph, s));
InstallMethod(DigraphFromSparse6StringCons, "for IsMutableDigraph and a string",
[IsMutableDigraph, IsString],
function(filt, s)
local list, n, start, blist, pos, num, bpos, k, range, source, len, v, i,
finish, x, j;
s := Chomp(s);
# Check non-emptiness
if Length(s) = 0 then
ErrorNoReturn("the 2nd argument <s> must be a non-empty string,");
fi;
# Check for the special ':' character
if s[1] <> ':' then
ErrorNoReturn("the 2nd argument <s> is not a valid sparse6 string,");
fi;
# Convert ASCII chars to integers
list := [];
for i in s do
Add(list, IntChar(i) - 63);
od;
# Get n the number of vertices of the graph
if list[2] <> 63 then
n := list[2];
start := 3;
elif list[3] = 63 then
if Length(list) <= 8 then
ErrorNoReturn("the 2nd argument <s> is not a valid sparse6 string,");
fi;
n := 0;
for i in [0 .. 5] do
n := n + 2 ^ (6 * i) * list[9 - i];
od;
start := 10;
elif Length(list) > 4 then
n := 0;
for i in [0 .. 2] do
n := n + 2 ^ (6 * i) * list[5 - i];
od;
start := 6;
else
ErrorNoReturn("the 2nd argument <s> is not a valid sparse6 string,");
fi;
# convert list into a list of bits;
blist := BlistList([1 .. (Length(list) - start + 1) * 6], []);
pos := 1;
for i in [start .. Length(list)] do
num := list[i];
bpos := 1;
while num > 0 do
if num mod 2 = 0 then
num := num / 2;
else
num := (num - 1) / 2;
blist[pos + 6 - bpos] := true;
fi;
bpos := bpos + 1;
od;
pos := pos + 6;
od;
if n > 1 then
k := LogInt(n - 1, 2) + 1;
else
k := 1;
fi;
range := [];
source := [];
len := 1;
v := 0;
i := 1;
# remove some of the padding
finish := Length(blist) - (Length(blist) mod (k + 1));
while i <= finish - k do
if blist[i] then
v := v + 1;
if v = n then # We have reached the end
break;
fi;
fi;
x := 0;
for j in [1 .. k] do
if blist[i + j] then
x := x + 2 ^ (k - j);
fi;
od;
if x = n then # We have reached the end
break;
elif x > v then
v := x;
else
range[len] := x;
source[len] := v;
len := len + 1;
if x <> v then
range[len] := v;
source[len] := x;
len := len + 1;
fi;
fi;
i := i + k + 1;
od;
range := range + 1;
source := source + 1;
return DigraphNC(filt, (rec(DigraphNrVertices := n,
DigraphRange := range,
DigraphSource := source)));
end);
InstallMethod(DigraphFromSparse6StringCons,
"for IsImmutableDigraph and a string",
[IsImmutableDigraph, IsString],
function(_, s)
local D;
D := MakeImmutable(DigraphFromSparse6String(IsMutableDigraph, s));
SetIsSymmetricDigraph(D, true);
return D;
end);
InstallMethod(DigraphFromSparse6String, "for a function and a string",
[IsFunction, IsString], DigraphFromSparse6StringCons);
InstallMethod(DigraphFromSparse6String, "for a string",
[IsString],
s -> DigraphFromSparse6String(IsImmutableDigraph, s));