Check if the queried number n is admitted by the admissible range list adm_range.
The admissible range list must be formatted as string containing admissible numbers; valid list are:
+ adm_range = '1-5'
=> 1, 2, 3, 4, 5 are admissible numbers;
+ adm_range = '1,3,5,10-12'
=> 1, 3, 5, 10, 11, 12 are admissible numbers;
+ adm_range = '1-4,8,21-22'
=> 1, 2, 3, 4, 8, 21, 22 are admissible numbers;
You can mix any number of range (min-max
format) and/or single number (,
comma separated) entries.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=I_P), | intent(in) | :: | n | Number queried. |
||
character(len=*), | intent(in) | :: | adm_range | Admissible range string. |
Is true is the number is in adm_range.
Nodes of different colours represent the following:
Solid arrows point from a procedure to one which it calls. Dashed arrows point from an interface to procedures which implement that interface. This could include the module procedures in a generic interface or the implementation in a submodule of an interface in a parent module.
elemental function is_admissible(n, adm_range)
!< Check if the queried number *n* is admitted by the *admissible* range list *adm_range*.
!<
!< The admissible range list must be formatted as string containing admissible numbers; valid list are:
!<+ `adm_range = '1-5'` => 1, 2, 3, 4, 5 are admissible numbers;
!<+ `adm_range = '1,3,5,10-12'` => 1, 3, 5, 10, 11, 12 are admissible numbers;
!<+ `adm_range = '1-4,8,21-22'` => 1, 2, 3, 4, 8, 21, 22 are admissible numbers;
!<
!< You can mix any number of range (`min-max` format) and/or single number (`,` comma separated) entries.
integer(I_P), intent(IN) :: n !< Number queried.
character(*), intent(IN) :: adm_range !< Admissible range string.
logical :: is_admissible !< Is true is the number is in *adm_range*.
character(len(adm_range)), allocatable :: tokens(:) !< Tokens for parsing *adm_range* string.
character(len(adm_range)), allocatable :: subtokens(:) !< Tokens for parsing *adm_range* string.
integer(I_P) :: t !< Counter.
integer(I_P) :: n_parsed(1:2) !< Values parsed from *adm_range*..
is_admissible = .false.
call tokenize(string=adm_range, delimiter=',', toks=tokens)
search_me : do t=1, size(tokens)
if (index(tokens(t), '-')>0) then
call tokenize(string=tokens(t), delimiter='-', toks=subtokens)
read(subtokens(1), *) n_parsed(1)
read(subtokens(2), *) n_parsed(2)
is_admissible = (n_parsed(1)<=n.and.n<=n_parsed(2))
else
read(tokens(t), *) n_parsed(1)
is_admissible = (n_parsed(1)==n)
endif
if (is_admissible) exit search_me
enddo search_me
endfunction is_admissible