spla
Loading...
Searching...
No Matches
top.hpp
Go to the documentation of this file.
1/**********************************************************************************/
2/* This file is part of spla project */
3/* https://github.com/SparseLinearAlgebra/spla */
4/**********************************************************************************/
5/* MIT License */
6/* */
7/* Copyright (c) 2023 SparseLinearAlgebra */
8/* */
9/* Permission is hereby granted, free of charge, to any person obtaining a copy */
10/* of this software and associated documentation files (the "Software"), to deal */
11/* in the Software without restriction, including without limitation the rights */
12/* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13/* copies of the Software, and to permit persons to whom the Software is */
14/* furnished to do so, subject to the following conditions: */
15/* */
16/* The above copyright notice and this permission notice shall be included in all */
17/* copies or substantial portions of the Software. */
18/* */
19/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20/* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21/* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22/* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23/* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24/* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25/* SOFTWARE. */
26/**********************************************************************************/
27
28#ifndef SPLA_TOP_HPP
29#define SPLA_TOP_HPP
30
31#include <spla/config.hpp>
32#include <spla/op.hpp>
33
34#include <core/ttype.hpp>
35
36#include <functional>
37#include <sstream>
38#include <string>
39
40namespace spla {
41
42#define DECL_OP_UNA(fname, key_prefix, A0, R, ...) \
43 { \
44 auto func = make_ref<TOpUnary<A0, R>>(); \
45 \
46 func->function = [](A0 a) -> R __VA_ARGS__; \
47 func->name = #fname; \
48 \
49 std::stringstream source_builder; \
50 source_builder << "(" \
51 << func->get_type_arg_0()->get_cpp() \
52 << " a)" #__VA_ARGS__; \
53 \
54 func->source = source_builder.str(); \
55 \
56 std::stringstream key_builder; \
57 key_builder << #key_prefix << "_" \
58 << func->get_type_arg_0()->get_code() \
59 << func->get_type_res()->get_code(); \
60 func->key = key_builder.str(); \
61 \
62 fname = func.as<OpUnary>(); \
63 }
64
65#define DECL_OP_UNA_S(name, key_prefix, T, ...) \
66 DECL_OP_UNA(name, key_prefix, T, T, __VA_ARGS__)
67
68#define DECL_OP_BIN(fname, key_prefix, A0, A1, R, ...) \
69 { \
70 auto func = make_ref<TOpBinary<A0, A1, R>>(); \
71 \
72 func->function = [](A0 a, A1 b) -> R __VA_ARGS__; \
73 func->name = #fname; \
74 \
75 std::stringstream source_builder; \
76 source_builder << "(" \
77 << func->get_type_arg_0()->get_cpp() \
78 << " a, " \
79 << func->get_type_arg_1()->get_cpp() \
80 << " b)" #__VA_ARGS__; \
81 \
82 func->source = source_builder.str(); \
83 \
84 std::stringstream key_builder; \
85 key_builder << #key_prefix << "_" \
86 << func->get_type_arg_0()->get_code() \
87 << func->get_type_arg_1()->get_code() \
88 << func->get_type_res()->get_code(); \
89 func->key = key_builder.str(); \
90 \
91 fname = func.as<OpBinary>(); \
92 }
93
94#define DECL_OP_BIN_S(name, key_prefix, T, ...) \
95 DECL_OP_BIN(name, key_prefix, T, T, T, __VA_ARGS__)
96
97#define DECL_OP_SELECT(fname, key_prefix, A0, ...) \
98 { \
99 auto func = make_ref<TOpSelect<A0>>(); \
100 \
101 func->function = [](A0 a) -> bool __VA_ARGS__; \
102 func->name = #fname; \
103 \
104 std::stringstream source_builder; \
105 source_builder << "(" \
106 << func->get_type_arg_0()->get_cpp() \
107 << " a)" #__VA_ARGS__; \
108 \
109 func->source = source_builder.str(); \
110 \
111 std::stringstream key_builder; \
112 key_builder << #key_prefix << "_" \
113 << func->get_type_arg_0()->get_code(); \
114 func->key = key_builder.str(); \
115 \
116 fname = func.as<OpSelect>(); \
117 }
118
124 template<typename A0, typename R>
125 class TOpUnary : public OpUnary {
126 public:
127 ~TOpUnary() override = default;
128
129 void set_label(std::string label) override;
130 const std::string& get_label() const override;
131 std::string get_name() override;
132 std::string get_source_cl() override;
133 std::string get_key() override;
134 ref_ptr<Type> get_type_arg_0() override;
135 ref_ptr<Type> get_type_res() override;
136
137 std::function<R(A0)> function;
138 std::string name;
139 std::string source;
140 std::string key;
141 std::string label;
142 };
143
144 template<typename A0, typename R>
145 void TOpUnary<A0, R>::set_label(std::string new_label) {
146 label = std::move(new_label);
147 }
148 template<typename A0, typename R>
149 const std::string& TOpUnary<A0, R>::get_label() const {
150 return label;
151 }
152 template<typename A0, typename R>
154 return name;
155 }
156 template<typename A0, typename R>
158 return source;
159 }
160 template<typename A0, typename R>
162 return key;
163 }
164 template<typename A0, typename R>
166 return get_ttype<A0>().template as<Type>();
167 }
168 template<typename A0, typename R>
170 return get_ttype<R>().template as<Type>();
171 }
172
173 template<typename A0, typename A1, typename R>
174 class TOpBinary : public OpBinary {
175 public:
176 ~TOpBinary() override = default;
177
178 void set_label(std::string label) override;
179 const std::string& get_label() const override;
180 std::string get_name() override;
181 std::string get_source_cl() override;
182 std::string get_key() override;
183 ref_ptr<Type> get_type_arg_0() override;
184 ref_ptr<Type> get_type_arg_1() override;
185 ref_ptr<Type> get_type_res() override;
186
187 std::function<R(A0, A1)> function;
188 std::string name;
189 std::string source;
190 std::string key;
191 std::string label;
192 };
193
194 template<typename A0, typename A1, typename R>
195 void TOpBinary<A0, A1, R>::set_label(std::string new_label) {
196 label = std::move(new_label);
197 }
198 template<typename A0, typename A1, typename R>
199 const std::string& TOpBinary<A0, A1, R>::get_label() const {
200 return label;
201 }
202 template<typename A0, typename A1, typename R>
204 return name;
205 }
206 template<typename A0, typename A1, typename R>
208 return source;
209 }
210 template<typename A0, typename A1, typename R>
212 return key;
213 }
214 template<typename A0, typename A1, typename R>
216 return get_ttype<A0>().template as<Type>();
217 }
218 template<typename A0, typename A1, typename R>
220 return get_ttype<A1>().template as<Type>();
221 }
222 template<typename A0, typename A1, typename R>
224 return get_ttype<R>().template as<Type>();
225 }
226
227 template<typename A0>
228 class TOpSelect : public OpSelect {
229 public:
230 ~TOpSelect() override = default;
231
232 void set_label(std::string label) override;
233 const std::string& get_label() const override;
234 std::string get_name() override;
235 std::string get_source_cl() override;
236 std::string get_key() override;
237 ref_ptr<Type> get_type_arg_0() override;
238 ref_ptr<Type> get_type_res() override;
239
240 std::function<bool(A0)> function;
241 std::string name;
242 std::string source;
243 std::string key;
244 std::string label;
245 };
246
247 template<typename A0>
248 void TOpSelect<A0>::set_label(std::string new_label) {
249 label = std::move(new_label);
250 }
251 template<typename A0>
252 const std::string& TOpSelect<A0>::get_label() const {
253 return label;
254 }
255 template<typename A0>
257 return name;
258 }
259 template<typename A0>
261 return source;
262 }
263 template<typename A0>
265 return key;
266 }
267 template<typename A0>
269 return get_ttype<A0>().template as<Type>();
270 }
271 template<typename A0>
273 return BOOL.template as<Type>();
274 }
275
279 void register_ops();
280
285}// namespace spla
286
287#endif//SPLA_TOP_HPP
Binary operation with 2-arity.
Definition op.hpp:73
Select operation with 1-arity and bool return type.
Definition op.hpp:87
Unary operation with 1-arity.
Definition op.hpp:60
Definition top.hpp:174
std::string source
Definition top.hpp:189
std::function< R(A0, A1)> function
Definition top.hpp:187
~TOpBinary() override=default
std::string name
Definition top.hpp:188
std::string key
Definition top.hpp:190
std::string label
Definition top.hpp:191
Definition top.hpp:228
std::function< bool(A0)> function
Definition top.hpp:240
~TOpSelect() override=default
std::string source
Definition top.hpp:242
std::string name
Definition top.hpp:241
std::string label
Definition top.hpp:244
std::string key
Definition top.hpp:243
Definition top.hpp:125
std::string source
Definition top.hpp:139
std::string label
Definition top.hpp:141
std::string key
Definition top.hpp:140
~TOpUnary() override=default
std::function< R(A0)> function
Definition top.hpp:137
std::string name
Definition top.hpp:138
Automates reference counting and behaves as shared smart pointer.
Definition ref.hpp:117
std::string get_key() override
Definition top.hpp:264
std::string get_source_cl() override
Definition top.hpp:260
ref_ptr< Type > get_type_res() override
Definition top.hpp:272
std::string get_name() override
Definition top.hpp:256
ref_ptr< Type > get_type_arg_0() override
Definition top.hpp:215
std::string get_name() override
Definition top.hpp:203
const std::string & get_label() const override
Definition top.hpp:252
ref_ptr< Type > get_type_res() override
Definition top.hpp:223
ref_ptr< Type > get_type_arg_1() override
Definition top.hpp:219
std::string get_source_cl() override
Definition top.hpp:157
void set_label(std::string label) override
Definition top.hpp:145
const std::string & get_label() const override
Definition top.hpp:149
std::string get_source_cl() override
Definition top.hpp:207
ref_ptr< Type > get_type_arg_0() override
Definition top.hpp:165
void register_ops()
Register all ops on library initialization.
Definition op.cpp:157
std::string get_name() override
Definition top.hpp:153
ref_ptr< Type > get_type_arg_0() override
Definition top.hpp:268
ref_ptr< Type > get_type_res() override
Definition top.hpp:169
std::string get_key() override
Definition top.hpp:211
const std::string & get_label() const override
Definition top.hpp:199
void set_label(std::string label) override
Definition top.hpp:195
std::string get_key() override
Definition top.hpp:161
void set_label(std::string label) override
Definition top.hpp:248
ref_ptr< Type > BOOL
Definition type.cpp:32
Definition algorithm.hpp:37