add possibility to iterate over SparseVector data at runtime (#143)

This commit is contained in:
kritz 2020-08-09 09:44:49 +02:00 committed by GitHub
parent e714a28c83
commit f981cea2ae
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 1 deletions

View File

@ -60,7 +60,7 @@ private:
}
public:
static constexpr size_t non_zeros() {
constexpr size_t non_zeros() const {
return N;
}
@ -94,6 +94,16 @@ public:
return _data[compressed_index];
}
inline Type atCompressedIndex(size_t i) const {
assert(i < N);
return _data[i];
}
inline Type& atCompressedIndex(size_t i) {
assert(i < N);
return _data[i];
}
void setZero() {
for (size_t i = 0; i < N; i++) {
_data[i] = Type(0);

View File

@ -31,6 +31,16 @@ TEST(sparseVectorTest, initialisationFromVector) {
EXPECT_FLOAT_EQ(a.at<2>(), vec(2));
}
TEST(sparseVectorTest, accessDataWithCompressedIndices) {
const Vector3f vec(1.f, 2.f, 3.f);
SparseVectorf<3, 0, 2> a(vec);
for (size_t i = 0; i < a.non_zeros(); i++) {
a.atCompressedIndex(i) = static_cast<float>(i);
}
EXPECT_FLOAT_EQ(a.at<0>(), a.atCompressedIndex(0));
EXPECT_FLOAT_EQ(a.at<2>(), a.atCompressedIndex(1));
}
TEST(sparseVectorTest, setZero) {
const float data[3] = {1.f, 2.f, 3.f};
SparseVectorf<24, 4, 6, 22> a(data);