vec_int32_take Subroutine

private pure subroutine vec_int32_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_int32_t

Arguments

Type IntentOptional Attributes Name
class(vector_int32_t), intent(inout) :: self
integer(kind=int32), intent(out), allocatable :: array(:)
type(error_t), intent(inout), optional :: err

Calls

proc~~vec_int32_take~~CallsGraph proc~vec_int32_take vector_int32_t%vec_int32_take proc~vec_int32_capacity vector_int32_t%vec_int32_capacity proc~vec_int32_take->proc~vec_int32_capacity proc~vec_int32_shrink_to_fit vector_int32_t%vec_int32_shrink_to_fit proc~vec_int32_take->proc~vec_int32_shrink_to_fit proc~vec_int32_shrink_to_fit->proc~vec_int32_capacity proc~error_raise error_raise proc~vec_int32_shrink_to_fit->proc~error_raise proc~error_set error_t%error_set proc~error_raise->proc~error_set

Source Code

   pure subroutine vec_int32_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_int32_t), intent(inout) :: self
      integer(int32), 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_int32_shrink_to_fit(self, err)
      if (vec_int32_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_int32_take