-
Notifications
You must be signed in to change notification settings - Fork 189
Addition of sort_adj
to sort an rank 1 array in the same order as an input array
#849
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
jvdp1
wants to merge
12
commits into
fortran-lang:master
Choose a base branch
from
jvdp1:add_sort_adj
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5f79d2e
rename sort_index to sort_adj
jvdp1 230a85c
sort_index: call of sort_adj
jvdp1 5230234
call of sort_adj in sort_index
jvdp1 962bcc4
cleaning
jvdp1 3cf4711
addition of specs and of example
jvdp1 01faf83
Apply suggestions from code review
jvdp1 28b6f71
some formatting
jvdp1 b1e51da
rename adj and index to adjoint
jvdp1 c8981f9
update specs
jvdp1 55c29c3
fix typos
jvdp1 54fc607
Apply suggestions from code review
jvdp1 724fbee
addition of tests for sort_adjoint
jvdp1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
ADD_EXAMPLE(ord_sort) | ||
ADD_EXAMPLE(sort) | ||
ADD_EXAMPLE(sort_adj) | ||
ADD_EXAMPLE(sort_index) | ||
ADD_EXAMPLE(radix_sort) | ||
ADD_EXAMPLE(sort_bitset) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
program example_sort_adj | ||
use stdlib_sorting, only: sort_adj | ||
implicit none | ||
integer, allocatable :: array(:) | ||
real, allocatable :: adj(:) | ||
|
||
array = [5, 4, 3, 1, 10, 4, 9] | ||
allocate(adj, source=real(array)) | ||
|
||
call sort_adj(array, adj) | ||
|
||
print *, array !print [1, 3, 4, 4, 5, 9, 10] | ||
print *, adj !print [1., 3., 4., 4., 5., 9., 10.] | ||
|
||
end program example_sort_adj |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
#! This approach allows us to have the same code for all input types. | ||
#:set IRSCB_TYPES_ALT_NAME = INT_TYPES_ALT_NAME + REAL_TYPES_ALT_NAME + STRING_TYPES_ALT_NAME + CHAR_TYPES_ALT_NAME & | ||
& + BITSET_TYPES_ALT_NAME | ||
#:set IR_INDEX_TYPES_ALT_NAME = INT_TYPES_ALT_NAME + REAL_TYPES_ALT_NAME | ||
|
||
!! Licensing: | ||
!! | ||
|
@@ -292,6 +293,76 @@ module stdlib_sorting | |
!! ! Sort the random data | ||
!! call radix_sort( array ) | ||
!! ... | ||
!!``` | ||
|
||
public sort_adj | ||
!! Version: experimental | ||
!! | ||
!! The generic subroutine implementing the `SORT_ADJ` algorithm to | ||
!! return an index array whose elements are sorted in the same order | ||
!! as the input array in the | ||
!! desired direction. It is primarily intended to be used to sort a | ||
!! rank 1 `integer` or `real` array based on the values of a component of the array. | ||
!! Its use has the syntax: | ||
!! | ||
!! call sort_adj( array, index[, work, iwork, reverse ] ) | ||
!! | ||
!! with the arguments: | ||
!! | ||
!! * array: the rank 1 array to be sorted. It is an `intent(inout)` | ||
!! argument of any of the types `integer(int8)`, `integer(int16)`, | ||
!! `integer(int32)`, `integer(int64)`, `real(real32)`, `real(real64)`, | ||
!! `real(real128)`, `character(*)`, `type(string_type)`, | ||
!! `type(bitset_64)`, `type(bitset_large)`. If both the | ||
!! type of `array` is real and at least one of the elements is a `NaN`, | ||
!! then the ordering of the `array` and `index` results is undefined. | ||
!! Otherwise it is defined to be as specified by reverse. | ||
!! | ||
!! * index: a rank 1 `integer` or `real` array. It is an `intent(inout)` | ||
!! argument of the type `integer(int_index)`. Its size shall be the | ||
!! same as `array`. On return, its elements are sorted in the same order | ||
!! as the input `array` in the direction specified by `reverse`. | ||
!! | ||
!! * work (optional): shall be a rank 1 array of the same type as | ||
!! `array`, and shall have at least `size(array)/2` elements. It is an | ||
!! `intent(out)` argument to be used as "scratch" memory | ||
!! for internal record keeping. If associated with an array in static | ||
!! storage, its use can significantly reduce the stack memory requirements | ||
!! for the code. Its value on return is undefined. | ||
!! | ||
!! * iwork (optional): shall be a rank 1 integer array of the same type as `index`, | ||
!! and shall have at least `size(array)/2` elements. It is an | ||
!! `intent(out)` argument to be used as "scratch" memory | ||
!! for internal record keeping. If associated with an array in static | ||
!! storage, its use can significantly reduce the stack memory requirements | ||
!! for the code. Its value on return is undefined. | ||
!! | ||
!! * `reverse` (optional): shall be a scalar of type default logical. It | ||
!! is an `intent(in)` argument. If present with a value of `.true.` then | ||
!! `array` will be sorted in order of non-increasing values in stable | ||
!! order. Otherwise `array` will be sorted in order of non-decreasing | ||
!! values in stable order. | ||
!! | ||
!!#### Examples | ||
!! | ||
!! Sorting a related rank one array: | ||
!! | ||
!!```Fortran | ||
!!program example_sort_adj | ||
!! use stdlib_sorting, only: sort_adj | ||
!! implicit none | ||
!! integer, allocatable :: array(:) | ||
!! real, allocatable :: adj(:) | ||
!! | ||
!! array = [5, 4, 3, 1, 10, 4, 9] | ||
!! allocate(adj, source=real(array)) | ||
!! | ||
!! call sort_adj(array, adj) | ||
!! | ||
!! print *, array !print [1, 3, 4, 4, 5, 9, 10] | ||
!! print *, adj !print [1., 3., 4., 4., 5., 9., 10.] | ||
!! | ||
!!end program example_sort_adj | ||
!!``` | ||
|
||
public sort_index | ||
|
@@ -505,6 +576,43 @@ module stdlib_sorting | |
|
||
end interface sort | ||
|
||
interface sort_adj | ||
!! Version: experimental | ||
!! | ||
!! The generic subroutine interface implementing the `SORT_ADJ` algorithm, | ||
!! based on the `"Rust" sort` algorithm found in `slice.rs` | ||
!! https://github.com/rust-lang/rust/blob/90eb44a5897c39e3dff9c7e48e3973671dcd9496/src/liballoc/slice.rs#L2159 | ||
!! but modified to return an array of indices that would provide a stable | ||
!! sort of the rank one `ARRAY` input. | ||
!! ([Specification](../page/specs/stdlib_sorting.html#sort_adj-creates-an-array-of-sorting-indices-for-an-input-array-while-also-sorting-the-array)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to be updated |
||
!! | ||
!! The indices by default correspond to a | ||
!! non-decreasing sort, but if the optional argument `REVERSE` is present | ||
!! with a value of `.TRUE.` the indices correspond to a non-increasing sort. | ||
|
||
#:for ti, tii, namei in IR_INDEX_TYPES_ALT_NAME | ||
#:for t1, t2, name1 in IRSCB_TYPES_ALT_NAME | ||
module subroutine ${name1}$_${namei}$_sort_adj( array, index, work, iwork, & | ||
reverse ) | ||
!! Version: experimental | ||
!! | ||
!! `${name1}$_${namei}$_sort_adj( array, index[, work, iwork, reverse] )` sorts | ||
!! an input `ARRAY` of type `${t1}$` | ||
!! using a hybrid sort based on the `"Rust" sort` algorithm found in `slice.rs` | ||
!! and returns the sorted `ARRAY` and an array `INDEX` of indices in the | ||
!! order that would sort the input `ARRAY` in the desired direction. | ||
${t1}$, intent(inout) :: array(0:) | ||
${ti}$, intent(inout) :: index(0:) | ||
${t2}$, intent(out), optional :: work(0:) | ||
${ti}$, intent(out), optional :: iwork(0:) | ||
logical, intent(in), optional :: reverse | ||
end subroutine ${name1}$_${namei}$_sort_adj | ||
|
||
#:endfor | ||
#:endfor | ||
|
||
end interface sort_adj | ||
|
||
interface sort_index | ||
!! Version: experimental | ||
!! | ||
|
@@ -521,7 +629,24 @@ module stdlib_sorting | |
|
||
#:for ki, ti, namei in INT_INDEX_TYPES_ALT_NAME | ||
#:for t1, t2, name1 in IRSCB_TYPES_ALT_NAME | ||
module subroutine ${name1}$_sort_index_${namei}$( array, index, work, iwork, & | ||
!> Version: experimental | ||
!> | ||
!> `${name1}$_sort_index_${namei}$( array, index[, work, iwork, reverse] )` sorts | ||
!> an input `ARRAY` of type `${t1}$` | ||
!> using a hybrid sort based on the `"Rust" sort` algorithm found in `slice.rs` | ||
!> and returns the sorted `ARRAY` and an array `INDEX` of indices in the | ||
!> order that would sort the input `ARRAY` in the desired direction. | ||
module procedure ${name1}$_sort_index_${namei}$ | ||
#:endfor | ||
#:endfor | ||
|
||
end interface sort_index | ||
|
||
contains | ||
|
||
#:for ki, ti, namei in INT_INDEX_TYPES_ALT_NAME | ||
#:for t1, t2, name1 in IRSCB_TYPES_ALT_NAME | ||
subroutine ${name1}$_sort_index_${namei}$( array, index, work, iwork, & | ||
reverse ) | ||
!! Version: experimental | ||
!! | ||
|
@@ -535,12 +660,29 @@ module stdlib_sorting | |
${t2}$, intent(out), optional :: work(0:) | ||
${ti}$, intent(out), optional :: iwork(0:) | ||
logical, intent(in), optional :: reverse | ||
|
||
jvdp1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
integer(int_index) :: array_size, i | ||
|
||
array_size = size(array, kind=int_index) | ||
|
||
if ( array_size > huge(index)) then | ||
error stop "Too many entries for the kind of index." | ||
end if | ||
|
||
if ( array_size > size(index, kind=int_index) ) then | ||
error stop "Too many entries for the size of index." | ||
end if | ||
|
||
do i = 0, array_size-1 | ||
index(i) = int(i+1, kind=${ki}$) | ||
end do | ||
|
||
call sort_adj(array, index, work, iwork, reverse) | ||
|
||
end subroutine ${name1}$_sort_index_${namei}$ | ||
|
||
#:endfor | ||
#:endfor | ||
|
||
end interface sort_index | ||
|
||
|
||
end module stdlib_sorting |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.