Skip to content

Commit

Permalink
fixing removal of attributes from graphs - #23 (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
mpan322 authored May 20, 2024
1 parent ed26df3 commit b0d67a1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 4 deletions.
30 changes: 27 additions & 3 deletions gap/dot.gi
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,33 @@ InstallMethod(GraphvizRemoveAttr,
"for a graphviz (di)graph or context and an object",
[IsGraphvizGraphDigraphOrContext, IsObject],
function(obj, attr)
local attrs;
attrs := GraphvizAttrs(obj);
obj!.Attrs := Filtered(attrs, item -> item[1] <> String(attr));
local attrs, i, match;
attrs := GraphvizAttrs(obj);
attr := String(attr);

# checks if they attribute names match the one being removed
match := function(key, str)
for i in [1 .. Length(key)] do
if i > Length(str) or key[i] <> str[i] then
return false;
fi;
od;

i := i + 1;
while i <= Length(str) do
if str[i] = '=' then
return true;
elif str[i] <> '\s' and str[i] <> '\t' then
return false;
fi;
i := i + 1;
od;

# attributes which are not key value or removal by value
return true;
end;

obj!.Attrs := Filtered(attrs, s -> not match(attr, s));
return obj;
end);

Expand Down
18 changes: 17 additions & 1 deletion tst/graph.tst
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,30 @@ gap> GraphvizSetAttr(g, 1, 2);
<graphviz graph with 0 nodes and 0 edges>
gap> GraphvizAttrs(g);
[ "label=test", "1=2" ]
gap> GraphvizRemoveAttr(g, "label=tes");;
gap> GraphvizAttrs(g);
[ "label=test", "1=2" ]
gap> GraphvizRemoveAttr(g, "1=2");
<graphviz graph with 0 nodes and 0 edges>
gap> GraphvizAttrs(g);
[ "label=test" ]
gap> GraphvizSetAttr(g, 1, 2);;
#I unknown attribute "1", the graphviz object may no longer be valid, it can be removed using GraphvizRemoveAttr
gap> GraphvizAttrs(g);
[ "label=test", "1=2" ]
gap> GraphvizRemoveAttr(g, "1");
<graphviz graph with 0 nodes and 0 edges>
gap> GraphvizAttrs(g);
[ "label=test", "1=2" ]
[ "label=test" ]
gap> GraphvizRemoveAttr(g, "label");;
gap> GraphvizAttrs(g);
[ ]
gap> GraphvizSetAttr(g, "label", "test");;
gap> GraphvizAttrs(g);
[ "label=test" ]
gap> GraphvizRemoveAttr(g, "label=test");;
gap> GraphvizAttrs(g);
[ ]

# Test set color (graph)
gap> g := GraphvizGraph();;
Expand Down

0 comments on commit b0d67a1

Please sign in to comment.