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(
36 const bool,
37 const Index_,
38 const Index_,
39 const Index_ length,
40 const InputValue_* const left_buffer,
41 const InputValue_* const right_buffer,
42 OutputValue_* const output_buffer)
43 const {
44 for (Index_ i = 0; i < length; ++i) {
45 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
46 auto& val = output_buffer[i];
47 val = delayed_boolean<op_>(val, right_buffer[i]);
48 } else {
49 output_buffer[i] = delayed_boolean<op_>(left_buffer[i], right_buffer[i]);
50 }
51 }
52 }
53
54 void dense(
55 const bool,
56 const Index_,
57 const std::vector<Index_>& indices,
58 const InputValue_* const left_buffer,
59 const InputValue_* const right_buffer,
60 OutputValue_* const output_buffer)
61 const {
62 const Index_ length = indices.size();
63 for (Index_ i = 0; i < length; ++i) {
64 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
65 auto& val = output_buffer[i];
66 val = delayed_boolean<op_>(val, right_buffer[i]);
67 } else {
68 output_buffer[i] = delayed_boolean<op_>(left_buffer[i], right_buffer[i]);
69 }
70 }
71 }
72
73 Index_ sparse(
74 const bool,
75 const Index_,
78 OutputValue_* const value_buffer,
79 Index_* const index_buffer,
80 const bool needs_value,
81 const bool needs_index)
82 const {
83 // Don't bother storing an explicit zero for AND operations when either
84 // entry is zero. This should be NaN-safe as NaNs are truthy, so
85 // applying AND on that would just be false anyway.
86 constexpr bool must_have_both = (op_ == BooleanOperation::AND);
87 return delayed_binary_isometric_sparse_operation<must_have_both>(
88 left,
89 right,
90 value_buffer,
91 index_buffer,
92 needs_value,
93 needs_index,
94 [](InputValue_ l, InputValue_ r) -> auto {
95 return delayed_boolean<op_>(l, r);
96 }
97 );
98 }
99
100public:
104 // It's sparse if f(0, 0) == 0.
105 static constexpr bool known_sparse = (op_ != BooleanOperation::EQUAL);
110 OutputValue_ fill(const bool, const Index_) const {
111 if constexpr(known_sparse) {
112 return 0;
113 } else {
114 return 1;
115 }
116 }
117
118 bool is_sparse() const {
119 return known_sparse;
120 }
121
122public:
123 std::optional<Index_> nrow() const {
124 return std::nullopt;
125 }
126
127 std::optional<Index_> ncol() const {
128 return std::nullopt;
129 }
130};
131
139template<typename OutputValue_, typename InputValue_, typename Index_>
141
149template<typename OutputValue_, typename InputValue_, typename Index_>
151
159template<typename OutputValue_, typename InputValue_, typename Index_>
161
169template<typename OutputValue_, typename InputValue_, typename Index_>
171
175template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
176std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricBooleanEqual() {
177 return std::make_shared<DelayedBinaryIsometricBooleanEqualHelper<OutputValue_, InputValue_, Index_> >();
178}
179
180template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
181std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricBooleanAnd() {
182 return std::make_shared<DelayedBinaryIsometricBooleanAndHelper<OutputValue_, InputValue_, Index_> >();
183}
184
185template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
186std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricBooleanOr() {
187 return std::make_shared<DelayedBinaryIsometricBooleanOrHelper<OutputValue_, InputValue_, Index_> >();
188}
189
190template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
191std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricBooleanXor() {
192 return std::make_shared<DelayedBinaryIsometricBooleanXorHelper<OutputValue_, InputValue_, Index_> >();
193}
198}
199
200#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
void dense(const bool, const Index_, const std::vector< Index_ > &indices, const InputValue_ *const left_buffer, const InputValue_ *const right_buffer, OutputValue_ *const output_buffer) const
Definition boolean_helpers.hpp:54
bool non_zero_depends_on_column() const
Definition boolean_helpers.hpp:32
bool is_sparse() const
Definition boolean_helpers.hpp:118
void dense(const bool, const Index_, const Index_, const Index_ length, const InputValue_ *const left_buffer, const InputValue_ *const right_buffer, OutputValue_ *const output_buffer) const
Definition boolean_helpers.hpp:35
std::optional< Index_ > nrow() const
Definition boolean_helpers.hpp:123
bool zero_depends_on_column() const
Definition boolean_helpers.hpp:30
Index_ sparse(const bool, const Index_, const SparseRange< InputValue_, Index_ > &left, const SparseRange< InputValue_, Index_ > &right, OutputValue_ *const value_buffer, Index_ *const index_buffer, const bool needs_value, const bool needs_index) const
Definition boolean_helpers.hpp:73
std::optional< Index_ > ncol() const
Definition boolean_helpers.hpp:127
OutputValue_ fill(const bool, const Index_) const
Definition boolean_helpers.hpp:110
bool non_zero_depends_on_row() const
Definition boolean_helpers.hpp:31
A range of a sparse vector.
Definition SparseRange.hpp:32