30 integer(int64),
pointer :: a(:) => null()
32 integer,
allocatable :: indices(:)
33 integer,
allocatable :: sizes(:)
43 class(heap_t),
intent(inout) :: heap
44 integer(int64),
target,
intent(in) :: list(:)
45 integer,
intent(in) :: sizes(:)
53 heap%length = ubound(sizes, dim=1)
55 do i = 1, ubound(sizes, dim=1)
56 if (sizes(i) == 0)
then
57 heap%length = heap%length - 1
60 safe_allocate(heap%sizes(heap%length))
61 safe_allocate(heap%indices(heap%length))
64 do i = 1, ubound(sizes, dim=1)
65 if (sizes(i) == 0) cycle
66 heap%sizes(j) = sizes(i)
72 heap%indices(i) = heap%indices(i-1) + heap%sizes(i-1)
75 if (sum(heap%sizes) /= ubound(list, dim=1))
then
76 message(1) =
"Error! Mismatch in size of array when initializing heap!"
86 class(heap_t),
intent(inout) :: heap
91 safe_deallocate_a(heap%sizes)
92 safe_deallocate_a(heap%indices)
97 class(heap_t),
intent(inout) :: heap
98 integer(int64),
intent(inout) :: merged(:)
99 integer,
optional,
intent(inout) :: index_map(:)
106 assert(
associated(heap%a))
108 if (sum(heap%sizes) /= ubound(merged, dim=1))
then
109 message(1) =
"Error! Mismatch in size of array when doing k-way merge!"
113 do i = 1, sum(heap%sizes)
115 merged(i) = heap%a(heap%indices(1))
116 if (
present(index_map))
then
117 index_map(i) = heap%indices(1)
120 heap%indices(1) = heap%indices(1) + 1
121 heap%sizes(1) = heap%sizes(1) - 1
122 if (heap%sizes(1) == 0)
then
125 call swap(heap, 1_4, heap%length)
126 heap%length = heap%length - 1
136 class(
heap_t),
intent(inout) :: heap
141 do i = heap%length/2, 1, -1
148 class(
heap_t),
intent(inout) :: heap
149 integer,
intent(in) :: i
151 integer :: left, right, smallest
158 if (left <= heap%length)
then
163 if (right <= heap%length)
then
169 if (smallest /= i)
then
170 call swap(heap, i, smallest)
176 subroutine swap(heap, i, j)
177 class(
heap_t),
intent(inout) :: heap
178 integer,
intent(in) :: i
179 integer,
intent(in) :: j
188 integer,
intent(inout) :: a(:)
189 integer,
intent(in) :: i
190 integer,
intent(in) :: j
201 class(
heap_t),
intent(inout) :: heap
202 integer,
intent(in) :: i
203 integer,
intent(in) :: j
205 is_smaller = heap%a(heap%indices(i)) < heap%a(heap%indices(j))
subroutine heap_end(heap)
subroutine heap_init(heap, list, sizes)
logical function is_smaller(heap, i, j)
recursive subroutine minheapify(heap, i)
subroutine heap_merge(heap, merged, index_map)
subroutine swap(heap, i, j)
subroutine build_minheap(heap)
subroutine swap_int(a, i, j)
character(len=256), dimension(max_lines), public message
to be output by fatal, warning
subroutine, public messages_fatal(no_lines, only_root_writes, namespace)