diff --git a/doc/ref/lists.xml b/doc/ref/lists.xml index 2c5f90243c..e440b79392 100644 --- a/doc/ref/lists.xml +++ b/doc/ref/lists.xml @@ -1410,6 +1410,7 @@ The latter can be done also using . <#Include Label="PositionProperty"> <#Include Label="PositionsProperty"> <#Include Label="PositionBound"> +<#Include Label="PositionsBound"> <#Include Label="PositionNot"> <#Include Label="PositionNonZero"> <#Include Label="PositionSublist"> diff --git a/lib/list.gd b/lib/list.gd index 8f7a6bb9b9..2eb4f8e45a 100644 --- a/lib/list.gd +++ b/lib/list.gd @@ -985,7 +985,7 @@ DeclareOperation( "PositionsProperty", [ IsList, IsFunction ] ); ## ## ## -## returns the first index for which an element is bound in the list +## returns the first bound position of the list ## list. ## For the empty list it returns fail. ##

@@ -1002,6 +1002,34 @@ DeclareOperation( "PositionsProperty", [ IsList, IsFunction ] ); DeclareOperation( "PositionBound", [ IsList ] ); +############################################################################# +## +#O PositionsBound( ) . . . . . . . . . positions of all bound entries +## +## <#GAPDoc Label="PositionsBound"> +## +## +## +## +## returns the set of all bound positions in the list +## list. +##

+## PositionsBound([1,2,3]); +## [ 1 .. 3 ] +## gap> PositionsBound([,1,,3]); +## [ 2, 4 ] +## gap> PositionsBound([]); +## [] +## ]]> +## +## +## <#/GAPDoc> +## +DeclareGlobalFunction( "PositionsBound", [IsList] ); + + + ############################################################################# ## #O PositionSublist( , [, ] ) diff --git a/lib/list.gi b/lib/list.gi index 362b250674..67952d8de5 100644 --- a/lib/list.gi +++ b/lib/list.gi @@ -1692,6 +1692,28 @@ InstallMethod( PositionBound, end ); +############################################################################# +## +#M PositionsBound( ) . . . . . . . . . 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( ,[,] )