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(kind=long_k), | intent(in) | :: | Position1 | |||
integer(kind=long_k), | intent(in) | :: | Position2 | |||
integer(kind=long_k), | intent(in) | :: | Value |
value to append |
subroutine tem_appendIntLong2dArray(Array, Position1, Position2, Value )
! ---------------------------------------------------------------------------
!> array to append value to
integer(kind=long_k),intent(inout), allocatable :: Array(:,:)
!>
integer(kind=long_k),intent(in) :: Position1
!>
integer(kind=long_k),intent(in) :: Position2
!> value to append
integer(kind=long_k),intent(in) :: Value
! ---------------------------------------------------------------------------
integer(kind=long_k),allocatable :: tempArray(:,:)
integer(kind=long_k) :: ArraySize1,ArraySize2,ierr
integer(kind=long_k) :: NewSize1,NewSize2
logical :: changeSize
logical :: sizeZero
! ---------------------------------------------------------------------------
! Get size of array
ArraySize1 = size(Array,1)
ArraySize2 = size(Array,2)
changeSize = .false.
sizeZero = .false.
! Compare position, where to store with size
if(Position1 .gt. ArraySize1) then
if( ArraySize1 .eq. 0) then
ArraySize1 = 1
sizeZero = .true.
endif
NewSize1 = max(Position1, ArraySize1*2)
changeSize = .true.
else
NewSize1 = ArraySize1
endif
if(Position2 .gt. ArraySize2) then
if( ArraySize2 .eq. 0) then
ArraySize2 = 1
sizeZero = .true.
endif
NewSize2 = max( Position2, ArraySize2*2 )
changeSize = .true.
else
NewSize2 = ArraySize2
endif
if(changeSize) then
! allocate temporary array with double size
allocate(tempArray(NewSize1, NewSize2),stat=ierr)
! Copy to temp array
if(.not. sizeZero) &
& tempArray( 1:ArraySize1,1:ArraySize2 ) = &
& Array( 1:ArraySize1,1:ArraySize2 )
! Deallocate Array
deallocate(Array)
! Reallocate Array
allocate(Array(NewSize1, NewSize2),stat=ierr)
Array(1:ArraySize1,1:ArraySize2) = tempArray(1:ArraySize1,1:ArraySize2)
! Deallocate temp array
deallocate(tempArray)
if(ierr .ne. 0) Write(*,*) 'Error in reallocating array'
endif
Array(Position1,Position2) = Value
end subroutine tem_appendIntLong2dArray