Generate and return the number of occurrences of the input pattern
in the input array
optionally subject to user-specified memory blindness.
The instances of pattern
are found via linear search.
Therefore, the procedures under this generic interface have a worst-case complexity of O(size(array))
.
- Parameters
-
[in] | array | : The input contiguous array of rank 1 of either
-
type
character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU), or
-
type
integer of kind any supported by the processor (e.g., IK, IK8, IK16, IK32, or IK64), or
-
type
logical of kind any supported by the processor (e.g., LK), or
-
type
complex of kind any supported by the processor (e.g., CK, CK32, CK64, or CK128), or
-
type
real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
or
-
a scalar assumed-length
character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU),
within which the starting indices of the requested instances of pattern is to be found.
|
[in] | pattern | : The input object of the same or lower rank than the input array , and of the same type and kind as array containing the pattern that must be found within the input array .
|
[in] | border | : The input scalar constant object that can be any of the following,
-
the constant discrete or an object of type discrete_type, implying that only non-overlapping and non-adjacent pattern locations must be identified.
For example, if pattern is a blank character, and multiple adjacent blanks appear in array , the only the location of the first blank is identified and the rest are ignored until a blank reappears separately from the first group of contiguous blanks.
Note that border adjacency and overlapping is measured by the input blindness and not by the length of pattern .
This option is extremely useful for identifying and parsing separators that can be repeated in text file records, for example, the white-space (blank) character in list-directed Fortran IO.
(optional. The default behavior does not recognize any borders for pattern .) |
| iseq | : The external user-specified function that takes two input explicit-shape arguments of the same type and kind as the input array and possibly, also the length of the arguments as the third argument, if the arguments are array-valued.
It returns a scalar logical of default kind LK that is .true. if all elements of the two input arguments are equivalent (e.g., equal) according to the user-defined criterion, otherwise, it is .false. .
If pattern is an array of rank 1 , then the last argument to iseq is the length of the input pattern , preceded by a segment of array and pattern as the first and second arguments, whose lengths are given by the third argument lenPattern .
The following illustrates the generic interface of iseq where pattern is array-valued, function iseq(Segment, pattern, lenPattern) result(equivalent)
integer(IK) , intent(in) :: lenPattern
TYPE(KIND) , intent(in) :: Segment(lenPattern), pattern(lenPattern)
logical(LK) :: equivalent
end function
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
integer, parameter LK The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
integer, parameter IK The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
where TYPE(KIND) represents the type and kind of the input argument array , which can be one of the following,
character(*, SK), intent(in) :: Segment(lenPattern), pattern(lenPattern)
integer(IK) , intent(in) :: Segment(lenPattern), pattern(lenPattern)
logical(LK) , intent(in) :: Segment(lenPattern), pattern(lenPattern)
complex(CK) , intent(in) :: Segment(lenPattern), pattern(lenPattern)
real(RK) , intent(in) :: Segment(lenPattern), pattern(lenPattern)
where the kinds SK , IK , LK , CK , RK , can refer to any kind type parameter that is supported by the processor.
The following illustrates the generic interface of iseq where pattern is scalar-valued (including Fortran scalar strings),
function iseq(segment, pattern) result(equivalent)
TYPE(KIND) , intent(in) :: segment, pattern
logical(LK) :: equivalent
end function
where TYPE(KIND) represents the type and kind of the input argument array , which can be one of the following,
character(*, SK), intent(in) :: segment, pattern
integer(IK) , intent(in) :: segment, pattern
logical(LK) , intent(in) :: segment, pattern
complex(CK) , intent(in) :: segment, pattern
real(RK) , intent(in) :: segment, pattern
integer, parameter RK The default real kind in the ParaMonte library: real64 in Fortran, c_double in C-Fortran Interoperati...
integer, parameter CK The default complex kind in the ParaMonte library: real64 in Fortran, c_double_complex in C-Fortran I...
integer, parameter SK The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
where the kinds SK , IK , LK , CK , RK , can refer to any kind type parameter that is supported by the processor.
This user-defined equivalence check is extremely useful where a user-defined equivalence test other than exact equality or identity is needed, for example, when the array segments should match the input pattern only within a given threshold or, when the case-sensitivity in character comparisons do not matter.
In such cases, user can define a custom equivalence criterion within the user-defined external function iseq to achieve the goal.
(optional, the default equivalence operator is .eqv. if the input array is logical , otherwise == .) |
[in] | blindness | : The input positive integer of default kind IK representing the length of the segment of array that should be ignored after finding an instance of the input pattern in the array.
Setting blindness = len(pattern) (for assumed-length character pattern ) or blindness = size(pattern) (for other types of array-valued pattern ) will lead to a search for exclusive non-overlapping instances of pattern in the input array .
See the examples below for more illustration of the utility of this input argument.
(optional, default = 1_IK ) |
- Returns
count
: The output non-negative scalar of type integer
of default kind IK containing the number of times the input pattern appears in the input array
.
Possible calling interfaces ⛓
count
= getCountLoc(array, pattern, blindness
= blindness)
count
= getCountLoc(array, pattern, iseq, blindness
= blindness)
count
= getCountLoc(array(:), pattern, blindness
= blindness)
count
= getCountLoc(array(:), pattern, iseq, blindness
= blindness)
count
= getCountLoc(array(:), pattern(:), blindness
= blindness)
count
= getCountLoc(array(:), pattern(:), iseq, blindness
= blindness)
count
= getCountLoc(array, pattern, border, blindness
= blindness)
count
= getCountLoc(array, pattern, border, iseq, blindness
= blindness)
count
= getCountLoc(array(:), pattern, border, blindness
= blindness)
count
= getCountLoc(array(:), pattern, border, iseq, blindness
= blindness)
count
= getCountLoc(array(:), pattern(:), border, blindness
= blindness)
count
= getCountLoc(array(:), pattern(:), border, iseq, blindness
= blindness)
!
Generate and return the number of occurrences of the input pattern in the input array optionally subj...
This module contains procedures and generic interfaces for finding locations of a pattern in arrays o...
- Warning
- The condition
0 < blindness
must hold for the corresponding input arguments.
This condition is verified only if the library is built with the preprocessor macro CHECK_ENABLED=1
.
-
The procedures under this generic interface are
impure
when the user-specified external
procedure iseq
is specified as input argument.
-
Note that in Fortran, trailing blanks are ignored in character comparison, that is,
"Fortran" == "Fortran "
yields .true.
.
-
The
pure
procedure(s) documented herein become impure
when the ParaMonte library is compiled with preprocessor macro CHECK_ENABLED=1
.
By default, these procedures are pure
in release
build and impure
in debug
and testing
builds.
- See also
- setLoc
getBin
setReplaced
getReplaced
setInserted
setSplit
Example usage ⛓
12 type(display_type) :: disp
26 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%")
27 call disp%show(
"!count character scalar.")
28 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%")
33 character(:,SKG),
allocatable :: array, pattern
34 call disp%show(
"array = 'Paramonte is a Machine Learning Library '")
35 array
= 'Paramonte is a Machine Learning Library '
38 call disp%show(
"getCountLoc(array, pattern)")
40 call disp%show(
"getCountLoc(array, pattern, blindness = 3_IK)")
42 call disp%show(
"getCountLoc(array, pattern, border = discrete)")
44 call disp%show(
"getCountLoc(array, pattern, border = discrete, blindness = 3_IK)")
46 call disp%show(
"getCountLoc(array, SKG_'m', iseq_SK) ! find with custom case-insensitive search.")
48 call disp%show(
"getCountLoc(array, SKG_'m')")
53 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%")
54 call disp%show(
"!count character array.")
55 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%")
60 character(
9,SKG),
allocatable :: array(:), pattern
61 call disp%show(
"array = [character(9,SKG) :: 'xxx', 'Paramonte', 'XXX', 'is', 'XXX', 'a', 'XXX', 'Monte', 'XXX', 'Carlo', 'XXX', 'XXX', 'XXX', 'Library.', 'XXX']")
62 array
= [
character(
9,SKG) ::
'xxx',
'Paramonte',
'XXX',
'is',
'XXX',
'a',
'XXX',
'Monte',
'XXX',
'Carlo',
'XXX',
'XXX',
'XXX',
'Library.',
'XXX']
65 call disp%show(
"getCountLoc(array, pattern)")
67 call disp%show(
"getCountLoc(array, pattern, blindness = 3_IK)")
69 call disp%show(
"getCountLoc(array, pattern, border = discrete)")
71 call disp%show(
"getCountLoc(array, pattern, border = discrete, blindness = 3_IK)")
73 call disp%show(
"getCountLoc(array, SKG_'xxx', iseq_SK) ! find with custom case-insensitive search.")
75 call disp%show(
"getCountLoc(array, SKG_'xxx')")
77 call disp%show(
"getCountLoc(array, [character(3,SKG) :: 'XXX', 'XXX']) ! vector pattern")
82 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%")
83 call disp%show(
"!count integer array.")
84 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%")
89 integer(IKG),
allocatable :: array(:), pattern
90 call disp%show(
"array = [-1, 1, -2, 0, 0, 0, 2, 0, 3, 0, 4]")
91 array
= [
-1,
1,
-2,
0,
0,
0,
2,
0,
3,
0,
4]
94 call disp%show(
"getCountLoc(array, pattern)")
96 call disp%show(
"getCountLoc(array, pattern, blindness = 3_IK)")
98 call disp%show(
"getCountLoc(array, pattern, border = discrete)")
100 call disp%show(
"getCountLoc(array, pattern, border = discrete, blindness = 3_IK)")
104 call disp%show(
"getCountLoc(array, pattern, iseq_IK) ! find any pattern+-1 with custom search.")
106 call disp%show(
"getCountLoc(array, pattern)")
110 call disp%show(
"getCountLoc(array, [pattern, pattern]) ! vector pattern")
115 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%")
116 call disp%show(
"!count logical array.")
117 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%")
122 logical(LKG),
allocatable :: array(:), pattern
123 call disp%show(
"array = [.false., .true., .false., .true., .false., .false., .false., .true., .false.]")
124 array
= [
.false.,
.true.,
.false.,
.true.,
.false.,
.false.,
.false.,
.true.,
.false.]
127 call disp%show(
"getCountLoc(array, pattern)")
129 call disp%show(
"getCountLoc(array, pattern, blindness = 3_IK)")
131 call disp%show(
"getCountLoc(array, pattern, border = discrete)")
133 call disp%show(
"getCountLoc(array, pattern, border = discrete, blindness = 3_IK)")
137 call disp%show(
.not."getCountLoc(array, [pattern, pattern], iseqall_LK) ! find any non-equivalent logical pair with custom search: [.true., .false.] or [.false., .true.].")
139 call disp%show(
.not."getCountLoc(array, [pattern, pattern])")
144 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%")
145 call disp%show(
"!count complex array.")
146 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%")
151 complex(CKG),
allocatable :: array(:), pattern
152 call disp%show(
"array = [(-1., +1.), (1., 1.), (-1., 0.), (0., 0.), (0., 0.), (0., 0.), (0., 1.), (0., 0.), (3., -3.), (0., 0.), (4., -4)]")
153 array
= [(
-1.,
+1.), (
1.,
1.), (
-1.,
0.), (
0.,
0.), (
0.,
0.), (
0.,
0.), (
0.,
1.), (
0.,
0.), (
3.,
-3.), (
0.,
0.), (
4.,
-4)]
156 call disp%show(
"getCountLoc(array, pattern)")
158 call disp%show(
"getCountLoc(array, pattern, blindness = 3_IK)")
160 call disp%show(
"getCountLoc(array, pattern, border = discrete)")
162 call disp%show(
"getCountLoc(array, pattern, border = discrete, blindness = 3_IK)")
164 call disp%show(
"pattern = (0., 0.) ! dummy search value.")
166 call disp%show(
"getCountLoc(array, pattern, iseq_CK) ! find any complex value whose components have opposite signs.")
168 call disp%show(
"getCountLoc(array, pattern)")
172 call disp%show(
"getCountLoc(array, [pattern, pattern]) ! vector pattern")
177 call disp%show(
"!%%%%%%%%%%%%%%%%%")
178 call disp%show(
"!count real array.")
179 call disp%show(
"!%%%%%%%%%%%%%%%%%")
184 real(RKG),
allocatable :: array(:), pattern
185 call disp%show(
"array = [-1, 1, -2, 0, 0, 0, 2, 0, 3, 0, 4]")
186 array
= [
-1,
1,
-2,
0,
0,
0,
2,
0,
3,
0,
4]
189 call disp%show(
"getCountLoc(array, pattern)")
191 call disp%show(
"getCountLoc(array, pattern, blindness = 3_IK)")
193 call disp%show(
"getCountLoc(array, pattern, border = discrete)")
195 call disp%show(
"getCountLoc(array, pattern, border = discrete, blindness = 3_IK)")
199 call disp%show(
"getCountLoc(array, pattern, iseq_RK) ! find any whose absolute value is within 0.5 of `pattern`.")
201 call disp%show(
"getCountLoc(array, pattern)")
205 call disp%show(
"getCountLoc(array, [pattern, pattern]) ! vector pattern")
211 pure function iseq_SK(segment, pattern)
result(equivalent)
213 character(
*, SK),
intent(in) :: segment, pattern
214 logical(LK) :: equivalent
218 function iseq_IK(segment, pattern)
result(equivalent)
220 integer(IKG) ,
intent(in) :: segment, pattern
221 logical(LK) :: equivalent
222 equivalent
= pattern
- 2 < segment
.and. segment
< pattern
+ 2
225 function iseqall_LK(segment, pattern, lenPattern)
result(equivalent)
227 integer(IK) ,
intent(in) :: lenPattern
228 logical(LKG) ,
intent(in) :: pattern(lenPattern), segment(lenPattern)
229 logical(LK) :: equivalent
230 equivalent
= segment(
1)
.neqv. segment(
2)
233 function iseq_CK(segment, pattern)
result(equivalent)
235 complex(CKG) ,
intent(in) :: segment, pattern
236 logical(LK) :: equivalent
237 equivalent
= segment
%re
* segment
%im
< 0._CKG
240 function iseq_RK(segment, pattern)
result(equivalent)
242 real(RKG) ,
intent(in) :: segment, pattern
243 logical(LK) :: equivalent
244 equivalent
= abs(abs(segment)
- pattern)
<= 0.5_RKG
This is a generic method of the derived type display_type with pass attribute.
This is a generic method of the derived type display_type with pass attribute.
Generate and return the input string where the uppercase English alphabets are all converted to lower...
This module contains classes and procedures for input/output (IO) or generic display operations on st...
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
integer, parameter RKS
The single-precision real kind in Fortran mode. On most platforms, this is an 32-bit real kind.
This module contains the uncommon and hardly representable ASCII characters as well as procedures for...
Generate and return an object of type display_type.
Example Unix compile command via Intel ifort
compiler ⛓
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example Windows Batch compile command via Intel ifort
compiler ⛓
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
Example Unix / MinGW compile command via GNU gfortran
compiler ⛓
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
Example output ⛓
6array
= 'Paramonte is a Machine Learning Library '
14getCountLoc(array, pattern, border
= discrete, blindness
= 3_IK)
25array
= [
character(
9,SKG) ::
'xxx',
'Paramonte',
'XXX',
'is',
'XXX',
'a',
'XXX',
'Monte',
'XXX',
'Carlo',
'XXX',
'XXX',
'XXX',
'Library.',
'XXX']
33getCountLoc(array, pattern, border
= discrete, blindness
= 3_IK)
39getCountLoc(array, [
character(
3,SKG) ::
'XXX',
'XXX'])
46array
= [
-1,
1,
-2,
0,
0,
0,
2,
0,
3,
0,
4]
54getCountLoc(array, pattern, border
= discrete, blindness
= 3_IK)
69array
= [
.false.,
.true.,
.false.,
.true.,
.false.,
.false.,
.false.,
.true.,
.false.]
77getCountLoc(array, pattern, border
= discrete, blindness
= 3_IK)
80getCountLoc(array, [pattern,
.not. pattern], iseqall_LK)
89array
= [(
-1.,
+1.), (
1.,
1.), (
-1.,
0.), (
0.,
0.), (
0.,
0.), (
0.,
0.), (
0.,
1.), (
0.,
0.), (
3.,
-3.), (
0.,
0.), (
4.,
-4)]
97getCountLoc(array, pattern, border
= discrete, blindness
= 3_IK)
112array
= [
-1,
1,
-2,
0,
0,
0,
2,
0,
3,
0,
4]
120getCountLoc(array, pattern, border
= discrete, blindness
= 3_IK)
- Test:
- test_pm_arrayFind
- Bug:
Status: Unresolved
Source: Intel Classic Fortran Compiler ifort
version 2021.2.0, GNU Fortran Compiler gfortran
version 10-12
Description: The Intel Fortran compiler Classic 2021.2.0 has a bug for the following interface definition
character(len(array),SK), allocatable :: count(:)
leading to an internal compiler error.
For now, the remedy seems to be to redefine the interface as,
character(:, SK), allocatable :: count(:)
and changing the allocation method accordingly in the implementation to,
allocate(character(len(array, kind = IK)) :: count(lenLoc))
However, this introduces internal compiler error: Segmentation fault
with gfortran versions 10 and 11.
Here is a code snippet to regenerate the bug in Intel ifort (uncomment the commented line to reproduce the gfortran bug),
module pm_explicitLenResult
implicit none
interface
pure module function bug(array) result(count)
character(*, SK), intent(in), contiguous :: array(:)
character(len(array),SK) , allocatable :: count(:)
end function
end interface
end module pm_explicitLenResult
submodule (pm_explicitLenResult) routines
implicit none
contains
module procedure bug
allocate(count, source = array)
end procedure
end submodule routines
use pm_explicitLenResult, only: bug
character(2) :: array(3) = ["AA", "BB", "CC"]
character(2), allocatable :: count(:)
count = bug(array)
end program main
program main
This is main entry to the tests of the ParaMonte kernel library.
It turns out that both gfortran and Intel do not tolerate the separation of interface from implementation in the above code snippet.
Remedy (as of ParaMonte Library version 2.0.0): If one duplicates the interface in the implementation submodule, then both compilers compile and run the code with no errors.
This is the remedy that is currently used in this getCountLoc generic interface (interface duplication where the bug exists).
Here is a workaround example for the bug in the above code snippet,
module pm_explicitLenResult
implicit none
interface
pure module function bug(array) result(count)
character(*, SK), intent(in), contiguous :: array(:)
character(len(array),SK), allocatable :: count(:)
end function
end interface
end module pm_explicitLenResult
submodule (pm_explicitLenResult) routines
implicit none
contains
module procedure bug
allocate(count, source = array)
end procedure
end submodule routines
use pm_explicitLenResult, only: bug
character(2) :: array(3) = ["AA", "BB", "CC"]
character(2), allocatable :: count(:)
count = bug(array)
end program main
- Todo:
- Low Priority: This generic interface can be extended to higher-dimensional input arrays.
- Todo:
- Critical Priority: Currently, the value of
blindness
is checked for being non-zero in the implementation.
However, the documentation of blindness
requires it to be positive.
This conflict between the implementation and documentation must be resolved.
- Todo:
- Normal Priority: The functionality of this generic interface can be extended with an optional
border
argument as in getCountLoc.
Final Remarks ⛓
If you believe this algorithm or its documentation can be improved, we appreciate your contribution and help to edit this page's documentation and source file on GitHub.
For details on the naming abbreviations, see this page.
For details on the naming conventions, see this page.
This software is distributed under the MIT license with additional terms outlined below.
-
If you use any parts or concepts from this library to any extent, please acknowledge the usage by citing the relevant publications of the ParaMonte library.
-
If you regenerate any parts/ideas from this library in a programming environment other than those currently supported by this ParaMonte library (i.e., other than C, C++, Fortran, MATLAB, Python, R), please also ask the end users to cite this original ParaMonte library.
This software is available to the public under a highly permissive license.
Help us justify its continued development and maintenance by acknowledging its benefit to society, distributing it, and contributing to it.
- Copyright
- Computational Data Science Lab
- Author:
- Amir Shahmoradi, September 1, 2017, 12:00 AM, Institute for Computational Engineering and Sciences (ICES), The University of Texas Austin
Definition at line 366 of file pm_arrayFind.F90.