Skip to content

Commit

Permalink
Cancel out repeated DelayedTransposes in make_DelayedTranspose.
Browse files Browse the repository at this point in the history
The function now directly returns the seed of the first DelayedTranspose, thus
eliminating two unnecessary layers of redirection.
LTLA committed Dec 18, 2024
1 parent c11653e commit 92b8083
Showing 2 changed files with 37 additions and 4 deletions.
26 changes: 22 additions & 4 deletions include/tatami/other/DelayedTranspose.hpp
Original file line number Diff line number Diff line change
@@ -67,6 +67,16 @@ class DelayedTranspose : public Matrix<Value_, Index_> {

using Matrix<Value_, Index_>::sparse;

/**
* @cond
*/
const std::shared_ptr<const Matrix<Value_, Index_> >& seed() const {
return my_matrix;
}
/**
* @endcond
*/

/********************
*** Myopic dense ***
********************/
@@ -133,26 +143,34 @@ class DelayedTranspose : public Matrix<Value_, Index_> {
};

/**
* A `make_*` helper function to enable partial template deduction of supplied types during delayed transposition.
* A `make_*` helper function for delayed transposition.
* This will typically return a `DelayedTranspose` instance wrapping `matrix`.
* However, if `matrix` was already a `DelayedTranspose`, then the two delayed transpositions cancel out;
* the underlying seed is returned directly, eliminating two unnecessary operations.
*
* @tparam Value_ Type of matrix value.
* @tparam Index_ Type of index value.
*
* @param matrix Pointer to a (possibly `const`) `Matrix` instance.
*
* @return A pointer to a `DelayedTranspose` instance.
* @return A pointer to a `Matrix` representing the transposition of `matrix`.
*/
template<typename Value_, typename Index_>
std::shared_ptr<Matrix<Value_, Index_> > make_DelayedTranspose(std::shared_ptr<const Matrix<Value_, Index_> > matrix) {
return std::shared_ptr<Matrix<Value_, Index_> >(new DelayedTranspose<Value_, Index_>(std::move(matrix)));
auto tptr = dynamic_cast<const DelayedTranspose<Value_, Index_>*>(matrix.get());
if (tptr == NULL) {
return std::shared_ptr<Matrix<Value_, Index_> >(new DelayedTranspose<Value_, Index_>(std::move(matrix)));
} else {
return std::const_pointer_cast<Matrix<Value_, Index_> >(tptr->seed()); // TODO: jesus christ, get rid of this.
}
}

/**
* @cond
*/
template<typename Value_, typename Index_>
std::shared_ptr<Matrix<Value_, Index_> > make_DelayedTranspose(std::shared_ptr<Matrix<Value_, Index_> > matrix) {
return std::shared_ptr<Matrix<Value_, Index_> >(new DelayedTranspose<Value_, Index_>(std::move(matrix)));
return make_DelayedTranspose(std::shared_ptr<const Matrix<Value_, Index_> >(std::move(matrix)));
}
/**
* @endcond
15 changes: 15 additions & 0 deletions tests/src/other/DelayedTranspose.cpp
Original file line number Diff line number Diff line change
@@ -76,6 +76,21 @@ TEST_F(TransposeTest, ConstOverload) {
EXPECT_EQ(dense->ncol(), tdense->nrow());
}

TEST_F(TransposeTest, Simplified) {
auto stacked = tatami::make_DelayedTranspose(tdense);
EXPECT_EQ(stacked->nrow(), nrow);
EXPECT_EQ(stacked->ncol(), ncol);

// This should collapse to the seed class.
auto ptr = dynamic_cast<tatami::DelayedTranspose<double, int>*>(stacked.get());
EXPECT_TRUE(ptr == NULL);
auto ref = dynamic_cast<tatami::DelayedTranspose<double, int>*>(tdense.get());
EXPECT_TRUE(ref != NULL); // as a control.

tatami_test::test_simple_row_access(*stacked, *dense);
tatami_test::test_simple_column_access(*stacked, *dense);
}

class TransposeFullTest :
public ::testing::TestWithParam<tatami_test::StandardTestAccessOptions>,
public TransposeUtils {

0 comments on commit 92b8083

Please sign in to comment.