Skip to content

Commit 6758e11

Browse files
committed
Add set algorithms as free functions
1 parent bd2aafa commit 6758e11

File tree

4 files changed

+573
-3
lines changed

4 files changed

+573
-3
lines changed

doc/modules/set.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Sets are sorted containers of unique elements. The ``flc_set`` module
1212
defines sets of ``integer`` and of ``type(String)``.
1313

1414
Common functionality
15-
====================
15+
===================
1616

1717
All set types support the following basic operations.
1818

@@ -49,9 +49,10 @@ Here's an example of creating, modifying, and destroying a set::
4949
call s%release() ! Free memory
5050

5151
Set operations
52-
--------------
52+
==============
53+
54+
Operations that take two sets are implemented as free functions.
5355

54-
Union, intersection, etc. are not yet implemented.
5556

5657
Numeric sets
5758
===============

include/flc_set.i

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,34 @@
1111

1212
%include <std_set.i>
1313

14+
// Support for set operations
15+
%{
16+
#include <algorithm>
17+
#include <iterator>
18+
%}
19+
1420
/* -------------------------------------------------------------------------
1521
* Macro definitions
1622
* ------------------------------------------------------------------------- */
1723

24+
%define %flc_set_algorithm(FUNCNAME)
25+
%inline {
26+
template<class Set_t>
27+
static Set_t FUNCNAME(const Set_t& left, const Set_t& right)
28+
{
29+
Set_t result;
30+
std::FUNCNAME(left.begin(), left.end(),
31+
right.begin(), right.end(),
32+
std::inserter(result, result.end()));
33+
return result;
34+
}
35+
}
36+
37+
%template(FUNCNAME) FUNCNAME<std::set<int> >;
38+
%template(FUNCNAME) FUNCNAME<std::set<std::string> >;
39+
40+
%enddef
41+
1842
%define %flc_extend_set_pod(CTYPE)
1943
%apply (const SWIGTYPE *DATA, ::size_t SIZE)
2044
{ (const CTYPE* DATA, size_type SIZE) };
@@ -84,3 +108,24 @@ namespace std {
84108
%import "flc_string.i"
85109
%template(SetString) std::set<std::string>;
86110

111+
/* -------------------------------------------------------------------------
112+
* Algorithms
113+
* ------------------------------------------------------------------------- */
114+
115+
%flc_set_algorithm(set_difference)
116+
%flc_set_algorithm(set_intersection)
117+
%flc_set_algorithm(set_symmetric_difference)
118+
%flc_set_algorithm(set_union)
119+
120+
%inline %{
121+
template<class Set_t>
122+
static bool includes(const Set_t& left, const Set_t& right)
123+
{
124+
return std::includes(left.begin(), left.end(),
125+
right.begin(), right.end());
126+
}
127+
%}
128+
129+
%template(includes) includes<std::set<int> >;
130+
%template(includes) includes<std::set<std::string> >;
131+

src/flc_set.f90

Lines changed: 280 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,26 @@ module flc_set
6565
interface SetString
6666
module procedure swigf_create_SetString
6767
end interface
68+
interface includes
69+
module procedure swigf_includes__SWIG_1, swigf_includes__SWIG_2
70+
end interface
71+
public :: includes
72+
interface set_difference
73+
module procedure swigf_set_difference__SWIG_1, swigf_set_difference__SWIG_2
74+
end interface
75+
public :: set_difference
76+
interface set_symmetric_difference
77+
module procedure swigf_set_symmetric_difference__SWIG_1, swigf_set_symmetric_difference__SWIG_2
78+
end interface
79+
public :: set_symmetric_difference
80+
interface set_union
81+
module procedure swigf_set_union__SWIG_1, swigf_set_union__SWIG_2
82+
end interface
83+
public :: set_union
84+
interface set_intersection
85+
module procedure swigf_set_intersection__SWIG_1, swigf_set_intersection__SWIG_2
86+
end interface
87+
public :: set_intersection
6888

6989
! WRAPPER DECLARATIONS
7090
interface
@@ -250,6 +270,106 @@ subroutine swigc_SetString_op_assign__(farg1, farg2) &
250270
type(SwigClassWrapper) :: farg2
251271
end subroutine
252272

273+
function swigc_set_difference__SWIG_1(farg1, farg2) &
274+
bind(C, name="_wrap_set_difference__SWIG_1") &
275+
result(fresult)
276+
use, intrinsic :: ISO_C_BINDING
277+
import :: swigclasswrapper
278+
type(SwigClassWrapper) :: farg1
279+
type(SwigClassWrapper) :: farg2
280+
type(SwigClassWrapper) :: fresult
281+
end function
282+
283+
function swigc_set_difference__SWIG_2(farg1, farg2) &
284+
bind(C, name="_wrap_set_difference__SWIG_2") &
285+
result(fresult)
286+
use, intrinsic :: ISO_C_BINDING
287+
import :: swigclasswrapper
288+
type(SwigClassWrapper) :: farg1
289+
type(SwigClassWrapper) :: farg2
290+
type(SwigClassWrapper) :: fresult
291+
end function
292+
293+
function swigc_set_intersection__SWIG_1(farg1, farg2) &
294+
bind(C, name="_wrap_set_intersection__SWIG_1") &
295+
result(fresult)
296+
use, intrinsic :: ISO_C_BINDING
297+
import :: swigclasswrapper
298+
type(SwigClassWrapper) :: farg1
299+
type(SwigClassWrapper) :: farg2
300+
type(SwigClassWrapper) :: fresult
301+
end function
302+
303+
function swigc_set_intersection__SWIG_2(farg1, farg2) &
304+
bind(C, name="_wrap_set_intersection__SWIG_2") &
305+
result(fresult)
306+
use, intrinsic :: ISO_C_BINDING
307+
import :: swigclasswrapper
308+
type(SwigClassWrapper) :: farg1
309+
type(SwigClassWrapper) :: farg2
310+
type(SwigClassWrapper) :: fresult
311+
end function
312+
313+
function swigc_set_symmetric_difference__SWIG_1(farg1, farg2) &
314+
bind(C, name="_wrap_set_symmetric_difference__SWIG_1") &
315+
result(fresult)
316+
use, intrinsic :: ISO_C_BINDING
317+
import :: swigclasswrapper
318+
type(SwigClassWrapper) :: farg1
319+
type(SwigClassWrapper) :: farg2
320+
type(SwigClassWrapper) :: fresult
321+
end function
322+
323+
function swigc_set_symmetric_difference__SWIG_2(farg1, farg2) &
324+
bind(C, name="_wrap_set_symmetric_difference__SWIG_2") &
325+
result(fresult)
326+
use, intrinsic :: ISO_C_BINDING
327+
import :: swigclasswrapper
328+
type(SwigClassWrapper) :: farg1
329+
type(SwigClassWrapper) :: farg2
330+
type(SwigClassWrapper) :: fresult
331+
end function
332+
333+
function swigc_set_union__SWIG_1(farg1, farg2) &
334+
bind(C, name="_wrap_set_union__SWIG_1") &
335+
result(fresult)
336+
use, intrinsic :: ISO_C_BINDING
337+
import :: swigclasswrapper
338+
type(SwigClassWrapper) :: farg1
339+
type(SwigClassWrapper) :: farg2
340+
type(SwigClassWrapper) :: fresult
341+
end function
342+
343+
function swigc_set_union__SWIG_2(farg1, farg2) &
344+
bind(C, name="_wrap_set_union__SWIG_2") &
345+
result(fresult)
346+
use, intrinsic :: ISO_C_BINDING
347+
import :: swigclasswrapper
348+
type(SwigClassWrapper) :: farg1
349+
type(SwigClassWrapper) :: farg2
350+
type(SwigClassWrapper) :: fresult
351+
end function
352+
353+
function swigc_includes__SWIG_1(farg1, farg2) &
354+
bind(C, name="_wrap_includes__SWIG_1") &
355+
result(fresult)
356+
use, intrinsic :: ISO_C_BINDING
357+
import :: swigclasswrapper
358+
type(SwigClassWrapper) :: farg1
359+
type(SwigClassWrapper) :: farg2
360+
integer(C_INT) :: fresult
361+
end function
362+
363+
function swigc_includes__SWIG_2(farg1, farg2) &
364+
bind(C, name="_wrap_includes__SWIG_2") &
365+
result(fresult)
366+
use, intrinsic :: ISO_C_BINDING
367+
import :: swigclasswrapper
368+
type(SwigClassWrapper) :: farg1
369+
type(SwigClassWrapper) :: farg2
370+
integer(C_INT) :: fresult
371+
end function
372+
253373
end interface
254374

255375

@@ -578,5 +698,165 @@ subroutine swigf_SetString_op_assign__(self, other)
578698
self%swigdata = farg1
579699
end subroutine
580700

701+
function swigf_set_difference__SWIG_1(left, right) &
702+
result(swig_result)
703+
use, intrinsic :: ISO_C_BINDING
704+
type(SetInt) :: swig_result
705+
class(SetInt), intent(in) :: left
706+
class(SetInt), intent(in) :: right
707+
type(SwigClassWrapper) :: fresult
708+
type(SwigClassWrapper) :: farg1
709+
type(SwigClassWrapper) :: farg2
710+
711+
farg1 = left%swigdata
712+
farg2 = right%swigdata
713+
fresult = swigc_set_difference__SWIG_1(farg1, farg2)
714+
swig_result%swigdata = fresult
715+
end function
716+
717+
function swigf_set_difference__SWIG_2(left, right) &
718+
result(swig_result)
719+
use, intrinsic :: ISO_C_BINDING
720+
type(SetString) :: swig_result
721+
class(SetString), intent(in) :: left
722+
class(SetString), intent(in) :: right
723+
type(SwigClassWrapper) :: fresult
724+
type(SwigClassWrapper) :: farg1
725+
type(SwigClassWrapper) :: farg2
726+
727+
farg1 = left%swigdata
728+
farg2 = right%swigdata
729+
fresult = swigc_set_difference__SWIG_2(farg1, farg2)
730+
swig_result%swigdata = fresult
731+
end function
732+
733+
function swigf_set_intersection__SWIG_1(left, right) &
734+
result(swig_result)
735+
use, intrinsic :: ISO_C_BINDING
736+
type(SetInt) :: swig_result
737+
class(SetInt), intent(in) :: left
738+
class(SetInt), intent(in) :: right
739+
type(SwigClassWrapper) :: fresult
740+
type(SwigClassWrapper) :: farg1
741+
type(SwigClassWrapper) :: farg2
742+
743+
farg1 = left%swigdata
744+
farg2 = right%swigdata
745+
fresult = swigc_set_intersection__SWIG_1(farg1, farg2)
746+
swig_result%swigdata = fresult
747+
end function
748+
749+
function swigf_set_intersection__SWIG_2(left, right) &
750+
result(swig_result)
751+
use, intrinsic :: ISO_C_BINDING
752+
type(SetString) :: swig_result
753+
class(SetString), intent(in) :: left
754+
class(SetString), intent(in) :: right
755+
type(SwigClassWrapper) :: fresult
756+
type(SwigClassWrapper) :: farg1
757+
type(SwigClassWrapper) :: farg2
758+
759+
farg1 = left%swigdata
760+
farg2 = right%swigdata
761+
fresult = swigc_set_intersection__SWIG_2(farg1, farg2)
762+
swig_result%swigdata = fresult
763+
end function
764+
765+
function swigf_set_symmetric_difference__SWIG_1(left, right) &
766+
result(swig_result)
767+
use, intrinsic :: ISO_C_BINDING
768+
type(SetInt) :: swig_result
769+
class(SetInt), intent(in) :: left
770+
class(SetInt), intent(in) :: right
771+
type(SwigClassWrapper) :: fresult
772+
type(SwigClassWrapper) :: farg1
773+
type(SwigClassWrapper) :: farg2
774+
775+
farg1 = left%swigdata
776+
farg2 = right%swigdata
777+
fresult = swigc_set_symmetric_difference__SWIG_1(farg1, farg2)
778+
swig_result%swigdata = fresult
779+
end function
780+
781+
function swigf_set_symmetric_difference__SWIG_2(left, right) &
782+
result(swig_result)
783+
use, intrinsic :: ISO_C_BINDING
784+
type(SetString) :: swig_result
785+
class(SetString), intent(in) :: left
786+
class(SetString), intent(in) :: right
787+
type(SwigClassWrapper) :: fresult
788+
type(SwigClassWrapper) :: farg1
789+
type(SwigClassWrapper) :: farg2
790+
791+
farg1 = left%swigdata
792+
farg2 = right%swigdata
793+
fresult = swigc_set_symmetric_difference__SWIG_2(farg1, farg2)
794+
swig_result%swigdata = fresult
795+
end function
796+
797+
function swigf_set_union__SWIG_1(left, right) &
798+
result(swig_result)
799+
use, intrinsic :: ISO_C_BINDING
800+
type(SetInt) :: swig_result
801+
class(SetInt), intent(in) :: left
802+
class(SetInt), intent(in) :: right
803+
type(SwigClassWrapper) :: fresult
804+
type(SwigClassWrapper) :: farg1
805+
type(SwigClassWrapper) :: farg2
806+
807+
farg1 = left%swigdata
808+
farg2 = right%swigdata
809+
fresult = swigc_set_union__SWIG_1(farg1, farg2)
810+
swig_result%swigdata = fresult
811+
end function
812+
813+
function swigf_set_union__SWIG_2(left, right) &
814+
result(swig_result)
815+
use, intrinsic :: ISO_C_BINDING
816+
type(SetString) :: swig_result
817+
class(SetString), intent(in) :: left
818+
class(SetString), intent(in) :: right
819+
type(SwigClassWrapper) :: fresult
820+
type(SwigClassWrapper) :: farg1
821+
type(SwigClassWrapper) :: farg2
822+
823+
farg1 = left%swigdata
824+
farg2 = right%swigdata
825+
fresult = swigc_set_union__SWIG_2(farg1, farg2)
826+
swig_result%swigdata = fresult
827+
end function
828+
829+
function swigf_includes__SWIG_1(left, right) &
830+
result(swig_result)
831+
use, intrinsic :: ISO_C_BINDING
832+
logical :: swig_result
833+
class(SetInt), intent(in) :: left
834+
class(SetInt), intent(in) :: right
835+
integer(C_INT) :: fresult
836+
type(SwigClassWrapper) :: farg1
837+
type(SwigClassWrapper) :: farg2
838+
839+
farg1 = left%swigdata
840+
farg2 = right%swigdata
841+
fresult = swigc_includes__SWIG_1(farg1, farg2)
842+
call SWIGTM_fout_bool(fresult, swig_result)
843+
end function
844+
845+
function swigf_includes__SWIG_2(left, right) &
846+
result(swig_result)
847+
use, intrinsic :: ISO_C_BINDING
848+
logical :: swig_result
849+
class(SetString), intent(in) :: left
850+
class(SetString), intent(in) :: right
851+
integer(C_INT) :: fresult
852+
type(SwigClassWrapper) :: farg1
853+
type(SwigClassWrapper) :: farg2
854+
855+
farg1 = left%swigdata
856+
farg2 = right%swigdata
857+
fresult = swigc_includes__SWIG_2(farg1, farg2)
858+
call SWIGTM_fout_bool(fresult, swig_result)
859+
end function
860+
581861

582862
end module

0 commit comments

Comments
 (0)