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 authored and fingolfin committed Mar 7, 2018
1 parent 902100b commit 20b545a
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
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
30 changes: 29 additions & 1 deletion lib/list.gd
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,7 @@ DeclareOperation( "PositionsProperty", [ IsList, IsFunction ] );
## <Oper Name="PositionBound" Arg='list'/>
##
## <Description>
## returns the first index for which an element is bound in the list
## returns the first bound position of the list
## <A>list</A>.
## For the empty list it returns <K>fail</K>.
## <P/>
Expand All @@ -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 bound positions in the list
## <A>list</A>.
## <P/>
## <Example><![CDATA[
## gap> PositionsBound([1,2,3]);
## [ 1 .. 3 ]
## gap> PositionsBound([,1,,3]);
## [ 2, 4 ]
## gap> PositionsBound([]);
## []
## ]]></Example>
## </Description>
## </ManSection>
## <#/GAPDoc>
##
DeclareGlobalFunction( "PositionsBound", [IsList] );



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


#############################################################################
##
#M PositionsBound( <list> ) . . . . . . . . . positions of all bound entries
##
InstallGlobalFunction( PositionsBound, function( list )
local i, bound;

if IsDenseList( list ) then
return [ 1 .. Length( list ) ];
fi;

bound := [];
for i in [ 1 .. Length( list ) ] do
if IsBound( list[i] ) then
Add( bound, i );
fi;
od;

return bound;
end );


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

0 comments on commit 20b545a

Please sign in to comment.