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

Merge two ascending-sorted arrays such that the resulting merged array contains all elements of the two arrays in ascending order. More...

Detailed Description

Merge two ascending-sorted arrays such that the resulting merged array contains all elements of the two arrays in ascending order.

Parameters
[in]mergedSortedArray: The output scalar of either
  • type character of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU) or
the output 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 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
  • type css_pdt of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU) or
  • type css_type of default kind SK,
whose size/length is the sum of the sizes/lengths of sortedArray1 and sortedArray2 and whose elements are the combined elements of the input sortedArray1 and sortedArray2 in ascending order.
[in]sortedArray1: The input contiguous array of the same type, kind, and rank as the output mergedSortedArray argument whose elements are already sorted in ascending order.
When the type of the input argument is complex, the array must be sorted only based on the real component of the values.
[in]sortedArray2: The input contiguous array of the same type, kind, and rank as the output mergedSortedArray argument whose elements are already sorted in ascending order.
When the type of the input argument is complex, the array must be sorted only based on the real component of the values.
isSorted: The external user-specified function that takes two input scalar arguments of the same type and kind as the input Array.
It returns a scalar logical of default kind LK that is .true. if the first input scalar argument is sorted with respect to the second input argument according to the user-defined condition within isSorted, otherwise, it is .false..
If array is a scalar string (i.e., an assumed-length scalar character), then both input arguments to isSorted() are scalar characters of length 1 of kind any supported by the processor (e.g., SK, SKA, SKD , or SKU).
The following illustrates the generic interface of isSorted(),
function isSorted(lhs, rhs) result(sorted)
use pm_kind, only: LK
TYPE(KIND) , intent(in) :: lhs, rhs
logical(LK) :: sorted
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
where TYPE(KIND) is the same as the type and kind of the input argument Array, which can be one of the following.
character(*, SK), intent(in) :: lhs, rhs
character(1, SK), intent(in) :: lhs, rhs
type(css_type) , intent(in) :: lhs, rhs
type(css_pdt) , intent(in) :: lhs, rhs
integer(IK) , intent(in) :: lhs, rhs
logical(LK) , intent(in) :: lhs, rhs
complex(CK) , intent(in) :: lhs, rhs
real(RK) , intent(in) :: lhs, rhs
This module contains the derived types for generating allocatable containers of scalar,...
This is the css_pdt parameterized type for generating instances of container of scalar of string obje...
This is the css_type type for generating instances of container of scalar of string objects.
where the specified kind type parameters (SK, IK, LK, CK, RK) can refer to any of the supported kinds by the processor.
This user-defined equivalence check is extremely useful where a user-defined sorting criterion other than simple ascending order is needed, for example, when the case-sensitivity of an input string or array of strings is irrelevant or when sorting of the absolute values matters excluding the signs of the numbers, or when descending order is desired.
In such cases, user can define a custom sorting condition within the user-defined external function isSorted to achieve the goal.
(optional, the default sorting condition is ascending order, that is a < b.)


Possible calling interfaces

call setMerged(mergedSortedArray, sortedArray1, sortedArray2)
call setMerged(mergedSortedArray, sortedArray1, sortedArray2, isSorted)
Merge two ascending-sorted arrays such that the resulting merged array contains all elements of the t...
This module contains procedures and generic interfaces for sorting and merging two previously-sorted ...
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 condition size(mergedSortedArray) == size(sortedArray1) + size(sortedArray2) must hold for all interfaces with array-like arguments.
The condition len(mergedSortedArray) == len(sortedArray1) + len(sortedArray2) must hold for all interfaces with scalar arguments of type character.
The input arguments sortedArray1 and sortedArray2 must be sorted in ascending order when isSorted() is missing, or properly sorted when isSorted() is present.
These conditions are verified only if the library is built with the preprocessor macro CHECK_ENABLED=1.
See also
getMerged


Example usage

1program example
2
3 use pm_kind, only: SK, IK
4 use pm_arraySort, only: getSorted
5 use pm_distUnif, only: getUnifRand
6 use pm_arrayMerge, only: setMerged
8 use pm_io, only: display_type
9
10 implicit none
11
12 type(display_type) :: disp
13 integer(IK) :: fileUnit, i
14 integer(IK) , parameter :: NP1 = 2_IK, NP2 = 3_IK
15
16 disp = display_type(file = SK_"main.out.F90")
17
18 call disp%skip()
19 call disp%show("!%%%%%%%%%%%%%%%%%")
20 call disp%show("!character merger.")
21 call disp%show("!%%%%%%%%%%%%%%%%%")
22 call disp%skip()
23
24 block
25 use pm_kind, only: TKG => SK ! All kinds are supported.
26 character(:,TKG), allocatable :: sortedArray1, sortedArray2, sortedMerger
27 integer(IK) :: len1, len2
28 call disp%skip()
29 call disp%show("len1 = getUnifRand(0_IK, 5_IK); len2 = getUnifRand(0_IK, 5_IK)")
30 len1 = getUnifRand(0_IK, 5_IK); len2 = getUnifRand(0_IK, 5_IK)
31 call disp%show("[len1, len2]")
32 call disp%show( [len1, len2] )
33 call disp%show("sortedArray1 = getSorted(getUnifRand(repeat('A', len1), repeat('Z', len1)))")
34 sortedArray1 = getSorted(getUnifRand(repeat('A', len1), repeat('Z', len1)))
35 call disp%show("sortedArray1")
36 call disp%show( sortedArray1 , deliml = SK_"""" )
37 call disp%show("sortedArray2 = getSorted(getUnifRand(repeat('A', len2), repeat('Z', len2)))")
38 sortedArray2 = getSorted(getUnifRand(repeat('A', len2), repeat('Z', len2)))
39 call disp%show("sortedArray2")
40 call disp%show( sortedArray2 , deliml = SK_"""" )
41 call disp%show("call setResized(sortedMerger, len1 + len2)")
42 call setResized(sortedMerger, len1 + len2)
43 call disp%show("call setMerged(sortedMerger, sortedArray1, sortedArray2)")
44 call setMerged(sortedMerger, sortedArray1, sortedArray2)
45 call disp%show("sortedMerger")
46 call disp%show( sortedMerger , deliml = SK_"""" )
47 call disp%skip()
48 end block
49
50 call disp%skip()
51 call disp%show("!%%%%%%%%%%%%%%%")
52 call disp%show("!integer merger.")
53 call disp%show("!%%%%%%%%%%%%%%%")
54 call disp%skip()
55
56 block
57 use pm_kind, only: TKG => IK ! All kinds are supported.
58 integer(TKG), allocatable :: sortedArray1(:), sortedArray2(:), sortedMerger(:)
59 integer(IK) :: len1, len2
60 call disp%skip()
61 call disp%show("len1 = getUnifRand(0_IK, 5_IK); len2 = getUnifRand(0_IK, 5_IK)")
62 len1 = getUnifRand(0_IK, 5_IK); len2 = getUnifRand(0_IK, 5_IK)
63 call disp%show("[len1, len2]")
64 call disp%show( [len1, len2] )
65 call disp%show("sortedArray1 = getSorted(getUnifRand(0, 9, len1))")
66 sortedArray1 = getSorted(getUnifRand(0, 9, len1))
67 call disp%show("sortedArray1")
68 call disp%show( sortedArray1 , deliml = SK_"""" )
69 call disp%show("sortedArray2 = getSorted(getUnifRand(0, 9, len2))")
70 sortedArray2 = getSorted(getUnifRand(0, 9, len2))
71 call disp%show("sortedArray2")
72 call disp%show( sortedArray2 , deliml = SK_"""" )
73 call disp%show("call setResized(sortedMerger, len1 + len2)")
74 call setResized(sortedMerger, len1 + len2)
75 call disp%show("call setMerged(sortedMerger, sortedArray1, sortedArray2)")
76 call setMerged(sortedMerger, sortedArray1, sortedArray2)
77 call disp%show("sortedMerger")
78 call disp%show( sortedMerger , deliml = SK_"""" )
79 call disp%skip()
80 end block
81
82 call disp%skip()
83 call disp%show("!%%%%%%%%%%%%")
84 call disp%show("!real merger.")
85 call disp%show("!%%%%%%%%%%%%")
86 call disp%skip()
87
88 block
89 use pm_kind, only: TKG => IK ! All kinds are supported.
90 real(TKG), allocatable :: sortedArray1(:), sortedArray2(:), sortedMerger(:)
91 integer(IK) :: len1, len2
92 call disp%skip()
93 call disp%show("len1 = getUnifRand(0_IK, 5_IK); len2 = getUnifRand(0_IK, 5_IK)")
94 len1 = getUnifRand(0_IK, 5_IK); len2 = getUnifRand(0_IK, 5_IK)
95 call disp%show("[len1, len2]")
96 call disp%show( [len1, len2] )
97 call disp%show("sortedArray1 = getSorted(getUnifRand(0, 9, len1))")
98 sortedArray1 = getSorted(getUnifRand(0, 9, len1))
99 call disp%show("sortedArray1")
100 call disp%show( sortedArray1 , deliml = SK_"""" )
101 call disp%show("sortedArray2 = getSorted(getUnifRand(0, 9, len2))")
102 sortedArray2 = getSorted(getUnifRand(0, 9, len2))
103 call disp%show("sortedArray2")
104 call disp%show( sortedArray2 , deliml = SK_"""" )
105 call disp%show("call setResized(sortedMerger, len1 + len2)")
106 call setResized(sortedMerger, len1 + len2)
107 call disp%show("call setMerged(sortedMerger, sortedArray1, sortedArray2)")
108 call setMerged(sortedMerger, sortedArray1, sortedArray2)
109 call disp%show("sortedMerger")
110 call disp%show( sortedMerger , deliml = SK_"""" )
111 call disp%skip()
112 end block
113
114end program example
Allocate or resize (shrink or expand) an input allocatable scalar string or array of rank 1....
Generate and return the sorted elements of the input scalar string or contiguous vector in ascending ...
Generate and return a scalar or a contiguous array of rank 1 of length s1 of randomly uniformly distr...
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
This module contains procedures and generic interfaces for resizing allocatable arrays of various typ...
This module contains procedures and generic interfaces for various sorting tasks.
This module contains classes and procedures for computing various statistical quantities related to t...
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 IK
The default integer kind in the ParaMonte library: int32 in Fortran, c_int32_t in C-Fortran Interoper...
Definition: pm_kind.F90:540
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
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!character merger.
4!%%%%%%%%%%%%%%%%%
5
6
7len1 = getUnifRand(0_IK, 5_IK); len2 = getUnifRand(0_IK, 5_IK)
8[len1, len2]
9+3, +4
10sortedArray1 = getSorted(getUnifRand(repeat('A', len1), repeat('Z', len1)))
11sortedArray1
12"ACR"
13sortedArray2 = getSorted(getUnifRand(repeat('A', len2), repeat('Z', len2)))
14sortedArray2
15"JSTT"
16call setResized(sortedMerger, len1 + len2)
17call setMerged(sortedMerger, sortedArray1, sortedArray2)
18sortedMerger
19"ACJRSTT"
20
21
22!%%%%%%%%%%%%%%%
23!integer merger.
24!%%%%%%%%%%%%%%%
25
26
27len1 = getUnifRand(0_IK, 5_IK); len2 = getUnifRand(0_IK, 5_IK)
28[len1, len2]
29+3, +3
30sortedArray1 = getSorted(getUnifRand(0, 9, len1))
31sortedArray1
32"+0", "+2", "+8"
33sortedArray2 = getSorted(getUnifRand(0, 9, len2))
34sortedArray2
35"+2", "+4", "+8"
36call setResized(sortedMerger, len1 + len2)
37call setMerged(sortedMerger, sortedArray1, sortedArray2)
38sortedMerger
39"+0", "+2", "+2", "+4", "+8", "+8"
40
41
42!%%%%%%%%%%%%
43!real merger.
44!%%%%%%%%%%%%
45
46
47len1 = getUnifRand(0_IK, 5_IK); len2 = getUnifRand(0_IK, 5_IK)
48[len1, len2]
49+3, +0
50sortedArray1 = getSorted(getUnifRand(0, 9, len1))
51sortedArray1
52"+0.00000000", "+4.00000000", "+7.00000000"
53sortedArray2 = getSorted(getUnifRand(0, 9, len2))
54sortedArray2
55
56call setResized(sortedMerger, len1 + len2)
57call setMerged(sortedMerger, sortedArray1, sortedArray2)
58sortedMerger
59"+0.00000000", "+4.00000000", "+7.00000000"
60
61
Test:
test_pm_arrayMerge


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, April 21, 2017, 1:54 AM, Institute for Computational Engineering and Sciences (ICES), The University of Texas at Austin

Definition at line 1146 of file pm_arrayMerge.F90.


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