Skip to content
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

Rename int size to int_index #824

Merged
merged 2 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions doc/specs/stdlib_sorting.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,15 @@ module's `string_type` type.
## Overview of the module

The module `stdlib_sorting` defines several public entities, one
default integer parameter, `int_size`, and four overloaded
default integer parameter, `int_index`, and four overloaded
subroutines: `ORD_SORT`, `SORT`, `RADIX_SORT` and `SORT_INDEX`. The
overloaded subroutines also each have several specific names for
versions corresponding to different types of array arguments.

### The `int_size` parameter
### The `int_index` parameter

The `int_size` parameter is used to specify the kind of integer used
in indexing the various arrays. Currently the module sets `int_size`
The `int_index` parameter is used to specify the kind of integer used
in indexing the various arrays. Currently the module sets `int_index`
to the value of `int64` from the `stdlib_kinds` module.

### The module subroutines
Expand Down Expand Up @@ -414,7 +414,7 @@ It is an `intent(inout)` argument. On input it
will be an array whose sorting indices are to be determined. On return
it will be the sorted array.

`index`: shall be a rank one integer array of kind `int_size` and of
`index`: shall be a rank one integer array of kind `int_index` and of
the size of `array`. It is an `intent(out)` argument. On return it
shall have values that are the indices needed to sort the original
array in the desired direction.
Expand All @@ -427,7 +427,7 @@ static storage, its use can significantly reduce the stack memory
requirements for the code. Its contents on return are undefined.

`iwork` (optional): shall be a rank one integer array of kind
`int_size`, and shall have at least `size(array)/2` elements. It
`int_index`, and shall have at least `size(array)/2` elements. It
is an `intent(out)` argument. It is intended 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
Expand Down Expand Up @@ -465,8 +465,8 @@ Sorting a related rank one array:
integer, intent(inout) :: a(:)
integer(int32), intent(inout) :: b(:) ! The same size as a
integer(int32), intent(out) :: work(:)
integer(int_size), intent(out) :: index(:)
integer(int_size), intent(out) :: iwork(:)
integer(int_index), intent(out) :: index(:)
integer(int_index), intent(out) :: iwork(:)
! Find the indices to sort a
call sort_index(a, index(1:size(a)),&
work(1:size(a)/2), iwork(1:size(a)/2))
Expand All @@ -483,8 +483,8 @@ Sorting a rank 2 array based on the data in a column
integer, intent(inout) :: array(:,:)
integer(int32), intent(in) :: column
integer(int32), intent(out) :: work(:)
integer(int_size), intent(out) :: index(:)
integer(int_size), intent(out) :: iwork(:)
integer(int_index), intent(out) :: index(:)
integer(int_index), intent(out) :: iwork(:)
integer, allocatable :: dummy(:)
integer :: i
allocate(dummy(size(array, dim=1)))
Expand All @@ -508,8 +508,8 @@ Sorting an array of a derived type based on the data in one component
type(a_type), intent(inout) :: a_data(:)
integer(int32), intent(inout) :: a(:)
integer(int32), intent(out) :: work(:)
integer(int_size), intent(out) :: index(:)
integer(int_size), intent(out) :: iwork(:)
integer(int_index), intent(out) :: index(:)
integer(int_index), intent(out) :: iwork(:)
! Extract a component of `a_data`
a(1:size(a_data)) = a_data(:) % a
! Find the indices to sort the component
Expand Down
32 changes: 16 additions & 16 deletions src/stdlib_sorting.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ module stdlib_sorting
implicit none
private

integer, parameter, public :: int_size = int64 !! Integer kind for indexing
integer, parameter, public :: int_index = int64 !! Integer kind for indexing

! Constants for use by tim_sort
integer, parameter :: &
Expand All @@ -152,8 +152,8 @@ module stdlib_sorting
!!
!! Used to pass state around in a stack among helper functions for the
!! `ORD_SORT` and `SORT_INDEX` algorithms
integer(int_size) :: base = 0
integer(int_size) :: len = 0
integer(int_index) :: base = 0
integer(int_index) :: len = 0
end type run_type

public ord_sort
Expand Down Expand Up @@ -313,7 +313,7 @@ module stdlib_sorting
!! Otherwise it is defined to be as specified by reverse.
!!
!! * index: a rank 1 array of sorting indices. It is an `intent(out)`
!! argument of the type `integer(int_size)`. Its size shall be the
!! argument of the type `integer(int_index)`. Its size shall be the
!! same as `array`. On return, if defined, its elements would
!! sort the input `array` in the direction specified by `reverse`.
!!
Expand All @@ -324,7 +324,7 @@ module stdlib_sorting
!! 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 kind `int_size`,
!! * iwork (optional): shall be a rank 1 integer array of kind `int_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
Expand All @@ -347,8 +347,8 @@ module stdlib_sorting
!! integer, intent(inout) :: a(:)
!! integer(int32), intent(inout) :: b(:) ! The same size as a
!! integer(int32), intent(out) :: work(:)
!! integer(int_size), intent(out) :: index(:)
!! integer(int_size), intent(out) :: iwork(:)
!! integer(int_index), intent(out) :: index(:)
!! integer(int_index), intent(out) :: iwork(:)
!! ! Find the indices to sort a
!! call sort_index(a, index(1:size(a)),&
!! work(1:size(a)/2), iwork(1:size(a)/2))
Expand All @@ -365,8 +365,8 @@ module stdlib_sorting
!! integer, intent(inout) :: a(:,:)
!! integer(int32), intent(in) :: column
!! integer(int32), intent(out) :: work(:)
!! integer(int_size), intent(out) :: index(:)
!! integer(int_size), intent(out) :: iwork(:)
!! integer(int_index), intent(out) :: index(:)
!! integer(int_index), intent(out) :: iwork(:)
!! integer, allocatable :: dummy(:)
!! integer :: i
!! allocate(dummy(size(a, dim=1)))
Expand All @@ -389,8 +389,8 @@ module stdlib_sorting
!! type(a_type), intent(inout) :: a_data(:)
!! integer(int32), intent(inout) :: a(:)
!! integer(int32), intent(out) :: work(:)
!! integer(int_size), intent(out) :: index(:)
!! integer(int_size), intent(out) :: iwork(:)
!! integer(int_index), intent(out) :: index(:)
!! integer(int_index), intent(out) :: iwork(:)
!! ! Extract a component of `a_data`
!! a(1:size(a_data)) = a_data(:) % a
!! ! Find the indices to sort the component
Expand Down Expand Up @@ -525,11 +525,11 @@ module stdlib_sorting
!! 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:)
integer(int_size), intent(out) :: index(0:)
${t2}$, intent(out), optional :: work(0:)
integer(int_size), intent(out), optional :: iwork(0:)
logical, intent(in), optional :: reverse
${t1}$, intent(inout) :: array(0:)
integer(int_index), intent(out) :: index(0:)
${t2}$, intent(out), optional :: work(0:)
integer(int_index), intent(out), optional :: iwork(0:)
logical, intent(in), optional :: reverse
end subroutine ${name1}$_sort_index

#:endfor
Expand Down
46 changes: 23 additions & 23 deletions src/stdlib_sorting_ord_sort.fypp
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,12 @@ contains
${t3}$, intent(out), optional :: work(0:)

${t2}$, allocatable :: buf(:)
integer(int_size) :: array_size
integer(int_index) :: array_size
integer :: stat

array_size = size( array, kind=int_size )
array_size = size( array, kind=int_index )
if ( present(work) ) then
if ( size( work, kind=int_size) < array_size/2 ) then
if ( size( work, kind=int_index) < array_size/2 ) then
error stop "${name1}$_${sname}$_ord_sort: work array is too small."
endif
! Use the work array as scratch memory
Expand All @@ -141,17 +141,17 @@ contains
!! Returns the minimum length of a run from 32-63 so that N/MIN_RUN is
!! less than or equal to a power of two. See
!! https://svn.python.org/projects/python/trunk/Objects/listsort.txt
integer(int_size) :: min_run
integer(int_size), intent(in) :: n
integer(int_index) :: min_run
integer(int_index), intent(in) :: n

integer(int_size) :: num, r
integer(int_index) :: num, r

num = n
r = 0_int_size
r = 0_int_index

do while( num >= 64 )
r = ior( r, iand(num, 1_int_size) )
num = ishft(num, -1_int_size)
r = ior( r, iand(num, 1_int_index) )
num = ishft(num, -1_int_index)
end do
min_run = num + r

Expand All @@ -162,10 +162,10 @@ contains
! Sorts `ARRAY` using an insertion sort.
${t1}$, intent(inout) :: array(0:)

integer(int_size) :: i, j
integer(int_index) :: i, j
${t3}$ :: key

do j=1, size(array, kind=int_size)-1
do j=1, size(array, kind=int_index)-1
key = array(j)
i = j - 1
do while( i >= 0 )
Expand All @@ -185,13 +185,13 @@ contains
!
! 1. len(-3) > len(-2) + len(-1)
! 2. len(-2) > len(-1)
integer(int_size) :: r
integer(int_index) :: r
type(run_type), intent(in), target :: runs(0:)

integer(int_size) :: n
integer(int_index) :: n
logical :: test

n = size(runs, kind=int_size)
n = size(runs, kind=int_index)
test = .false.
if (n >= 2) then
if ( runs( n-1 ) % base == 0 .or. &
Expand Down Expand Up @@ -240,10 +240,10 @@ contains
${t1}$, intent(inout) :: array(0:)

${t3}$ :: tmp
integer(int_size) :: i
integer(int_index) :: i

tmp = array(0)
find_hole: do i=1, size(array, kind=int_size)-1
find_hole: do i=1, size(array, kind=int_index)-1
if ( array(i) ${signt}$= tmp ) exit find_hole
array(i-1) = array(i)
end do find_hole
Expand Down Expand Up @@ -275,11 +275,11 @@ contains
${t1}$, intent(inout) :: array(0:)
${t3}$, intent(inout) :: buf(0:)

integer(int_size) :: array_size, finish, min_run, r, r_count, &
integer(int_index) :: array_size, finish, min_run, r, r_count, &
start
type(run_type) :: runs(0:max_merge_stack-1), left, right

array_size = size(array, kind=int_size)
array_size = size(array, kind=int_index)

! Very short runs are extended using insertion sort to span at least
! min_run elements. Slices of up to this length are sorted using insertion
Expand Down Expand Up @@ -361,12 +361,12 @@ contains
! `ARRAY(0:)`. `MID` must be > 0, and < `SIZE(ARRAY)-1`. Buffer `BUF`
! must be long enough to hold the shorter of the two runs.
${t1}$, intent(inout) :: array(0:)
integer(int_size), intent(in) :: mid
integer(int_index), intent(in) :: mid
${t3}$, intent(inout) :: buf(0:)

integer(int_size) :: array_len, i, j, k
integer(int_index) :: array_len, i, j, k

array_len = size(array, kind=int_size)
array_len = size(array, kind=int_index)

! Merge first copies the shorter run into `buf`. Then, depending on which
! run was shorter, it traces the copied run and the longer run forwards
Expand Down Expand Up @@ -417,11 +417,11 @@ contains
! Reverse a segment of an array in place
${t1}$, intent(inout) :: array(0:)

integer(int_size) :: lo, hi
integer(int_index) :: lo, hi
${t3}$ :: temp

lo = 0
hi = size( array, kind=int_size ) - 1
hi = size( array, kind=int_index ) - 1
do while( lo < hi )
temp = array(lo)
array(lo) = array(hi)
Expand Down
Loading
Loading