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_BINARY_BOOLEAN_HELPERS_H
2#define TATAMI_ISOMETRIC_BINARY_BOOLEAN_HELPERS_H
3
5#include "utils.hpp"
7
14namespace tatami {
15
26template<BooleanOperation op_, typename OutputValue_, typename InputValue_, typename Index_>
27struct DelayedBinaryIsometricBooleanHelper final : public DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> {
28public:
29 bool zero_depends_on_row() const { return false; }
30 bool zero_depends_on_column() const { return false; }
31 bool non_zero_depends_on_row() const { return false; }
32 bool non_zero_depends_on_column() const { return false; }
33
34public:
35 void dense(bool, Index_, Index_, Index_ length, const InputValue_* left_buffer, const InputValue_* right_buffer, OutputValue_* output_buffer) const {
36 for (Index_ i = 0; i < length; ++i) {
37 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
38 auto& val = output_buffer[i];
39 val = delayed_boolean<op_>(val, right_buffer[i]);
40 } else {
41 output_buffer[i] = delayed_boolean<op_>(left_buffer[i], right_buffer[i]);
42 }
43 }
44 }
45
46 void dense(bool, Index_, const std::vector<Index_>& indices, const InputValue_* left_buffer, const InputValue_* right_buffer, OutputValue_* output_buffer) const {
47 Index_ length = indices.size();
48 for (Index_ i = 0; i < length; ++i) {
49 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
50 auto& val = output_buffer[i];
51 val = delayed_boolean<op_>(val, right_buffer[i]);
52 } else {
53 output_buffer[i] = delayed_boolean<op_>(left_buffer[i], right_buffer[i]);
54 }
55 }
56 }
57
58 Index_ sparse(
59 bool,
60 Index_,
63 OutputValue_* value_buffer,
64 Index_* index_buffer,
65 bool needs_value,
66 bool needs_index)
67 const {
68 // Don't bother storing an explicit zero for AND operations when either
69 // entry is zero. This should be NaN-safe as NaNs are truthy, so
70 // applying AND on that would just be false anyway.
71 constexpr bool must_have_both = (op_ == BooleanOperation::AND);
72 return delayed_binary_isometric_sparse_operation<must_have_both>(
73 left,
74 right,
75 value_buffer,
76 index_buffer,
77 needs_value,
78 needs_index,
79 [](InputValue_ l, InputValue_ r) -> auto {
80 return delayed_boolean<op_>(l, r);
81 }
82 );
83 }
84
85public:
89 // It's sparse if f(0, 0) == 0.
90 static constexpr bool known_sparse = (op_ != BooleanOperation::EQUAL);
95 OutputValue_ fill(bool, Index_) const {
96 if constexpr(known_sparse) {
97 return 0;
98 } else {
99 return 1;
100 }
101 }
102
103 bool is_sparse() const {
104 return known_sparse;
105 }
106
107public:
108 std::optional<Index_> nrow() const {
109 return std::nullopt;
110 }
111
112 std::optional<Index_> ncol() const {
113 return std::nullopt;
114 }
115};
116
124template<typename OutputValue_, typename InputValue_, typename Index_>
126
134template<typename OutputValue_, typename InputValue_, typename Index_>
136
144template<typename OutputValue_, typename InputValue_, typename Index_>
146
154template<typename OutputValue_, typename InputValue_, typename Index_>
156
160template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
161std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricBooleanEqual() {
162 return std::make_shared<DelayedBinaryIsometricBooleanEqualHelper<OutputValue_, InputValue_, Index_> >();
163}
164
165template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
166std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricBooleanAnd() {
167 return std::make_shared<DelayedBinaryIsometricBooleanAndHelper<OutputValue_, InputValue_, Index_> >();
168}
169
170template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
171std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricBooleanOr() {
172 return std::make_shared<DelayedBinaryIsometricBooleanOrHelper<OutputValue_, InputValue_, Index_> >();
173}
174
175template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
176std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricBooleanXor() {
177 return std::make_shared<DelayedBinaryIsometricBooleanXorHelper<OutputValue_, InputValue_, Index_> >();
178}
183}
184
185#endif
Interface for tatami::DelayedBinaryIsometricOperation helpers.
Utilities for delayed boolean operations.
Helper operation interface for DelayedBinaryIsometricOperation.
Definition helper_interface.hpp:26
Flexible representations for matrix data.
Definition Extractor.hpp:15
Helper for delayed binary isometric boolean operations.
Definition boolean_helpers.hpp:27
bool zero_depends_on_row() const
Definition boolean_helpers.hpp:29
bool non_zero_depends_on_column() const
Definition boolean_helpers.hpp:32
void dense(bool, Index_, const std::vector< Index_ > &indices, const InputValue_ *left_buffer, const InputValue_ *right_buffer, OutputValue_ *output_buffer) const
Definition boolean_helpers.hpp:46
bool is_sparse() const
Definition boolean_helpers.hpp:103
void dense(bool, Index_, Index_, Index_ length, const InputValue_ *left_buffer, const InputValue_ *right_buffer, OutputValue_ *output_buffer) const
Definition boolean_helpers.hpp:35
std::optional< Index_ > nrow() const
Definition boolean_helpers.hpp:108
bool zero_depends_on_column() const
Definition boolean_helpers.hpp:30
Index_ sparse(bool, Index_, const SparseRange< InputValue_, Index_ > &left, const SparseRange< InputValue_, Index_ > &right, OutputValue_ *value_buffer, Index_ *index_buffer, bool needs_value, bool needs_index) const
Definition boolean_helpers.hpp:58
OutputValue_ fill(bool, Index_) const
Definition boolean_helpers.hpp:95
std::optional< Index_ > ncol() const
Definition boolean_helpers.hpp:112
bool non_zero_depends_on_row() const
Definition boolean_helpers.hpp:31
A range of a sparse vector.
Definition SparseRange.hpp:32