Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions doc/io.xml
Original file line number Diff line number Diff line change
Expand Up @@ -987,4 +987,37 @@ gap> DigraphFromDIMACSString(last);
<immutable digraph with 4 vertices, 8 edges>]]></Example>
</Description>
</ManSection>
<#/GAPDoc>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also need to add <#Include Label="LADString"> in one of the files z-chap*.xml, otherwise the documentation you've written here is never actually picked up.

<#GAPDoc Label="LADString">
<ManSection>
<Oper Name="LADString" Arg="D"/>
<Returns>A string.</Returns>
<Description>
Returns a string representing the digraph <A>D</A> in the LAD format.
The first line contains the number of vertices <C>n</C> and it is followed by <C>n</C> lines, one per vertex.
Each of these lines consists of the out-degree of the vertex followed by its out-neighbours, all separated by spaces.
Vertices are numbered from <C>0</C>.
<P/>

The digraph <A>D</A> must not have multiple edges or loops.
<Example><![CDATA[
gap> LADString(CompleteDigraph(3));
"3\n2 1 2\n2 0 2\n2 0 1\n"]]></Example>
</Description>
</ManSection>
<#/GAPDoc>

<#GAPDoc Label="DigraphFromLADString">
<ManSection>
<Oper Name="DigraphFromLADString" Arg="s"/>
<Returns>A digraph.</Returns>
<Description>
Returns the digraph represented by the string <A>s</A> in the LAD format (see <Ref Oper="LADString"/>).
An error is raised if <A>s</A> is not a valid LAD string or if it describes a digraph with loops.
<Example><![CDATA[
gap> DigraphFromLADString("3\n2 1 2\n2 0 2\n2 0 1\n");
<immutable digraph with 3 vertices, 6 edges>]]></Example>
</Description>
</ManSection>
<#/GAPDoc>
2 changes: 2 additions & 0 deletions gap/io.gd
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,5 @@ DeclareOperation("DIMACSString", [IsDigraph]);
DeclareGlobalFunction("WritePlainTextDigraph");
DeclareGlobalFunction("DreadnautString");
DeclareGlobalFunction("DigraphPlainTextLineEncoder");
DeclareOperation("LADString", [IsDigraph]);
DeclareOperation("DigraphFromLADString", [IsString]);
61 changes: 61 additions & 0 deletions gap/io.gi
Original file line number Diff line number Diff line change
Expand Up @@ -222,10 +222,12 @@ end);
BindGlobal("WholeFileDecoders", HashSet());
AddSet(WholeFileDecoders, "DigraphFromDreadnautString");
AddSet(WholeFileDecoders, "DigraphFromDIMACSString");
AddSet(WholeFileDecoders, "DigraphFromLADString");

BindGlobal("WholeFileEncoders", HashSet());
AddSet(WholeFileEncoders, "DreadnautString");
AddSet(WholeFileEncoders, "DIMACSString");
AddSet(WholeFileEncoders, "LADString");

InstallGlobalFunction(IsWholeFileDecoder,
decoder -> NameFunction(decoder) in WholeFileDecoders);
Expand Down Expand Up @@ -297,6 +299,8 @@ function(filename)
return DigraphFromDreadnautString;
elif extension = "dimacs" then
return DigraphFromDIMACSString;
elif extension = "lad" then
return DigraphFromLADString;
fi;

return fail;
Expand Down Expand Up @@ -335,6 +339,8 @@ function(filename)
return DreadnautString;
elif extension = "dimacs" then
return DIMACSString;
elif extension = "lad" then
return LADString;
fi;
return fail;
end);
Expand Down Expand Up @@ -2602,3 +2608,58 @@ end);

InstallMethod(PlainTextString, "for a digraph", [IsDigraph],
D -> DigraphPlainTextLineEncoder(" ", " ", -1)(D));

InstallMethod(LADString, "for a digraph", [IsDigraph],
function(D)
local result, nbs, v;
if IsMultiDigraph(D) then
ErrorNoReturn("the argument <D> must not have multiple edges");
elif DigraphHasLoops(D) then
ErrorNoReturn("the argument <D> must not have loops");
fi;
result := StringFormatted("{}\n", DigraphNrVertices(D));
for nbs in OutNeighbours(D) do
Append(result, String(Length(nbs)));
for v in SortedList(nbs) do
Append(result, " ");
Append(result, String(v - 1));
od;
Append(result, "\n");
od;
return result;
end);

InstallMethod(DigraphFromLADString, "for a String", [IsString],
function(s)
# validating and parsing the string into separate lines
local lines, n, nbs, line, i, v;
lines := Filtered(SplitString(s, "\n"), x -> not IsEmpty(x));
if IsEmpty(lines) then
ErrorNoReturn("the argument <s> must be a non-empty string");
fi;
n := Int(NormalizedWhitespace(lines[1]));
if n = fail or n < 0 then
ErrorNoReturn("the first line of <s> must be the number of vertices");
elif Length(lines) <> n + 1 then
ErrorNoReturn("<s> must have exactly one line per vertex after the first");
fi;

# Convert LAD to Digraph
nbs := [];
for i in [1 .. n] do
line := List(SplitString(NormalizedWhitespace(lines[i + 1]), " "), Int);
if IsEmpty(line) or fail in line or line[1] <> Length(line) - 1 then
ErrorNoReturn("line ", i + 1, " of <s> is not valid");
fi;
Remove(line, 1);
for v in line do
if v < 0 or v >= n then
ErrorNoReturn("line ", i + 1, " of <s> has a vertex out of range");
elif v = i - 1 then
ErrorNoReturn("line ", i + 1, " of <s> contains a loop");
fi;
od;
Add(nbs, line + 1); # converting from 0 indexed to 1 indexed
od;
return ConvertToImmutableDigraphNC(nbs);
end);
53 changes: 53 additions & 0 deletions tst/standard/io.tst
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,59 @@ gap> DigraphFromDreadnautString("$=1n3dg\n1 : 2 3.\nf=[1\nf=2");
Error, Unexpected character 'f' in partition specification (line 4)
gap> IO_Close(file);;

# LADString and DigraphFromLADString
gap> D := CompleteDigraph(3);;
gap> str := LADString(D);
"3\n2 1 2\n2 0 2\n2 0 1\n"
gap> DigraphFromLADString(str) = D;
true
gap> D := EmptyDigraph(0);;
gap> LADString(D);
"0\n"
gap> DigraphFromLADString(LADString(D)) = D;
true
gap> D := EmptyDigraph(4);;
gap> DigraphFromLADString(LADString(D)) = D;
true
gap> D := Digraph([[2], [], [1]]);;
gap> LADString(D);
"3\n1 1\n0\n1 0\n"
gap> DigraphFromLADString(LADString(D)) = D;
true

# LADString: bad input
gap> LADString(Digraph([[2, 2], []]));
Error, the argument <D> must not have multiple edges
gap> LADString(Digraph([[1], []]));
Error, the argument <D> must not have loops

# DigraphFromLADString: bad input
gap> DigraphFromLADString("");
Error, the argument <s> must be a non-empty string
gap> DigraphFromLADString("abc\n");
Error, the first line of <s> must be the number of vertices
gap> DigraphFromLADString("2\n1 1\n");
Error, <s> must have exactly one line per vertex after the first
gap> DigraphFromLADString("2\n1 5\n0\n");
Error, line 2 of <s> has a vertex out of range
gap> DigraphFromLADString("2\n2 1\n0\n");
Error, line 2 of <s> is not valid
gap> DigraphFromLADString("2\n1 x\n0\n");
Error, line 2 of <s> is not valid

# LAD files
gap> filename := Concatenation(DIGRAPHS_Dir(), "/tst/out/test.lad");;
gap> D := CompleteDigraph(3);;
gap> WriteDigraphs(filename, D, "w");
IO_OK
gap> ReadDigraphs(filename)[1] = D;
true
gap> WriteDigraphs(filename, D);
Error, LADString is a whole file encoder and so the argument <mode> must be "w\
".
gap> DigraphFromLADString("1\n1 0\n");
Error, line 2 of <s> contains a loop

# Ensure all files introduced by tests are closed
gap> IO.OpenFiles = files;
true
Expand Down
Loading