Open zoziha opened 3 years ago
swap
using move_alloc
for string_type
(or in combination with allocatable character (character(len=:), allocatable
)) might be a useful addition.
swap
usingmove_alloc
forstring_type
(or in combination with allocatable character (character(len=:), allocatable
)) might be a useful addition.
Hello @Aman-Godara , for string_type
, is there any significant difference in efficiency when swap
uses move_alloc
or assignment(=)
?
a = string_type( "... a very long string ..." )
b = string_type( "... another long string ..." )
Now if you want to swap a
and b
, using assignment operator your approach will be something like this:
c = a ! This will create a deep copy (since the underlying `raw` variable is `allocatable` and NOT `pointer`) of `a` and assign it to `c`)
a = b
b = c
Using move_alloc
it will be like this:
move_alloc( a, c ) ! happens in O(1) time since no new copy has been created
move_alloc( b, a )
move_alloc( c, b )
So move_alloc
is faster and takes O(1) extra space whereas assignment operator takes O(m) extra space and executes in O(m + n). Here m and n are the lengths of the input a
and b
Thank you for your reply. I just tested it and found that string_type%raw
is of a private
attribute, so we can't directly access allocatable
raw
using move_alloc
.
type :: string_type
! Use the sequence statement below as a hack to prevent extending this type.
! It is not used for storage association.
sequence
private
character(len=:), allocatable :: raw !! Component 'raw' is a PRIVATE component of 'string_type'
end type string_type
And move_alloc
requires its arguments to be allocatable
.
type(string_type) :: a, b !! They are not allocatable.
a = string_type( "... a very long string ..." )
b = string_type( "... another long string ..." )
call move_alloc( b, a ) !! Error: `move_alloc` requires its arguments to be `allocatable`.
I also found that move_alloc
is indeed more efficient than assignment
operation. 👍
you cannot access raw
variable outside of stdlib_stringtype.f90
file.
Yes you are right move_alloc
requires it arguments to be allocatable. Excuse me if I gave you wrong impression above. First we need to create a function which does equivalent of move_alloc
for string_type
and then use it in swap
function.
In my comment above, by move_alloc( a, c ) I meant call a subroutine which does equivalent of move_alloc
for string_type
.
Look at this PR to know more.
Thank you very much!😘 Understood!
Description
Does
stdlib
need simpleswap
routines as follows? (Wait for #449 )The use of
swap
already instdlib
: