The enqueue
subroutine is responsible for allocating heap structure and
holding the disjoined segment data with ascending priority order.
Note
This implementation shall be rewritten using the move_alloc
statement.
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
class(priority_queue_t), | intent(inout) | :: | pq | |||
type(segment_t), | intent(in) | :: | seg |
pure subroutine enqueue(pq, seg) implicit none class(priority_queue_t), intent(inout) :: pq type(segment_t), intent(in) :: seg type(segment_t) :: t type(segment_t), allocatable :: tmp(:) integer(int32) :: n, i if (.not.allocated(pq%heap)) allocate(pq%heap(1)) ! Managing the size of array in the queue. !! @note This implementation shall be rewritten using the `move_alloc` statement. n = pq%number if (n == size(pq%heap)) then allocate(tmp(n)) tmp(:) = pq%heap(:) deallocate(pq%heap) allocate(pq%heap(n*2)) pq%heap(1:n) = tmp(1:n) end if pq%number = pq%number + 1 pq%heap(pq%number) = seg ! Implementing a queue using arrays. ! The following loop ensures that the data structure is a heap: n = pq%number do while (n > 1) i = n/2 if (pq%heap(n)%min < pq%heap(i)%min & .or. (pq%heap(n)%min == pq%heap(i)%min .and. pq%heap(n)%max < pq%heap(i)%max)) then t = pq%heap(n) pq%heap(n) = pq%heap(i) pq%heap(i) = t end if n = i end do end subroutine enqueue