Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A solution without linked list. </p> <p>Here, one assumes that the vectors "r" contain double precision values. </p> <p>Notice that this solution uses no pointer, just allocatable arrays, which warrants to avoid memory leaks. The number of reallocations is limited (log2(list%n)) but one accepts to allocate list%result with a size larger than really needed (maximum twice). </p> <p>At last, the vectors "r" are duplicated in the list (this is not the case in the python version).</p> <pre><code>module extendable_list implicit none type result_type double precision,allocatable :: vector(:) end type type list_type integer :: n type(result_type),allocatable :: result(:) end type contains subroutine append(list,r) type(list_type),intent(inout) :: list double precision,intent(in) :: r(:) type(result_type),allocatable :: temporary(:) integer :: i if(.not.allocated(list%result)) then allocate(list%result(10)) list%n=0 else if(list%n &gt;= size(list%result)) then allocate(temporary(2*list%n)) do i=1,list%n call move_alloc(list%result(i)%vector,temporary(i)%vector) enddo call move_alloc(temporary,list%result) endif list%n=list%n+1 allocate(list%result(list%n)%vector(size(r))) list%result(list%n)%vector=r end subroutine end module program main use extendable_list implicit none type(list_type) :: list integer :: i do i=1,10 call append(list,(/1.d0,3.d0/)) call append(list,(/7.d0,-9.d0,45.d0/)) enddo do i=1,list%n write(*,*) list%result(i)%vector enddo end program </code></pre> <p>Result :</p> <pre><code>coul@b10p5001:~/test$ ifort t65.f90 coul@b10p5001:~/test$ ./a.out 1.00000000000000 3.00000000000000 7.00000000000000 -9.00000000000000 45.0000000000000 1.00000000000000 3.00000000000000 7.00000000000000 -9.00000000000000 45.0000000000000 1.00000000000000 3.00000000000000 7.00000000000000 -9.00000000000000 45.0000000000000 1.00000000000000 3.00000000000000 7.00000000000000 -9.00000000000000 45.0000000000000 1.00000000000000 3.00000000000000 7.00000000000000 -9.00000000000000 45.0000000000000 1.00000000000000 3.00000000000000 7.00000000000000 -9.00000000000000 45.0000000000000 1.00000000000000 3.00000000000000 7.00000000000000 -9.00000000000000 45.0000000000000 1.00000000000000 3.00000000000000 7.00000000000000 -9.00000000000000 45.0000000000000 1.00000000000000 3.00000000000000 7.00000000000000 -9.00000000000000 45.0000000000000 1.00000000000000 3.00000000000000 7.00000000000000 -9.00000000000000 45.0000000000000 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload