问题
Suppose that I need to implement at least 5 queues in one procedure, each of it from a different defined type. How can achive this in a simple and short way?.
Another way to see the question is the way that came to me: after a lot of time of defining my own structures in fortran, I had to make a program in C++, and then I saw how easy is the use of templates... now, I want the same in my mother tongue....
seems that the knowledge is not always confortable
Thanks a lot!
回答1:
If you really want templates, there is Pyf95++. It brings templating and an STL for Fortran using a preprocessor. (for download here)
A generic linked-list that uses transfer()
can be found in FLIBS.
(Otherwise with a bleeding edge compiler you can use unlimited polymorphism as suggested by Richard Lozes.)
回答2:
Have you considered unlimited polymorphic pointers? See, for ex., pp 269 ff in "Modern Fortran Explained".
回答3:
I have implemented the fortran template in Ruby, which is integrated in my CodeMate. The template syntax is similar to C++. I have implemented double linked list. The template definition snippets are as following:
...
template:list_t <type elem_t>
type list_t
integer :: num_elem = 0
type(elem_t), pointer :: head, tail
contains
...
procedure :: insert => list_insert
...
end type list
...
template:list_insert@list_t <type elem_t>
subroutine list_insert(this, elem, ...)
class(list_t), intent(inout) :: this
type(elem_t), intent(out), pointer :: elem
...
end subroutine list_insert
And the template instance is as following:
type(list_t<foo_t>) foo_list
where foo_t
is a user-defined type that extends list_elem_t<foo_t>
. The you can insert element into foo_list
by
call foo_list%insert(elem, ...)
I think my solution to Fortran template is natural.
来源:https://stackoverflow.com/questions/12906127/simple-way-to-implement-a-group-of-queues-in-fortran