Allocate or resize (shrink or expand) an input allocatable
scalar string or array of rank 1..3
to an arbitrary size while preserving the original contents or a subset of it.
The new array
size is set to twice its current size or, to the requested input size(..)
.
The array contents or a requested subset of it are kept in the original indices in the output resized array or shifted to a new starting location lbc
in the output array
.
The following figure illustrates example resizing of a 1D array and transferal of its contents.
- Parameters
-
[in,out] | array | : The input/output allocatable scalar of
-
type
character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU) type character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU)
or array of rank 1..3 of either
-
type css_pdt (parameterized container of string of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU)) or,
-
type css_type (container of string of default kind SK) or,
-
type
character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU)
-
type
integer of kind any supported by the processor (e.g., IK, IK8, IK16, IK32, or IK64)
-
type
logical of kind any supported by the processor (e.g., LK)
-
type
complex of kind any supported by the processor (e.g., CK, CK32, CK64, or CK128)
-
type
real of kind any supported by the processor (e.g., RK, RK32, RK64, or RK128)
On output, the array will be (re)allocated to the requested new size with the same lower bound as before (or 1 if unallocated). |
[in] | size | : The input non-negative scalar or array of type integer of default kind IK representing the new size of the output array.
-
If
array is a scalar or array of rank 1 , then size must be a scalar.
-
If
array is an array of rank > 1 , then size must be a vector of the same length as rank(array) .
(optional, default = 2 * len/shape(array) where the condition 0 < len/shape(array) must hold, otherwise infinite loops within the program can occur.) |
[in] | lbc | : The input scalar or array of type integer of default kind IK, representing the Lower Bound(s) of the Contents in the newly resized output array .
-
If
array is a scalar or array of rank 1 , then lbc must be a scalar.
-
If
array is an array of rank > 1 , then lbc must be a vector of the same length as rank(array) .
(optional, default = lbcold . It can be present only if the size argument is also present.) |
[in] | lbcold | : The input scalar or array of type integer of default kind IK, representing the Lower Bound(s) of the Contents in the original (old) input array that is to be copied to the newly allocated output array starting at the new lower bound(s) lbc .
(optional, default = ubound(array) . If array is a scalar string, then default = 1 . It can be present only if the size , lbc , and ubcold input arguments are also present.) |
[in] | ubcold | : The input scalar or array of type integer of default kind IK, representing the Upper Bound(s) of the Contents in the original (old) input array that is to be copied to the newly allocated output array starting at the new lower bound(s) lbc .
(optional, default = ubound(array) . If array is a scalar string, then default = len(array) It can be present only if the size and lbc and lbcold input arguments are also present.) |
[out] | failed | : The output scalar logical of default kind LK that is .false. if and only if the requested array resizing is successful, otherwise it is set to .true. to signal the occurrence of an allocation error.
The value of failed is .true. only if the stat argument returned by the Fortran intrinsic allocate() statement is non-zero.
(optional, if missing and an allocation error occurs, the processor dictates the program behavior (normally execution stops).) |
[out] | errmsg | : The output scalar character of default kind SK of arbitrary length type parameter.
If the optional output argument failed is present and an error occurs, errmsg will be set to a message describing the nature of the error.
This behavior conforms with the standard Fortran behavior for the intrinsic allocate() statement.
A length type parameter of 127 or more for errmsg should be sufficient for capturing most if not all error messages in entirety.
(optional. Its presence is relevant if and only if the optional output argument failed is also present.) |
Possible calling interfaces ⛓
call setResized(array, failed
= failed,
errmsg = errmsg)
call setResized(array, size, failed
= failed,
errmsg = errmsg)
call setResized(array, size, lbc, failed
= failed,
errmsg = errmsg)
call setResized(array, size, lbc, lbcold, ubcold, failed
= failed,
errmsg = errmsg)
!
Allocate or resize (shrink or expand) an input allocatable scalar string or array of rank 1....
This module contains procedures and generic interfaces for resizing allocatable arrays of various typ...
- Warning
- Note that the new elements of the newly allocated
array
are not initialized to any particular value on output.
if array
is a container of an allocatable
component, the new elements remain unallocated upon return.
In such a case, the contents of the new elements of the output array
is processor dependent, frequently meaningless, and should not be relied upon, even if they seem to have been initialized.
If the initialization of the new elements with a specific fill
is necessary, use setRefilled to resize arrays and filling the new elements.
-
The condition
all(0 < len/shape(array))
must hold for the corresponding input arguments when the input size
argument is missing.
The condition allocated(array)
must hold (the input array
must be preallocated) when any or all of the optional input arguments lbc, lbcold, ubcold
are present or when the size
argument is missing.
The condition all(0 <= size)
must hold for the corresponding input argument.
The condition all(lbound(array) <= lbcold .and. lbcold <= ubound(array))
must hold for the corresponding input arguments.
The condition all(lbound(array) <= ubcold .and. ubcold <= ubound(array))
must hold for the corresponding input arguments.
The condition all(lbound(array) <= lbc)
must hold for the corresponding input arguments.
The condition all(lbc + ubcold - lbcold <= lbound(array) + size - 1)
must hold for the corresponding input arguments (i.e., the upper bound(s) of contents cannot overflow the upper bound(s) of the new array).
These conditions are verified only if the library is built with the preprocessor macro CHECK_ENABLED=1
.
-
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.
- Note
- If the input
array
is unallocated, it will be allocated to the requested shape, equivalent to allocate(array(@size))
.
-
The sole purpose of this generic interface is to provide a convenient but fast method of resizing allocatable arrays without losing the contents of the array.
See pm_arrayResize for the relevant benchmarks.
- Developer Remark:
- An optional dummy argument
stat
(instead of failed
) for the procedures of this generic interface are impossible as it creates ambiguous interfaces.
- See also
- setResized
setRebound
setRefilled
setRebilled
getCoreHalo
setCoreHalo
getCentered
setCentered
getPadded
setPadded
Example usage ⛓
6 call disp
%show(
'array'); \
7 call disp
%show( array , deliml
= SK_
"""" ); \
8 call disp
%show(
'lbound(array)'); \
9 call disp
%show(
lbound(array) ); \
10 call disp
%show(
'ubound(array)'); \
11 call disp
%show(
ubound(array) ); \
12 call disp
%show(
'size'); \
13 call disp
%show( SIZE ); \
14 call disp
%show(
'array'); \
15 call disp
%show( array , deliml
= SK_
"""" ); \
16 call disp
%show(
'lbound(array)'); \
17 call disp
%show(
lbound(array) ); \
18 call disp
%show(
'ubound(array)'); \
19 call disp
%show(
ubound(array) ); \
23#define RESIZE_SHIFT_ARRAY \
27 call disp
%show(
'array'); \
28 call disp
%show( array , deliml
= SK_
"""" ); \
29 call disp
%show(
'lbound(array)'); \
30 call disp
%show(
lbound(array) ); \
31 call disp
%show(
'ubound(array)'); \
32 call disp
%show(
ubound(array) ); \
33 call disp
%show(
'size'); \
34 call disp
%show( SIZE ); \
35 call disp
%show(
'lbc'); \
36 call disp
%show( LBC ); \
37 call disp
%show(
'array'); \
38 call disp
%show( array , deliml
= SK_
"""" ); \
39 call disp
%show(
'lbound(array)'); \
40 call disp
%show(
lbound(array) ); \
41 call disp
%show(
'ubound(array)'); \
42 call disp
%show(
ubound(array) ); \
46#define RESIZE_SHIFT_SUBSET_ARRAY \
50 call disp
%show(
'array'); \
51 call disp
%show( array , deliml
= SK_
"""" ); \
52 call disp
%show(
'lbound(array)'); \
53 call disp
%show(
lbound(array) ); \
54 call disp
%show(
'ubound(array)'); \
55 call disp
%show(
ubound(array) ); \
56 call disp
%show(
'size'); \
57 call disp
%show( SIZE ); \
58 call disp
%show(
'lbc'); \
59 call disp
%show( LBC ); \
60 call disp
%show(
'lbcold'); \
61 call disp
%show( LBCOLD ); \
62 call disp
%show(
'ubcold'); \
63 call disp
%show( UBCOLD ); \
64 call disp
%show(
'array'); \
65 call disp
%show( array , deliml
= SK_
"""" ); \
66 call disp
%show(
'lbound(array)'); \
67 call disp
%show(
lbound(array) ); \
68 call disp
%show(
'ubound(array)'); \
69 call disp
%show(
ubound(array) ); \
85 type(display_type) :: disp
89 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
90 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
91 call disp%show(
"! Expand an array with specific size.")
92 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
93 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
97 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%")
98 call disp%show(
"! Expand `character` vector.")
99 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%")
103#define DECLARE character(2,SKG), allocatable :: array(:)
104#define CONSTRUCT allocate(array(3:8)); array(:) = ["AA", "BB", "CC", "DD", "EE", "FF"]
108 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%")
109 call disp%show(
"! Expand `integer` vector.")
110 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%")
114#define DECLARE integer(IKG), allocatable :: array(:)
115#define CONSTRUCT allocate(array(3:8)); array(:) = [1, 2, 3, 4, 5, 6]
119 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%")
120 call disp%show(
"! Expand `logical` vector.")
121 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%")
125#define DECLARE logical(LKG), allocatable :: array(:)
126#define CONSTRUCT allocate(array(3:8)); array(:) = [.true., .true., .true., .true., .true., .true.]
130 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%")
131 call disp%show(
"! Expand `complex` vector.")
132 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%")
136#define DECLARE complex(CKG), allocatable :: array(:)
137#define CONSTRUCT allocate(array(3:8)); array(:) = [(1., -1.), (2., -2.), (3., -3.), (4., -4.), (5., -5.), (6., -6.)]
141 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%")
142 call disp%show(
"! Expand `real` vector.")
143 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%")
147#define DECLARE real(RKG), allocatable :: array(:)
148#define CONSTRUCT allocate(array(3:8)); array(:) = [1., 2., 3., 4., 5., 6.]
152 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%")
153 call disp%show(
"! Expand `character` matrix.")
154 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%")
157#define SIZE [9_IK, 9_IK]
158#define DECLARE character(2,SKG), allocatable :: array(:,:)
159#define CONSTRUCT allocate(array(2:3,3:5)); array(:,:) = reshape(["AA", "BB", "CC", "DD", "EE", "FF"], shape = shape(array), order = [2, 1])
163 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%")
164 call disp%show(
"! Expand `character` cube.")
165 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%")
168#define SIZE [9_IK, 9_IK, 3_IK]
169#define DECLARE character(2,SKG), allocatable :: array(:,:,:)
170#define CONSTRUCT allocate(array(2:3,3:5,2:2)); array(:,:,:) = reshape(["AA", "BB", "CC", "DD", "EE", "FF"], shape = shape(array), order = [3, 2, 1])
174 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
175 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
176 call disp%show(
"! Expand an array and shift its contents.")
177 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
178 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
182 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
183 call disp%show(
"! Expand and shift `character` vector.")
184 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
189#define DECLARE character(2,SKG), allocatable :: array(:)
190#define CONSTRUCT allocate(array(3:8)); array(:) = ["AA", "BB", "CC", "DD", "EE", "FF"]
194 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
195 call disp%show(
"! Expand and shift `character` matrix.")
196 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
199#define SIZE [6_IK, 6_IK]
200#define LBC [4_IK, 4_IK]
201#define DECLARE character(2,SKG), allocatable :: array(:,:)
202#define CONSTRUCT allocate(array(2:3,3:5)); array(:,:) = reshape(["AA", "BB", "CC", "DD", "EE", "FF"], shape = shape(array), order = [2, 1])
206 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
207 call disp%show(
"! Expand and shift `character` cube.")
208 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
211#define SIZE [6_IK, 6_IK, 3_IK]
212#define LBC [3_IK, 4_IK, 2_IK]
213#define DECLARE character(2,SKG), allocatable :: array(:,:,:)
214#define CONSTRUCT allocate(array(1:2,1:3,1:1)); array(:,:,:) = reshape(["AA", "BB", "CC", "DD", "EE", "FF"], shape = shape(array), order = [3, 2, 1])
218 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
219 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
220 call disp%show(
"! Expand an array and shift a subset of its contents.")
221 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
222 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
226 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
227 call disp%show(
"! Expand and shift `character` vector.")
228 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
235#define DECLARE character(2,SKG), allocatable :: array(:)
236#define CONSTRUCT allocate(array(3:8)); array(:) = ["AA", "BB", "CC", "DD", "EE", "FF"]
237RESIZE_SHIFT_SUBSET_ARRAY
240 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
241 call disp%show(
"! Expand and shift `character` matrix.")
242 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
245#define SIZE [2_IK, 3_IK]
246#define LBC [2_IK, 4_IK]
247#define LBCOLD [2_IK, 4_IK]
248#define UBCOLD [3_IK, 5_IK]
249#define DECLARE character(2,SKG), allocatable :: array(:,:)
250#define CONSTRUCT allocate(array(2:3,3:5)); array(:,:) = reshape(["AA", "BB", "CC", "DD", "EE", "FF"], shape = shape(array), order = [2, 1])
251RESIZE_SHIFT_SUBSET_ARRAY
254 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
255 call disp%show(
"! Expand and shift `character` cube.")
256 call disp%show(
"!%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%")
259#define SIZE [2_IK, 2_IK, 4_IK]
260#define LBC [1_IK, 1_IK, 3_IK]
261#define LBCOLD [1_IK, 2_IK, 2_IK]
262#define UBCOLD [2_IK, 3_IK, 2_IK]
263#define DECLARE character(2,SKG), allocatable :: array(:,:,:)
264#define CONSTRUCT allocate(array(1:2,1:3,1:2)); array(:,:,:) = reshape(["AA", "BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ", "KK", "LL"], shape = shape(array), order = [3, 2, 1])
265RESIZE_SHIFT_SUBSET_ARRAY
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.
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.
This module defines the relevant Fortran kind type-parameters frequently used in the ParaMonte librar...
integer, parameter RK
The default real kind in the ParaMonte library: real64 in Fortran, c_double in C-Fortran Interoperati...
integer, parameter LK
The default logical kind in the ParaMonte library: kind(.true.) in Fortran, kind(....
integer, parameter CK
The default complex kind in the ParaMonte library: real64 in Fortran, c_double_complex in C-Fortran I...
integer, parameter IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
integer, parameter SK
The default character kind in the ParaMonte library: kind("a") in Fortran, c_char in C-Fortran Intero...
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 ⛓
14"AA",
"BB",
"CC",
"DD",
"EE",
"FF"
22"AA",
"BB",
"CC",
"DD",
"EE",
"FF"
33"+1",
"+2",
"+3",
"+4",
"+5",
"+6"
41"+1",
"+2",
"+3",
"+4",
"+5",
"+6"
52"T",
"T",
"T",
"T",
"T",
"T"
60"T",
"T",
"T",
"T",
"T",
"T"
71"+1.0000000000000000, -1.0000000000000000",
"+2.0000000000000000, -2.0000000000000000",
"+3.0000000000000000, -3.0000000000000000",
"+4.0000000000000000, -4.0000000000000000",
"+5.0000000000000000, -5.0000000000000000",
"+6.0000000000000000, -6.0000000000000000"
79"+1.0000000000000000, -1.0000000000000000",
"+2.0000000000000000, -2.0000000000000000",
"+3.0000000000000000, -3.0000000000000000",
"+4.0000000000000000, -4.0000000000000000",
"+5.0000000000000000, -5.0000000000000000",
"+6.0000000000000000, -6.0000000000000000"
90"+1.0000000000000000",
"+2.0000000000000000",
"+3.0000000000000000",
"+4.0000000000000000",
"+5.0000000000000000",
"+6.0000000000000000"
98"+1.0000000000000000",
"+2.0000000000000000",
"+3.0000000000000000",
"+4.0000000000000000",
"+5.0000000000000000",
"+6.0000000000000000"
160"AA",
"BB",
"CC",
"DD",
"EE",
"FF"
170"AA",
"BB",
"CC",
"DD",
"EE",
"FF"
236"AA",
"BB",
"CC",
"DD",
"EE",
"FF"
250"AA",
"BB",
"CC",
"DD",
"EE",
"FF"
- Test:
- test_pm_arrayResize
- Todo:
- Very Low Priority: This generic interface can be extended to arrays of higher ranks than currently supported.
- Bug:
Status: Unresolved
Source: GNU Fortran Compiler gfortran
version 10-12
Description: There is an annoying gfortran bug concerning allocation of allocatable arrays of strings with assumed length type parameter.
The typical compiler error message is around line 230: Error allocating 283223642230368 bytes: Cannot allocate memory
.
This requires the allocation statement be explicit for character
arrays of non-zero rank.
This makes the already complex code superbly more complex and messy.
Remedy (as of ParaMonte Library version 2.0.0): For now, the allocatable
arrays of type character
are allocated with explicit shape in the allocation statement.
This explicit allocation for character
types must be removed and replaced with the generic allocation once the bug is resolved.
- Bug:
Status: Unresolved
Source: Intel Classic Fortran Compiler ifort
version 2021-2022
Description: There is an Intel compiler 2020-2022 bug in processing multiple CHECK_ASSERTION
macros in individual routines of this module in debug
compile mode.
The problem does not appear to exist in other compilation modes.
However, this bug does seem to be related to other similar instances where Intel Classic Fortran Compiler ifort
cannot tolerate frequent appearance of use
statements within a single submodule
.
Remedy (as of ParaMonte Library version 2.0.0): For now, the resolution was to remove and replace the checking macros with explicit merged block
statements.
A similar problem also was present in the implementation of pm_quadPack.
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 249 of file pm_arrayResize.F90.