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 Feb 28, 2018
1 parent 24c69d0 commit 816a4c6
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
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> ) . . . . position of first bound element in a list
##
## <#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 816a4c6

Please sign in to comment.