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

Replace the requested instances of the input pattern with the input replacement in the allocatable input/output array. More...

Detailed Description

Replace the requested instances of the input pattern with the input replacement in the allocatable input/output array.

If an input vector of instance is specified, representing the specific instances of pattern to change, then only those specific instances will be changed.

Parameters
[in,out]array: The input/output allocatable array of rank 1 of either
  • type character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU),
  • type logical of kind any supported by the processor (e.g., LK), or
  • type integer of kind any supported by the processor (e.g., IK, IK8, IK16, IK32, or IK64), 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
scalar character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU),
within which the specific instances of the input pattern must be replaced.
On output, the array will be possibly (if needed) reallocated to its new value
with the requested instances of pattern replaced with replacement.
[in]pattern: The input contiguous array of rank 1 of the same type and kind as the input array, containing the pattern whose instances will have to be replaced in the input array.
[in]replacement: The input contiguous array of rank 1 of the same type and kind as the input array, containing the replacement that will replace in the instances of pattern in the input array.
iseq: The external user-specified function that takes either two input assumed-length character arguments (if the input array is also an assumed-length character) or two array-valued explicit-shape arguments of the same type and kind as the input array.
It must return a scalar of type 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..
The the input array is array-valued, then the last argument to iseq is the length of the input pattern.
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,
use pm_kind, only: SK, IK, LK, CK, RK
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)
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.
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,
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
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 an equivalence test other than exact 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]instance: The input contiguous array of rank 1 of type integer of default kind IK, containing the instances of the input pattern in the input array that should be replaced with the input replacement.
Any element of instance that points to an out-of-scope instance of pattern in the input array will be ignored.
Any element of instance that is negatively valued will be counted from end of the input array.
For example, instance = [2,-1] requests replacing the second instance of pattern in array from the beginning and replacing the first instance of pattern starting from the end of array.
(optional, the default value corresponds to replacing all instances of pattern with replacement in array)
[in]sorted: The input logical of default kind LK indicating whether the elements of the specified input instance are all in ascending-order.
This includes the negative elements of instance after they are translated to the corresponding positive instances from the beginning of the input array.
Setting sorted = .true. will lead to faster runtime of the procedure.
However, the onus will be strictly on the user to ensure all elements of instance are in ascending-order.
This is generally not an easy guarantee to make if there are negative elements in instance.
Therefore, set sorted = .true. only if you can guarantee the validity of the condition.
(optional, default = .false.. It can be present as input argument only if the input argument instance is present.)
[in]unique: The input logical of default kind LK indicating whether the elements of the specified input instance are all unique.
This includes the negative elements of instance after they are translated to the corresponding positive instances from the beginning of the input array.
Setting unique = .true. will lead to faster runtime of the procedure.
However, the onus will be strictly on the user to ensure all elements of instance are unique.
This is generally not an easy guarantee to make if there are negative elements in instance.
Therefore, set unique = .true. only if you can guarantee the validity of the condition.
(optional, default = .false.. It can be present as input argument only if the input argument instance is present.)


Possible calling interfaces

call setReplaced(array, pattern, replacement)
call setReplaced(array, pattern, replacement, iseq)
call setReplaced(array, pattern, replacement, instance, sorted = sorted, unique = unique)
call setReplaced(array, pattern, replacement, iseq, instance, sorted = sorted, unique = unique)
Replace the requested instances of the input pattern with the input replacement in the allocatable in...
This module contains procedures and generic interfaces for replacing patterns within arrays of variou...
Warning
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.
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..
Remarks
The logic behind making the argument array an allocatable with intent(inout) is to make the procedures potentially more efficient with a well-defined behavior.
The alternative to the current interface is to pass an extra intent(out), allocatable :: arrayNew(:) argument along with an intent(in), contiguous :: array(:).
This, however, leads to unnecessary copies of array to arrayNew when no replacement practically has occurred.
Furthermore, the size of the output arrayNew must be set a priori by the user which is only determined within the algorithm.
The subroutines under this interface are slightly faster than the getReplaced function implementations.
See pm_arrayReplace for the relevant benchmarks.
Note
The procedures under this generic interface perform replacements within the input array in-place.
To return the result in a new array, use getReplaced.
See pm_arrayReplace for the relevant performance benchmarks.
Upon return, the allocatable, intent(inout) :: array argument is guaranteed to have the same lower bound as before, although its upper bound is potentially different.
See also
getReplaced
setInserted
setRemoved
setSplit


Example usage

1program example
2
3 use pm_kind, only: LK
4 use pm_kind, only: SK ! All kinds are supported.
5 use pm_kind, only: IK ! All kinds are supported.
6 use pm_kind, only: CK ! All kinds are supported.
7 use pm_kind, only: RK ! All kinds are supported.
8 use pm_io, only: display_type
10
11 implicit none
12
13 integer(IK) , allocatable :: instance(:) ! Must be of default kind IK
14 character(:, SK), allocatable :: string_SK , stringPattern_SK , stringReplacement_SK
15 character(9, SK), allocatable :: Array_SK(:) , ArrayPattern_SK(:), ArrayReplacement_SK(:) ! Can be any processor-supported kind.
16 integer(IK) , allocatable :: Array_IK(:) , ArrayPattern_IK(:), ArrayReplacement_IK(:) ! Can be any processor-supported kind.
17 complex(CK) , allocatable :: Array_CK(:) , ArrayPattern_CK(:), ArrayReplacement_CK(:) ! Can be any processor-supported kind.
18 real(RK) , allocatable :: Array_RK(:) , ArrayPattern_RK(:), ArrayReplacement_RK(:) ! Can be any processor-supported kind.
19 logical(LK) , allocatable :: Array_LK(:) , ArrayPattern_LK(:), ArrayReplacement_LK(:)
20
21 type(display_type) :: disp
22
23 disp = display_type(file = "main.out.F90")
24
25 call disp%skip()
26 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
27 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
28 call disp%show("! Replace all instances of pattern in array.")
29 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
30 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
31 call disp%skip()
32
33 string_SK = "ParaMonte is a Monte Carlo Library."
34 Array_SK = ["ParaMonte", "is ", "a ", "Monte ", "Carlo ", "Library. "]
35 Array_IK = [1_IK, 2_IK, 3_IK, 4_IK]
36 Array_RK = [1._RK, 2._RK, 3._RK, 4._RK]
37 Array_CK = [(1._CK, -1._CK), (2._CK, -2._CK), (3._CK, -3._CK), (4._CK, -4._CK)]
38 Array_LK = [.false._LK, .true._LK, .true._LK, .false._LK]
39
40 stringPattern_SK = "a Monte Carlo"
41 ArrayPattern_SK = ["a ", "Monte", "Carlo"]
42 ArrayPattern_IK = [3_IK]
43 ArrayPattern_RK = [3._RK]
44 ArrayPattern_CK = [(3._CK, -3._CK)]
45 ArrayPattern_LK = [.true._LK, .true._LK]
46
47 stringReplacement_SK = "now a Machine Learning"
48 ArrayReplacement_SK = ["now ", "a ", "Machine ", "Learning"]
49 ArrayReplacement_IK = [-1_IK, 0_IK, 1_IK]
50 ArrayReplacement_RK = [-1._RK, 0._RK, 1._RK]
51 ArrayReplacement_CK = [(-1._CK, +1._CK), (0._CK, 0._CK), (+1._CK, -1._CK)]
52 ArrayReplacement_LK = [.false._LK]
53
54 call disp%skip()
55 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%")
56 call disp%show("! Replace character scalar.")
57 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%")
58 call disp%skip()
59
60 call disp%show("string_SK")
61 call disp%show( string_SK, deliml = SK_"""" )
62 call disp%show("stringPattern_SK")
63 call disp%show( stringPattern_SK, deliml = SK_"""" )
64 call disp%show("stringReplacement_SK")
65 call disp%show( stringReplacement_SK, deliml = SK_"""" )
66 call disp%show("call setReplaced(string_SK, stringPattern_SK, stringReplacement_SK)")
67 call setReplaced(string_SK, stringPattern_SK, stringReplacement_SK)
68 call disp%show("string_SK")
69 call disp%show( string_SK, deliml = SK_"""" )
70
71 call disp%skip()
72 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%")
73 call disp%show("! Replace character array.")
74 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%")
75 call disp%skip()
76
77 call disp%show("Array_SK")
78 call disp%show( Array_SK, deliml = SK_"""" )
79 call disp%show("ArrayPattern_SK")
80 call disp%show( ArrayPattern_SK, deliml = SK_"""" )
81 call disp%show("ArrayReplacement_SK")
82 call disp%show( ArrayReplacement_SK, deliml = SK_"""" )
83 call disp%show("call setReplaced(Array_SK, ArrayPattern_SK, ArrayReplacement_SK)")
84 call setReplaced(Array_SK, ArrayPattern_SK, ArrayReplacement_SK)
85 call disp%show("Array_SK")
86 call disp%show( Array_SK, deliml = SK_"""" )
87
88 call disp%skip()
89 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%")
90 call disp%show("! Replace logical array.")
91 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%")
92 call disp%skip()
93
94 call disp%show("Array_LK")
95 call disp%show( Array_LK )
96 call disp%show("ArrayPattern_LK")
97 call disp%show( ArrayPattern_LK )
98 call disp%show("ArrayReplacement_LK")
99 call disp%show( ArrayReplacement_LK )
100 call disp%show("call setReplaced(Array_LK, ArrayPattern_LK, ArrayReplacement_LK)")
101 call setReplaced(Array_LK, ArrayPattern_LK, ArrayReplacement_LK)
102 call disp%show("Array_LK")
103 call disp%show( Array_LK )
104
105 call disp%skip()
106 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%")
107 call disp%show("! Replace integer array.")
108 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%")
109 call disp%skip()
110
111 call disp%show("Array_IK")
112 call disp%show( Array_IK )
113 call disp%show("ArrayPattern_IK")
114 call disp%show( ArrayPattern_IK )
115 call disp%show("ArrayReplacement_IK")
116 call disp%show( ArrayReplacement_IK )
117 call disp%show("call setReplaced(Array_IK, ArrayPattern_IK, ArrayReplacement_IK)")
118 call setReplaced(Array_IK, ArrayPattern_IK, ArrayReplacement_IK)
119 call disp%show("Array_IK")
120 call disp%show( Array_IK )
121
122 call disp%skip()
123 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%")
124 call disp%show("! Replace complex array.")
125 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%")
126 call disp%skip()
127
128 call disp%show("Array_CK")
129 call disp%show( Array_CK )
130 call disp%show("ArrayPattern_CK")
131 call disp%show( ArrayPattern_CK )
132 call disp%show("ArrayReplacement_CK")
133 call disp%show( ArrayReplacement_CK )
134 call disp%show("call setReplaced(Array_CK, ArrayPattern_CK, ArrayReplacement_CK)")
135 call setReplaced(Array_CK, ArrayPattern_CK, ArrayReplacement_CK)
136 call disp%show("Array_CK")
137 call disp%show( Array_CK )
138
139 call disp%skip()
140 call disp%show("!%%%%%%%%%%%%%%%%%%%%")
141 call disp%show("! Replace real array.")
142 call disp%show("!%%%%%%%%%%%%%%%%%%%%")
143 call disp%skip()
144
145 call disp%show("Array_RK")
146 call disp%show( Array_RK )
147 call disp%show("ArrayPattern_RK")
148 call disp%show( ArrayPattern_RK )
149 call disp%show("ArrayReplacement_RK")
150 call disp%show( ArrayReplacement_RK )
151 call disp%show("call setReplaced(Array_RK, ArrayPattern_RK, ArrayReplacement_RK)")
152 call setReplaced(Array_RK, ArrayPattern_RK, ArrayReplacement_RK)
153 call disp%show("Array_RK")
154 call disp%show( Array_RK )
155
156 call disp%skip()
157 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
158 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
159 call disp%show("! Replace only particular instances of pattern in array.")
160 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
161 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
162 call disp%skip()
163
164 string_SK = "1ParaMonte-2ParaMonte-3ParaMonte-4ParaMonte-5ParaMonte"
165 Array_SK = ["ParaMonte", "ParaMonte", "ParaMonte", "ParaMonte", "ParaMonte"]
166 Array_IK = [1_IK, 1_IK, 1_IK, 1_IK, 1_IK]
167 Array_RK = [1._RK, 1._RK, 1._RK, 1._RK, 1._RK]
168 Array_CK = [(+1._CK, -1._CK), (+1._CK, -1._CK), (+1._CK, -1._CK), (+1._CK, -1._CK), (+1._CK, -1._CK)]
169
170 stringPattern_SK = "ParaMonte"
171 ArrayPattern_SK = "ParaMonte"
172 ArrayPattern_IK = [1_IK]
173 ArrayPattern_RK = [1._RK]
174 ArrayPattern_CK = [(+1._CK, -1._CK)]
175
176 stringReplacement_SK = ""
177 ArrayReplacement_SK = ["XXXXXXXXX"]
178 ArrayReplacement_IK = [-2_IK,-3_IK]
179 ArrayReplacement_RK = [-2._RK,-3._RK]
180 ArrayReplacement_CK = [(-2._CK, +2._CK), (-3._CK, +3._CK)]
181
182 ! Replace only the second occurrence from the beginning as well as the first and the second occurrences from the end.
183
184 instance = [-1_IK, -2_IK, 2_IK]
185
186 call disp%skip()
187 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
188 call disp%show("! Replace specific instances within the character scalar.")
189 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
190 call disp%skip()
191
192 call disp%show("string_SK")
193 call disp%show( string_SK, deliml = SK_"""" )
194 call disp%show("stringPattern_SK")
195 call disp%show( stringPattern_SK, deliml = SK_"""" )
196 call disp%show("stringReplacement_SK")
197 call disp%show( stringReplacement_SK, deliml = SK_"""" )
198 call disp%show("instance")
199 call disp%show( instance )
200 call disp%show("call setReplaced(string_SK, stringPattern_SK, stringReplacement_SK, instance)")
201 call setReplaced(string_SK, stringPattern_SK, stringReplacement_SK, instance)
202 call disp%show("string_SK")
203 call disp%show( string_SK, deliml = SK_"""" )
204
205 call disp%skip()
206 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
207 call disp%show("! Replace specific instances within the character array.")
208 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
209 call disp%skip()
210
211 call disp%show("Array_SK")
212 call disp%show( Array_SK, deliml = SK_"""" )
213 call disp%show("ArrayPattern_SK")
214 call disp%show( ArrayPattern_SK, deliml = SK_"""" )
215 call disp%show("ArrayReplacement_SK")
216 call disp%show( ArrayReplacement_SK, deliml = SK_"""" )
217 call disp%show("call setReplaced(Array_SK, ArrayPattern_SK, ArrayReplacement_SK, instance)")
218 call setReplaced(Array_SK, ArrayPattern_SK, ArrayReplacement_SK, instance)
219 call disp%show("Array_SK")
220 call disp%show( Array_SK, deliml = SK_"""" )
221
222 call disp%skip()
223 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
224 call disp%show("! Replace specific instances within the integer array.")
225 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
226 call disp%skip()
227
228 call disp%show("Array_IK")
229 call disp%show( Array_IK )
230 call disp%show("ArrayPattern_IK")
231 call disp%show( ArrayPattern_IK )
232 call disp%show("ArrayReplacement_IK")
233 call disp%show( ArrayReplacement_IK )
234 call disp%show("instance")
235 call disp%show( instance )
236 call disp%show("call setReplaced(Array_IK, ArrayPattern_IK, ArrayReplacement_IK, instance)")
237 call setReplaced(Array_IK, ArrayPattern_IK, ArrayReplacement_IK, instance)
238 call disp%show("Array_IK")
239 call disp%show( Array_IK )
240
241 call disp%skip()
242 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
243 call disp%show("! Replace specific instances within the complex array.")
244 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
245 call disp%skip()
246
247 call disp%show("Array_CK")
248 call disp%show( Array_CK )
249 call disp%show("ArrayPattern_CK")
250 call disp%show( ArrayPattern_CK )
251 call disp%show("ArrayReplacement_CK")
252 call disp%show( ArrayReplacement_CK )
253 call disp%show("instance")
254 call disp%show( instance )
255 call disp%show("call setReplaced(Array_CK, ArrayPattern_CK, ArrayReplacement_CK, instance)")
256 call setReplaced(Array_CK, ArrayPattern_CK, ArrayReplacement_CK, instance)
257 call disp%show("Array_CK")
258 call disp%show( Array_CK )
259
260 call disp%skip()
261 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
262 call disp%show("! Replace specific instances within the real array.")
263 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
264 call disp%skip()
265
266 call disp%show("Array_RK")
267 call disp%show( Array_RK )
268 call disp%show("ArrayPattern_RK")
269 call disp%show( ArrayPattern_RK )
270 call disp%show("ArrayReplacement_RK")
271 call disp%show( ArrayReplacement_RK )
272 call disp%show("instance")
273 call disp%show( instance )
274 call disp%show("call setReplaced(Array_RK, ArrayPattern_RK, ArrayReplacement_RK, instance)")
275 call setReplaced(Array_RK, ArrayPattern_RK, ArrayReplacement_RK, instance)
276 call disp%show("Array_RK")
277 call disp%show( Array_RK )
278
279 call disp%skip()
280 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
281 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
282 call disp%show("! Replace specific instances with a user-defined equality test.")
283 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
284 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
285 call disp%skip()
286
287 call disp%skip()
288 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
289 call disp%show("! Replace specific instances within the real array.")
290 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
291 call disp%skip()
292
293 Array_RK = [1.01_RK, 1.04_RK, 0.98_RK, 1.0_RK, 1.02_RK]
294 ArrayPattern_RK = [-1._RK, 1._RK]
295 ArrayReplacement_RK = [0._RK,0._RK]
296 instance = [-2]
297
298 call disp%show("Array_RK")
299 call disp%show( Array_RK )
300 call disp%show("ArrayPattern_RK")
301 call disp%show( ArrayPattern_RK )
302 call disp%show("ArrayReplacement_RK")
303 call disp%show( ArrayReplacement_RK )
304 call disp%show("instance")
305 call disp%show( instance )
306 call disp%show("call setReplaced(Array_RK, ArrayPattern_RK, ArrayReplacement_RK, iseq = iseq_RK, instance = instance)")
307 call setReplaced(Array_RK, ArrayPattern_RK, ArrayReplacement_RK, iseq = iseq_RK, instance = instance)
308 call disp%show("Array_RK")
309 call disp%show( Array_RK )
310
311 call disp%skip()
312 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
313 call disp%show("! Replace case-insensitive instances within the character array.")
314 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
315 call disp%skip()
316
317 string_SK = "ABBAbbA"
318 stringPattern_SK = "bb"
319 stringReplacement_SK = "x"
320
321 call disp%show("string_SK")
322 call disp%show( string_SK, deliml = SK_"""" )
323 call disp%show("stringPattern_SK")
324 call disp%show( stringPattern_SK, deliml = SK_"""" )
325 call disp%show("stringReplacement_SK")
326 call disp%show( stringReplacement_SK, deliml = SK_"""" )
327 call disp%show("call setReplaced(string_SK, stringPattern_SK, stringReplacement_SK, iseq = iseq_SK)")
328 call setReplaced(string_SK, stringPattern_SK, stringReplacement_SK, iseq = iseq_SK)
329 call disp%show("string_SK")
330 call disp%show( string_SK, deliml = SK_"""" )
331
332 call disp%skip()
333 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
334 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
335 call disp%show("! Call to setReplaced() preserves the lower bound of the input `Array`.")
336 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
337 call disp%show("!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
338 call disp%skip()
339
340 deallocate(Array_IK)
341 ArrayPattern_IK = [0_IK]
342 ArrayReplacement_IK = [-1_IK]
343 allocate(Array_IK(-7:-1), source = [1_IK, 0_IK, 2_IK, 0_IK, 3_IK, 0_IK, 4_IK])
344 call disp%show("[lbound(Array_IK,1), ubound(Array_IK,1)]")
345 call disp%show( [lbound(Array_IK,1), ubound(Array_IK,1)] )
346 call disp%show("Array_IK")
347 call disp%show( Array_IK )
348 call disp%show("ArrayPattern_IK")
349 call disp%show( ArrayPattern_IK )
350 call disp%show("ArrayReplacement_IK")
351 call disp%show( ArrayReplacement_IK )
352 call disp%show("call setReplaced(Array_IK, ArrayPattern_IK, ArrayReplacement_IK)")
353 call setReplaced(Array_IK, ArrayPattern_IK, ArrayReplacement_IK)
354 call disp%show("Array_IK")
355 call disp%show( Array_IK )
356 call disp%show("[lbound(Array_IK,1), ubound(Array_IK,1)]")
357 call disp%show( [lbound(Array_IK,1), ubound(Array_IK,1)] )
358 call disp%skip()
359
360 deallocate(Array_IK)
361 ArrayPattern_IK = [0_IK]
362 ArrayReplacement_IK = [-999_IK, -999_IK]
363 allocate(Array_IK(-7:-1), source = [1_IK, 0_IK, 2_IK, 0_IK, 3_IK, 0_IK, 4_IK])
364 call disp%show("[lbound(Array_IK,1), ubound(Array_IK,1)]")
365 call disp%show( [lbound(Array_IK,1), ubound(Array_IK,1)] )
366 call disp%show("Array_IK")
367 call disp%show( Array_IK )
368 call disp%show("instance")
369 call disp%show( instance )
370 call disp%show("ArrayPattern_IK")
371 call disp%show( ArrayPattern_IK )
372 call disp%show("ArrayReplacement_IK")
373 call disp%show( ArrayReplacement_IK )
374 call disp%show("call setReplaced(Array_IK, ArrayPattern_IK, ArrayReplacement_IK, instance = instance)")
375 call setReplaced(Array_IK, ArrayPattern_IK, ArrayReplacement_IK, instance = instance)
376 call disp%show("Array_IK")
377 call disp%show( Array_IK )
378 call disp%show("[lbound(Array_IK,1), ubound(Array_IK,1)]")
379 call disp%show( [lbound(Array_IK,1), ubound(Array_IK,1)] )
380
381contains
382
383 function iseq_RK(Segment, pattern, lenPattren) result(equivalent)
384 integer(IK) , intent(in) :: lenPattren
385 real(RK) , intent(in) :: Segment(lenPattren)
386 real(RK) , intent(in) :: pattern(lenPattren)
387 logical(LK) :: equivalent
388 equivalent = all(abs(abs(pattern) - abs(Segment)) < 0.05_RK)
389 end function
390
391 function iseq_SK(segment, pattern) result(equivalent)
392 use pm_strASCII, only: getStrLower
393 character(*, SK), intent(in) :: segment, pattern
394 logical(LK) :: equivalent
395 equivalent = pattern == getStrLower(segment)
396 end function
397
398end 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
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!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
4! Replace all instances of pattern in array.
5!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
7
8
9!%%%%%%%%%%%%%%%%%%%%%%%%%%
10! Replace character scalar.
11!%%%%%%%%%%%%%%%%%%%%%%%%%%
12
13string_SK
14"ParaMonte is a Monte Carlo Library."
15stringPattern_SK
16"a Monte Carlo"
17stringReplacement_SK
18"now a Machine Learning"
19call setReplaced(string_SK, stringPattern_SK, stringReplacement_SK)
20string_SK
21"ParaMonte is now a Machine Learning Library."
22
23!%%%%%%%%%%%%%%%%%%%%%%%%%
24! Replace character array.
25!%%%%%%%%%%%%%%%%%%%%%%%%%
26
27Array_SK
28"ParaMonte", "is ", "a ", "Monte ", "Carlo ", "Library. "
29ArrayPattern_SK
30"a ", "Monte ", "Carlo "
31ArrayReplacement_SK
32"now ", "a ", "Machine ", "Learning "
33call setReplaced(Array_SK, ArrayPattern_SK, ArrayReplacement_SK)
34Array_SK
35"ParaMonte", "is ", "now ", "a ", "Machine ", "Learning ", "Library. "
36
37!%%%%%%%%%%%%%%%%%%%%%%%
38! Replace logical array.
39!%%%%%%%%%%%%%%%%%%%%%%%
40
41Array_LK
42F, T, T, F
43ArrayPattern_LK
44T, T
45ArrayReplacement_LK
46F
47call setReplaced(Array_LK, ArrayPattern_LK, ArrayReplacement_LK)
48Array_LK
49F, F, F
50
51!%%%%%%%%%%%%%%%%%%%%%%%
52! Replace integer array.
53!%%%%%%%%%%%%%%%%%%%%%%%
54
55Array_IK
56+1, +2, +3, +4
57ArrayPattern_IK
58+3
59ArrayReplacement_IK
60-1, +0, +1
61call setReplaced(Array_IK, ArrayPattern_IK, ArrayReplacement_IK)
62Array_IK
63+1, +2, -1, +0, +1, +4
64
65!%%%%%%%%%%%%%%%%%%%%%%%
66! Replace complex array.
67!%%%%%%%%%%%%%%%%%%%%%%%
68
69Array_CK
70(+1.0000000000000000, -1.0000000000000000), (+2.0000000000000000, -2.0000000000000000), (+3.0000000000000000, -3.0000000000000000), (+4.0000000000000000, -4.0000000000000000)
71ArrayPattern_CK
72(+3.0000000000000000, -3.0000000000000000)
73ArrayReplacement_CK
74(-1.0000000000000000, +1.0000000000000000), (+0.0000000000000000, +0.0000000000000000), (+1.0000000000000000, -1.0000000000000000)
75call setReplaced(Array_CK, ArrayPattern_CK, ArrayReplacement_CK)
76Array_CK
77(+1.0000000000000000, -1.0000000000000000), (+2.0000000000000000, -2.0000000000000000), (-1.0000000000000000, +1.0000000000000000), (+0.0000000000000000, +0.0000000000000000), (+1.0000000000000000, -1.0000000000000000), (+4.0000000000000000, -4.0000000000000000)
78
79!%%%%%%%%%%%%%%%%%%%%
80! Replace real array.
81!%%%%%%%%%%%%%%%%%%%%
82
83Array_RK
84+1.0000000000000000, +2.0000000000000000, +3.0000000000000000, +4.0000000000000000
85ArrayPattern_RK
86+3.0000000000000000
87ArrayReplacement_RK
88-1.0000000000000000, +0.0000000000000000, +1.0000000000000000
89call setReplaced(Array_RK, ArrayPattern_RK, ArrayReplacement_RK)
90Array_RK
91+1.0000000000000000, +2.0000000000000000, -1.0000000000000000, +0.0000000000000000, +1.0000000000000000, +4.0000000000000000
92
93!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95! Replace only particular instances of pattern in array.
96!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
97!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
98
99
100!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101! Replace specific instances within the character scalar.
102!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103
104string_SK
105"1ParaMonte-2ParaMonte-3ParaMonte-4ParaMonte-5ParaMonte"
106stringPattern_SK
107"ParaMonte"
108stringReplacement_SK
109""
110instance
111-1, -2, +2
112call setReplaced(string_SK, stringPattern_SK, stringReplacement_SK, instance)
113string_SK
114"1ParaMonte-2-3ParaMonte-4-5"
115
116!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117! Replace specific instances within the character array.
118!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119
120Array_SK
121"ParaMonte", "ParaMonte", "ParaMonte", "ParaMonte", "ParaMonte"
122ArrayPattern_SK
123"ParaMonte", "ParaMonte", "ParaMonte"
124ArrayReplacement_SK
125"XXXXXXXXX"
126call setReplaced(Array_SK, ArrayPattern_SK, ArrayReplacement_SK, instance)
127Array_SK
128"XXXXXXXXX", "ParaMonte", "ParaMonte"
129
130!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
131! Replace specific instances within the integer array.
132!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
133
134Array_IK
135+1, +1, +1, +1, +1
136ArrayPattern_IK
137+1
138ArrayReplacement_IK
139-2, -3
140instance
141-1, -2, +2
142call setReplaced(Array_IK, ArrayPattern_IK, ArrayReplacement_IK, instance)
143Array_IK
144+1, -2, -3, +1, -2, -3, -2, -3
145
146!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147! Replace specific instances within the complex array.
148!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149
150Array_CK
151(+1.0000000000000000, -1.0000000000000000), (+1.0000000000000000, -1.0000000000000000), (+1.0000000000000000, -1.0000000000000000), (+1.0000000000000000, -1.0000000000000000), (+1.0000000000000000, -1.0000000000000000)
152ArrayPattern_CK
153(+1.0000000000000000, -1.0000000000000000)
154ArrayReplacement_CK
155(-2.0000000000000000, +2.0000000000000000), (-3.0000000000000000, +3.0000000000000000)
156instance
157-1, -2, +2
158call setReplaced(Array_CK, ArrayPattern_CK, ArrayReplacement_CK, instance)
159Array_CK
160(+1.0000000000000000, -1.0000000000000000), (-2.0000000000000000, +2.0000000000000000), (-3.0000000000000000, +3.0000000000000000), (+1.0000000000000000, -1.0000000000000000), (-2.0000000000000000, +2.0000000000000000), (-3.0000000000000000, +3.0000000000000000), (-2.0000000000000000, +2.0000000000000000), (-3.0000000000000000, +3.0000000000000000)
161
162!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
163! Replace specific instances within the real array.
164!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165
166Array_RK
167+1.0000000000000000, +1.0000000000000000, +1.0000000000000000, +1.0000000000000000, +1.0000000000000000
168ArrayPattern_RK
169+1.0000000000000000
170ArrayReplacement_RK
171-2.0000000000000000, -3.0000000000000000
172instance
173-1, -2, +2
174call setReplaced(Array_RK, ArrayPattern_RK, ArrayReplacement_RK, instance)
175Array_RK
176+1.0000000000000000, -2.0000000000000000, -3.0000000000000000, +1.0000000000000000, -2.0000000000000000, -3.0000000000000000, -2.0000000000000000, -3.0000000000000000
177
178!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180! Replace specific instances with a user-defined equality test.
181!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183
184
185!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
186! Replace specific instances within the real array.
187!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188
189Array_RK
190+1.0100000000000000, +1.0400000000000000, +0.97999999999999998, +1.0000000000000000, +1.0200000000000000
191ArrayPattern_RK
192-1.0000000000000000, +1.0000000000000000
193ArrayReplacement_RK
194+0.0000000000000000, +0.0000000000000000
195instance
196-2
197call setReplaced(Array_RK, ArrayPattern_RK, ArrayReplacement_RK, iseq = iseq_RK, instance = instance)
198Array_RK
199+0.0000000000000000, +0.0000000000000000, +0.97999999999999998, +1.0000000000000000, +1.0200000000000000
200
201!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202! Replace case-insensitive instances within the character array.
203!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204
205string_SK
206"ABBAbbA"
207stringPattern_SK
208"bb"
209stringReplacement_SK
210"x"
211call setReplaced(string_SK, stringPattern_SK, stringReplacement_SK, iseq = iseq_SK)
212string_SK
213"AxAxA"
214
215!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217! Call to setReplaced() preserves the lower bound of the input `Array`.
218!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220
221[lbound(Array_IK,1), ubound(Array_IK,1)]
222-7, -1
223Array_IK
224+1, +0, +2, +0, +3, +0, +4
225ArrayPattern_IK
226+0
227ArrayReplacement_IK
228-1
229call setReplaced(Array_IK, ArrayPattern_IK, ArrayReplacement_IK)
230Array_IK
231+1, -1, +2, -1, +3, -1, +4
232[lbound(Array_IK,1), ubound(Array_IK,1)]
233-7, -1
234
235[lbound(Array_IK,1), ubound(Array_IK,1)]
236-7, -1
237Array_IK
238+1, +0, +2, +0, +3, +0, +4
239instance
240-2
241ArrayPattern_IK
242+0
243ArrayReplacement_IK
244-999, -999
245call setReplaced(Array_IK, ArrayPattern_IK, ArrayReplacement_IK, instance = instance)
246Array_IK
247+1, +0, +2, -999, -999, +3, +0, +4
248[lbound(Array_IK,1), ubound(Array_IK,1)]
249-7, +0
250
Test:
test_pm_arrayReplace
Todo:
Low Priority: This logic behind using intent(inout), allocatable :: array argument as opposed to returning the result in a new array is that the size of the output array is not known a priori, but is determined within the algorithm.
If the use of allocatable dummy arguments is to be truly avoided, a new interface can be added where the output is returned in a contiguous ArrayReplaced while the input array remains intact.
Todo:
Low Priority: This generic interface can be extended to 2D input objects.
Todo:
High Priority: This generic interface can be extended to scalar non-string input pattern and replacement arguments.
Such an extension will likely lead to runtime performance gain for cases where pattern and replacement are arrays of length 1. See pm_arraySplit for an example relevant benchmark about this matter.
Todo:
High Priority: A benchmark comparing the performance of setReplaced with and without sorted, unique optional input arguments should be added.


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 8076 of file pm_arrayReplace.F90.


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