Skip to content

Commit

Permalink
ttg::matrix::Triplet is read-once by default
Browse files Browse the repository at this point in the history
  • Loading branch information
evaleev committed Jul 24, 2024
1 parent e620b53 commit b22fd8d
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions examples/ttg_matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename Value, typename StorageIndex=typename Eigen::SparseMatrix<Value>::StorageIndex >
/// move-capable replacement for Eigen::Triplet
template<typename Value, ReadOnceTriplet ReadOnce = ReadOnceTriplet::yes, typename StorageIndex=typename Eigen::SparseMatrix<Value>::StorageIndex >
class Triplet {
public:
Triplet() = default;
Expand All @@ -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<ReadOnce == ReadOnceTriplet::yes, Value&&, const Value&> 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
Expand Down

0 comments on commit b22fd8d

Please sign in to comment.