Skip to content

Commit

Permalink
Ranges: improve printing and conversion to strings
Browse files Browse the repository at this point in the history
Also align the code for String/ViewString with the code of PrintObj for ranges.
  • Loading branch information
fingolfin committed Aug 2, 2022
1 parent 2d793c7 commit 9a2e32e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 44 deletions.
52 changes: 31 additions & 21 deletions lib/list.gi
Original file line number Diff line number Diff line change
Expand Up @@ -382,24 +382,36 @@ local str,ls, i;
return str;
end );

InstallMethod( String,
"for a range",
[ IsRange ],
BindGlobal("STRING_FOR_RANGE",
function( list )
local str;
str := Concatenation( "[ ", String( list[ 1 ] ) );
if Length( list ) > 1 then
if list[ 2 ] - list[ 1 ] <> 1 then
Append( str, ", " );
Append( str, String( list[ 2 ] ) );
fi;
str := "[ ";
Append( str, String( list[1] ) );
if Length( list ) > 1 then
if Length( list ) = 2 or list[2] - list[1] <> 1 then
Append( str, ", " );
Append( str, String( list[2] ) );
fi;
if Length( list ) > 2 then
Append( str, " .. " );
Append( str, String( list[ Length( list ) ] ) );
fi;
fi;
Append( str, " ]" );
Assert(0, IsStringRep(str));
ConvertToStringRep( str );
return str;
end );
end );

InstallMethod( ViewString,
"for a range",
[ IsRange ],
STRING_FOR_RANGE );

InstallMethod( String,
"for a range",
[ IsRange ],
STRING_FOR_RANGE );


#############################################################################
Expand Down Expand Up @@ -3829,7 +3841,7 @@ LIST_WITH_IDENTICAL_ENTRIES );
## and in the 'ViewString' method for finite lists.
##
InstallMethod( ViewObj,
"for finite lists",
"for a finite list",
[ IsList and IsFinite ],
{} -> RankFilter(IsList) + 1 - RankFilter(IsList and IsFinite),
function( list )
Expand Down Expand Up @@ -3857,19 +3869,17 @@ function( list )
end );

InstallMethod( ViewObj,
"for ranges",
"for a range",
[ IsList and IsFinite and IsRange ],
function( list )
Print( "[ " );
if Length( list ) = 1 then
Print( list[1] );
elif Length( list ) = 2 then
Print( list[1], ", ", list[2] );
elif 2 < Length( list ) then
if list[2] - list[1] <> 1 then
Print( list[1], ", ", list[2], " .. ", list[ Length( list ) ] );
else
Print( list[1], " .. ", list[ Length( list ) ] );
Print( list[1] );
if Length( list ) > 1 then
if Length( list ) = 2 or list[2] - list[1] <> 1 then
Print( ", ", list[2] );
fi;
if Length( list ) > 2 then
Print( " .. ", list[ Length( list ) ] );
fi;
fi;
Print( " ]" );
Expand Down
13 changes: 8 additions & 5 deletions src/range.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,14 +166,17 @@ static Obj CopyRange(Obj list, Int mut)
*/
static void PrintRange(Obj list)
{
Pr( "%2>[ %2>%d",
GET_LOW_RANGE(list), 0 );
Int len = GET_LEN_RANGE(list);
Pr( "%2>[ %2>%d", GET_LOW_RANGE(list), 0 );
if ( len == 2 ) {
Pr( "%<,%< %2>%d%4< ]", GET_LOW_RANGE(list)+GET_INC_RANGE(list), 0);
return;
}
if ( GET_INC_RANGE(list) != 1 ) {
Pr( "%<,%< %2>%d",
GET_LOW_RANGE(list)+GET_INC_RANGE(list), 0);
Pr( "%<,%< %2>%d", GET_LOW_RANGE(list)+GET_INC_RANGE(list), 0);
}
Pr( "%2< .. %2>%d%4< ]",
GET_LOW_RANGE(list)+(GET_LEN_RANGE(list)-1)*GET_INC_RANGE(list), 0 );
GET_LOW_RANGE(list)+(len-1)*GET_INC_RANGE(list), 0 );
}


Expand Down
2 changes: 1 addition & 1 deletion tst/testinstall/list.tst
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ gap> l := [10, 20];
gap> IsRange(l);
true
gap> String(l);
"[ 10, 20 .. 20 ]"
"[ 10, 20 ]"
gap> l := [2, 10, 18, 26, 34, 42];
[ 2, 10, 18, 26, 34, 42 ]
gap> IsRange(l);
Expand Down
22 changes: 11 additions & 11 deletions tst/testinstall/range.tst
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,44 @@ gap> TestPrintRangeRep:=function(r)
#
gap> TestPrintRangeRep([0..1]);
ViewObj: [ 0, 1 ]
PrintObj: [ 0 .. 1 ]
Display: [ 0 .. 1 ]
PrintObj: [ 0, 1 ]
Display: [ 0, 1 ]
DisplayString: <object>
ViewString: [ 0, 1 ]
PrintString: [ 0 .. 1 ]
String: [ 0 .. 1 ]
PrintString: [ 0, 1 ]
String: [ 0, 1 ]
gap> TestPrintRangeRep([0..2]);
ViewObj: [ 0 .. 2 ]
PrintObj: [ 0 .. 2 ]
Display: [ 0 .. 2 ]
DisplayString: <object>
ViewString: [ 0, 1, 2 ]
ViewString: [ 0 .. 2 ]
PrintString: [ 0 .. 2 ]
String: [ 0 .. 2 ]
gap> TestPrintRangeRep([0,2..4]);
ViewObj: [ 0, 2 .. 4 ]
PrintObj: [ 0, 2 .. 4 ]
Display: [ 0, 2 .. 4 ]
DisplayString: <object>
ViewString: [ 0, 2, 4 ]
ViewString: [ 0, 2 .. 4 ]
PrintString: [ 0, 2 .. 4 ]
String: [ 0, 2 .. 4 ]
gap> TestPrintRangeRep([0,-1..-2]);
ViewObj: [ 0, -1 .. -2 ]
PrintObj: [ 0, -1 .. -2 ]
Display: [ 0, -1 .. -2 ]
DisplayString: <object>
ViewString: [ 0, -1, -2 ]
ViewString: [ 0, -1 .. -2 ]
PrintString: [ 0, -1 .. -2 ]
String: [ 0, -1 .. -2 ]
gap> TestPrintRangeRep([0,-1..-1]);
ViewObj: [ 0, -1 ]
PrintObj: [ 0, -1 .. -1 ]
Display: [ 0, -1 .. -1 ]
PrintObj: [ 0, -1 ]
Display: [ 0, -1 ]
DisplayString: <object>
ViewString: [ 0, -1 ]
PrintString: [ 0, -1 .. -1 ]
String: [ 0, -1 .. -1 ]
PrintString: [ 0, -1 ]
String: [ 0, -1 ]

#
gap> 0 in [0..0];
Expand Down
12 changes: 6 additions & 6 deletions tst/testinstall/reesmat.tst
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ false

# IsReesMatrixSemigroup: for a Rees matrix subsemigroup with generators, 1
gap> R := ReesMatrixSemigroup(SymmetricGroup(2), [[(1,2)]]);
<Rees matrix semigroup 1x1 over Sym( [ 1 .. 2 ] )>
<Rees matrix semigroup 1x1 over Sym( [ 1, 2 ] )>
gap> IsReesMatrixSemigroup(R);
true
gap> S := Semigroup(Elements(R));
Expand Down Expand Up @@ -302,7 +302,7 @@ false

# IsReesZeroMatrixSemigroup: for a Rees matrix subsemigroup with generators, 1
gap> R := ReesZeroMatrixSemigroup(SymmetricGroup(2), [[(1,2)]]);
<Rees 0-matrix semigroup 1x1 over Sym( [ 1 .. 2 ] )>
<Rees 0-matrix semigroup 1x1 over Sym( [ 1, 2 ] )>
gap> IsReesZeroMatrixSemigroup(R);
true
gap> S := Semigroup(Elements(R));
Expand All @@ -320,13 +320,13 @@ false

# IsReesZeroMatrixSemigroup: for a Rees matrix subsemigroup with generators, 1
gap> R := ReesZeroMatrixSemigroup(SymmetricGroup(2), [[(1,2)]]);
<Rees 0-matrix semigroup 1x1 over Sym( [ 1 .. 2 ] )>
<Rees 0-matrix semigroup 1x1 over Sym( [ 1, 2 ] )>
gap> S := Semigroup(RMSElement(R, 1, (1,2), 1), MultiplicativeZero(R));
<subsemigroup of 1x1 Rees 0-matrix semigroup with 2 generators>
gap> IsReesZeroMatrixSemigroup(S);
false
gap> R := ReesZeroMatrixSemigroup(SymmetricGroup(2), [[()]]);
<Rees 0-matrix semigroup 1x1 over Sym( [ 1 .. 2 ] )>
<Rees 0-matrix semigroup 1x1 over Sym( [ 1, 2 ] )>
gap> S := Semigroup(RMSElement(R, 1, (), 1), MultiplicativeZero(R));
<subsemigroup of 1x1 Rees 0-matrix semigroup with 2 generators>
gap> IsReesZeroMatrixSemigroup(S);
Expand Down Expand Up @@ -402,7 +402,7 @@ infinity

# Enumerator: for a Rees matrix semigroup
gap> R := ReesMatrixSemigroup(SymmetricGroup(2), [[()]]);
<Rees matrix semigroup 1x1 over Sym( [ 1 .. 2 ] )>
<Rees matrix semigroup 1x1 over Sym( [ 1, 2 ] )>
gap> enum := Enumerator(R);
<enumerator of Rees matrix semigroup>
gap> enum[1];
Expand All @@ -422,7 +422,7 @@ fail

# Enumerator: for a Rees 0-matrix semigroup
gap> R := ReesZeroMatrixSemigroup(SymmetricGroup(2), [[()]]);
<Rees 0-matrix semigroup 1x1 over Sym( [ 1 .. 2 ] )>
<Rees 0-matrix semigroup 1x1 over Sym( [ 1, 2 ] )>
gap> enum := Enumerator(R);;
gap> enum[Position(enum, enum[1])] = enum[1];
true
Expand Down

0 comments on commit 9a2e32e

Please sign in to comment.