From 1b6ae52b374737cddc5e631e10e452abf0913d3c Mon Sep 17 00:00:00 2001 From: Jeremie Vandenplas Date: Wed, 1 Dec 2021 22:06:03 +0100 Subject: [PATCH 1/6] update stdlib_selection typo --- doc/specs/stdlib_selection.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/specs/stdlib_selection.md b/doc/specs/stdlib_selection.md index b6694047c..79802ff2b 100644 --- a/doc/specs/stdlib_selection.md +++ b/doc/specs/stdlib_selection.md @@ -159,7 +159,7 @@ Generic subroutine. `array` : shall be a rank one array of any of the types: `integer(int8)`, `integer(int16)`, `integer(int32)`, `integer(int64)`, -`real(sp)`, `real(dp)`, `real(xdp), `real(qp)`. It is an `intent(in)` argument. On input it is +`real(sp)`, `real(dp)`, `real(xdp)`, `real(qp)`. It is an `intent(in)` argument. On input it is the array in which we search for the k-th smallest entry. `indx`: shall be a rank one array with the same size as `array`, containing all integers @@ -198,7 +198,7 @@ The code does not support `NaN` elements in `array`; it will run, but there is no consistent interpretation given to the order of `NaN` entries of `array` compared to other entries. -While it is essential that that `indx` contains a permutation of the integers `1:size(array)`, +While it is essential that `indx` contains a permutation of the integers `1:size(array)`, the code does not check for this. For example if `size(array) == 4`, then we could have `indx = [4, 2, 1, 3]` or `indx = [1, 2, 3, 4]`, but not `indx = [2, 1, 2, 4]`. It is the user's responsibility to avoid such errors. From 64b1b0ecfe622aa9d046a97b2367904ccf5d5c2d Mon Sep 17 00:00:00 2001 From: Jeremie Vandenplas Date: Wed, 1 Dec 2021 22:13:18 +0100 Subject: [PATCH 2/6] add checks --- src/stdlib_selection.fypp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/stdlib_selection.fypp b/src/stdlib_selection.fypp index 4a963c5b8..3e41f4a05 100644 --- a/src/stdlib_selection.fypp +++ b/src/stdlib_selection.fypp @@ -86,6 +86,7 @@ contains if(present(right)) r = right if(k < 1_ip .or. k > size(a, kind=ip) .or. l > r .or. l < 1_ip .or. & + k < l .or. k > r .or. & !i.e. if k is not in the interval [l; r] r > size(a, kind=ip)) then error stop "select must have 1 <= k <= size(a), and 1 <= left <= right <= size(a)"; end if @@ -202,6 +203,7 @@ contains end if if(k < 1_ip .or. k > size(a, kind=ip) .or. l > r .or. l < 1_ip .or. & + k < l .or. k > r .or. & !i.e. if k is not in the interval [l; r] r > size(a, kind=ip)) then error stop "arg_select must have 1 <= k <= size(a), and 1 <= left <= right <= size(a)"; end if From 1822ccd54840d770d1f2a3efcef2545919cebdba Mon Sep 17 00:00:00 2001 From: Jeremie Vandenplas Date: Thu, 2 Dec 2021 07:34:23 +0100 Subject: [PATCH 3/6] remove surplus conditions --- src/stdlib_selection.fypp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/stdlib_selection.fypp b/src/stdlib_selection.fypp index 3e41f4a05..3c56ea972 100644 --- a/src/stdlib_selection.fypp +++ b/src/stdlib_selection.fypp @@ -85,9 +85,9 @@ contains r = size(a, kind=ip) if(present(right)) r = right - if(k < 1_ip .or. k > size(a, kind=ip) .or. l > r .or. l < 1_ip .or. & - k < l .or. k > r .or. & !i.e. if k is not in the interval [l; r] - r > size(a, kind=ip)) then + if(l > r .or. l < 1_ip .or. r > size(a, kind=ip) & + .or. k < l .or. k > r & !i.e. if k is not in the interval [l; r] + ) then error stop "select must have 1 <= k <= size(a), and 1 <= left <= right <= size(a)"; end if @@ -202,9 +202,9 @@ contains error stop "arg_select must have size(a) == size(indx)" end if - if(k < 1_ip .or. k > size(a, kind=ip) .or. l > r .or. l < 1_ip .or. & - k < l .or. k > r .or. & !i.e. if k is not in the interval [l; r] - r > size(a, kind=ip)) then + if(l > r .or. l < 1_ip .or. r > size(a, kind=ip) & + .or. k < l .or. k > r & !i.e. if k is not in the interval [l; r] + ) then error stop "arg_select must have 1 <= k <= size(a), and 1 <= left <= right <= size(a)"; end if From 0312d3dbe7ff95c01fb2443dcd3dea315844ef84 Mon Sep 17 00:00:00 2001 From: Jeremie Vandenplas Date: Fri, 3 Dec 2021 20:25:47 +0100 Subject: [PATCH 4/6] change comment following @ivan-pi --- src/stdlib_selection.fypp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/stdlib_selection.fypp b/src/stdlib_selection.fypp index 3c56ea972..04a12d28c 100644 --- a/src/stdlib_selection.fypp +++ b/src/stdlib_selection.fypp @@ -88,7 +88,7 @@ contains if(l > r .or. l < 1_ip .or. r > size(a, kind=ip) & .or. k < l .or. k > r & !i.e. if k is not in the interval [l; r] ) then - error stop "select must have 1 <= k <= size(a), and 1 <= left <= right <= size(a)"; + error stop "select must have 1 <= left <= k <= right <= size(a)"; end if searchk: do @@ -205,7 +205,7 @@ contains if(l > r .or. l < 1_ip .or. r > size(a, kind=ip) & .or. k < l .or. k > r & !i.e. if k is not in the interval [l; r] ) then - error stop "arg_select must have 1 <= k <= size(a), and 1 <= left <= right <= size(a)"; + error stop "arg_select must have 1 <= left <= k <= right <= size(a)"; end if searchk: do From b6f442c44416c166728ac3fc44d1f222e94a012e Mon Sep 17 00:00:00 2001 From: Jeremie Vandenplas Date: Fri, 3 Dec 2021 20:26:13 +0100 Subject: [PATCH 5/6] change following @gareth-nx --- doc/specs/stdlib_selection.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/specs/stdlib_selection.md b/doc/specs/stdlib_selection.md index 79802ff2b..8523ef289 100644 --- a/doc/specs/stdlib_selection.md +++ b/doc/specs/stdlib_selection.md @@ -23,6 +23,7 @@ which implements selection algorithms. ## Overview of the module The module `stdlib_selection` defines two generic subroutines: + * `select` is used to find the k-th smallest entry of an array. The input array is also modified in-place, and on return will be partially sorted such that `all(array(1:k) <= array(k)))` and `all(array(k) <= array((k+1):size(array)))` is true. From cbc3262c32ea482990777a5d8637c1dcc1823187 Mon Sep 17 00:00:00 2001 From: Jeremie Vandenplas Date: Sun, 5 Dec 2021 18:42:27 +0100 Subject: [PATCH 6/6] add commment for module pr_selection --- src/stdlib_selection.fypp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/stdlib_selection.fypp b/src/stdlib_selection.fypp index 04a12d28c..277fd2eb4 100644 --- a/src/stdlib_selection.fypp +++ b/src/stdlib_selection.fypp @@ -4,6 +4,8 @@ ! The index arrays are of all INT_KINDS_TYPES module stdlib_selection +!! Quickly find the k-th smallest value of an array, or the index of the k-th smallest value. +!! ([Specification](../page/specs/stdlib_selection.html)) ! ! This code was modified from the "Coretran" implementation "quickSelect" by ! Leon Foks, https://github.com/leonfoks/coretran/tree/master/src/sorting