Skip to content

Commit 818c071

Browse files
author
Carl Burkert
committed
Use the optval function in the sorting routines
The optval function replaces some nested if statemens and allows to remove the reverse_ variable which is a clone of the optional reverse variable.
1 parent 49dc570 commit 818c071

File tree

4 files changed

+8
-20
lines changed

4 files changed

+8
-20
lines changed

src/stdlib_sorting.fypp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,8 @@ module stdlib_sorting
123123
dp, &
124124
qp
125125

126+
use stdlib_optval, only: optval
127+
126128
use stdlib_string_type, only: string_type, assignment(=), operator(>), &
127129
operator(>=), operator(<), operator(<=)
128130

src/stdlib_sorting_ord_sort.fypp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,7 @@ contains
7575
${t3}$, intent(out), optional :: work(0:)
7676
logical, intent(in), optional :: reverse
7777

78-
logical :: reverse_
79-
80-
reverse_ = .false.
81-
if(present(reverse)) reverse_ = reverse
82-
83-
if (reverse_) then
78+
if (optval(reverse, .false.)) then
8479
call ${name1}$_decrease_ord_sort(array, work)
8580
else
8681
call ${name1}$_increase_ord_sort(array, work)

src/stdlib_sorting_sort.fypp

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,7 @@ contains
7878
${t1}$, intent(inout) :: array(0:)
7979
logical, intent(in), optional :: reverse
8080

81-
logical :: reverse_
82-
83-
reverse_ = .false.
84-
if(present(reverse)) reverse_ = reverse
85-
86-
if(reverse_)then
81+
if(optval(reverse, .false.))then
8782
call ${name1}$_decrease_sort(array)
8883
else
8984
call ${name1}$_increase_sort(array)

src/stdlib_sorting_sort_index.fypp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,8 @@ contains
107107
index(i) = i+1
108108
end do
109109

110-
if ( present(reverse) ) then
111-
if ( reverse ) then
112-
call reverse_segment( array, index )
113-
end if
110+
if ( optval(reverse, .false.) ) then
111+
call reverse_segment( array, index )
114112
end if
115113

116114
! If necessary allocate buffers to serve as scratch memory.
@@ -148,10 +146,8 @@ contains
148146
end if
149147
end if
150148

151-
if ( present(reverse) ) then
152-
if ( reverse ) then
153-
call reverse_segment( array, index )
154-
end if
149+
if ( optval(reverse, .false.) ) then
150+
call reverse_segment( array, index )
155151
end if
156152

157153
contains

0 commit comments

Comments
 (0)