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(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_compare<op_>(val, right_buffer[i]);
40 } else {
41 output_buffer[i] = delayed_compare<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_compare<op_>(val, right_buffer[i]);
52 } else {
53 output_buffer[i] = delayed_compare<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 // None of the operations will return zero if one entry is zero and the other entry is non-zero...
69 // except for equality, but then, the sparse() method would never even be used.
70 return delayed_binary_isometric_sparse_operation<false>(
71 left,
72 right,
73 value_buffer,
74 index_buffer,
75 needs_value,
76 needs_index,
77 [](InputValue_ l, InputValue_ r) -> auto {
78 return delayed_compare<op_>(l, r);
79 }
80 );
81 }
82
83public:
87 // It's sparse if f(0, 0) == 0.
88 static constexpr bool known_sparse = (op_ != CompareOperation::EQUAL &&
89 op_ != CompareOperation::GREATER_THAN_OR_EQUAL &&
90 op_ != CompareOperation::LESS_THAN_OR_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
164template<typename OutputValue_, typename InputValue_, typename Index_>
166
174template<typename OutputValue_, typename InputValue_, typename Index_>
176
184template<typename OutputValue_, typename InputValue_, typename Index_>
186
190template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
191std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricEqual() {
192 return std::make_shared<DelayedBinaryIsometricEqualHelper<OutputValue_, InputValue_, Index_> >();
193}
194
195template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
196std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricGreaterThan() {
197 return std::make_shared<DelayedBinaryIsometricGreaterThanHelper<OutputValue_, InputValue_, Index_> >();
198}
199
200template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
201std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricLessThan() {
202 return std::make_shared<DelayedBinaryIsometricLessThanHelper<OutputValue_, InputValue_, Index_> >();
203}
204
205template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
206std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricGreaterThanOrEqual() {
207 return std::make_shared<DelayedBinaryIsometricGreaterThanOrEqualHelper<OutputValue_, InputValue_, Index_> >();
208}
209
210template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
211std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricLessThanOrEqual() {
212 return std::make_shared<DelayedBinaryIsometricLessThanOrEqualHelper<OutputValue_, InputValue_, Index_> >();
213}
214
215template<typename OutputValue_ = double, typename InputValue_ = double, typename Index_ = int>
216std::shared_ptr<DelayedBinaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedBinaryIsometricNotEqual() {
217 return std::make_shared<DelayedBinaryIsometricNotEqualHelper<OutputValue_, InputValue_, Index_> >();
218}
223}
224
225#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:112
void dense(bool, Index_, Index_, Index_ length, const InputValue_ *left_buffer, const InputValue_ *right_buffer, OutputValue_ *output_buffer) const
Definition compare_helpers.hpp:35
bool zero_depends_on_column() const
Definition compare_helpers.hpp:30
bool non_zero_depends_on_row() const
Definition compare_helpers.hpp:31
bool is_sparse() const
Definition compare_helpers.hpp:103
bool zero_depends_on_row() const
Definition compare_helpers.hpp:29
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 compare_helpers.hpp:58
std::optional< Index_ > nrow() const
Definition compare_helpers.hpp:108
OutputValue_ fill(bool, Index_) const
Definition compare_helpers.hpp:95
bool non_zero_depends_on_column() const
Definition compare_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 compare_helpers.hpp:46
A range of a sparse vector.
Definition SparseRange.hpp:32