From b22fd8d3302e44f9304696d1d17bbc8928f5e139 Mon Sep 17 00:00:00 2001 From: Eduard Valeyev Date: Wed, 24 Jul 2024 13:59:24 -0400 Subject: [PATCH] ttg::matrix::Triplet is read-once by default --- examples/ttg_matrix.h | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/examples/ttg_matrix.h b/examples/ttg_matrix.h index ce563cb4a..e0d6cb3f6 100644 --- a/examples/ttg_matrix.h +++ b/examples/ttg_matrix.h @@ -16,10 +16,12 @@ namespace ttg { namespace matrix { + enum class ReadOnceTriplet { yes, no }; + /// element of a sparse matrix = {row index, col index, value} - /// movable replacement for Eigen::Triplet - template::StorageIndex > + /// move-capable replacement for Eigen::Triplet + template::StorageIndex > class Triplet { public: Triplet() = default; @@ -42,10 +44,15 @@ namespace ttg { const StorageIndex& col() const { return m_col; } /** \returns the value of the element */ - const Value& value() const { return m_value; } + std::conditional_t value() const { + if constexpr (ReadOnce == ReadOnceTriplet::yes) + return std::move(m_value); + else + return m_value; + } protected: StorageIndex m_row = -1, m_col = -1; - Value m_value; + mutable Value m_value; }; // matrix shape = maps {column,row} index to {row,column} indices