append an entry to an allocatable array 1d with single integer If the array is too small, reallocate with double size
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
integer(kind=int_k), | intent(inout), | allocatable | :: | Array(:,:) |
array to append value to |
|
integer, | intent(in) | :: | Position1 | |||
integer, | intent(in) | :: | Position2 | |||
integer(kind=int_k), | intent(in) | :: | Value |
value to append |
subroutine tem_appendInt2dArray(Array, Position1, Position2, Value ) ! --------------------------------------------------------------------------- !> array to append value to integer(kind=int_k),intent(inout), allocatable :: Array(:,:) !> integer,intent(in) :: Position1 !> integer,intent(in) :: Position2 !> value to append integer(kind=int_k),intent(in) :: Value ! --------------------------------------------------------------------------- integer,allocatable :: tempArray(:,:) integer :: ArraySize1,ArraySize2,ierr integer :: 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( ArraySize2*2, Position2 ) 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_appendInt2dArray