Fortran 77 (Fortran-II) 中的抽象数据类型?
我正在尝试在 Fotran 77 中工作,并且发现需要基于树的数据结构。除了使用数组实现树之外,还有什么方法可以按照大多数语言的标准实现来构建具有指向其他节点的指针节点的树?
关于这个野兽的文档很少,并且似乎没有任何标准结构类型可以使这成为可能。
想法?
I'm attempting to work in Fotran 77, and I've found the need for a tree based data structure. Aside from implementing a tree with an array, is there any way to build a tree with pointer nodes to other nodes, as per a standard implementation in most languages?
The documentation for this beast is scarce, and there doesn't appear to be any standard structure type that would make this possible.
Thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我建议您迁移到 Fortran 90 或更高版本。 FORTRAN77 及更早版本的语言规范中没有指针,因此编译器编写者(和用户)想出了一大堆聪明*的方法来添加必要的功能来完成您想做的事情。 Fortran 90 具有用于动态数据结构的适当指针。
当然,聪明*意味着需要高级编程技能以及对内存、指针、引用和取消引用(所有这些对于大多数 Fortran 程序员来说都是陌生的)的理解,不可避免的结果是聪明*程序 不能在编译器之间、硬件平台之间、程序员之间移植。
我不明白为什么你会被限制在 FORTRAN77 中工作——标准 FORTRAN77 在语法上仍然是正确的并且可以与 Fortran 90 编译器编译。当然,您必须将新的树处理代码与旧语言的现有代码库集成,但这并不意味着您必须用旧语言编写新单元。
顺便说一句,FORTRAN77 比 FORTRANII 更现代。
I suggest you move to Fortran 90 or later. FORTRAN77 and earlier didn't have pointers in the language specification, so compiler writers (and users) came up with a whole raft of clever* ways of adding the necessary functionality to do just the sort of thing you want to do. Fortran 90 has proper pointers for dynamic data structures.
clever* means, of course requiring advanced programming skills and understanding of memory, pointers, referencing and de-referencing (all of which are alien to most Fortran programmers) with the inevitable consequence that clever* programs are not portable between compilers, nor between hardware platforms, nor between programmers.
I don't understand why you would be restricted to working in FORTRAN77 -- standard FORTRAN77 remains syntactically correct and compilable with Fortran 90 compilers. Sure, you have to integrate your new tree-processing code with the existing codebase in the old language, but that doesn't mean that you have to write new units in the old language.
And, in passing, FORTRAN77 was way more modern than FORTRANII.
这在 Fortran 95/2003 中会容易得多,因为它具有用户定义的派生类型和指针类型。使用这些功能可以设置数据结构,例如链表和树。 (指针类型称为指针,但它们更像是别名,因为指针算术是不可能的)。 Fortran >=95 比 Fortran 77 有许多改进。我的建议是不要使用 Fortran 77,除非对 Fortran 77 中的遗留代码进行较小的修改。一本好书是 Metcalf、Reid 所著的《Fortran 95/2003 解释》和科恩。
This would be much easier in Fortran 95/2003, which has user-defined derived types and pointer types. Using these features one can setup data structures such as linked lists and trees. (The pointer types are called pointers, but they are more like alias, in that pointer arithmetic isn't possible). Fortran >=95 has many improvements over Fortran 77. My recommendation is not to use Fortran 77 unless one is making minor modifications to legacy code that is in Fortran 77. A good book is "Fortran 95/2003 explained" by Metcalf, Reid and Cohen.
如果您确实无法使用 Fortran-77,则可以使用 Cray 指针:
http ://gcc.gnu.org/onlinedocs/gfortran/Cray-pointers.html
Cray 指针是非标准的,并且有一些缺点,但它们会给您类似于 C 指针的东西。它们受到 gfortran 和大多数商业编译器的支持。
话虽如此,您可能最好使用较新的 Fortran 功能,例如 Fortran-90 指针或 Fortran 2003 中的 C 互操作性功能。
If you're really stuck with Fortran-77, you can use Cray Pointers:
http://gcc.gnu.org/onlinedocs/gfortran/Cray-pointers.html
Cray Pointers are non-standard and have some drawbacks, but they'll give you something similar to a C pointer. They're supported by gfortran and most commercial compilers.
With that said, you would probably be better off using newer Fortran features, like Fortran-90 pointers or the C-interoperability features in Fortran 2003.
如果没有 Cray 指针或其他黑客技术,实现“数据类型”的唯一方法是使用并行数组,每个数组代表一个字段。那么,索引可以引用数据类型的实例。
Without Cray pointers or other hackery, the only way to implement a "data type" is with parallel arrays, each of which represents a field. An index, then, can refer to an instantiation of the data type.