diff --git a/matrix/SparseVector.hpp b/matrix/SparseVector.hpp index 3910130f64..ae7cc3a608 100644 --- a/matrix/SparseVector.hpp +++ b/matrix/SparseVector.hpp @@ -19,57 +19,59 @@ template struct force_constexpr_eval { // Vector that only store nonzero elements, // which indices are specified as parameter pack -template +template class SparseVector { private: static constexpr size_t N = sizeof...(Idxs); - static constexpr int _indices[N] {Idxs...}; + static constexpr size_t _indices[N] {Idxs...}; static constexpr bool duplicateIndices() { - for (int i = 0; i < N; i++) - for (int j = 0; j < i; j++) + for (size_t i = 0; i < N; i++) + for (size_t j = 0; j < i; j++) if (_indices[i] == _indices[j]) return true; return false; } - static constexpr int findMaxIndex() { - int maxIndex = -1; - for (int i = 0; i < N; i++) { + static constexpr size_t findMaxIndex() { + size_t maxIndex = 0; + for (size_t i = 0; i < N; i++) { if (maxIndex < _indices[i]) { maxIndex = _indices[i]; } } return maxIndex; } + static_assert(duplicateIndices() == false, "Duplicate indices"); static_assert(N < M, "More entries than elements, use a dense vector"); + static_assert(N > 0, "A sparse vector needs at least one element"); static_assert(findMaxIndex() < M, "Largest entry doesn't fit in sparse vector"); Type _data[N] {}; - static constexpr int findCompressedIndex(int index) { + static constexpr int findCompressedIndex(size_t index) { int compressedIndex = -1; - for (int i = 0; i < N; i++) { + for (size_t i = 0; i < N; i++) { if (index == _indices[i]) { - compressedIndex = i; + compressedIndex = static_cast(i); } } return compressedIndex; } public: - static constexpr int non_zeros() { + static constexpr size_t non_zeros() { return N; } - constexpr int index(int i) const { + constexpr size_t index(size_t i) const { return SparseVector::_indices[i]; } SparseVector() = default; SparseVector(const matrix::Vector& data) { - for (int i = 0; i < N; i++) { + for (size_t i = 0; i < N; i++) { _data[i] = data(_indices[i]); } } @@ -78,14 +80,14 @@ public: memcpy(_data, data, sizeof(_data)); } - template + template inline Type at() const { static constexpr int compressed_index = force_constexpr_eval::value; static_assert(compressed_index >= 0, "cannot access unpopulated indices"); return _data[compressed_index]; } - template + template inline Type& at() { static constexpr int compressed_index = force_constexpr_eval::value; static_assert(compressed_index >= 0, "cannot access unpopulated indices"); @@ -141,7 +143,7 @@ public: } }; -template +template matrix::Vector operator*(const matrix::Matrix& mat, const matrix::SparseVector& vec) { matrix::Vector res; for (size_t i = 0; i < Q; i++) { @@ -151,10 +153,10 @@ matrix::Vector operator*(const matrix::Matrix& mat, const m return res; } -template -constexpr int SparseVector::_indices[SparseVector::N]; +template +constexpr size_t SparseVector::_indices[SparseVector::N]; -template +template using SparseVectorf = SparseVector; }