|
| 1 | +/*! |
| 2 | + * \file flc_set.i |
| 3 | + * |
| 4 | + * Copyright (c) 2019 Oak Ridge National Laboratory, UT-Battelle, LLC. |
| 5 | + * Distributed under an MIT open source license: see LICENSE for details. |
| 6 | + */ |
| 7 | + |
| 8 | +%module "flc_set" |
| 9 | +%include "import_flc.i" |
| 10 | +%flc_add_header |
| 11 | + |
| 12 | +%include <std_set.i> |
| 13 | + |
| 14 | +// Support for set operations |
| 15 | +%{ |
| 16 | +#include <algorithm> |
| 17 | +#include <iterator> |
| 18 | +%} |
| 19 | + |
| 20 | +/* ------------------------------------------------------------------------- |
| 21 | + * Macro definitions |
| 22 | + * ------------------------------------------------------------------------- */ |
| 23 | + |
| 24 | +%define %flc_define_set_algorithm(FUNCNAME) |
| 25 | + %insert("header") { |
| 26 | + template<class Set_t> |
| 27 | + static Set_t flc_##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 | + } // end %insert |
| 36 | +%enddef |
| 37 | + |
| 38 | +%define %flc_extend_set_algorithm(FUNCNAME, RETVAL, TYPE) |
| 39 | + // The rename with the stringifying macro is necessary because 'union' is a |
| 40 | + // keyword. |
| 41 | + %rename(#FUNCNAME) std::set<TYPE>::set_##FUNCNAME; |
| 42 | + %extend std::set<TYPE> { |
| 43 | + RETVAL set_##FUNCNAME(const std::set<TYPE>& other) |
| 44 | + { return flc_set_##FUNCNAME(*$self, other); } |
| 45 | + } // end %extend |
| 46 | +%enddef |
| 47 | + |
| 48 | +%define %flc_extend_set_pod(CTYPE) |
| 49 | + %apply (const SWIGTYPE *DATA, ::size_t SIZE) |
| 50 | + { (const CTYPE* DATA, size_type SIZE) }; |
| 51 | + |
| 52 | + // Construct from an array of data |
| 53 | + set(const CTYPE* DATA, size_type SIZE) { |
| 54 | + return new std::set<CTYPE>(DATA, DATA + SIZE); |
| 55 | + } |
| 56 | + |
| 57 | + // Insert an array of data |
| 58 | + void insert(const CTYPE* DATA, size_type SIZE) { |
| 59 | + $self->insert(DATA, DATA + SIZE); |
| 60 | + } |
| 61 | +%enddef |
| 62 | + |
| 63 | +/* ------------------------------------------------------------------------- */ |
| 64 | +/*! \def %specialize_std_set_pod |
| 65 | + * |
| 66 | + * Inject member functions and typemaps for POD classes. |
| 67 | + * |
| 68 | + * These provide an efficient constructor from a Fortan array view. It also |
| 69 | + * offers a "view" functionality for getting an array pointer to the |
| 70 | + * set-owned data. |
| 71 | + * |
| 72 | + * This definition is considered part of the \em public API so that downstream |
| 73 | + * apps that generate FLC-based bindings can instantiate their own POD sets. |
| 74 | + */ |
| 75 | +%define %specialize_std_set_pod(T) |
| 76 | + |
| 77 | +// Automatically free temporary sets as appropriate |
| 78 | +%fortran_autofree_rvalue(std::set<T>); |
| 79 | + |
| 80 | +namespace std { |
| 81 | + template<> class set<T> { |
| 82 | + |
| 83 | + SWIG_STD_SET_COMMON(set, T, std::less<T>, std::allocator<T>) |
| 84 | + %extend { |
| 85 | + %flc_extend_set_pod(T) |
| 86 | + } |
| 87 | + }; |
| 88 | +} |
| 89 | +%enddef |
| 90 | + |
| 91 | +/* ------------------------------------------------------------------------- |
| 92 | + * Algorithms |
| 93 | + * ------------------------------------------------------------------------- */ |
| 94 | + |
| 95 | +%flc_define_set_algorithm(set_difference) |
| 96 | +%flc_define_set_algorithm(set_intersection) |
| 97 | +%flc_define_set_algorithm(set_symmetric_difference) |
| 98 | +%flc_define_set_algorithm(set_union) |
| 99 | + |
| 100 | +%insert("header") %{ |
| 101 | +template<class Set_t> |
| 102 | +static bool flc_set_includes(const Set_t& left, const Set_t& right) |
| 103 | +{ |
| 104 | + return std::includes(left.begin(), left.end(), |
| 105 | + right.begin(), right.end()); |
| 106 | +} |
| 107 | +%} |
| 108 | + |
| 109 | +%define %flc_extend_algorithms(TYPE) |
| 110 | + %flc_extend_set_algorithm(difference, std::set<TYPE >, TYPE) |
| 111 | + %flc_extend_set_algorithm(intersection, std::set<TYPE >, TYPE) |
| 112 | + %flc_extend_set_algorithm(symmetric_difference, std::set<TYPE >, TYPE) |
| 113 | + %flc_extend_set_algorithm(union, std::set<TYPE >, TYPE) |
| 114 | + %flc_extend_set_algorithm(includes, bool, TYPE) |
| 115 | +%enddef |
| 116 | + |
| 117 | +/* ------------------------------------------------------------------------- |
| 118 | + * Numeric sets |
| 119 | + * ------------------------------------------------------------------------- */ |
| 120 | + |
| 121 | +%flc_extend_algorithms(int) |
| 122 | +%specialize_std_set_pod(int) |
| 123 | + |
| 124 | +%template(SetInt) std::set<int>; |
| 125 | + |
| 126 | +/* ------------------------------------------------------------------------- |
| 127 | + * String sets |
| 128 | + * ------------------------------------------------------------------------- */ |
| 129 | + |
| 130 | +%fortran_autofree_rvalue(std::set<std::string>); |
| 131 | + |
| 132 | +// Allow direct insertion of a wrapped std::string |
| 133 | +%extend std::set<std::string> { |
| 134 | + void insert_ref(std::string& str) { |
| 135 | + $self->insert(str); |
| 136 | + } |
| 137 | +} |
| 138 | + |
| 139 | +%include <std_string.i> |
| 140 | +%import "flc_string.i" |
| 141 | +%flc_extend_algorithms(std::string) |
| 142 | +%template(SetString) std::set<std::string>; |
0 commit comments