Clicky

Fortran Wiki
Generic programming (changes)

Skip the Navigation Links | Home Page | All Pages | Recently Revised | Authors | Feeds | Export |

Showing changes from revision #5 to #6: (追記) Added (追記ここまで) | (削除) Removed (削除ここまで) | (削除) Chan (削除ここまで)(追記) ged (追記ここまで)

Generic programming is a method for reusing a single piece in different contexts, such as with multiple data types. For example, the main body of a single numerical procedure can be reused for both single and double precision real variables or the code for a linked list can be reused for lists of integers, reals, or strings.

Generic programming is not available in Fortran 95 and it is only available in the form of parametrized user-defined types in Fortran 2003. However, the use of a preprocessor can assist with generic programming.

Some kinds of generic programming can be achieved using the transfer intrinsic, which copies bit patterns from one datatype to another without casting the value. Using this process, many data structures such as lists, stacks, queues, etc can be implemented by making the routines accept arrays of characters, and then using the transfer intrinsic to convert values before and after they are stored in the structure.

Examples

(追記)

Here is an example of using the transfer intrinsic function to achieve simple generic data storage. The following code compiles with gfortran 4.6:

(追記ここまで)
(追記) (追記ここまで)(追記)
module data_mod
 implicit none
 private
 integer,parameter::N = 10
 character,dimension(1),parameter::void = [achar(0)]
 type::data_t
 character,dimension(:),allocatable::d
 end type
 type(data_t),dimension(N)::data
 public::void
 public::set,get
contains
 subroutine set(k,d)
 integer,intent(in)::k
 character,dimension(:),intent(in)::d
 if(allocated(data(k)%d)) deallocate(data(k)%d)
 allocate(data(k)%d(size(d)))
 data(k)%d = d
 end subroutine set
 function get(k) result(d)
 integer,intent(in)::k
 character,dimension(:),allocatable::d
 allocate(d(size(data(k)%d)))
 d = data(k)%d
 end function get
end module data_mod
program main
 use data_mod
 implicit none
 call set(1,transfer(10,void))
 call set(2,transfer(10.0,void))
 call set(3,transfer(10.0d0,void))
 write(*,*) transfer(get(1),0)
 write(*,*) transfer(get(2),0.0)
 write(*,*) transfer(get(3),0.0d0)
end program main
(追記ここまで)
(追記) (追記ここまで)(追記)

The output of the above code is as follows

(追記ここまで)
(追記) (追記ここまで)(追記)
 10
 10.000000 
 10.000000000000000
(追記ここまで)
(追記) (追記ここまで)

See McGavin and Young (2001) and FLIBS for generic linked list implementations.

James Van Buskirk has written a generic version of BLAS available here: http://home.comcast.net/~kmbtib/Fortran_stuff/GENERIC_BLAS.ZIP

See Also

References

Revised on March 11, 2012 10:28:33 by Anonymous Coward (67.41.225.190) (3322 characters / 1.0 pages)
Edit | Back in time (5 revisions) | Hide changes | History | Views: Print | TeX | Source | Linked from: HomePage, Code, 2009, Linked list, FLIBS, Hash tables, Forpedo, Preprocessors, gen_list, hash table example

AltStyle によって変換されたページ (->オリジナル) /