Skip to content

Commit 3cde0ba

Browse files
committed
Define 'index_int' type parameter for Fortran
1 parent a6b9b28 commit 3cde0ba

File tree

8 files changed

+234
-354
lines changed

8 files changed

+234
-354
lines changed

doc/modules/algorithm.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,18 +53,18 @@ A routine that provides the indices that correspond to a sorted array, like
5353
Numpy's argsort_ ,
5454
takes an array to analyze and an empty array of integers to fill::
5555

56-
use flc_algorithm, only : argsort
56+
use flc_algorithm, only : argsort, INDEX_INT
5757
implicit none
5858
integer, dimension(5) :: iarr = [ 2, 5, -2, 3, -10000]
59-
integer(C_INT), dimension(5) :: idx
59+
integer(INDEX_INT), dimension(5) :: idx
6060

6161
call argsort(iarr, idx)
6262
! This line prints a sorted array:
6363
write(*,*) iarr(idx)
6464

65-
Note that the index array is always a ``C_INT``. On some compilers and
66-
platforms, this may be the same as native Fortran integer, but it's not
67-
guaranteed.
65+
Note that the index array is always a ``INDEX_INT``, which is an alias to
66+
``C_INT``. On some compilers and platforms, this may be the same as native
67+
Fortran integer, but it's not guaranteed.
6868

6969
The ``data`` and ``idx`` arguments to ``argsort`` *must* be the same size. If
7070
the index array is larger than the data, invalid entries will be filled with
@@ -86,9 +86,9 @@ The input array **must** be sorted.
8686

8787
Example::
8888

89-
use flc_algorithm, only : binary_search
89+
use flc_algorithm, only : binary_search, INDEX_INT
9090
implicit none
91-
integer :: idx
91+
integer(INDEX_INT) :: idx
9292
integer, dimension(6) :: iarr = [ -5, 1, 1, 2, 4, 9]
9393

9494
idx = binary_search(iarr, -100) ! returns 0

src/flc_algorithm.i

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,25 @@ static RETURN_TYPE FUNCNAME ## _cmp(ARGS, bool (*cmp)(T, T)) {
3535

3636
%enddef
3737

38+
/******************************
39+
* Types
40+
******************************/
41+
42+
%inline %{
43+
typedef int index_int;
44+
%}
45+
%insert("fdecl") %{integer, parameter, public :: INDEX_INT = C_INT
46+
%}
47+
48+
%apply int { index_int };
49+
%typemap(ftype, in={integer(INDEX_INT), intent(in)}) index_int
50+
%{integer(INDEX_INT)%}
51+
3852
/******************************
3953
* Sorting
4054
******************************/
4155

42-
%apply (SWIGTYPE *DATA, size_t SIZE) { (int *IDX, size_t IDXSIZE) };
56+
%apply (SWIGTYPE *DATA, size_t SIZE) { (index_int *IDX, size_t IDXSIZE) };
4357

4458
%{
4559
template<class T, class Compare>
@@ -54,7 +68,7 @@ static bool is_sorted_impl(const T *data, size_t size, Compare cmp) {
5468

5569
template<class T, class Compare>
5670
static void argsort_impl(const T *data, size_t size,
57-
int *index, size_t index_size,
71+
index_int *index, size_t index_size,
5872
Compare cmp) {
5973
// Fill invalid indices with zero
6074
if (size < index_size) {
@@ -64,7 +78,7 @@ static void argsort_impl(const T *data, size_t size,
6478
// Fill the indices with 1 through size
6579
std::iota(index, index + size, 1);
6680
// Define a comparator that accesses the original data
67-
auto int_sort_cmp = [cmp, data](int left, int right)
81+
auto int_sort_cmp = [cmp, data](index_int left, index_int right)
6882
{ return cmp(data[left - 1], data[right - 1]); };
6983
// Let the standard library do all the hard work!
7084
std::sort(index, index + size, int_sort_cmp);
@@ -77,7 +91,7 @@ static void argsort_impl(const T *data, size_t size,
7791
%flc_cmp_algorithm(bool, is_sorted, %arg(const T *DATA, size_t DATASIZE),
7892
%arg(DATA, DATASIZE))
7993
%flc_cmp_algorithm(void, argsort, %arg(const T *DATA, size_t DATASIZE,
80-
int *IDX, size_t IDXSIZE),
94+
index_int *IDX, size_t IDXSIZE),
8195
%arg(DATA, DATASIZE, IDX, IDXSIZE))
8296

8397
/******************************
@@ -86,17 +100,17 @@ static void argsort_impl(const T *data, size_t size,
86100

87101
%{
88102
template<class T, class Compare>
89-
static int binary_search_impl(const T *data, size_t size, T value, Compare cmp) {
103+
static index_int binary_search_impl(const T *data, size_t size, T value, Compare cmp) {
90104
const T *end = data + size;
91105
auto iter = std::lower_bound(data, end, value, cmp);
92106
if (iter == end || cmp(*iter, value) || cmp(value, *iter))
93-
return 0;
107+
return 0;
94108
// Index of the found item *IN FORTAN INDEXING*
95109
return (iter - data) + 1;
96110
}
97111
%}
98112

99-
%flc_cmp_algorithm(int, binary_search, %arg(const T *DATA, size_t DATASIZE,
113+
%flc_cmp_algorithm(index_int, binary_search, %arg(const T *DATA, size_t DATASIZE,
100114
T value),
101115
%arg(DATA, DATASIZE, value))
102116

src/generated/flcFORTRAN_wrap.cxx

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,6 @@ using std::size_t;
221221

222222
#include <stdint.h>
223223

224-
#ifdef __cplusplus
225224
extern "C" {
226-
#endif
227-
#ifdef __cplusplus
228-
}
229-
#endif
225+
} // extern
230226

src/generated/flc_algorithm.f90

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,19 @@ module flc_algorithm
1616
private
1717

1818
! DECLARATION CONSTRUCTS
19+
integer, parameter, public :: INDEX_INT = C_INT
20+
1921
type, bind(C) :: SwigArrayWrapper
2022
type(C_PTR), public :: data = C_NULL_PTR
2123
integer(C_SIZE_T), public :: size = 0
2224
end type
23-
enum, bind(c)
24-
enumerator :: SWIG_NULL
25-
enumerator :: SWIG_OWN
26-
enumerator :: SWIG_MOVE
27-
enumerator :: SWIG_REF
28-
enumerator :: SWIG_CREF
29-
end enum
30-
integer, parameter :: SwigMemState = kind(SWIG_NULL)
25+
26+
integer, parameter :: swig_cmem_own_bit = 0
27+
integer, parameter :: swig_cmem_rvalue_bit = 1
28+
integer, parameter :: swig_cmem_const_bit = 2
3129
type, bind(C) :: SwigClassWrapper
3230
type(C_PTR), public :: cptr = C_NULL_PTR
33-
integer(C_INT), public :: mem = SWIG_NULL
31+
integer(C_INT), public :: cmemflags = 0
3432
end type
3533
interface binary_search
3634
module procedure swigf_binary_search__SWIG_1, swigf_binary_search__SWIG_2, swigf_binary_search__SWIG_3, &
@@ -746,7 +744,7 @@ subroutine swigf_argsort__SWIG_6(data, idx, cmp)
746744
function swigf_binary_search__SWIG_1(data, value) &
747745
result(swig_result)
748746
use, intrinsic :: ISO_C_BINDING
749-
integer(C_INT) :: swig_result
747+
integer(INDEX_INT) :: swig_result
750748
integer(C_INT32_T), dimension(:), intent(in), target :: data
751749
integer(C_INT32_T), pointer :: farg1_view
752750
integer(C_INT32_T), intent(in) :: value
@@ -770,7 +768,7 @@ function swigf_binary_search__SWIG_1(data, value) &
770768
function swigf_binary_search__SWIG_2(data, value) &
771769
result(swig_result)
772770
use, intrinsic :: ISO_C_BINDING
773-
integer(C_INT) :: swig_result
771+
integer(INDEX_INT) :: swig_result
774772
integer(C_INT64_T), dimension(:), intent(in), target :: data
775773
integer(C_INT64_T), pointer :: farg1_view
776774
integer(C_INT64_T), intent(in) :: value
@@ -794,7 +792,7 @@ function swigf_binary_search__SWIG_2(data, value) &
794792
function swigf_binary_search__SWIG_3(data, value) &
795793
result(swig_result)
796794
use, intrinsic :: ISO_C_BINDING
797-
integer(C_INT) :: swig_result
795+
integer(INDEX_INT) :: swig_result
798796
real(C_DOUBLE), dimension(:), intent(in), target :: data
799797
real(C_DOUBLE), pointer :: farg1_view
800798
real(C_DOUBLE), intent(in) :: value
@@ -818,7 +816,7 @@ function swigf_binary_search__SWIG_3(data, value) &
818816
function swigf_binary_search__SWIG_4(data, value, cmp) &
819817
result(swig_result)
820818
use, intrinsic :: ISO_C_BINDING
821-
integer(C_INT) :: swig_result
819+
integer(INDEX_INT) :: swig_result
822820
integer(C_INT32_T), dimension(:), intent(in), target :: data
823821
integer(C_INT32_T), pointer :: farg1_view
824822
integer(C_INT32_T), intent(in) :: value
@@ -845,7 +843,7 @@ function swigf_binary_search__SWIG_4(data, value, cmp) &
845843
function swigf_binary_search__SWIG_5(data, value, cmp) &
846844
result(swig_result)
847845
use, intrinsic :: ISO_C_BINDING
848-
integer(C_INT) :: swig_result
846+
integer(INDEX_INT) :: swig_result
849847
integer(C_INT64_T), dimension(:), intent(in), target :: data
850848
integer(C_INT64_T), pointer :: farg1_view
851849
integer(C_INT64_T), intent(in) :: value
@@ -872,7 +870,7 @@ function swigf_binary_search__SWIG_5(data, value, cmp) &
872870
function swigf_binary_search__SWIG_6(data, value, cmp) &
873871
result(swig_result)
874872
use, intrinsic :: ISO_C_BINDING
875-
integer(C_INT) :: swig_result
873+
integer(INDEX_INT) :: swig_result
876874
real(C_DOUBLE), dimension(:), intent(in), target :: data
877875
real(C_DOUBLE), pointer :: farg1_view
878876
real(C_DOUBLE), intent(in) :: value

0 commit comments

Comments
 (0)