tatami
C++ API for different matrix representations
Loading...
Searching...
No Matches
compare_helpers.hpp
Go to the documentation of this file.
1#ifndef TATAMI_ISOMETRIC_BINARY_COMPARE_HELPERS_H
2#define TATAMI_ISOMETRIC_BINARY_COMPARE_HELPERS_H
3
5#include "utils.hpp"
7
14namespace tatami {
15
26template<CompareOperation op_, typename OutputValue_, typename InputValue_, typename Index_>
27struct DelayedBinaryIsometricCompareHelper 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_compare<op_>(val, right_buffer[i]);
48 } else {
49 output_buffer[i] = delayed_compare<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_compare<op_>(val, right_buffer[i]);
67 } else {
68 output_buffer[i] = delayed_compare<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 // None of the operations will return zero if one entry is zero and the other entry is non-zero...
84 // except for equality, but then, the sparse() method would never even be used.
85 return delayed_binary_isometric_sparse_operation<false>(
86 left,
87 right,
88 value_buffer,
89 index_buffer,
90 needs_value,
91 needs_index,
92 [](InputValue_ l, InputValue_ r) -> auto {
93 return delayed_compare<op_>(l, r);
94 }
95 );
96 }
97
98public:
102 // It's sparse if f(0, 0) == 0.
103 static constexpr bool known_sparse = (op_ != CompareOperation::EQUAL &&
104 op_ != CompareOperation::GREATER_THAN_OR_EQUAL &&
105 op_ != CompareOperation::LESS_THAN_OR_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
179template<typename OutputValue_, typename InputValue_, typename Index_>
181
189template<typename OutputValue_, typename InputValue_, typename Index_>
191
199template<typename OutputValue_, typename InputValue_, typename Index_>
201
205template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
206std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricEqual() {
207 return std::make_shared<DelayedBinaryIsometricEqualHelper<OutputValue_, InputValue_, Index_> >();
208}
209
210template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
211std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricGreaterThan() {
212 return std::make_shared<DelayedBinaryIsometricGreaterThanHelper<OutputValue_, InputValue_, Index_> >();
213}
214
215template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
216std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricLessThan() {
217 return std::make_shared<DelayedBinaryIsometricLessThanHelper<OutputValue_, InputValue_, Index_> >();
218}
219
220template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
221std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricGreaterThanOrEqual() {
222 return std::make_shared<DelayedBinaryIsometricGreaterThanOrEqualHelper<OutputValue_, InputValue_, Index_> >();
223}
224
225template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
226std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricLessThanOrEqual() {
227 return std::make_shared<DelayedBinaryIsometricLessThanOrEqualHelper<OutputValue_, InputValue_, Index_> >();
228}
229
230template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
231std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricNotEqual() {
232 return std::make_shared<DelayedBinaryIsometricNotEqualHelper<OutputValue_, InputValue_, Index_> >();
233}
238}
239
240#endif
Interface for tatami::DelayedBinaryIsometricOperation helpers.
Helper operation interface for DelayedBinaryIsometricOperation.
Definition helper_interface.hpp:26
Utilities for delayed comparison operations.
Flexible representations for matrix data.
Definition Extractor.hpp:15
Helper for delayed binary isometric comparisons.
Definition compare_helpers.hpp:27
std::optional< Index_ > ncol() const
Definition compare_helpers.hpp:127
bool zero_depends_on_column() const
Definition compare_helpers.hpp:30
bool non_zero_depends_on_row() const
Definition compare_helpers.hpp:31
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 compare_helpers.hpp:54
bool is_sparse() const
Definition compare_helpers.hpp:118
bool zero_depends_on_row() const
Definition compare_helpers.hpp:29
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 compare_helpers.hpp:35
std::optional< Index_ > nrow() const
Definition compare_helpers.hpp:123
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 compare_helpers.hpp:73
bool non_zero_depends_on_column() const
Definition compare_helpers.hpp:32
OutputValue_ fill(const bool, const Index_) const
Definition compare_helpers.hpp:110
A range of a sparse vector.
Definition SparseRange.hpp:32