cube_add__segment_list Subroutine

private pure subroutine cube_add__segment_list(self, seglist)

Type Bound

cube_t

Arguments

Type IntentOptional Attributes Name
class(cube_t), intent(inout) :: self
type(segment_t), intent(in) :: seglist(:)

Source Code

   pure subroutine cube_add__segment_list(self, seglist)
      implicit none
      class(cube_t), intent(inout) :: self
      type(segment_t), intent(in) :: seglist(:)

      integer :: cp_min, cp_max, siz, i, j, k, m, n, p

      type(segment_t), allocatable :: tmp(:), ret(:)
      type(segment_t) :: what_to_add

      integer :: upper

      if (allocated(self%sps)) then
         m = size(self%sps)
      else
         m = 0
      end if
      n = size(seglist, dim=1)

      if (any(seglist == SEG_EPSILON)) then
         self%epsilon_flag = .true.
      end if

      ! Scan all segments in the list to find max value of them.
      upper = 0
      do i = 1, n
         upper = max(seglist(i)%max, upper)
      end do

      ! If all values of the list is within the range of ASCII, register them to self%ascii and retrun.
      if (upper < ASCII_SIZE_BIT .and. .not. self%is_switched_to_bmp) then
         do i = 1, n
            cp_min = seglist(i)%min
            cp_max = seglist(i)%max
            call self%ascii%add(cp_min, cp_max)
         end do
         if (self%single_flag) self%single_flag = self%num() == 1
         return
      end if

      if (.not. self%is_switched_to_bmp) call self%switch_bmp()

      siz = m + n
      allocate(tmp(n))
      allocate(ret(siz+1))

      k = 0 ! for tmp
      j = 1 ! for segments to add
      do while ( j <= n)
         cp_min = seglist(j)%min
         cp_max = seglist(j)%max
         call self%bmp%add(cp_min, cp_max)
         if (cp_max > BMP_SIZE_BIT) then
            k = k + 1
            what_to_add = segment_t(max(cp_min, BMP_SIZE_BIT), cp_max)
            tmp(k) = what_to_add
         end if

         j = j + 1
      end do

      if (k /= 0) then
         joint: block
            type(segment_t), allocatable :: cache(:)
            if (allocated(self%sps)) then
               p = ubound(self%sps, dim=1)
               cache(1:p) = self%sps(1:p)
               deallocate(self%sps)

               allocate(self%sps(1:p+k))
               self%sps(1:p) = cache(1:p)
               self%sps(p+1:p+k) = tmp(1:k)
            else
               allocate(self%sps(1:k))
               self%sps(1:k) = tmp(1:k)
            end if

         end block joint
      end if

      if (self%single_flag) self%single_flag = self%num() == 1
   end subroutine cube_add__segment_list