Module 将Fortran变量分配给模块实例中的变量
我想构建一个哈希表模块,该模块可以使用Fortran 90中的不同实例(我不确定该如何命名)。这就是我努力实现的目标:Module 将Fortran变量分配给模块实例中的变量,module,fortran,Module,Fortran,我想构建一个哈希表模块,该模块可以使用Fortran 90中的不同实例(我不确定该如何命名)。这就是我努力实现的目标: module hashing implicit none private real, dimension(:), allocatable :: & real1 public :: & Hash, h
module hashing
implicit none
private
real, dimension(:), allocatable :: &
real1
public :: &
Hash, hash_init, hash_push, hash_set, hash_get, &
hash_print
type Hash
character (len=128) :: &
data_type
integer :: &
array_size
end type Hash
contains
subroutine hash_init (this)
type(Hash), intent (in) :: &
this
if (trim(this%dtype) == "real1) then
call make_real1(this%array_size)
end if
end subroutine hash_init
subroutine hash_print (this)
type(Hash), intent (in) :: &
this
if (trim(this%dtype) == "real1) then
print *, real1
end if
end subroutine hash_print
end module hashing
因此,在我的测试程序中,两个call hash_print
语句都打印大小为5的数组,因为real1数组是模块散列的一般实例,而不是(我试图实现的)打印大小为10的第一个实例和大小为5的第二个实例
program test
use hashing
implicit none
type(Hash) :: &
inst1, inst2
inst1 = Hash("real1", 10)
inst2 = Hash("real1", 5)
call hash_init(inst1)
call hash_init(inst2)
call hash_print(inst1)
call hash_print(inst2)
end program test
主要思想是添加整数和字符类型的维度,并使用if语句进行扩展,但我需要知道在模块中的何处需要声明这些可分配维度,以便它们仅绑定到特定实例。如果可能的话!我已尝试在
hash_init
子例程中声明可分配数组,但它不适用于hash_print
子例程。您真的想将自己限制在旧的Fortran 90吗?答案坚持这一点重要吗?Fortran 90中不允许使用可分配的派生类型组件,也不允许使用可分配的字符串。请您更清楚地说明问题是什么?你有没有尝试过在网络上使用通用哈希表实现?@VladimirF我只使用Fortran90进行工作。我的问题是(在这个问题中):我希望同时有两个real1维度的实例(一个被分配给大小为10的inst1,另一个被分配给大小为5的inst2)。我想我想要一些类似于python类实例的东西。既然你已经用oop标记了你的问题,我必须说使用模块变量并不是一种oop方式来做事情——你也应该将它们包装成一个类型。在这种情况下,您只需要在程序中保存holder类型的实例,而不需要声明任何公共模块变量——从OOP的角度来看,这被认为是不安全的。我真的建议允许至少使用Fortran 95,更现代的2003/2008来处理任何对象/实例等。如果希望real1
位于派生类型内,则需要Fortran 2003。但我还是不明白这个问题,但那可能是我的错。