class - Use of unlimited polymorphic type for array operation in Fortran 03/08 (gfortran compiler) -
i'd realize useful array operations (add element, remove element, different realizations allocatable/pointer/binary tree structures) class(*)
feature (unlimited polymorphism). use gfortran 5.0 should handle such feature. need not repeating identical code each type use.
this should like
function add_element(array,element) class(*),intent(in)::array(:) class(*),intent(in)::element class(*)::add_element(size(array)+1) add_element=[array,element] end function
the problem when try use function definite type, have error returning result. can not assign class(*)
definite type variable without select type
, , surely don't want have select type structures every time use it. , inside subroutine should not know of types want use, because create many of them.
i tried variants move_alloc
, source, tried use subroutine intent(out)
argument etc. didn't work. think should defined in argument attributes, same size (with source keyword?) didn't find example or definition of such structure in standard. of course study standard more (i'm not professional programmer physicist trying make programs testable, checkable , more comfortable change) , repeat code in waiting better solution, maybe knows search in standard or book? think not arrays use of class(*)
@ think there should methods don't know of types...
don't know if should add examples of other not working forms of subroutine or says error - or question unfocused. can compiled, in cases assigning definite type in call doesn't work. argument intent(out)
or (inout)
can not go dummy argument actual argument. reallocation source makes object has type (and result of assigning in example too), type hidden... , can't use select type in subroutine because don't know type.
also don't know constructs check "the same type as" or in context...
this not easy problem can use select type
, fortran doesn't have type is(type_of(x))
. on other hand, there same_type_as()
, extends type_of()
intrinsics, cannot use them type guards.
it necessary assure, dynamic types of both array
, element
same.
i think deficiency in standard.
but still, there error in approach. should make function result allocatable, able allocate correct dynamic type:
class(*), allocatable ::add_element(:)
you may think along lines of: (untested! compiles gfortran-4.9 ifort14)
allocate(add_element(size(array)+1), mold=array)
but how transfer values don't know , worried might not possible without resorting dirty tricks.
you cannot use transfer
, see real deficiency. eventhough can call transfer polymorphic mold
transfer(element, add_element(1))
you have no way assign array element
add_element(1) = transfer(element, add_element(1))
my opinion fortran lacks option type guards ensures 2 variables have same dynamic type.
you may think along lines of: (untested! compiles gfortran-4.9 ifort14)
function add_element(array,element) use iso_c_binding implicit none class(*),intent(in)::array(:) class(*),intent(in)::element class(*), allocatable ::add_element(:) type(c_ptr) :: tmp interface function memcpy(dest, src, n) bind(c) use iso_c_binding integer(c_intptr_t),value :: dest, src integer(c_size_t) :: n type(c_ptr) :: memcpy end function end interface allocate(add_element(size(array)+1), mold=array) tmp = memcpy(loc(add_element(size(array)+1)), & loc(array), & size(array, kind=c_size_t) * storage_size(array, c_size_t)/8_c_size_t ) tmp = memcpy(loc(add_element(size(array)+1)), & loc(array(1)), & storage_size(element, c_size_t)/8_c_size_t ) end function
Comments
Post a Comment