Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Another question: redefine operator

Zhanghong_T_
Novice
527 Views
My code is listed below:
module test
type link
integer num
type(link), pointer:: next
end type
type(link), pointer::head
interface operator(#)
module procedure structurevalue
end interface
contains
function structurevalue(in, num)result(out)
type(link), pointer, intent(in)::in
integer, intent(in)::num
integer, intent(out)::out
out=in%num
end function
function structaddress(num)result(out)
integer num
type(link), pointer:: out
out=>head
do while(associated(out%next))
if(out%num==num)exit
enddo
end function
end module
use test
...
print *, structaddress(5)#num
end
Can anyone tell me how should I modify the code to realize my idea?
Thanks!
Zhanghong, Tang
0 Kudos
4 Replies
Jugoslav_Dujic
Valued Contributor II
527 Views

Your code is fine, except for thefact that # is not allowed as operator name. You can only redefine standard operators (+, -, *, //, >, etc.) or define your own in form of .OP. Thus, you need either

interface operator (.OF.)

or, for example, < could look like a good candidate (just from the "aesthetical" viewpoint)

interface operator (<)
print *, structaddress(5)

Unfortunately, the most "natural" choice for the task in question would be something like [ ], e.g.

print *, structaddress(5)[num]

but this is not allowed.

Jugoslav

0 Kudos
Zhanghong_T_
Novice
527 Views

Thank you very much!

My task is very simple: I just want to define a allocatable array, and the refer it like a normal array, but the array must be able to be appended when its size is not enough. When reallocated, the former allocated can't be deallocated, because some other pointers pointed to it!

How can I realize this task?

Zhanghong, Tang

0 Kudos
Jugoslav_Dujic
Valued Contributor II
527 Views
Your definition of "simple" is a bit heavy... :-).
It can't be done in general -- you cannot "reallocate" an array of abasic type whilst ensuring that the original allocation will remain in the same place. The resulting array has to be contiguous, but there's no guarantee that there will be enough free space on the heap immediatelly following the original array.
Attached is a (very) old code of mine which fulfills just one of your requirements -- it allows the original array to stay in place. However, the contents have to be wrapped in a simple type. The idea is to have up to n=16 "parts" of an array, which makes it discontiguous. When access to an element exceeding original size is required, another chunk is allocated. However, the access is not very efficient. Operator < is overloaded to allow access to n-th element of the array. For example:
TYPE(Dynarray):: daX
b = DAConstruct(daX, 10)
do i=1,20
!This will get "reallocated" on i=11
call daSet(daX, i, i)
end do
do i=1,20
write(*,*) daX<(i)
end do
Alas, you cannot use overloaded operator < on the left side of assignment, i.e. it would be nice to write
daX<(i) = i
but it's not possible -- you cannot have an expression on left side of assignment.
Jugoslav
0 Kudos
Zhanghong_T_
Novice
527 Views

Thank you very much!

Each time I can study so much from you!

0 Kudos
Reply