Skip to content

Commit 1ad6725

Browse files
committed
Further changes suggested by jvdp1
1 parent ddec12c commit 1ad6725

File tree

2 files changed

+27
-25
lines changed

2 files changed

+27
-25
lines changed

doc/specs/stdlib_selection.md

Lines changed: 22 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -33,44 +33,42 @@ to find a smaller or larger rank (that will have led to partial sorting of
3333

3434
* `arg_select` is used to find the index of the k-th smallest entry of an array.
3535
In this case the input array is not modified, but the user must provide an
36-
input index array with the same size as `array`, having unique indices from
36+
input index array with the same size as `array`, having indices that are a permutation of
3737
`1:size(array)`, which is modified instead. On return the index array is modified
3838
such that `all(array(index(1:k)) <= array(index(k)))` and `all(array(k) <= array(k+1:size(array)))`.
3939
The user can optionally specify `left` and `right` indices to constrain the search
4040
for the k-th smallest value. This can be useful if you have previously called `arg_select`
4141
to find a smaller or larger rank (that will have led to partial sorting of
4242
`index`, thus implying some constraints on the location).
4343

44-
#### Licensing
44+
### Licensing
4545

4646
The Fortran Standard Library is distributed under the MIT
4747
License. It is worth noting that `select` and `arg_select`
4848
were derived using some code from quickSelect in the Coretran library, by Leon Foks,
4949
https://github.com/leonfoks/coretran. Leon Foks has given permission for the code here
5050
to be released under stdlib's MIT license.
5151

52-
### Specifications of the `stdlib_selection` procedures
52+
## `select` - find the k-th smallest value in an input array
5353

54-
#### `select` - find the k-th smallest value in an input array
55-
56-
##### Status
54+
### Status
5755

5856
Experimental
5957

60-
##### Description
58+
### Description
6159

6260
Returns the k-th smallest value of `array(:)`, and also partially sorts `array(:)`
6361
such that `all(array(1:k) <= array(k))` and `all(array(k) <= array((k+1):size(array)))`
6462

65-
##### Syntax
63+
### Syntax
6664

6765
`call [[stdlib_selection(module):select(interface)]]( array, k, kth_smallest [, left, right ] )`
6866

69-
##### Class
67+
### Class
7068

7169
Generic subroutine.
7270

73-
##### Arguments
71+
### Arguments
7472

7573
`array` : shall be a rank one array of any of the types:
7674
`integer(int8)`, `integer(int16)`, `integer(int32)`, `integer(int64)`,
@@ -95,16 +93,13 @@ in `array(1:right)`. If `right` is not present, the default is `size(array)`. Th
9593
to `select` are made, because the partial sorting of `array` implies constraints on where
9694
we need to search.
9795

98-
##### Notes
96+
### Notes
9997

10098
Selection of a single value should have runtime of O(`size(array)`), so it is
10199
asymptotically faster than sorting `array` entirely. The test program at the
102100
end of this document shows that is the case.
103101

104-
On return the elements of `array` will be partially sorted such that:
105-
`all( array(1:k-1) <= array(k) )` and `all(array(k) <= array(k+1:size(array)))`.
106-
107-
##### Example
102+
### Example
108103

109104
```fortran
110105
program demo_select
@@ -136,27 +131,27 @@ On return the elements of `array` will be partially sorted such that:
136131
end program demo_select
137132
```
138133

139-
#### `arg_select` - find the kth smallest value in an input array
134+
## `arg_select` - find the kth smallest value in an input array
140135

141-
##### Status
136+
### Status
142137

143138
Experimental
144139

145-
##### Description
140+
### Description
146141

147142
Returns the index of the k-th smallest value of `array(:)`, and also partially sorts
148143
the index-array `indx(:)` such that `all(array(indx(1:k)) <= array(indx(k)))` and
149144
`all(array(indx(k)) <= array(indx((k+1):size(array))))`
150145

151-
##### Syntax
146+
### Syntax
152147

153148
`call [[stdlib_selection(module):arg_select(interface)]]( array, indx, k, kth_smallest [, left, right ] )`
154149

155-
##### Class
150+
### Class
156151

157152
Generic subroutine.
158153

159-
##### Arguments
154+
### Arguments
160155

161156
`array` : shall be a rank one array of any of the types:
162157
`integer(int8)`, `integer(int16)`, `integer(int32)`, `integer(int64)`,
@@ -187,19 +182,21 @@ in `array(indx(1:right))`. If `right` is not present, the default is `size(array
187182
to `arg_select` are made, because the reordering of `indx` implies constraints on
188183
where we need to search.
189184

190-
##### Notes
185+
### Notes
191186

192187
`arg_select` does not modify `array`, unlike `select`.
193188

194-
While it is essential that that `indx` contains the integers `1:size(array)` (in any
195-
order), the code does not check for this.
189+
While it is essential that that `indx` contains a permutation of the integers `1:size(array)`,
190+
the code does not check for this. For example if `size(array) == 4`, then we could have
191+
`indx = [4, 2, 1, 3]` or `indx = [1, 2, 3, 4]`, but not `indx = [2, 1, 2, 4]`. It is the user's
192+
responsibility to avoid such errors.
196193

197194
Selection of a single value should have runtime of O(`size(array)`), so it is
198195
asymptotically faster than sorting `array` entirely. The test program at the end of
199196
these documents confirms that is the case.
200197

201198

202-
##### Example
199+
### Example
203200

204201

205202
```fortran

src/stdlib_selection.fypp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ private
2121
public select, arg_select
2222

2323
interface select
24+
!! version: experimental
25+
!! ([Specification](..//page/specs/stdlib_selection.html#description))
26+
2427
#:for arraykind, arraytype in ARRAY_KINDS_TYPES
2528
#:for intkind, inttype in INT_KINDS_TYPES
2629
#:set name = rname("select", 1, arraytype, arraykind, intkind)
@@ -30,6 +33,8 @@ interface select
3033
end interface
3134

3235
interface arg_select
36+
!! version: experimental
37+
!! ([Specification](..//page/specs/stdlib_selection.html#description_1))
3338
#:for arraykind, arraytype in ARRAY_KINDS_TYPES
3439
#:for intkind, inttype in INT_KINDS_TYPES
3540
#:set name = rname("arg_select", 1, arraytype, arraykind, intkind)

0 commit comments

Comments
 (0)