Skip to content

Commit c7c356e

Browse files
authored
Add std::complex support (#23)
* Tweak build script * Simplify bool conversion * Add complex number support to std::vector
1 parent 186a982 commit c7c356e

File tree

10 files changed

+1204
-43
lines changed

10 files changed

+1204
-43
lines changed

doc/modules/vector.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ Vector
99
******
1010

1111
Vectors are resizeable arrays of elements. The ``flc_vector`` module
12-
instantiates vectors of ``integer(4)``, ``integer(8)``, ``real(8)``, and
13-
``type(String)``.
12+
instantiates vectors of ``integer(4)``, ``integer(8)``, ``real(8)``,
13+
``complex(8)``, and ``type(String)``.
1414

1515
Common functionality
1616
====================
@@ -94,11 +94,13 @@ Numeric vectors
9494
===============
9595

9696
As with the algorithms and other methods, the ``flc_vector`` module includes
97-
three numeric instantiations. They each have distinct derived types:
97+
three scalar numeric instantiations, but it also includes an instantiation for
98+
complex numbers. Each instantiation has a distinct derived type:
9899

99100
- ``VectorInt4``: each element is ``integer(4)``
100101
- ``VectorInt8``: each element is ``integer(8)``
101102
- ``VectorReal8``: each element is ``real(8)``
103+
- ``VectorComplex8``: each element is ``complex(8)``
102104

103105
Construct from an array
104106
-----------------------

include/flc_vector.i

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
/*!
22
* \file flc_vector.i
33
*
4-
* Copyright (c) 2019 Oak Ridge National Laboratory, UT-Battelle, LLC.
4+
* Copyright (c) 2019-2020 Oak Ridge National Laboratory, UT-Battelle, LLC.
55
* Distributed under an MIT open source license: see LICENSE for details.
66
*/
77

88
%module "flc_vector"
99
%include "import_flc.i"
1010
%flc_add_header
1111

12+
%include <complex.i>
1213
%include <std_vector.i>
1314

1415
/* -------------------------------------------------------------------------
1516
* Macro definitions
1617
* ------------------------------------------------------------------------- */
1718

18-
%define %flc_std_vector_extend_pod(CTYPE)
19+
%define %flc_std_vector_extend_pod(CTYPE, IMTYPE)
1920
%extend {
2021
%apply (const SWIGTYPE *DATA, ::size_t SIZE)
2122
{ (const CTYPE* DATA, size_type SIZE) };
@@ -31,7 +32,7 @@
3132
}
3233

3334
// Get a mutable view to ourself
34-
%fortran_array_pointer(CTYPE, vector<CTYPE>& view);
35+
%fortran_array_pointer(IMTYPE, vector<CTYPE>& view);
3536

3637
%typemap(out, noblock=1) vector<CTYPE>& view {
3738
$result.data = ($1->empty() ? NULL : &(*$1->begin()));
@@ -63,7 +64,7 @@ namespace std {
6364

6465
%swig_std_vector(T, const T&)
6566
%swig_std_vector_extend_ref(T)
66-
%flc_std_vector_extend_pod(T)
67+
%flc_std_vector_extend_pod(T, T)
6768
};
6869
}
6970

@@ -72,6 +73,31 @@ namespace std {
7273

7374
%enddef
7475

76+
77+
/* ------------------------------------------------------------------------- */
78+
/*! \def %flc_template_std_vector_complex
79+
*
80+
* Inject member functions and typemaps for std::complex instantiations.
81+
*
82+
* This definition is considered part of the \em public API so that downstream
83+
* apps that generate FLC-based bindings can instantiate their own POD vectors.
84+
*/
85+
%define %flc_template_std_vector_complex(NAME, T)
86+
87+
namespace std {
88+
template<> class vector<std::complex<T> > {
89+
90+
%swig_std_vector(std::complex<T>, const std::complex<T>&)
91+
%swig_std_vector_extend_ref(std::complex<T>)
92+
%flc_std_vector_extend_pod(std::complex<T>, SwigComplex_ ## T)
93+
};
94+
}
95+
96+
// Instantiate the template
97+
%template(NAME) std::vector<std::complex<T> >;
98+
99+
%enddef
100+
75101
/* -------------------------------------------------------------------------
76102
* Numeric vectors
77103
* ------------------------------------------------------------------------- */
@@ -80,6 +106,8 @@ namespace std {
80106
%flc_template_std_vector_pod(VectorInt8, int64_t)
81107
%flc_template_std_vector_pod(VectorReal8, double)
82108

109+
%flc_template_std_vector_complex(VectorComplex8, double)
110+
83111
/* -------------------------------------------------------------------------
84112
* String vectors
85113
* ------------------------------------------------------------------------- */
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/bin/sh -ex
2-
SOURCE=$(cd $(dirname "${BASH_SOURCE[0]}")/.. && pwd)
2+
SOURCE=$(cd $(dirname "${BASH_SOURCE[0]}")/../.. && pwd)
33
BUILD=${SOURCE}/build
44
PREFIX=${SOURCE}/install
55

@@ -12,11 +12,13 @@ ${CMAKE} --version
1212
${CMAKE} \
1313
-G Ninja \
1414
-D FLIBCPP_DEV=ON \
15-
-D SWIG_EXECUTABLE="/rnsdhpc/code/build/swig-debug/swig" \
16-
-D SWIG_DIR="/rnsdhpc/code/src/swig/Lib" \
15+
-D SWIG_EXECUTABLE="/rnsdhpc/code/_build/swig-debug/swig" \
16+
-D SWIG_DIR="/rnsdhpc/code/swig/Lib" \
1717
-D BUILD_SHARED_LIBS=ON \
1818
-D CMAKE_Fortran_FLAGS="-Wall -Wextra -Wimplicit-procedure -Wimplicit-interface -Wno-compare-reals" \
19-
-D CMAKE_CXX_FLAGS="-Wall -Wextra" \
19+
-D CMAKE_CXX_FLAGS="-Wall -Wextra -pedantic" \
2020
-D CMAKE_INSTALL_PREFIX="${PREFIX}" \
2121
${SOURCE}
22-
ninja
22+
ninja -v
23+
ctest --output-on-failure
24+
ninja install

src/flc_algorithm.f90

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -780,12 +780,7 @@ subroutine SWIGTM_fout_bool(imout, fout)
780780
use, intrinsic :: ISO_C_BINDING
781781
integer(kind=C_INT), intent(in) :: imout
782782
logical, intent(out) :: fout
783-
! TODO: fout = (imout /= 0) ???
784-
if (imout /= 0) then
785-
fout = .true.
786-
else
787-
fout = .false.
788-
end if
783+
fout = (imout /= 0)
789784
end subroutine
790785

791786
function swigf_is_sorted__SWIG_1(data) &

src/flc_map.f90

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -416,12 +416,7 @@ subroutine SWIGTM_fout_bool(imout, fout)
416416
use, intrinsic :: ISO_C_BINDING
417417
integer(kind=C_INT), intent(in) :: imout
418418
logical, intent(out) :: fout
419-
! TODO: fout = (imout /= 0) ???
420-
if (imout /= 0) then
421-
fout = .true.
422-
else
423-
fout = .false.
424-
end if
419+
fout = (imout /= 0)
425420
end subroutine
426421

427422
function swigf_MapIntInt_empty(self) &

src/flc_set.f90

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -383,12 +383,7 @@ subroutine SWIGTM_fout_bool(imout, fout)
383383
use, intrinsic :: ISO_C_BINDING
384384
integer(kind=C_INT), intent(in) :: imout
385385
logical, intent(out) :: fout
386-
! TODO: fout = (imout /= 0) ???
387-
if (imout /= 0) then
388-
fout = .true.
389-
else
390-
fout = .false.
391-
end if
386+
fout = (imout /= 0)
392387
end subroutine
393388

394389
function swigf_SetInt_empty(self) &

src/flc_string.f90

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,7 @@ subroutine SWIGTM_fout_bool(imout, fout)
443443
use, intrinsic :: ISO_C_BINDING
444444
integer(kind=C_INT), intent(in) :: imout
445445
logical, intent(out) :: fout
446-
! TODO: fout = (imout /= 0) ???
447-
if (imout /= 0) then
448-
fout = .true.
449-
else
450-
fout = .false.
451-
end if
446+
fout = (imout /= 0)
452447
end subroutine
453448

454449
function swigf_string_empty(self) &

0 commit comments

Comments
 (0)