Skip to content
Snippets Groups Projects
Commit ac04676b authored by Stéphane Del Pino's avatar Stéphane Del Pino
Browse files

Add numberOfValues() and check that IndexType is valid

parent 0a158b16
Branches
Tags
1 merge request!26Feature/linear systems
...@@ -2,11 +2,16 @@ ...@@ -2,11 +2,16 @@
#define SPARSE_MATRIX_DESCRIPTOR_HPP #define SPARSE_MATRIX_DESCRIPTOR_HPP
#include <Array.hpp> #include <Array.hpp>
#include <map> #include <map>
#include <type_traits>
template <typename DataType = double, typename IndexType = size_t> template <typename DataType = double, typename IndexType = size_t>
class SparseMatrixDescriptor 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: public:
using data_type = DataType; using data_type = DataType;
using index_type = IndexType; using index_type = IndexType;
...@@ -17,6 +22,12 @@ class SparseMatrixDescriptor ...@@ -17,6 +22,12 @@ class SparseMatrixDescriptor
std::map<IndexType, DataType> m_id_value_map; std::map<IndexType, DataType> m_id_value_map;
public: public:
IndexType
numberOfValues() const noexcept
{
return m_id_value_map.size();
}
const DataType& operator[](const IndexType& j) const const DataType& operator[](const IndexType& j) const
{ {
auto i_value = m_id_value_map.find(j); auto i_value = m_id_value_map.find(j);
...@@ -52,6 +63,13 @@ class SparseMatrixDescriptor ...@@ -52,6 +63,13 @@ class SparseMatrixDescriptor
Array<SparseRowDescriptor> m_row_array; Array<SparseRowDescriptor> m_row_array;
public: public:
PUGS_INLINE
size_t
numberOfRows() const noexcept
{
return m_row_array.size();
}
SparseRowDescriptor& SparseRowDescriptor&
row(const IndexType i) row(const IndexType i)
{ {
...@@ -73,7 +91,8 @@ class SparseMatrixDescriptor ...@@ -73,7 +91,8 @@ class SparseMatrixDescriptor
const DataType& const DataType&
operator()(const IndexType& i, const IndexType& j) const 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& friend std::ostream&
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment