Skip to content

Commit e836175

Browse files
authored
Merge pull request #1 from jvdp1/sorting_fypp_proposition_2
Proposition to generate code for integer, real and string from one single peace of code
2 parents abf10c6 + 4b16aca commit e836175

5 files changed

+118
-2008
lines changed

src/common.fypp

+12-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#! Real kinds to be considered during templating
44
#:set REAL_KINDS = ["sp", "dp", "qp"]
55

6-
#! Real types to be considere during templating
6+
#! Real types to be considered during templating
77
#:set REAL_TYPES = ["real({})".format(k) for k in REAL_KINDS]
88

99
#! Collected (kind, type) tuples for real types
@@ -12,7 +12,7 @@
1212
#! Complex kinds to be considered during templating
1313
#:set CMPLX_KINDS = ["sp", "dp", "qp"]
1414

15-
#! Complex types to be considere during templating
15+
#! Complex types to be considered during templating
1616
#:set CMPLX_TYPES = ["complex({})".format(k) for k in CMPLX_KINDS]
1717

1818
#! Collected (kind, type) tuples for complex types
@@ -21,12 +21,21 @@
2121
#! Integer kinds to be considered during templating
2222
#:set INT_KINDS = ["int8", "int16", "int32", "int64"]
2323

24-
#! Integer types to be considere during templating
24+
#! Integer types to be considered during templating
2525
#:set INT_TYPES = ["integer({})".format(k) for k in INT_KINDS]
2626

2727
#! Collected (kind, type) tuples for integer types
2828
#:set INT_KINDS_TYPES = list(zip(INT_KINDS, INT_TYPES))
2929

30+
#! Derived type string_type
31+
#:set STRING_KINDS = ["string_type"]
32+
33+
#! String types to be considered during templating
34+
#:set STRING_TYPES = ["type({})".format(k) for k in STRING_KINDS]
35+
36+
#! Collected (kind, type) tuples for string derived types
37+
#:set STRING_KINDS_TYPES = list(zip(STRING_KINDS, STRING_TYPES))
38+
3039

3140
#! Whether Fortran 90 compatible code should be generated
3241
#:set VERSION90 = defined('VERSION90')

src/stdlib_sorting.fypp

+15-91
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#:include "common.fypp"
2+
#:set IRS_KINDS_TYPES = INT_KINDS_TYPES + REAL_KINDS_TYPES + STRING_KINDS_TYPES
23

34
!! Licensing:
45
!!
@@ -326,11 +327,11 @@ module stdlib_sorting
326327

327328
public char_sort_index
328329

329-
public string_ord_sort
330+
public string_type_ord_sort
330331

331-
public string_sort
332+
public string_type_sort
332333

333-
public string_sort_index
334+
public string_type_sort_index
334335

335336
interface ord_sort
336337
!! Version: experimental
@@ -346,29 +347,18 @@ module stdlib_sorting
346347
!! sorted data, having O(N) performance on uniformly non-increasing or
347348
!! non-decreasing data.
348349

349-
#:for k1 in INT_KINDS
350+
#:for k1, t1 in IRS_KINDS_TYPES
350351
module subroutine ${k1}$_ord_sort( array, work )
351352
!! Version: experimental
352353
!!
353-
!! `${k1}$_ord_sort( array )` sorts the input `ARRAY` of type `INTEGER(${k1}$)`
354+
!! `${k1}$_ord_sort( array )` sorts the input `ARRAY` of type `${t1}$`
354355
!! using a hybrid sort based on the `'Rust" sort` algorithm found in `slice.rs`
355-
integer(${k1}$), intent(inout) :: array(0:)
356-
integer(${k1}$), intent(inout), optional :: work(0:)
356+
${t1}$, intent(inout) :: array(0:)
357+
${t1}$, intent(inout), optional :: work(0:)
357358
end subroutine ${k1}$_ord_sort
358359

359360
#:endfor
360361

361-
#:for k1 in REAL_KINDS
362-
module subroutine ${k1}$_ord_sort( array, work )
363-
!! Version: experimental
364-
!!
365-
!! `${k1}$_ord_sort( array )` sorts the input `ARRAY` of type `REAL(${k1}$)`
366-
!! using a hybrid sort based on the `'Rust" sort` algorithm found in `slice.rs`
367-
real(${k1}$), intent(inout) :: array(0:)
368-
real(${k1}$), intent(inout), optional :: work(0:)
369-
end subroutine ${k1}$_ord_sort
370-
371-
#:endfor
372362
module subroutine char_ord_sort( array, work )
373363
!! Version: experimental
374364
!!
@@ -378,15 +368,6 @@ module stdlib_sorting
378368
character(len=len(array)), intent(inout), optional :: work(0:)
379369
end subroutine char_ord_sort
380370

381-
module subroutine string_ord_sort( array, work )
382-
!! Version: experimental
383-
!!
384-
!! `char_ord_sort( array )` sorts the input `ARRAY` of type `CHARACTER(*)`
385-
!! using a hybrid sort based on the `'Rust" sort` algorithm found in `slice.rs`
386-
type(string_type), intent(inout) :: array(0:)
387-
type(string_type), intent(inout), optional :: work(0:)
388-
end subroutine string_ord_sort
389-
390371
end interface ord_sort
391372

392373
interface sort
@@ -395,30 +376,16 @@ module stdlib_sorting
395376
!! The generic subroutine interface implementing the `SORT` algorithm, based
396377
!! on the `introsort` of David Musser.
397378

398-
#:for k1 in INT_KINDS
399-
pure module subroutine ${k1}$_sort( array )
400-
!! Version: experimental
401-
!!
402-
!! `${k1}$_sort( array )` sorts the input `ARRAY` of type `INTEGER(${k1}$)`
403-
!! using a hybrid sort based on the `introsort` of David Musser.
404-
!! The algorithm is of order O(N Ln(N)) for all inputs.
405-
!! Because it relies on `quicksort`, the coefficient of the O(N Ln(N))
406-
!! behavior is small for random data compared to other sorting algorithms.
407-
integer(${k1}$), intent(inout) :: array(0:)
408-
end subroutine ${k1}$_sort
409-
410-
#:endfor
411-
412-
#:for k1 in REAL_KINDS
379+
#:for k1, t1 in IRS_KINDS_TYPES
413380
pure module subroutine ${k1}$_sort( array )
414381
!! Version: experimental
415382
!!
416-
!! `${k1}$_sort( array )` sorts the input `ARRAY` of type `REAL(${k1}$)`
383+
!! `${k1}$_sort( array )` sorts the input `ARRAY` of type `${t1}$`
417384
!! using a hybrid sort based on the `introsort` of David Musser.
418385
!! The algorithm is of order O(N Ln(N)) for all inputs.
419386
!! Because it relies on `quicksort`, the coefficient of the O(N Ln(N))
420387
!! behavior is small for random data compared to other sorting algorithms.
421-
real(${k1}$), intent(inout) :: array(0:)
388+
${t1}$, intent(inout) :: array(0:)
422389
end subroutine ${k1}$_sort
423390

424391
#:endfor
@@ -434,17 +401,6 @@ module stdlib_sorting
434401
character(len=*), intent(inout) :: array(0:)
435402
end subroutine char_sort
436403

437-
pure module subroutine string_sort( array )
438-
!! Version: experimental
439-
!!
440-
!! `string_sort( array )` sorts the input `ARRAY` of type `STRING_TYPE`
441-
!! using a hybrid sort based on the `introsort` of David Musser.
442-
!! The algorithm is of order O(N Ln(N)) for all inputs.
443-
!! Because it relies on `quicksort`, the coefficient of the O(N Ln(N))
444-
!! behavior is small for random data compared to other sorting algorithms.
445-
type(string_type), intent(inout) :: array(0:)
446-
end subroutine string_sort
447-
448404
end interface sort
449405

450406
interface sort_index
@@ -458,41 +414,24 @@ module stdlib_sorting
458414
!! non-decreasing sort, but if the optional argument `REVERSE` is present
459415
!! with a value of `.TRUE.` the indices correspond to a non-increasing sort.
460416

461-
#:for k1 in INT_KINDS
417+
#:for k1, t1 in IRS_KINDS_TYPES
462418
module subroutine ${k1}$_sort_index( array, index, work, iwork, &
463419
reverse )
464420
!! Version: experimental
465421
!!
466-
!! `${k1}$_sort_index( array )` sorts an input `ARRAY` of type `INTEGER(${k1}$)`
422+
!! `${k1}$_sort_index( array )` sorts an input `ARRAY` of type `${t1}$`
467423
!! using a hybrid sort based on the `'Rust" sort` algorithm found in `slice.rs`
468424
!! and returns the sorted `ARRAY` and an array `INDEX of indices in the
469425
!! order that would sort the input `ARRAY` in the desired direction.
470-
integer(${k1}$), intent(inout) :: array(0:)
426+
${t1}$, intent(inout) :: array(0:)
471427
integer(int_size), intent(inout) :: index(0:)
472-
integer(${k1}$), intent(inout), optional :: work(0:)
428+
${t1}$, intent(inout), optional :: work(0:)
473429
integer(int_size), intent(inout), optional :: iwork(0:)
474430
logical, intent(in), optional :: reverse
475431
end subroutine ${k1}$_sort_index
476432

477433
#:endfor
478434

479-
#:for k1 in REAL_KINDS
480-
module subroutine ${k1}$_sort_index( array, index, work, iwork, &
481-
reverse )
482-
!! Version: experimental
483-
!!
484-
!! `${k1}$_sort_index( array )` sorts an input `ARRAY` of type `REAL(${k1}$)`
485-
!! using a hybrid sort based on the `'Rust" sort` algorithm found in `slice.rs`
486-
!! and returns the sorted `ARRAY` and an array `INDEX of indices in the
487-
!! order that would sort the input `ARRAY` in the desired direction.
488-
real(${k1}$), intent(inout) :: array(0:)
489-
integer(int_size), intent(inout) :: index(0:)
490-
real(${k1}$), intent(inout), optional :: work(0:)
491-
integer(int_size), intent(inout), optional :: iwork(0:)
492-
logical, intent(in), optional :: reverse
493-
end subroutine ${k1}$_sort_index
494-
495-
#:endfor
496435
module subroutine char_sort_index( array, index, work, iwork, &
497436
reverse )
498437
!! Version: experimental
@@ -508,21 +447,6 @@ module stdlib_sorting
508447
logical, intent(in), optional :: reverse
509448
end subroutine char_sort_index
510449

511-
module subroutine string_sort_index( array, index, work, iwork, &
512-
reverse )
513-
!! Version: experimental
514-
!!
515-
!! `string_sort_index( array )` sorts an input `ARRAY` of type `STRING_TYPE`
516-
!! using a hybrid sort based on the `'Rust" sort` algorithm found in `slice.rs`
517-
!! and returns the sorted `ARRAY` and an array `INDEX of indices in the
518-
!! order that would sort the input `ARRAY` in the desired direction.
519-
type(string_type), intent(inout) :: array(0:)
520-
integer(int_size), intent(inout) :: index(0:)
521-
type(string_type), intent(inout), optional :: work(0:)
522-
integer(int_size), intent(inout), optional :: iwork(0:)
523-
logical, intent(in), optional :: reverse
524-
end subroutine string_sort_index
525-
526450
end interface sort_index
527451

528452

0 commit comments

Comments
 (0)