Skip to content

Commit 26b6f58

Browse files
authored
Merge pull request #585 from jvdp1/pr_selection
stdlib_selection: correction of typos and addition of some checks
2 parents a457d88 + cbc3262 commit 26b6f58

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

doc/specs/stdlib_selection.md

+3-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ which implements selection algorithms.
2323
## Overview of the module
2424

2525
The module `stdlib_selection` defines two generic subroutines:
26+
2627
* `select` is used to find the k-th smallest entry of an array. The input
2728
array is also modified in-place, and on return will be partially sorted
2829
such that `all(array(1:k) <= array(k)))` and `all(array(k) <= array((k+1):size(array)))` is true.
@@ -159,7 +160,7 @@ Generic subroutine.
159160

160161
`array` : shall be a rank one array of any of the types:
161162
`integer(int8)`, `integer(int16)`, `integer(int32)`, `integer(int64)`,
162-
`real(sp)`, `real(dp)`, `real(xdp), `real(qp)`. It is an `intent(in)` argument. On input it is
163+
`real(sp)`, `real(dp)`, `real(xdp)`, `real(qp)`. It is an `intent(in)` argument. On input it is
163164
the array in which we search for the k-th smallest entry.
164165

165166
`indx`: shall be a rank one array with the same size as `array`, containing all integers
@@ -198,7 +199,7 @@ The code does not support `NaN` elements in `array`; it will run, but there is
198199
no consistent interpretation given to the order of `NaN` entries of `array`
199200
compared to other entries.
200201

201-
While it is essential that that `indx` contains a permutation of the integers `1:size(array)`,
202+
While it is essential that `indx` contains a permutation of the integers `1:size(array)`,
202203
the code does not check for this. For example if `size(array) == 4`, then we could have
203204
`indx = [4, 2, 1, 3]` or `indx = [1, 2, 3, 4]`, but not `indx = [2, 1, 2, 4]`. It is the user's
204205
responsibility to avoid such errors.

src/stdlib_selection.fypp

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
! The index arrays are of all INT_KINDS_TYPES
55

66
module stdlib_selection
7+
!! Quickly find the k-th smallest value of an array, or the index of the k-th smallest value.
8+
!! ([Specification](../page/specs/stdlib_selection.html))
79
!
810
! This code was modified from the "Coretran" implementation "quickSelect" by
911
! Leon Foks, https://github.com/leonfoks/coretran/tree/master/src/sorting
@@ -85,9 +87,10 @@ contains
8587
r = size(a, kind=ip)
8688
if(present(right)) r = right
8789

88-
if(k < 1_ip .or. k > size(a, kind=ip) .or. l > r .or. l < 1_ip .or. &
89-
r > size(a, kind=ip)) then
90-
error stop "select must have 1 <= k <= size(a), and 1 <= left <= right <= size(a)";
90+
if(l > r .or. l < 1_ip .or. r > size(a, kind=ip) &
91+
.or. k < l .or. k > r & !i.e. if k is not in the interval [l; r]
92+
) then
93+
error stop "select must have 1 <= left <= k <= right <= size(a)";
9194
end if
9295

9396
searchk: do
@@ -201,9 +204,10 @@ contains
201204
error stop "arg_select must have size(a) == size(indx)"
202205
end if
203206

204-
if(k < 1_ip .or. k > size(a, kind=ip) .or. l > r .or. l < 1_ip .or. &
205-
r > size(a, kind=ip)) then
206-
error stop "arg_select must have 1 <= k <= size(a), and 1 <= left <= right <= size(a)";
207+
if(l > r .or. l < 1_ip .or. r > size(a, kind=ip) &
208+
.or. k < l .or. k > r & !i.e. if k is not in the interval [l; r]
209+
) then
210+
error stop "arg_select must have 1 <= left <= k <= right <= size(a)";
207211
end if
208212

209213
searchk: do

0 commit comments

Comments
 (0)