merge_segments Subroutine

public pure subroutine merge_segments(segments)

Arguments

Type IntentOptional Attributes Name
type(segment_t), intent(inout), allocatable :: segments(:)

Source Code

   pure subroutine merge_segments(segments)
      implicit none
      type(segment_t), allocatable, intent(inout) :: segments(:)
      integer :: i, j, n, m

      if (.not. allocated(segments)) return
      n = size(segments)

      m = 1
      do i = 2, n
         if (segments(i) == SEG_INIT) exit
         m = m+1
      end do
      n = m

      if (n <= 1) then
         segments = segments(:n)
         return
      end if

      j = 1
      do i = 2, n
         if (segments(j)%max >= segments(i)%min-1) then
            segments(j)%max = max(segments(j)%max, segments(i)%max)
         else
            j = j + 1
            segments(j) = segments(i)
         endif
      end do

      if (j <= n) then
         segments = segments(:j)    ! reallocation implicitly.
      end if

   end subroutine merge_segments