append an entry to an allocatable array 1d with long integer If the array is too small, reallocate with double size
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=long_k), | intent(inout), | allocatable | :: | Array(:) |
array to append value to |
|
integer, | intent(in) | :: | Position |
position the value is appended to |
||
integer(kind=long_k), | intent(in) | :: | Value |
value to append |
subroutine tem_appendIntLong1dArray(Array, Position, Value )
! ---------------------------------------------------------------------------
!> array to append value to
integer(kind=long_k),intent(inout), allocatable :: Array(:)
!> position the value is appended to
integer,intent(in) :: Position
!> value to append
integer(kind=long_k),intent(in) :: Value
! ---------------------------------------------------------------------------
integer(kind=long_k),allocatable :: tempArray(:)
integer :: ArraySize,ierr,NewSize
logical :: sizeZero
! ---------------------------------------------------------------------------
! Get size of array
ArraySize = size(Array,1)
! Compare position, where to store with size
if(Position .gt. ArraySize) then ! If position is larger than size
sizeZero = .false.
if( ArraySize .eq. 0) then
ArraySize = 1
sizeZero = .true.
endif
NewSize = max(ArraySize*2, Position)
! allocate temporary array with double size
allocate(tempArray(NewSize), stat=ierr)
! Copy to temp array
if( .not. sizeZero ) tempArray(1:ArraySize) = Array(1:ArraySize)
! Deallocate Array
deallocate(Array)
! Reallocate Array
allocate(Array(NewSize), stat=ierr)
Array(1:ArraySize) = tempArray(1:ArraySize)
! Deallocate temp array
deallocate(tempArray)
if(ierr .ne. 0) Write(*,*) 'Error in reallocating array'
endif
Array(Position) = Value
end subroutine tem_appendIntLong1dArray