dequeue Subroutine

private pure subroutine dequeue(pq, res)

The dequeue function takes out and returns the prior segment from the queue.

Type Bound

priority_queue_t

Arguments

Type IntentOptional Attributes Name
class(priority_queue_t), intent(inout) :: pq
type(segment_t), intent(inout) :: res

Source Code

   pure subroutine dequeue(pq, res)
      implicit none
      class(priority_queue_t), intent(inout) :: pq
      type(segment_t),         intent(inout) :: res

      type(segment_t) :: tmp
      integer :: n, i, j

      ! Hold the number of data in a temporary variable.
      n = pq%number

      ! The prior element of the array is returned.
      res = pq%heap(1)

      ! The tailing data is moved to the beginning.
      pq%heap(1) = pq%heap(n)

      ! Reduce the number of data by one.
      pq%number = pq%number - 1

      ! The following loop ensures that the data structure is a heap:
      i = 1
      do while (2*i < n)
         j = 2*i
         if (j+1 < n .and. pq%heap(j+1)%min < pq%heap(j)%min) j = j + 1
         if (pq%heap(j)%min < pq%heap(i)%min) then
            tmp = pq%heap(j)
            pq%heap(j) = pq%heap(i)
            pq%heap(i) = tmp
         end if
         i = j
      end do
   end subroutine dequeue