tree_graph__reallocate Subroutine

private pure subroutine tree_graph__reallocate(self)

This procedure handles the reallcation of the tree_node_t type array within the component of the tree_t object. However, it is not be used in v4.2.

Type Bound

tree_t

Arguments

Type IntentOptional Attributes Name
class(tree_t), intent(inout) :: self

Source Code

   pure subroutine tree_graph__reallocate(self)
      implicit none
      class(tree_t), intent(inout) :: self
      integer :: new_part_begin, new_part_end, i
      type(tree_node_t), allocatable :: tmp(:)

      integer :: alloc_stat

      if (.not. allocated(self%nodes)) then
         allocate(self%nodes(TREE_NODE_BASE:TREE_NODE_UNIT))
         self%num_alloc = 1
      end if

      new_part_begin = ubound(self%nodes, dim=1) + 1
      new_part_end   = ubound(self%nodes, dim=1) * 2

      if (new_part_end > TREE_NODE_HARD_LIMIT) then
         self%is_valid = .false.
         self%code = SYNTAX_ERR_TOO_MANY_NODES
         return
         ! error stop "Exceeded the maximum number of tree nodes can be allocated."
      end if

      call move_alloc(self%nodes, tmp)

      allocate(self%nodes(TREE_NODE_BASE:new_part_end), stat=alloc_stat)

      if (alloc_stat /= 0) then
         self%is_valid = .false.
         self%code = SYNTAX_ERR_TOO_MANY_NODES
         return
         ! Restoration from tmp is unnecessary:
         ! nodes is not accessed after is_valid becomes false.
      end if

      self%nodes(TREE_NODE_BASE:new_part_begin-1) = tmp(TREE_NODE_BASE:new_part_begin-1)

      self%nodes(new_part_begin:new_part_end)%own_i = [(i, i = new_part_begin, new_part_end)]

      deallocate(tmp)

   end subroutine tree_graph__reallocate