ParaMonte Fortran 2.0.0
Parallel Monte Carlo and Machine Learning Library
See the latest version documentation.
pm_arrayFind::getCountLoc Interface Reference

Generate and return the number of occurrences of the input pattern in the input array optionally subject to user-specified memory blindness. More...

Detailed Description

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
  1. type character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU), or
  2. type integer of kind any supported by the processor (e.g., IK, IK8, IK16, IK32, or IK64), or
  3. type logical of kind any supported by the processor (e.g., LK), or
  4. type complex of kind any supported by the processor (e.g., CK, CK32, CK64, or CK128), or
  5. type real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128),
or
  1. 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,
  1. 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)
use pm_kind, only: IK, LK
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...
Definition: pm_kind.F90:268
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
Definition: pm_kind.F90:541
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
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)
use pm_kind, only: LK
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,
use pm_kind, only: SK, IK, LK, CK, RK
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...
Definition: pm_kind.F90:543
integer, parameter CK
The default complex kind in the ParaMonte library: real64 in Fortran, c_double_complex in C-Fortran I...
Definition: pm_kind.F90:542
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
Definition: pm_kind.F90:539
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

! `array` and `pattern` are scalar strings.
count = getCountLoc(array, pattern, blindness = blindness)
count = getCountLoc(array, pattern, iseq, blindness = blindness)
! `pattern` is a scalar of the same type and kind as `array`.
count = getCountLoc(array(:), pattern, blindness = blindness)
count = getCountLoc(array(:), pattern, iseq, blindness = blindness)
! `array` and `pattern` are both vectors.
count = getCountLoc(array(:), pattern(:), blindness = blindness)
count = getCountLoc(array(:), pattern(:), iseq, blindness = blindness)
! `array` and `pattern` are scalar strings.
count = getCountLoc(array, pattern, border, blindness = blindness)
count = getCountLoc(array, pattern, border, iseq, blindness = blindness)
! `pattern` is a scalar of the same type and kind as `array`.
count = getCountLoc(array(:), pattern, border, blindness = blindness)
count = getCountLoc(array(:), pattern, border, iseq, blindness = blindness)
! `array` and `pattern` are both vectors.
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

1program example
2
3 use pm_kind, only: SK, IK, LK
4 use pm_kind, only: IKG => IK ! All kinds are supported.
5 use pm_kind, only: CKG => CK ! All kinds are supported.
6 use pm_kind, only: RKG => RK ! All kinds are supported.
7 use pm_io, only: display_type
8 use pm_arrayFind, only: getCountLoc, discrete
9
10 implicit none
11
12 type(display_type) :: disp
13 disp = display_type(file = "main.out.F90")
14
15 !array = [1., 0., 2., 0., 3., 0., 4.]
16 !array = [(1., -1.), (0., -0.), (2., -2.), (0., -0.), (3., -3.), (0., -0.), (4., -4.)]
17 !array = [.false._LK, .true._LK, .false._LK, .true._LK, .false._LK, .true._LK, .false._LK]
18
19 !pattern = ["XXX"]
20 !pattern = [0_IK]
21 !pattern = [0._RK]
22 !pattern = [(0._CK, -0._CK)]
23 !pattern = [.true._LK]
24
25 call disp%skip()
26 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%")
27 call disp%show("!count character scalar.")
28 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%")
29 call disp%skip()
30
31 block
32 use pm_kind, only: SKG => SK ! All kinds are supported.
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 '
36 call disp%show("pattern = ' '")
37 pattern = ' '
38 call disp%show("getCountLoc(array, pattern)")
39 call disp%show( getCountLoc(array, pattern) )
40 call disp%show("getCountLoc(array, pattern, blindness = 3_IK)")
41 call disp%show( getCountLoc(array, pattern, blindness = 3_IK) )
42 call disp%show("getCountLoc(array, pattern, border = discrete)")
43 call disp%show( getCountLoc(array, pattern, border = discrete) )
44 call disp%show("getCountLoc(array, pattern, border = discrete, blindness = 3_IK)")
45 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.")
47 call disp%show( getCountLoc(array, SKG_'m', iseq_SK) )
48 call disp%show("getCountLoc(array, SKG_'m')")
49 call disp%show( getCountLoc(array, SKG_'m') )
50 end block
51
52 call disp%skip()
53 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%")
54 call disp%show("!count character array.")
55 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%")
56 call disp%skip()
57
58 block
59 use pm_kind, only: SKG => SK ! All kinds are supported.
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']
63 call disp%show("pattern = 'XXX'")
64 pattern = 'XXX'
65 call disp%show("getCountLoc(array, pattern)")
66 call disp%show( getCountLoc(array, pattern) )
67 call disp%show("getCountLoc(array, pattern, blindness = 3_IK)")
68 call disp%show( getCountLoc(array, pattern, blindness = 3_IK) )
69 call disp%show("getCountLoc(array, pattern, border = discrete)")
70 call disp%show( getCountLoc(array, pattern, border = discrete) )
71 call disp%show("getCountLoc(array, pattern, border = discrete, blindness = 3_IK)")
72 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.")
74 call disp%show( getCountLoc(array, SKG_'xxx', iseq_SK) )
75 call disp%show("getCountLoc(array, SKG_'xxx')")
76 call disp%show( getCountLoc(array, SKG_'xxx') )
77 call disp%show("getCountLoc(array, [character(3,SKG) :: 'XXX', 'XXX']) ! vector pattern")
78 call disp%show( getCountLoc(array, [character(3,SKG) :: 'XXX', 'XXX']) )
79 end block
80
81 call disp%skip()
82 call disp%show("!%%%%%%%%%%%%%%%%%%%%")
83 call disp%show("!count integer array.")
84 call disp%show("!%%%%%%%%%%%%%%%%%%%%")
85 call disp%skip()
86
87 block
88 use pm_kind, only: IKG => IK ! All kinds are supported.
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]
92 call disp%show("pattern = 0")
93 pattern = 0
94 call disp%show("getCountLoc(array, pattern)")
95 call disp%show( getCountLoc(array, pattern) )
96 call disp%show("getCountLoc(array, pattern, blindness = 3_IK)")
97 call disp%show( getCountLoc(array, pattern, blindness = 3_IK) )
98 call disp%show("getCountLoc(array, pattern, border = discrete)")
99 call disp%show( getCountLoc(array, pattern, border = discrete) )
100 call disp%show("getCountLoc(array, pattern, border = discrete, blindness = 3_IK)")
101 call disp%show( getCountLoc(array, pattern, border = discrete, blindness = 3_IK) )
102 call disp%show("pattern = 2")
103 pattern = 2
104 call disp%show("getCountLoc(array, pattern, iseq_IK) ! find any pattern+-1 with custom search.")
105 call disp%show( getCountLoc(array, pattern, iseq_IK) )
106 call disp%show("getCountLoc(array, pattern)")
107 call disp%show( getCountLoc(array, pattern) )
108 call disp%show("pattern = 0")
109 pattern = 0
110 call disp%show("getCountLoc(array, [pattern, pattern]) ! vector pattern")
111 call disp%show( getCountLoc(array, [pattern, pattern]) )
112 end block
113
114 call disp%skip()
115 call disp%show("!%%%%%%%%%%%%%%%%%%%%")
116 call disp%show("!count logical array.")
117 call disp%show("!%%%%%%%%%%%%%%%%%%%%")
118 call disp%skip()
119
120 block
121 use pm_kind, only: LKG => LK ! All kinds are supported.
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.]
125 call disp%show("pattern = .false.")
126 pattern = .false.
127 call disp%show("getCountLoc(array, pattern)")
128 call disp%show( getCountLoc(array, pattern) )
129 call disp%show("getCountLoc(array, pattern, blindness = 3_IK)")
130 call disp%show( getCountLoc(array, pattern, blindness = 3_IK) )
131 call disp%show("getCountLoc(array, pattern, border = discrete)")
132 call disp%show( getCountLoc(array, pattern, border = discrete) )
133 call disp%show("getCountLoc(array, pattern, border = discrete, blindness = 3_IK)")
134 call disp%show( getCountLoc(array, pattern, border = discrete, blindness = 3_IK) )
135 call disp%show("pattern = .true.")
136 pattern = .true.
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.].")
138 call disp%show( getCountLoc(array, [pattern, .not. pattern], iseqall_LK) )
139 call disp%show(.not."getCountLoc(array, [pattern, pattern])")
140 call disp%show( getCountLoc(array, [pattern, .not. pattern]) )
141 end block
142
143 call disp%skip()
144 call disp%show("!%%%%%%%%%%%%%%%%%%%%")
145 call disp%show("!count complex array.")
146 call disp%show("!%%%%%%%%%%%%%%%%%%%%")
147 call disp%skip()
148
149 block
150 use pm_kind, only: CKG => RKS ! All kinds are supported.
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)]
154 call disp%show("pattern = 0")
155 pattern = 0
156 call disp%show("getCountLoc(array, pattern)")
157 call disp%show( getCountLoc(array, pattern) )
158 call disp%show("getCountLoc(array, pattern, blindness = 3_IK)")
159 call disp%show( getCountLoc(array, pattern, blindness = 3_IK) )
160 call disp%show("getCountLoc(array, pattern, border = discrete)")
161 call disp%show( getCountLoc(array, pattern, border = discrete) )
162 call disp%show("getCountLoc(array, pattern, border = discrete, blindness = 3_IK)")
163 call disp%show( getCountLoc(array, pattern, border = discrete, blindness = 3_IK) )
164 call disp%show("pattern = (0., 0.) ! dummy search value.")
165 pattern = (0., 0.)
166 call disp%show("getCountLoc(array, pattern, iseq_CK) ! find any complex value whose components have opposite signs.")
167 call disp%show( getCountLoc(array, pattern, iseq_CK) )
168 call disp%show("getCountLoc(array, pattern)")
169 call disp%show( getCountLoc(array, pattern) )
170 call disp%show("pattern = 0")
171 pattern = 0
172 call disp%show("getCountLoc(array, [pattern, pattern]) ! vector pattern")
173 call disp%show( getCountLoc(array, [pattern, pattern]) )
174 end block
175
176 call disp%skip()
177 call disp%show("!%%%%%%%%%%%%%%%%%")
178 call disp%show("!count real array.")
179 call disp%show("!%%%%%%%%%%%%%%%%%")
180 call disp%skip()
181
182 block
183 use pm_kind, only: RKG => RKS ! All kinds are supported.
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]
187 call disp%show("pattern = 0")
188 pattern = 0
189 call disp%show("getCountLoc(array, pattern)")
190 call disp%show( getCountLoc(array, pattern) )
191 call disp%show("getCountLoc(array, pattern, blindness = 3_IK)")
192 call disp%show( getCountLoc(array, pattern, blindness = 3_IK) )
193 call disp%show("getCountLoc(array, pattern, border = discrete)")
194 call disp%show( getCountLoc(array, pattern, border = discrete) )
195 call disp%show("getCountLoc(array, pattern, border = discrete, blindness = 3_IK)")
196 call disp%show( getCountLoc(array, pattern, border = discrete, blindness = 3_IK) )
197 call disp%show("pattern = 2")
198 pattern = 2
199 call disp%show("getCountLoc(array, pattern, iseq_RK) ! find any whose absolute value is within 0.5 of `pattern`.")
200 call disp%show( getCountLoc(array, pattern, iseq_RK) )
201 call disp%show("getCountLoc(array, pattern)")
202 call disp%show( getCountLoc(array, pattern) )
203 call disp%show("pattern = 0")
204 pattern = 0
205 call disp%show("getCountLoc(array, [pattern, pattern]) ! vector pattern")
206 call disp%show( getCountLoc(array, [pattern, pattern]) )
207 end block
208
209contains
210
211 pure function iseq_SK(segment, pattern) result(equivalent)
212 use pm_strASCII, only: getStrLower
213 character(*, SK), intent(in) :: segment, pattern
214 logical(LK) :: equivalent
215 equivalent = pattern == getStrLower(segment)
216 end function
217
218 function iseq_IK(segment, pattern) result(equivalent)
219 use pm_kind, only: IKG => IK
220 integer(IKG) , intent(in) :: segment, pattern
221 logical(LK) :: equivalent
222 equivalent = pattern - 2 < segment .and. segment < pattern + 2
223 end function
224
225 function iseqall_LK(segment, pattern, lenPattern) result(equivalent)
226 use pm_kind, only: LKG => LK
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)
231 end function
232
233 function iseq_CK(segment, pattern) result(equivalent)
234 use pm_kind, only: CKG => RKS
235 complex(CKG) , intent(in) :: segment, pattern
236 logical(LK) :: equivalent
237 equivalent = segment%re * segment%im < 0._CKG
238 end function
239
240 function iseq_RK(segment, pattern) result(equivalent)
241 use pm_kind, only: RKG => RKS
242 real(RKG) , intent(in) :: segment, pattern
243 logical(LK) :: equivalent
244 equivalent = abs(abs(segment) - pattern) <= 0.5_RKG
245 end function
246
247end program example
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11726
This is a generic method of the derived type display_type with pass attribute.
Definition: pm_io.F90:11508
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...
Definition: pm_io.F90:252
type(display_type) disp
This is a scalar module variable an object of type display_type for general display.
Definition: pm_io.F90:11393
integer, parameter RKS
The single-precision real kind in Fortran mode. On most platforms, this is an 32-bit real kind.
Definition: pm_kind.F90:567
This module contains the uncommon and hardly representable ASCII characters as well as procedures for...
Definition: pm_strASCII.F90:61
Generate and return an object of type display_type.
Definition: pm_io.F90:10282

Example Unix compile command via Intel ifort compiler
1#!/usr/bin/env sh
2rm main.exe
3ifort -fpp -standard-semantics -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example Windows Batch compile command via Intel ifort compiler
1del main.exe
2set PATH=..\..\..\lib;%PATH%
3ifort /fpp /standard-semantics /O3 /I:..\..\..\include main.F90 ..\..\..\lib\libparamonte*.lib /exe:main.exe
4main.exe

Example Unix / MinGW compile command via GNU gfortran compiler
1#!/usr/bin/env sh
2rm main.exe
3gfortran -cpp -ffree-line-length-none -O3 -Wl,-rpath,../../../lib -I../../../inc main.F90 ../../../lib/libparamonte* -o main.exe
4./main.exe

Example output
1
2!%%%%%%%%%%%%%%%%%%%%%%%
3!count character scalar.
4!%%%%%%%%%%%%%%%%%%%%%%%
5
6array = 'Paramonte is a Machine Learning Library '
7pattern = ' '
8getCountLoc(array, pattern)
9+8
10getCountLoc(array, pattern, blindness = 3_IK)
11+5
12getCountLoc(array, pattern, border = discrete)
13+6
14getCountLoc(array, pattern, border = discrete, blindness = 3_IK)
15+4
16getCountLoc(array, SKG_'m', iseq_SK) ! find with custom case-insensitive search.
17+2
18getCountLoc(array, SKG_'m')
19+1
20
21!%%%%%%%%%%%%%%%%%%%%%%
22!count character array.
23!%%%%%%%%%%%%%%%%%%%%%%
24
25array = [character(9,SKG) :: 'xxx', 'Paramonte', 'XXX', 'is', 'XXX', 'a', 'XXX', 'Monte', 'XXX', 'Carlo', 'XXX', 'XXX', 'XXX', 'Library.', 'XXX']
26pattern = 'XXX'
27getCountLoc(array, pattern)
28+8
29getCountLoc(array, pattern, blindness = 3_IK)
30+4
31getCountLoc(array, pattern, border = discrete)
32+6
33getCountLoc(array, pattern, border = discrete, blindness = 3_IK)
34+4
35getCountLoc(array, SKG_'xxx', iseq_SK) ! find with custom case-insensitive search.
36+9
37getCountLoc(array, SKG_'xxx')
38+1
39getCountLoc(array, [character(3,SKG) :: 'XXX', 'XXX']) ! vector pattern
40+2
41
42!%%%%%%%%%%%%%%%%%%%%
43!count integer array.
44!%%%%%%%%%%%%%%%%%%%%
45
46array = [-1, 1, -2, 0, 0, 0, 2, 0, 3, 0, 4]
47pattern = 0
48getCountLoc(array, pattern)
49+5
50getCountLoc(array, pattern, blindness = 3_IK)
51+2
52getCountLoc(array, pattern, border = discrete)
53+3
54getCountLoc(array, pattern, border = discrete, blindness = 3_IK)
55+2
56pattern = 2
57getCountLoc(array, pattern, iseq_IK) ! find any pattern+-1 with custom search.
58+3
59getCountLoc(array, pattern)
60+1
61pattern = 0
62getCountLoc(array, [pattern, pattern]) ! vector pattern
63+2
64
65!%%%%%%%%%%%%%%%%%%%%
66!count logical array.
67!%%%%%%%%%%%%%%%%%%%%
68
69array = [.false., .true., .false., .true., .false., .false., .false., .true., .false.]
70pattern = .false.
71getCountLoc(array, pattern)
72+6
73getCountLoc(array, pattern, blindness = 3_IK)
74+3
75getCountLoc(array, pattern, border = discrete)
76+4
77getCountLoc(array, pattern, border = discrete, blindness = 3_IK)
78+3
79pattern = .true.
80getCountLoc(array, [pattern, .not. pattern], iseqall_LK) ! find any non-equivalent logical pair with custom search: [.true., .false.] or [.false., .true.].
81+6
82getCountLoc(array, [pattern, .not. pattern])
83+3
84
85!%%%%%%%%%%%%%%%%%%%%
86!count complex array.
87!%%%%%%%%%%%%%%%%%%%%
88
89array = [(-1., +1.), (1., 1.), (-1., 0.), (0., 0.), (0., 0.), (0., 0.), (0., 1.), (0., 0.), (3., -3.), (0., 0.), (4., -4)]
90pattern = 0
91getCountLoc(array, pattern)
92+5
93getCountLoc(array, pattern, blindness = 3_IK)
94+2
95getCountLoc(array, pattern, border = discrete)
96+3
97getCountLoc(array, pattern, border = discrete, blindness = 3_IK)
98+2
99pattern = (0., 0.) ! dummy search value.
100getCountLoc(array, pattern, iseq_CK) ! find any complex value whose components have opposite signs.
101+3
102getCountLoc(array, pattern)
103+5
104pattern = 0
105getCountLoc(array, [pattern, pattern]) ! vector pattern
106+2
107
108!%%%%%%%%%%%%%%%%%
109!count real array.
110!%%%%%%%%%%%%%%%%%
111
112array = [-1, 1, -2, 0, 0, 0, 2, 0, 3, 0, 4]
113pattern = 0
114getCountLoc(array, pattern)
115+5
116getCountLoc(array, pattern, blindness = 3_IK)
117+2
118getCountLoc(array, pattern, border = discrete)
119+3
120getCountLoc(array, pattern, border = discrete, blindness = 3_IK)
121+2
122pattern = 2
123getCountLoc(array, pattern, iseq_RK) ! find any whose absolute value is within 0.5 of `pattern`.
124+2
125getCountLoc(array, pattern)
126+1
127pattern = 0
128getCountLoc(array, [pattern, pattern]) ! vector pattern
129+2
130
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(:) ! catastrophic internal error with ifort 2021.2. Fine with gfortran 10.3
!character(:, SK) , allocatable :: count(:) ! catastrophic internal error with gfortran 10.3. Fine with ifort 2021.2
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
program main
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.
Definition: main.F90:27

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(:) ! catastrophic internal error with ifort 2021.2. Fine with gfortran 10.3
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
program main
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.

  1. 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.
  2. 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.

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.


The documentation for this interface was generated from the following file: