From e396a2b7f695fe7af02743dec26eeaafce21e5d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20BRIOL?= Date: Mon, 2 Dec 2024 15:47:00 +0100 Subject: [PATCH] Fix bug: incorrect mapping between waves and their indices. --- include/fes/wave/table.hpp | 11 ++++------- src/library/wave/table.cpp | 6 ++++-- tests/library/wave/table.cpp | 13 +++++++++++-- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/include/fes/wave/table.hpp b/include/fes/wave/table.hpp index 2aae8bf..31ca1a8 100644 --- a/include/fes/wave/table.hpp +++ b/include/fes/wave/table.hpp @@ -146,10 +146,10 @@ class Table { /// Get the wave properties from its index inline auto operator[](const size_t index) const -> const std::shared_ptr& { - if (index >= wave_identifiers_.size()) { + if (index >= wave_index_.size()) { throw std::out_of_range("index out of range"); } - return waves_[static_cast(wave_identifiers_[index])]; + return waves_[static_cast(wave_index_[index])]; } /// Get the wave properties from its identifier @@ -197,10 +197,7 @@ class Table { } /// Returns the size of the table - inline auto size() const -> size_t { - return std::count_if(waves_.begin(), waves_.end(), - [](auto& item) { return item != nullptr; }); - } + inline auto size() const -> size_t { return wave_index_.size(); } /// Searches the properties of a wave from its name. /// @param[in] name Name of the wave @@ -335,7 +332,7 @@ class Table { /// An array that maps linear indices (0, 1, 2, 3, ...) to the wave /// identifiers defined in the table. If the table is complete, this mapping /// is an identity mapping {0:0, 1:1, 2:2, ...}. - std::vector wave_identifiers_{}; + std::vector wave_index_{}; /// Get a wave from the table /// diff --git a/src/library/wave/table.cpp b/src/library/wave/table.cpp index 4587845..3c4536c 100644 --- a/src/library/wave/table.cpp +++ b/src/library/wave/table.cpp @@ -207,11 +207,13 @@ Table::Table(const std::vector& waves) { size() == waves_.size() ? &Table::direct_access : &Table::sparse_access; // Fill the index between to have a direct access to the wave - wave_identifiers_.reserve(waves_.size()); + wave_index_.reserve(waves_.size()); + uint8_t index = 0; for (const auto& item : waves_) { if (item) { - wave_identifiers_.emplace_back(item->ident()); + wave_index_.emplace_back(index); } + ++index; } } diff --git a/tests/library/wave/table.cpp b/tests/library/wave/table.cpp index 7ad6e15..4e8de42 100644 --- a/tests/library/wave/table.cpp +++ b/tests/library/wave/table.cpp @@ -457,9 +457,18 @@ TEST(WaveTable, IdentName) { TEST(WaveTable, Sparse) { auto table = - fes::wave::Table({"O1", "K1", "M2", "S2", "N2", "K2", "M4", "M6"}); - EXPECT_EQ(table.size(), 8); + fes::wave::Table({"O1", "K1", "M2", "S2", "N2", "K2", "M4", "M6", "Mf2"}); + EXPECT_EQ(table.size(), 9); EXPECT_EQ(table[fes::kO1]->ident(), fes::kO1); + EXPECT_EQ(table[0]->ident(), fes::kO1); + EXPECT_EQ(table[1]->ident(), fes::kK1); + EXPECT_EQ(table[2]->ident(), fes::kN2); + EXPECT_EQ(table[3]->ident(), fes::kM2); + EXPECT_EQ(table[4]->ident(), fes::kS2); + EXPECT_EQ(table[5]->ident(), fes::kK2); + EXPECT_EQ(table[6]->ident(), fes::kM4); + EXPECT_EQ(table[7]->ident(), fes::kM6); + EXPECT_EQ(table[8]->ident(), fes::kMf2); EXPECT_THROW(table[fes::kP1], std::out_of_range); EXPECT_THROW(table.admittance(), std::out_of_range); EXPECT_THROW(