Skip to content

Commit f4b2021

Browse files
wilfwilsonjames-d-mitchell
authored andcommitted
oper.gi: don't copy imm digraphs to add 0 vertices
1 parent 5d719e8 commit f4b2021

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

doc/oper.xml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -320,8 +320,10 @@ true
320320

321321
If <A>digraph</A> belongs to <Ref Filt="IsMutableDigraph"/>, then the
322322
vertices are added directly to <A>digraph</A>, which is changed in-place.
323-
If <A>digraph</A> belongs to <Ref Filt="IsImmutableDigraph"/>, then the
324-
result is returned as an immutable digraph.
323+
If <A>digraph</A> belongs to <Ref Filt="IsImmutableDigraph"/>, then
324+
<A>digraph</A> itself is returned if no vertices are added
325+
(i.e. <C><A>m</A>=0</C> or <A>labels</A> is empty), otherwise
326+
the result is a new immutable digraph.
325327
<P/>
326328

327329
<Example><![CDATA[

gap/oper.gi

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,12 @@ end);
6363

6464
InstallMethod(DigraphAddVertices, "for an immutable digraph and list",
6565
[IsImmutableDigraph, IsList],
66-
{D, labels} -> MakeImmutable(DigraphAddVertices(DigraphMutableCopy(D),
67-
labels)));
66+
function(D, labels)
67+
if IsEmpty(labels) then
68+
return D;
69+
fi;
70+
return MakeImmutable(DigraphAddVertices(DigraphMutableCopy(D), labels));
71+
end);
6872

6973
InstallMethod(DigraphAddVertices, "for a mutable digraph and an integer",
7074
[IsMutableDigraph, IsInt],
@@ -79,7 +83,12 @@ end);
7983

8084
InstallMethod(DigraphAddVertices, "for an immutable digraph and an integer",
8185
[IsImmutableDigraph, IsInt],
82-
{D, m} -> MakeImmutable(DigraphAddVertices(DigraphMutableCopy(D), m)));
86+
function(D, m)
87+
if m = 0 then
88+
return D;
89+
fi;
90+
return MakeImmutable(DigraphAddVertices(DigraphMutableCopy(D), m));
91+
end);
8392

8493
# Included for backwards compatibility, even though the 2nd arg is redundant.
8594
# See https://github.com/digraphs/Digraphs/issues/264

tst/standard/oper.tst

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -617,17 +617,9 @@ gap> DigraphVertexLabels(gr2);
617617
[ Alt( [ 1 .. 5 ] ), Sym( [ 1 .. 2 ] ), Group(()) ]
618618
gap> DigraphAddVertices(gr2, -1);
619619
Error, the 2nd argument <m> must be a non-negative integer,
620-
gap> gr3 := DigraphAddVertices(gr2, 0);
621-
<immutable digraph with 3 vertices, 1 edge>
622-
gap> IsIdenticalObj(gr2, gr3);
623-
false
624-
gap> gr2 = gr3;
620+
gap> IsIdenticalObj(gr2, DigraphAddVertices(gr2, 0));
625621
true
626-
gap> gr3 := DigraphAddVertices(gr2, []);
627-
<immutable digraph with 3 vertices, 1 edge>
628-
gap> IsIdenticalObj(gr2, gr3);
629-
false
630-
gap> gr2 = gr3;
622+
gap> IsIdenticalObj(gr2, DigraphAddVertices(gr2, []));
631623
true
632624
633625
# DigraphAddVertices (redundant three-argument version)

0 commit comments

Comments
 (0)