Skip to content

Commit

Permalink
Implement PositionsBound.
Browse files Browse the repository at this point in the history
Currently there is no library method to return the set of all bound
entries of a list. This commit implements this functionality in an
efficient manner.
  • Loading branch information
markusbaumeister committed Mar 1, 2018
1 parent 24c69d0 commit 3440f3f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
1 change: 1 addition & 0 deletions doc/ref/lists.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1410,6 +1410,7 @@ The latter can be done also using <Ref Func="ListWithIdenticalEntries"/>.
<#Include Label="PositionProperty">
<#Include Label="PositionsProperty">
<#Include Label="PositionBound">
<#Include Label="PositionsBound">
<#Include Label="PositionNot">
<#Include Label="PositionNonZero">
<#Include Label="PositionSublist">
Expand Down
28 changes: 28 additions & 0 deletions lib/list.gd
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,34 @@ DeclareOperation( "PositionsProperty", [ IsList, IsFunction ] );
DeclareOperation( "PositionBound", [ IsList ] );


#############################################################################
##
#O PositionsBound( <list> ) . . . . . . . . . positions of all bound entries
##
## <#GAPDoc Label="PositionsBound">
## <ManSection>
## <Oper Name="PositionsBound" Arg='list'/>
##
## <Description>
## returns the set of all indices for which an element is bound in the list
## <A>list</A>.
## <P/>
## <Example><![CDATA[
## gap> PositionsBound([1,2,3]);
## [ 1, 2, 3 ]
## gap> PositionsBound([,1,,3]);
## [ 2, 4 ]
## gap> PositionsBound([]);
## []
## ]]></Example>
## </Description>
## </ManSection>
## <#/GAPDoc>
##
DeclareOperation( "PositionsBound", [ IsList ] );



#############################################################################
##
#O PositionSublist( <list>, <sub>[, <from>] )
Expand Down
21 changes: 21 additions & 0 deletions lib/list.gi
Original file line number Diff line number Diff line change
Expand Up @@ -1692,6 +1692,27 @@ InstallMethod( PositionBound,
end );


#############################################################################
##
#M PositionsBound( <list> ) . . . . . . . . . positions of all bound entries
##
InstallMethod( PositionsBound,
"for a list",
[ IsList ],
function( list )
local i, bound, pos;
bound := [];
pos := 1;
for i in [ 1 .. Length( list ) ] do
if IsBound( list[i] ) then
bound[pos] := i;
pos := pos + 1;
fi;
od;
return bound;
end );


#############################################################################
##
#M PositionSublist( <list>,<sub>[,<ind>] )
Expand Down

0 comments on commit 3440f3f

Please sign in to comment.