Skip to content

Commit

Permalink
Add Helpers
Browse files Browse the repository at this point in the history
 * ClassOfOperation, to determine the "class" of an operation
   which is one of filter, category, representation, property,
   attribute, or operation.
 * IsCategory, IsRepresentation, IsProperty, IsAttribute
   determine wheter an object is a category, property, or
   attribute respectively
 * FilterByName retrieve filter by name
 * IdOfFilter return the index of a given filter in the global
   list FILTERS of filters
 * IdOfFilterByName return the index of a filter in the global
   list of filters given its name
 * CategoryByName return category by name
  • Loading branch information
markuspf committed Aug 18, 2017
1 parent c78bb4c commit d9a9111
Show file tree
Hide file tree
Showing 7 changed files with 351 additions and 79 deletions.
7 changes: 7 additions & 0 deletions doc/ref/create.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1177,6 +1177,12 @@ avoid their being overwritten accidentally.
See also Section <Ref Sect="More About Global Variables"/>.

<#Include Label="DeclareCategory">
<#Include Label="ClassOfOperation">
<#Include Label="IsCategory">
<#Include Label="IsRepresentation">
<#Include Label="IsProperty">
<#Include Label="IsAttribute">
<#Include Label="CategoryByName">
<#Include Label="DeclareRepresentation">
<#Include Label="DeclareAttribute">
<#Include Label="DeclareProperty">
Expand All @@ -1188,6 +1194,7 @@ See also Section <Ref Sect="More About Global Variables"/>.
<#Include Label="InstallValue">
<#Include Label="DeclareSynonym">
<#Include Label="FlushCaches">
<#Include Label="FilterByName">

</Section>

Expand Down
80 changes: 80 additions & 0 deletions lib/filter.g
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,86 @@ BIND_GLOBAL("NICE_FLAGS",QUO_INT(SUM_FLAGS,30));
BIND_GLOBAL( "CANONICAL_BASIS_FLAGS", QUO_INT(SUM_FLAGS,5) );



#############################################################################
##
#V IdOfFilter
##
## <#GAPDoc Label="IdOfFilter">
## <ManSection>
## <Func Name="IdOfFilter" Arg="filter"/>
## <Func Name="IdOfFilterByName" Arg="name"/>
##
## <Description>
## finds the id of the filter <A>filter</A>, or the id of the filter
## with name <A>name</A> respectively.
## The id of a filter is equal to the
## position of this filter in the global FILTERS list.
## <P/>
## Note that not every <C>filter</C> for which <C>IsFilter(filter)</C>
## returns <C>true</C> has an ID, only elementary filters do.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
## Note that the filter ID is stored in FLAG1_FILTER for most filters,
## testers have the ID stored in FLAG2_FILTER, so the code below is
## more efficient than just iterating over the FILTERS list.
##
##
BIND_GLOBAL( "IdOfFilter",
function(filter)
local fid;
fid := FLAG1_FILTER(filter);
if fid > 0 and FILTERS[fid] = filter then
return fid;
fi;
fid := FLAG2_FILTER(filter);
if fid > 0 and FILTERS[fid] = filter then
return fid;
fi;
return fail;
end);

BIND_GLOBAL( "IdOfFilterByName",
function(name)
local fid;
for fid in [1..LEN_LIST(FILTERS)] do
if NAME_FUNC(FILTERS[fid]) = name then
return fid;
fi;
od;
return fail;
end);


#############################################################################
##
#V FilterByName
##
## <#GAPDoc Label="FilterByName">
## <ManSection>
## <Func Name="FilterByName" Arg="name"/>
##
## <Description>
## finds the filter with name <A>name</A> in the global FILTERS list. This
## is useful to find filters that were created but not bound to a global
## variable.
## </Description>
## </ManSection>
## <#/GAPDoc>
##
BIND_GLOBAL( "FilterByName",
function(name)
local fid;
fid := IdOfFilterByName(name);
if fid <> fail then
return FILTERS[fid];
else
return fail;
fi;
end);

#############################################################################
##
#E
73 changes: 0 additions & 73 deletions lib/function.g
Original file line number Diff line number Diff line change
Expand Up @@ -636,79 +636,6 @@ InstallMethod( ViewObj, "for a function", true, [IsFunction], 0,
Print(" ) ... end");
end);

BIND_GLOBAL( "VIEW_STRING_OPERATION", function ( op )
local class, flags, types, catok, repok, propok, seenprop,
t, res;
class := "Operation";
if IS_IDENTICAL_OBJ(op,IS_OBJECT) then
class := "Filter";
elif IS_CONSTRUCTOR(op) then
class := "Constructor";
elif IsFilter(op) then
class := "Filter";
flags := FLAGS_FILTER(op);
if flags <> false then
flags := TRUES_FLAGS(flags);
else
flags := [];
fi;
types := INFO_FILTERS{flags};
catok := true;
repok := true;
propok := true;
seenprop := false;
for t in types do
if not t in FNUM_REPS then
repok := false;
fi;
if not t in FNUM_CATS then
catok := false;
fi;
if not t in FNUM_PROS and not t in FNUM_TPRS then
propok := false;
fi;
if t in FNUM_PROS then
seenprop := true;
fi;
od;
if seenprop and propok then
class := "Property";
elif catok then
class := "Category";
elif repok then
class := "Representation";
fi;
elif Tester(op) <> false then
# op is an attribute
class := "Attribute";
fi;

# Horrible.
res := "<";
APPEND_LIST(res, class);
APPEND_LIST(res, " \"");
APPEND_LIST(res, NAME_FUNC(op));
APPEND_LIST(res, "\">");
return res;
end);

BIND_GLOBAL( "PRINT_OPERATION",
function ( op )
Print(VIEW_STRING_OPERATION(op));
end);

InstallMethod( ViewObj,
"for an operation",
[ IsOperation ],
PRINT_OPERATION );

InstallMethod( ViewString,
"for an operation",
[ IsOperation ],
function(op)
return VIEW_STRING_OPERATION(op);
end);

#############################################################################
##
#E
37 changes: 31 additions & 6 deletions lib/function.gi
Original file line number Diff line number Diff line change
Expand Up @@ -60,25 +60,50 @@ function(func)
return result;
end);


InstallMethod(DisplayString, "for a function, using string stream", [IsFunction],
InstallMethod(DisplayString, "for a function, using string stream", [IsFunction],
function(fun)
local s, stream;
s := "";
stream := OutputTextString(s, true);
PrintTo(stream, fun);
CloseStream(stream);
Add(s, '\n');
Add(s, '\n');
return MakeImmutable(s);
end);

InstallMethod(String, "for a function, with whitespace reduced", [IsFunction],
InstallMethod(String, "for a function, with whitespace reduced", [IsFunction],
function(fun)
local s, str;
s := ShallowCopy(DisplayString(fun));
Remove(s);
Remove(s);
NormalizeWhitespace(s);
return MakeImmutable(s);
end);


BIND_GLOBAL( "VIEW_STRING_OPERATION",
function ( op )
return STRINGIFY("<", ClassOfOperation(op),
" \"", NAME_FUNC(op), "\">");
end);

BIND_GLOBAL( "PRINT_OPERATION",
function ( op )
Print(VIEW_STRING_OPERATION(op));
end);

InstallMethod( ViewObj,
"for an operation",
[ IsOperation ],
PRINT_OPERATION );

InstallMethod( ViewString,
"for an operation",
[ IsOperation ],
function(op)
return VIEW_STRING_OPERATION(op);
end);

#############################################################################
##
#E
>>>>>>> Add Helpers
114 changes: 114 additions & 0 deletions lib/type.gd
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,117 @@
##
DeclareOperation( "FiltersType", [ IsType ] );
DeclareOperation( "FiltersObj", [ IsObject ] );


#############################################################################
##
#F ClassOfOperation( <op> )
##
## Determine the class of the operation <A>op</A>.
##
## <#GAPDoc Label="ClassOfOperation">
## <ManSection>
## <Func Name="ClassOfOperation" Arg='object'/>
##
## <Description>
## returns a string from the list <C>[ "Attribute", "Operation", "Property",
## "Category", "Representation", "Filter"]</C> reflecting which type of
## operation <A>op</A> is.
## </Description>
## </ManSection>
## <#/GAPDoc>
DeclareGlobalFunction( "ClassOfOperation" );


#############################################################################
##
#F IsCategory( <object> )
##
## Determine whether the passed object is a category.
##
## <#GAPDoc Label="IsCategory">
## <ManSection>
## <Func Name="IsCategory" Arg='object'/>
##
## <Description>
## returns <C>true</C> if <A>object</A> is a category, and <C>false</C>
## otherwise.
## </Description>
## </ManSection>
## <#/GAPDoc>
DeclareGlobalFunction( "IsCategory" );

#############################################################################
##
#F IsRepresentation( <object> )
##
## Determine whether the passed object is a representation.
##
## <#GAPDoc Label="IsRepresentation">
## <ManSection>
## <Func Name="IsRepresentation" Arg='object'/>
##
## <Description>
## returns <C>true</C> if <A>object</A> is a representation, and
## <C>false</C> otherwise.
## </Description>
## </ManSection>
## <#/GAPDoc>
DeclareGlobalFunction( "IsRepresentation" );


#############################################################################
##
#F IsAttribute( <object> )
##
## Determine whether the passed object is an attribute.
##
## <#GAPDoc Label="IsAttribute">
## <ManSection>
## <Func Name="IsAttribute" Arg='object'/>
##
## <Description>
## returns <C>true</C> if <A>object</A> is an attribute, and <C>false</C>
## otherwise.
## </Description>
## </ManSection>
## <#/GAPDoc>
DeclareGlobalFunction( "IsAttribute" );


#############################################################################
##
#F IsProperty( <object> )
##
## Determine whether the passed object is a property.
##
## <#GAPDoc Label="IsProperty">
## <ManSection>
## <Func Name="IsProperty" Arg='object'/>
##
## <Description>
## returns <C>true</C> if <A>object</A> is a property, and <C>false</C>
## otherwise.
## </Description>
## </ManSection>
## <#/GAPDoc>
DeclareGlobalFunction( "IsProperty" );


#############################################################################
##
#F CategoryByName( <name> )
##
## Find a category given its name.
##
## <#GAPDoc Label="CategoryByName">
## <ManSection>
## <Func Name="CategoryByName" Arg='name'/>
##
## <Description>
## returns the category with name <A>name</A> if it is found, or fail otherwise.
## </Description>
## </ManSection>
## <#/GAPDoc>
DeclareGlobalFunction( "CategoryByName" );

Loading

0 comments on commit d9a9111

Please sign in to comment.