Octopus
merge_sorted.F90
Go to the documentation of this file.
1!! Copyright (C) 2021 S. Ohlmann
2!!
3!! This Source Code Form is subject to the terms of the Mozilla Public
4!! License, v. 2.0. If a copy of the MPL was not distributed with this
5!! file, You can obtain one at https://mozilla.org/MPL/2.0/.
6!!
7#include "global.h"
8
10 use debug_oct_m
11 use global_oct_m
12 use heap_oct_m
13 implicit none
14
15 private
16 public :: merge_sorted_arrays
17
18contains
19 ! Merge a number of sorted arrays
20 ! The sorted arrays are linearly ordered in array and their sizes are given in sizes.
21 ! The merged array is returned and optionally also an index map that can be used
22 ! to also merge another data array.
23 !
24 ! This routine uses a minheap to to a k-way merge.
25 subroutine merge_sorted_arrays(array, sizes, merged, index_map)
26 integer(int64), intent(in) :: array(:)
27 integer, intent(in) :: sizes(:)
28 integer(int64), intent(inout) :: merged(:)
29 integer, optional, intent(inout) :: index_map(:)
30
31 type(heap_t) :: heap
32
33 push_sub(merge_sorted_arrays)
34
35 call heap%init(array, sizes)
36 call heap%merge(merged, index_map)
37 call heap%end()
38
39 pop_sub(merge_sorted_arrays)
40 end subroutine merge_sorted_arrays
41end module merge_sorted_oct_m
subroutine, public merge_sorted_arrays(array, sizes, merged, index_map)