tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
boolean_helpers.hpp
Go to the documentation of this file.
1#ifndef TATAMI_ISOMETRIC_UNARY_BOOLEAN_HELPERS_H
2#define TATAMI_ISOMETRIC_UNARY_BOOLEAN_HELPERS_H
3
4#include "../boolean_utils.hpp"
5#include <vector>
6#include <type_traits>
7
14namespace tatami {
15
19template<typename InputValue_, typename Index_, typename OutputValue_>
21 for (Index_ i = 0; i < length; ++i) {
22 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
23 auto& val = output[i];
24 val = static_cast<bool>(val);
25 } else {
26 output[i] = static_cast<bool>(input[i]);
27 }
28 }
29}
30
31template<typename InputValue_, typename Index_, typename OutputValue_>
33 for (Index_ i = 0; i < length; ++i) {
34 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
35 auto& val = output[i];
36 val = !static_cast<bool>(val);
37 } else {
38 output[i] = !static_cast<bool>(input[i]);
39 }
40 }
41}
42
43template<BooleanOperation op_, typename InputValue_, typename Index_, typename OutputValue_>
45 if constexpr(op_ == BooleanOperation::AND) {
46 if (scalar) {
48 } else {
49 std::fill_n(output, length, 0);
50 }
51 } else if constexpr(op_ == BooleanOperation::OR) {
52 if (scalar) {
53 std::fill_n(output, length, 1);
54 } else {
56 }
57 } else if constexpr(op_ == BooleanOperation::XOR) {
58 if (scalar) {
60 } else {
62 }
63 } else { // EQUAL
64 if (scalar) {
66 } else {
68 }
69 }
70}
71
72template<BooleanOperation op_>
74 return delayed_boolean<op_>(0, scalar) == static_cast<bool>(0);
75}
90template<BooleanOperation op_>
92public:
97 my_sparse = delayed_boolean_actual_sparse<op_>(my_scalar);
98 }
99
100private:
101 bool my_scalar;
102 bool my_sparse;
103
104public:
108 static constexpr bool is_basic = false;
109
110 bool is_sparse() const {
111 return my_sparse;
112 }
117public:
121 template<typename Index_, typename InputValue_, typename OutputValue_>
122 void dense(bool, Index_, Index_, Index_ length, const InputValue_* input, OutputValue_* output) const {
124 }
125
126 template<typename Index_, typename InputValue_, typename OutputValue_>
127 void dense(bool, Index_, const std::vector<Index_>& indices, const InputValue_* input, OutputValue_* output) const {
128 delayed_boolean_run_simple<op_>(input, static_cast<Index_>(indices.size()), my_scalar, output);
129 }
130
131 template<typename Index_, typename InputValue_, typename OutputValue_>
132 void sparse(bool, Index_, Index_ number, const InputValue_* input_value, const Index_*, OutputValue_* output_value) const {
134 }
135
136 template<typename OutputValue_, typename InputValue_, typename Index_>
137 OutputValue_ fill(bool, Index_) const {
138 // Remember, the operation needs to be performed on the InputValue_
139 // to use in casting it to the OutputValue_.
140 return delayed_boolean<op_>(0, my_scalar);
141 }
145};
146
155public:
159 static constexpr bool is_basic = false;
160
161 bool is_sparse() const {
162 return false;
163 }
168private:
169
170public:
174 template<typename Index_, typename InputValue_, typename OutputValue_>
175 void dense(bool, Index_, Index_, Index_ length, const InputValue_* input, OutputValue_* output) const {
177 }
178
179 template<typename Index_, typename InputValue_, typename OutputValue_>
180 void dense(bool, Index_, const std::vector<Index_>& indices, const InputValue_* input, OutputValue_* output) const {
181 delayed_boolean_not(input, static_cast<Index_>(indices.size()), output);
182 }
183
184 template<typename Index_, typename InputValue_, typename OutputValue_>
185 void sparse(bool, Index_, Index_ number, const InputValue_* input_value, const Index_*, OutputValue_* output_value) const {
187 }
188
189 template<typename OutputValue_, typename InputValue_, typename Index_>
190 OutputValue_ fill(bool, Index_) const {
191 return 1;
192 }
196};
197
206public:
210 static constexpr bool is_basic = false;
211
212 bool is_sparse() const {
213 return true;
214 }
219public:
223 template<typename Index_, typename InputValue_, typename OutputValue_>
224 void dense(bool, Index_, Index_, Index_ length, const InputValue_* input, OutputValue_* output) const {
226 }
227
228 template<typename Index_, typename InputValue_, typename OutputValue_>
229 void dense(bool, Index_, const std::vector<Index_>& indices, const InputValue_* input, OutputValue_* output) const {
230 delayed_boolean_cast(input, static_cast<Index_>(indices.size()), output);
231 }
232
233 template<typename Index_, typename InputValue_, typename OutputValue_>
234 void sparse(bool, Index_, Index_ number, const InputValue_* input_value, const Index_*, OutputValue_* output_value) const {
236 }
237
238 template<typename OutputValue_, typename InputValue_, typename Index_>
239 OutputValue_ fill(bool, Index_) const {
240 return 0;
241 }
245};
246
257template<BooleanOperation op_, typename Vector_>
259public:
268 for (auto x : my_vector) {
270 my_sparse = false;
271 break;
272 }
273 }
274 }
275
276private:
277 Vector_ my_vector;
278 bool my_by_row;
279 bool my_sparse = true;
280
281public:
285 static constexpr bool is_basic = false;
286
287 bool zero_depends_on_row() const {
288 return my_by_row;
289 }
290
291 bool zero_depends_on_column() const {
292 return !my_by_row;
293 }
294
295 bool non_zero_depends_on_row() const {
296 return my_by_row;
297 }
298
299 bool non_zero_depends_on_column() const {
300 return !my_by_row;
301 }
302
303 bool is_sparse() const {
304 return my_sparse;
305 }
310public:
314 template<typename Index_, typename InputValue_, typename OutputValue_>
315 void dense(bool row, Index_ idx, Index_ start, Index_ length, const InputValue_* input, OutputValue_* output) const {
316 if (row == my_by_row) {
318 } else {
319 for (Index_ i = 0; i < length; ++i) {
320 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
321 auto& val = output[i];
322 val = delayed_boolean<op_>(val, my_vector[i + start]);
323 } else {
324 output[i] = delayed_boolean<op_>(input[i], my_vector[i + start]);
325 }
326 }
327 }
328 }
329
330 template<typename Index_, typename InputValue_, typename OutputValue_>
331 void dense(bool row, Index_ idx, const std::vector<Index_>& indices, const InputValue_* input, OutputValue_* output) const {
332 if (row == my_by_row) {
333 delayed_boolean_run_simple<op_>(input, static_cast<Index_>(indices.size()), my_vector[idx], output);
334 } else {
335 Index_ length = indices.size();
336 for (Index_ i = 0; i < length; ++i) {
337 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
338 auto& val = output[i];
339 val = delayed_boolean<op_>(val, my_vector[indices[i]]);
340 } else {
341 output[i] = delayed_boolean<op_>(input[i], my_vector[indices[i]]);
342 }
343 }
344 }
345 }
346
347 template<typename Index_, typename InputValue_, typename OutputValue_>
348 void sparse(bool row, Index_ idx, Index_ number, const InputValue_* input_value, const Index_* index, OutputValue_* output_value) const {
349 if (row == my_by_row) {
351 } else {
352 for (Index_ i = 0; i < number; ++i) {
353 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
354 auto& val = output_value[i];
355 val = delayed_boolean<op_>(val, my_vector[index[i]]);
356 } else {
357 output_value[i] = delayed_boolean<op_>(input_value[i], my_vector[index[i]]);
358 }
359 }
360 }
361 }
362
363 template<typename OutputValue_, typename InputValue_, typename Index_>
364 OutputValue_ fill(bool row, Index_ idx) const {
365 if (row == my_by_row) {
366 return delayed_boolean<op_>(0, my_vector[idx]);
367 } else {
368 // We should only get to this point if it's sparse, otherwise no
369 // single fill value would work across the length of my_vector.
370 return 0;
371 }
372 }
376};
377
385
394
403
412
421
429template<typename Vector_>
433
441template<typename Vector_>
445
453template<typename Vector_>
457
465template<typename Vector_>
469
470}
471
472#endif
Delayed unary isometric boolean cast.
Definition boolean_helpers.hpp:205
Delayed unary isometric boolean NOT operation.
Definition boolean_helpers.hpp:154
Delayed unary isometric scalar boolean operation.
Definition boolean_helpers.hpp:91
DelayedUnaryIsometricBooleanScalar(bool scalar)
Definition boolean_helpers.hpp:96
Delayed unary isometric vector boolean operations.
Definition boolean_helpers.hpp:258
DelayedUnaryIsometricBooleanVector(Vector_ vector, bool by_row)
Definition boolean_helpers.hpp:267
Flexible representations for matrix data.
Definition Extractor.hpp:15
DelayedUnaryIsometricBooleanScalar< BooleanOperation::AND > make_DelayedUnaryIsometricBooleanAndScalar(bool scalar)
Definition boolean_helpers.hpp:391
DelayedUnaryIsometricBooleanVector< BooleanOperation::OR, Vector_ > make_DelayedUnaryIsometricBooleanOrVector(Vector_ vector, bool by_row)
Definition boolean_helpers.hpp:442
DelayedUnaryIsometricBooleanScalar< BooleanOperation::OR > make_DelayedUnaryIsometricBooleanOrScalar(bool scalar)
Definition boolean_helpers.hpp:400
DelayedUnaryIsometricBooleanVector< BooleanOperation::EQUAL, Vector_ > make_DelayedUnaryIsometricBooleanEqualVector(Vector_ vector, bool by_row)
Definition boolean_helpers.hpp:466
DelayedUnaryIsometricBooleanVector< BooleanOperation::XOR, Vector_ > make_DelayedUnaryIsometricBooleanXorVector(Vector_ vector, bool by_row)
Definition boolean_helpers.hpp:454
DelayedUnaryIsometricBooleanNot make_DelayedUnaryIsometricBooleanNot()
Definition boolean_helpers.hpp:382
DelayedUnaryIsometricBooleanScalar< BooleanOperation::EQUAL > make_DelayedUnaryIsometricBooleanEqualScalar(bool scalar)
Definition boolean_helpers.hpp:418
DelayedUnaryIsometricBooleanScalar< BooleanOperation::XOR > make_DelayedUnaryIsometricBooleanXorScalar(bool scalar)
Definition boolean_helpers.hpp:409
DelayedUnaryIsometricBooleanVector< BooleanOperation::AND, Vector_ > make_DelayedUnaryIsometricBooleanAndVector(Vector_ vector, bool by_row)
Definition boolean_helpers.hpp:430
auto consecutive_extractor(const Matrix< Value_, Index_ > *mat, bool row, Index_ iter_start, Index_ iter_length, Args_ &&... args)
Definition consecutive_extractor.hpp:35