mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-05-02 05:04:08 +08:00
add possibility to iterate over SparseVector data at runtime (#143)
This commit is contained in:
parent
e714a28c83
commit
f981cea2ae
@ -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);
|
||||
|
||||
@ -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);
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user