Skip to content

Commit fe02d4b

Browse files
committed
elemental swap
1 parent 1d27d27 commit fe02d4b

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

doc/specs/stdlib_math.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,38 @@ Here inputs are of type `real` and kind `sp`
6161
{!example/math/example_clip_real.f90!}
6262
```
6363

64+
<!-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -->
65+
### `swap` function
66+
67+
#### Description
68+
69+
Swaps the values in `lhs` and `rhs`.
70+
71+
#### Syntax
72+
73+
`call` [[stdlib_math(module):swap(interface)]] ` (lhs, rhs)`
74+
75+
#### Status
76+
77+
Experimental
78+
79+
#### Class
80+
81+
Elemental function.
82+
83+
#### Argument(s)
84+
85+
`lhs`: scalar or array of any of the intrinsic types `integer`, `real`, `complex`, `logical`, `character` type. This argument is `intent(inout)`.
86+
`rhs`: scalar or array of any of the intrinsic types `integer`, `real`, `complex`, `logical`, `character` type. This argument is `intent(inout)`.
87+
88+
Note: All arguments must have same `type` and same `kind`.
89+
90+
#### Example
91+
92+
```fortran
93+
{!example/math/example_math_swap.f90!}
94+
```
95+
6496
### `gcd` function
6597

6698
#### Description

example/math/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ ADD_EXAMPLE(math_argpi)
1515
ADD_EXAMPLE(math_deg2rad)
1616
ADD_EXAMPLE(math_rad2deg)
1717
ADD_EXAMPLE(math_is_close)
18+
ADD_EXAMPLE(math_swap)
1819
ADD_EXAMPLE(meshgrid)

example/math/example_math_swap.f90

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
program example_math_swap
2+
use stdlib_math, only: swap
3+
implicit none
4+
5+
block
6+
integer :: x, y
7+
x = 9
8+
y = 18
9+
call swap(x,y)
10+
end block
11+
12+
block
13+
real :: x, y
14+
x = 4.0
15+
y = 8.0
16+
call swap(x,y)
17+
end block
18+
19+
block
20+
real :: x(3), y(3)
21+
x = [1.0,2.0,3.0]
22+
y = [4.0,5.0,6.0]
23+
call swap(x,y)
24+
end block
25+
26+
block
27+
character(5) :: x, y
28+
x = 'abcde'
29+
y = 'fghij'
30+
call swap(x,y)
31+
end block
32+
33+
end program example_math_swap

src/stdlib_math.fypp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module stdlib_math
88

99
implicit none
1010
private
11-
public :: clip, gcd, linspace, logspace
11+
public :: clip, swap, gcd, linspace, logspace
1212
public :: EULERS_NUMBER_SP, EULERS_NUMBER_DP
1313
#:if WITH_QP
1414
public :: EULERS_NUMBER_QP
@@ -42,6 +42,17 @@ module stdlib_math
4242
#:endfor
4343
end interface clip
4444

45+
interface swap
46+
#:for k1, t1 in INT_KINDS_TYPES + REAL_KINDS_TYPES
47+
module procedure :: swap_${k1}$
48+
#:endfor
49+
#:for k1, t1 in CMPLX_KINDS_TYPES
50+
module procedure :: swap_c${k1}$
51+
#:endfor
52+
module procedure :: swap_bool
53+
module procedure :: swap_str
54+
end interface
55+
4556
!> Returns the greatest common divisor of two integers
4657
!> ([Specification](../page/specs/stdlib_math.html#gcd))
4758
!>
@@ -509,5 +520,35 @@ contains
509520
end function gcd_${k1}$
510521

511522
#:endfor
523+
524+
#:for k1, t1 in INT_KINDS_TYPES + REAL_KINDS_TYPES
525+
elemental subroutine swap_${k1}$(lhs, rhs)
526+
${t1}$, intent(inout) :: lhs, rhs
527+
${t1}$ :: temp
528+
temp = lhs; lhs = rhs; rhs = temp
529+
end subroutine
530+
531+
#:endfor
532+
533+
#:for k1, t1 in CMPLX_KINDS_TYPES
534+
elemental subroutine swap_c${k1}$(lhs, rhs)
535+
${t1}$, intent(inout) :: lhs, rhs
536+
${t1}$ :: temp
537+
temp = lhs; lhs = rhs; rhs = temp
538+
end subroutine
539+
540+
#:endfor
541+
542+
elemental subroutine swap_bool(lhs, rhs)
543+
logical, intent(inout) :: lhs, rhs
544+
logical :: temp
545+
temp = lhs; lhs = rhs; rhs = temp
546+
end subroutine
547+
548+
elemental subroutine swap_str(lhs, rhs)
549+
character(1), intent(inout) :: lhs, rhs
550+
character(1) :: temp
551+
temp = lhs; lhs = rhs; rhs = temp
552+
end subroutine
512553

513554
end module stdlib_math

0 commit comments

Comments
 (0)