Quantcast
Channel: Intel® Fortran Compiler
Viewing all articles
Browse latest Browse all 3270

Generic subroutine for various types of input arguments

$
0
0

Hi,

I have a subroutine that receives a vector and rearranges its elements according to "mapping" vector. So for example the 1st element becomes 3rd, etc. This is done on integer, double, and double complex vectors. One way to do it is with the generic interface, e.g.:

module mapping_mod

   implicit none

   interface map
      module procedure imap
      module procedure dmap
      module procedure zmap
   end interface map

   private
   public :: map

contains

   subroutine imap (map, in_vector,out_vector)
      integer, dimension (:), intent (in) :: map
      integer, dimension (:), intent (in) :: in_vector
      integer, dimension (:), intent (out) :: out_vector
      ! do mapping
   end subroutine imap

   subroutine dmap (map, in_vector,out_vector)
      integer, dimension (:), intent (in) :: map
      double, dimension (:), intent (in) :: in_vector
      double, dimension (:), intent (out) :: out_vector
      ! do mapping
   end subroutine dmap

   subroutine zmap (map, in_vector,out_vector)
      integer, dimension (:), intent (in) :: map
      double complex, dimension (:), intent (in) :: in_vector
      double complex, dimension (:), intent (out) :: out_vector
      ! do mapping
   end subroutine zmap

end module mapping_mod

However, the actual mapping procedure needs then to be copy-pasted 3 times. Which is not very elegant. This is not a very complicated example, but there are similar functions in the program much more complicated that follow the same structure (integer, double, complex).

Is there a more elegant way to implement this using a single procedure? Maybe with the use of select type or some other fortran 2003 features?

Thanks in advance,

Petros


Viewing all articles
Browse latest Browse all 3270

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>