Skip to content

Commit 8aedf98

Browse files
Implement PositionsBound.
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.
1 parent 24c69d0 commit 8aedf98

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

doc/ref/lists.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1410,6 +1410,7 @@ The latter can be done also using <Ref Func="ListWithIdenticalEntries"/>.
14101410
<#Include Label="PositionProperty">
14111411
<#Include Label="PositionsProperty">
14121412
<#Include Label="PositionBound">
1413+
<#Include Label="PositionsBound">
14131414
<#Include Label="PositionNot">
14141415
<#Include Label="PositionNonZero">
14151416
<#Include Label="PositionSublist">

lib/list.gd

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ DeclareOperation( "PositionsProperty", [ IsList, IsFunction ] );
985985
## <Oper Name="PositionBound" Arg='list'/>
986986
##
987987
## <Description>
988-
## returns the first index for which an element is bound in the list
988+
## returns the first bound position of the list
989989
## <A>list</A>.
990990
## For the empty list it returns <K>fail</K>.
991991
## <P/>
@@ -1002,6 +1002,34 @@ DeclareOperation( "PositionsProperty", [ IsList, IsFunction ] );
10021002
DeclareOperation( "PositionBound", [ IsList ] );
10031003

10041004

1005+
#############################################################################
1006+
##
1007+
#O PositionsBound( <list> ) . . . . . . . . . positions of all bound entries
1008+
##
1009+
## <#GAPDoc Label="PositionsBound">
1010+
## <ManSection>
1011+
## <Oper Name="PositionsBound" Arg='list'/>
1012+
##
1013+
## <Description>
1014+
## returns the set of all bound positions in the list
1015+
## <A>list</A>.
1016+
## <P/>
1017+
## <Example><![CDATA[
1018+
## gap> PositionsBound([1,2,3]);
1019+
## [ 1, 2, 3 ]
1020+
## gap> PositionsBound([,1,,3]);
1021+
## [ 2, 4 ]
1022+
## gap> PositionsBound([]);
1023+
## []
1024+
## ]]></Example>
1025+
## </Description>
1026+
## </ManSection>
1027+
## <#/GAPDoc>
1028+
##
1029+
DeclareGlobalFunction( "PositionsBound", [IsList] );
1030+
1031+
1032+
10051033
#############################################################################
10061034
##
10071035
#O PositionSublist( <list>, <sub>[, <from>] )

lib/list.gi

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1692,6 +1692,28 @@ InstallMethod( PositionBound,
16921692
end );
16931693

16941694

1695+
#############################################################################
1696+
##
1697+
#M PositionsBound( <list> ) . . . . . . . . . positions of all bound entries
1698+
##
1699+
InstallGlobalFunction( PositionsBound, function( list )
1700+
local i, bound;
1701+
1702+
if IsDenseList( list ) then
1703+
return [ 1 .. Length( list ) ];
1704+
fi;
1705+
1706+
bound := [];
1707+
for i in [ 1 .. Length( list ) ] do
1708+
if IsBound( list[i] ) then
1709+
Add( bound, i );
1710+
fi;
1711+
od;
1712+
1713+
return bound;
1714+
end );
1715+
1716+
16951717
#############################################################################
16961718
##
16971719
#M PositionSublist( <list>,<sub>[,<ind>] )

0 commit comments

Comments
 (0)