1#ifndef TATAMI_ISOMETRIC_UNARY_BOOLEAN_HELPERS_H
2#define TATAMI_ISOMETRIC_UNARY_BOOLEAN_HELPERS_H
20template<
typename InputValue_,
typename Index_,
typename OutputValue_>
21void delayed_boolean_cast(
const InputValue_* input, Index_ length, OutputValue_* output) {
22 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
25 for (Index_ i = 0; i < length; ++i) {
26 output[i] =
static_cast<bool>(input[i]);
30template<
typename InputValue_,
typename Index_,
typename OutputValue_>
31void delayed_boolean_not(
const InputValue_* input, Index_ length, OutputValue_* output) {
32 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
35 for (Index_ i = 0; i < length; ++i) {
36 output[i] = !
static_cast<bool>(input[i]);
40template<BooleanOperation op_,
typename InputValue_,
typename Index_,
typename OutputValue_>
41void delayed_boolean_run_simple(
const InputValue_* input, Index_ length,
bool scalar, OutputValue_* output) {
42 if constexpr(op_ == BooleanOperation::AND) {
44 delayed_boolean_cast(input, length, output);
46 std::fill_n(output, length, 0);
48 }
else if constexpr(op_ == BooleanOperation::OR) {
50 std::fill_n(output, length, 1);
52 delayed_boolean_cast(input, length, output);
54 }
else if constexpr(op_ == BooleanOperation::XOR) {
56 delayed_boolean_not(input, length, output);
58 delayed_boolean_cast(input, length, output);
62 delayed_boolean_cast(input, length, output);
64 delayed_boolean_not(input, length, output);
69template<BooleanOperation op_>
70bool delayed_boolean_actual_sparse(
bool scalar) {
71 return delayed_boolean<op_>(0, scalar) ==
static_cast<bool>(0);
88template<BooleanOperation op_,
typename OutputValue_,
typename InputValue_,
typename Index_>
95 my_sparse = delayed_boolean_actual_sparse<op_>(my_scalar);
103 std::optional<Index_>
nrow()
const {
107 std::optional<Index_>
ncol()
const {
129 void dense(
bool, Index_, Index_, Index_ length,
const InputValue_* input, OutputValue_* output)
const {
130 delayed_boolean_run_simple<op_>(input, length, my_scalar, output);
133 void dense(
bool, Index_,
const std::vector<Index_>& indices,
const InputValue_* input, OutputValue_* output)
const {
134 delayed_boolean_run_simple<op_>(input,
static_cast<Index_
>(indices.size()), my_scalar, output);
142 void sparse(
bool, Index_, Index_ number,
const InputValue_* input_value,
const Index_*, OutputValue_* output_value)
const {
143 delayed_boolean_run_simple<op_>(input_value, number, my_scalar, output_value);
146 OutputValue_
fill(
bool, Index_)
const {
149 return delayed_boolean<op_>(0, my_scalar);
160template<
typename OutputValue_,
typename InputValue_,
typename Index_>
170template<
typename OutputValue_,
typename InputValue_,
typename Index_>
180template<
typename OutputValue_,
typename InputValue_,
typename Index_>
190template<
typename OutputValue_,
typename InputValue_,
typename Index_>
197template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int>
198std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricBooleanEqualScalar(
bool scalar) {
199 return std::make_shared<DelayedUnaryIsometricBooleanEqualScalarHelper<OutputValue_, InputValue_, Index_> >(scalar);
202template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int>
203std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricBooleanAndScalar(
bool scalar) {
204 return std::make_shared<DelayedUnaryIsometricBooleanAndScalarHelper<OutputValue_, InputValue_, Index_> >(scalar);
207template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int>
208std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricBooleanOrScalar(
bool scalar) {
209 return std::make_shared<DelayedUnaryIsometricBooleanOrScalarHelper<OutputValue_, InputValue_, Index_> >(scalar);
212template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int>
213std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricBooleanXorScalar(
bool scalar) {
214 return std::make_shared<DelayedUnaryIsometricBooleanXorScalarHelper<OutputValue_, InputValue_, Index_> >(scalar);
230template<
typename OutputValue_,
typename InputValue_,
typename Index_>
233 std::optional<Index_>
nrow()
const {
237 std::optional<Index_>
ncol()
const {
259 void dense(
bool, Index_, Index_, Index_ length,
const InputValue_* input, OutputValue_* output)
const {
260 delayed_boolean_not(input, length, output);
263 void dense(
bool, Index_,
const std::vector<Index_>& indices,
const InputValue_* input, OutputValue_* output)
const {
264 delayed_boolean_not(input,
static_cast<Index_
>(indices.size()), output);
272 void sparse(
bool, Index_, Index_ number,
const InputValue_* input_value,
const Index_*, OutputValue_* output_value)
const {
273 delayed_boolean_not(input_value, number, output_value);
276 OutputValue_
fill(
bool, Index_)
const {
291template<
typename OutputValue_,
typename InputValue_,
typename Index_>
294 std::optional<Index_>
nrow()
const {
298 std::optional<Index_>
ncol()
const {
320 void dense(
bool, Index_, Index_, Index_ length,
const InputValue_* input, OutputValue_* output)
const {
321 delayed_boolean_cast(input, length, output);
324 void dense(
bool, Index_,
const std::vector<Index_>& indices,
const InputValue_* input, OutputValue_* output)
const {
325 delayed_boolean_cast(input,
static_cast<Index_
>(indices.size()), output);
333 void sparse(
bool, Index_, Index_ number,
const InputValue_* input_value,
const Index_*, OutputValue_* output_value)
const {
334 delayed_boolean_cast(input_value, number, output_value);
337 OutputValue_
fill(
bool, Index_)
const {
346typedef DelayedUnaryIsometricBooleanCastHelper<double, double, int> DelayedUnaryIsometricBooleanCast;
347typedef DelayedUnaryIsometricBooleanNotHelper<double, double, int> DelayedUnaryIsometricBooleanNot;
364template<BooleanOperation op_,
typename OutputValue_,
typename InputValue_,
typename Index_,
typename Vector_>
375 for (
auto x : my_vector) {
376 if (!delayed_boolean_actual_sparse<op_>(x)) {
386 bool my_sparse =
true;
389 std::optional<Index_>
nrow()
const {
391 return my_vector.size();
397 std::optional<Index_>
ncol()
const {
401 return my_vector.size();
423 void dense(
bool row, Index_ idx, Index_ start, Index_ length,
const InputValue_* input, OutputValue_* output)
const {
424 if (row == my_by_row) {
425 delayed_boolean_run_simple<op_>(input, length, my_vector[idx], output);
427 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
430 for (Index_ i = 0; i < length; ++i) {
431 output[i] = delayed_boolean<op_>(input[i], my_vector[i + start]);
436 void dense(
bool row, Index_ idx,
const std::vector<Index_>& indices,
const InputValue_* input, OutputValue_* output)
const {
437 if (row == my_by_row) {
438 delayed_boolean_run_simple<op_>(input,
static_cast<Index_
>(indices.size()), my_vector[idx], output);
440 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
443 Index_ length = indices.size();
444 for (Index_ i = 0; i < length; ++i) {
445 output[i] = delayed_boolean<op_>(input[i], my_vector[indices[i]]);
455 void sparse(
bool row, Index_ idx, Index_ number,
const InputValue_* input_value,
const Index_* index, OutputValue_* output_value)
const {
456 if (row == my_by_row) {
457 delayed_boolean_run_simple<op_>(input_value, number, my_vector[idx], output_value);
459 if constexpr(std::is_same<InputValue_, OutputValue_>::value) {
460 input_value = output_value;
462 for (Index_ i = 0; i < number; ++i) {
463 output_value[i] = delayed_boolean<op_>(input_value[i], my_vector[index[i]]);
468 OutputValue_
fill(
bool row, Index_ idx)
const {
469 if (row == my_by_row) {
470 return delayed_boolean<op_>(0, my_vector[idx]);
486template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename Vector_>
496template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename Vector_>
506template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename Vector_>
516template<
typename OutputValue_,
typename InputValue_,
typename Index_,
typename Vector_>
523template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename Vector_>
524std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricBooleanEqualVector(Vector_ vector,
bool by_row) {
525 return std::make_shared<DelayedUnaryIsometricBooleanEqualVectorHelper<OutputValue_, InputValue_, Index_, Vector_> >(std::move(vector), by_row);
528template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename Vector_>
529std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricBooleanAndVector(Vector_ vector,
bool by_row) {
530 return std::make_shared<DelayedUnaryIsometricBooleanAndVectorHelper<OutputValue_, InputValue_, Index_, Vector_> >(std::move(vector), by_row);
533template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename Vector_>
534std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricBooleanOrVector(Vector_ vector,
bool by_row) {
535 return std::make_shared<DelayedUnaryIsometricBooleanOrVectorHelper<OutputValue_, InputValue_, Index_, Vector_> >(std::move(vector), by_row);
538template<
typename OutputValue_ =
double,
typename InputValue_ =
double,
typename Index_ =
int,
typename Vector_>
539std::shared_ptr<DelayedUnaryIsometricOperationHelper<OutputValue_, InputValue_, Index_> > make_DelayedUnaryIsometricBooleanXorVector(Vector_ vector,
bool by_row) {
540 return std::make_shared<DelayedUnaryIsometricBooleanXorVectorHelper<OutputValue_, InputValue_, Index_, Vector_> >(std::move(vector), by_row);
Utilities for delayed boolean operations.
Delayed unary isometric boolean cast.
Definition boolean_helpers.hpp:292
bool non_zero_depends_on_row() const
Definition boolean_helpers.hpp:311
bool zero_depends_on_row() const
Definition boolean_helpers.hpp:303
std::optional< Index_ > nrow() const
Definition boolean_helpers.hpp:294
std::optional< Index_ > ncol() const
Definition boolean_helpers.hpp:298
bool non_zero_depends_on_column() const
Definition boolean_helpers.hpp:315
OutputValue_ fill(bool, Index_) const
Definition boolean_helpers.hpp:337
void dense(bool, Index_, const std::vector< Index_ > &indices, const InputValue_ *input, OutputValue_ *output) const
Definition boolean_helpers.hpp:324
bool zero_depends_on_column() const
Definition boolean_helpers.hpp:307
void sparse(bool, Index_, Index_ number, const InputValue_ *input_value, const Index_ *, OutputValue_ *output_value) const
Definition boolean_helpers.hpp:333
void dense(bool, Index_, Index_, Index_ length, const InputValue_ *input, OutputValue_ *output) const
Definition boolean_helpers.hpp:320
bool is_sparse() const
Definition boolean_helpers.hpp:329
Helper for a delayed unary isometric boolean NOT operation.
Definition boolean_helpers.hpp:231
bool non_zero_depends_on_row() const
Definition boolean_helpers.hpp:250
OutputValue_ fill(bool, Index_) const
Definition boolean_helpers.hpp:276
std::optional< Index_ > nrow() const
Definition boolean_helpers.hpp:233
bool is_sparse() const
Definition boolean_helpers.hpp:268
std::optional< Index_ > ncol() const
Definition boolean_helpers.hpp:237
bool non_zero_depends_on_column() const
Definition boolean_helpers.hpp:254
void dense(bool, Index_, Index_, Index_ length, const InputValue_ *input, OutputValue_ *output) const
Definition boolean_helpers.hpp:259
bool zero_depends_on_row() const
Definition boolean_helpers.hpp:242
void sparse(bool, Index_, Index_ number, const InputValue_ *input_value, const Index_ *, OutputValue_ *output_value) const
Definition boolean_helpers.hpp:272
bool zero_depends_on_column() const
Definition boolean_helpers.hpp:246
void dense(bool, Index_, const std::vector< Index_ > &indices, const InputValue_ *input, OutputValue_ *output) const
Definition boolean_helpers.hpp:263
Helper for delayed unary isometric scalar boolean operations.
Definition boolean_helpers.hpp:89
bool non_zero_depends_on_column() const
Definition boolean_helpers.hpp:124
bool zero_depends_on_column() const
Definition boolean_helpers.hpp:116
void dense(bool, Index_, Index_, Index_ length, const InputValue_ *input, OutputValue_ *output) const
Definition boolean_helpers.hpp:129
OutputValue_ fill(bool, Index_) const
Definition boolean_helpers.hpp:146
std::optional< Index_ > nrow() const
Definition boolean_helpers.hpp:103
std::optional< Index_ > ncol() const
Definition boolean_helpers.hpp:107
bool is_sparse() const
Definition boolean_helpers.hpp:138
void sparse(bool, Index_, Index_ number, const InputValue_ *input_value, const Index_ *, OutputValue_ *output_value) const
Definition boolean_helpers.hpp:142
DelayedUnaryIsometricBooleanScalarHelper(bool scalar)
Definition boolean_helpers.hpp:94
bool zero_depends_on_row() const
Definition boolean_helpers.hpp:112
bool non_zero_depends_on_row() const
Definition boolean_helpers.hpp:120
void dense(bool, Index_, const std::vector< Index_ > &indices, const InputValue_ *input, OutputValue_ *output) const
Definition boolean_helpers.hpp:133
Helper for delayed unary isometric vector boolean operations.
Definition boolean_helpers.hpp:365
void dense(bool row, Index_ idx, Index_ start, Index_ length, const InputValue_ *input, OutputValue_ *output) const
Definition boolean_helpers.hpp:423
bool is_sparse() const
Definition boolean_helpers.hpp:451
bool non_zero_depends_on_row() const
Definition boolean_helpers.hpp:414
std::optional< Index_ > nrow() const
Definition boolean_helpers.hpp:389
bool zero_depends_on_row() const
Definition boolean_helpers.hpp:406
DelayedUnaryIsometricBooleanVectorHelper(Vector_ vector, bool by_row)
Definition boolean_helpers.hpp:374
void dense(bool row, Index_ idx, const std::vector< Index_ > &indices, const InputValue_ *input, OutputValue_ *output) const
Definition boolean_helpers.hpp:436
OutputValue_ fill(bool row, Index_ idx) const
Definition boolean_helpers.hpp:468
void sparse(bool row, Index_ idx, Index_ number, const InputValue_ *input_value, const Index_ *index, OutputValue_ *output_value) const
Definition boolean_helpers.hpp:455
bool zero_depends_on_column() const
Definition boolean_helpers.hpp:410
std::optional< Index_ > ncol() const
Definition boolean_helpers.hpp:397
bool non_zero_depends_on_column() const
Definition boolean_helpers.hpp:418
Helper operation interface for DelayedUnaryIsometricOperation.
Definition helper_interface.hpp:27
Flexible representations for matrix data.
Definition Extractor.hpp:15
Interface for tatami::DelayedUnaryIsometricOperation helpers.