Skip to content

Commit

Permalink
Fix bug: incorrect mapping between waves and their indices.
Browse files Browse the repository at this point in the history
  • Loading branch information
fbriol committed Dec 2, 2024
1 parent 72c346b commit e396a2b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
11 changes: 4 additions & 7 deletions include/fes/wave/table.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<Wave>& {
if (index >= wave_identifiers_.size()) {
if (index >= wave_index_.size()) {
throw std::out_of_range("index out of range");
}
return waves_[static_cast<size_t>(wave_identifiers_[index])];
return waves_[static_cast<size_t>(wave_index_[index])];
}

/// Get the wave properties from its identifier
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<Constituent> wave_identifiers_{};
std::vector<uint8_t> wave_index_{};

/// Get a wave from the table
///
Expand Down
6 changes: 4 additions & 2 deletions src/library/wave/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,13 @@ Table::Table(const std::vector<std::string>& 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;
}
}

Expand Down
13 changes: 11 additions & 2 deletions tests/library/wave/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit e396a2b

Please sign in to comment.