vec_string_take Subroutine

private pure subroutine vec_string_take(self, array, err)

Hand the storage over to array, exactly sized, without copying the elements. The vector is left empty and without storage.

This is the load-time idiom: build a vector while parsing an input of unknown length, then take the result into the permanent array. as_array copies; this does not.

Type Bound

vector_string_t

Arguments

Type IntentOptional Attributes Name
class(vector_string_t), intent(inout) :: self
type(string_type), intent(out), allocatable :: array(:)
type(error_t), intent(inout), optional :: err

Calls

proc~~vec_string_take~~CallsGraph proc~vec_string_take vector_string_t%vec_string_take proc~vec_string_capacity vector_string_t%vec_string_capacity proc~vec_string_take->proc~vec_string_capacity proc~vec_string_shrink_to_fit vector_string_t%vec_string_shrink_to_fit proc~vec_string_take->proc~vec_string_shrink_to_fit proc~vec_string_shrink_to_fit->proc~vec_string_capacity proc~error_raise error_raise proc~vec_string_shrink_to_fit->proc~error_raise proc~error_set error_t%error_set proc~error_raise->proc~error_set

Source Code

   pure subroutine vec_string_take(self, array, err)
      !! Hand the storage over to `array`, exactly sized, without copying the
      !! elements. The vector is left empty and without storage.
      !!
      !! This is the load-time idiom: build a vector while parsing an input of
      !! unknown length, then `take` the result into the permanent array.
      !! `as_array` copies; this does not.
      class(vector_string_t), intent(inout) :: self
      type(string_type), allocatable, intent(out) :: array(:)
      type(error_t), intent(inout), optional :: err

      ! Spare capacity would otherwise be handed over as live elements, so the
      ! shrink is part of the contract rather than an optimisation.
      call vec_string_shrink_to_fit(self, err)
      if (vec_string_capacity(self) /= self%n_items) return

      if (.not. allocated(self%items)) allocate (self%items(0))
      call move_alloc(self%items, array)
      self%n_items = 0
   end subroutine vec_string_take