From ac04676bc45f7f2813adfbc90320b7075d759bf5 Mon Sep 17 00:00:00 2001 From: Stephane Del Pino <stephane.delpino44@gmail.com> Date: Fri, 2 Aug 2019 14:07:17 +0200 Subject: [PATCH] Add numberOfValues() and check that IndexType is valid --- src/algebra/SparseMatrixDescriptor.hpp | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/algebra/SparseMatrixDescriptor.hpp b/src/algebra/SparseMatrixDescriptor.hpp index a36121aff..37b82d0b1 100644 --- a/src/algebra/SparseMatrixDescriptor.hpp +++ b/src/algebra/SparseMatrixDescriptor.hpp @@ -2,11 +2,16 @@ #define SPARSE_MATRIX_DESCRIPTOR_HPP #include <Array.hpp> + #include <map> +#include <type_traits> template <typename DataType = double, typename IndexType = size_t> class SparseMatrixDescriptor { + static_assert(std::is_integral_v<IndexType>, "Index type must be an integral type"); + static_assert(std::is_unsigned_v<IndexType>, "Index type must be unsigned"); + public: using data_type = DataType; using index_type = IndexType; @@ -17,6 +22,12 @@ class SparseMatrixDescriptor std::map<IndexType, DataType> m_id_value_map; public: + IndexType + numberOfValues() const noexcept + { + return m_id_value_map.size(); + } + const DataType& operator[](const IndexType& j) const { auto i_value = m_id_value_map.find(j); @@ -52,6 +63,13 @@ class SparseMatrixDescriptor Array<SparseRowDescriptor> m_row_array; public: + PUGS_INLINE + size_t + numberOfRows() const noexcept + { + return m_row_array.size(); + } + SparseRowDescriptor& row(const IndexType i) { @@ -73,7 +91,8 @@ class SparseMatrixDescriptor const DataType& operator()(const IndexType& i, const IndexType& j) const { - return m_row_array[i][j]; + const auto& r = m_row_array[i]; // split to ensure const-ness of call + return r[j]; } friend std::ostream& -- GitLab