diff --git a/simulation/simulationstateserializer.cpp b/simulation/simulationstateserializer.cpp index 33469c9f2..ec445700d 100644 --- a/simulation/simulationstateserializer.cpp +++ b/simulation/simulationstateserializer.cpp @@ -341,9 +341,12 @@ state_serializer::deserialize_ordered( deserializer_state &State, std::chrono::s auto const end { index + 1 < statements.size() ? statements[ index + 1 ].offset : State.tape.size() - 1 }; State.replayer.seek( static_cast( item.offset ), static_cast( end ), item.start, State.tapefiles ); restore_transformations( State.scratchpad.location, State.replaytable.contexts[ item.context ] ); + // a repeated name goes to the item of the later statement in the file, whatever the order they load in (D4) + table_statement_order() = index; if( auto const token { State.input.getToken() }; false == token.empty() ) { deserialize_statement( State, token ); } + table_statement_order().reset(); if( std::chrono::duration_cast( std::chrono::steady_clock::now() - Started ).count() >= 200 ) { Application.set_progress( static_cast( 100 * State.ordercursor / State.order.size() ), static_cast( 100 * State.ordercursor / State.order.size() ) ); return true; diff --git a/tests/unit/test_names_order.cpp b/tests/unit/test_names_order.cpp new file mode 100644 index 000000000..917600a87 --- /dev/null +++ b/tests/unit/test_names_order.cpp @@ -0,0 +1,43 @@ +#include "stdafx.h" +#include "doctest.h" +#include "utilities/Names.h" + +#include + +// the table of names: a repeated name goes to the later item, and while the statements of a scenery load out of the order of the file, +// to the item of the later statement (docs/world-format-eu07.md, D4) + +namespace { + +struct named { + std::string label; + std::string const &name() const { return label; } +}; + +} // namespace + +TEST_CASE("a repeated name goes to the item inserted later, as it always did") { + basic_table table; + auto *first{new named{"tor1"}}; + auto *second{new named{"tor1"}}; + CHECK(table.insert(first)); + CHECK_FALSE(table.insert(second)); + CHECK(table.find("tor1") == second); +} + +TEST_CASE("with the statements loading out of order, a repeated name goes to the item of the later statement") { + basic_table table; + auto *later{new named{"tor1"}}; + auto *earlier{new named{"tor1"}}; + // the statement 20 of the file loads first, the statement 5 after it + table_statement_order() = 20; + CHECK(table.insert(later)); + table_statement_order() = 5; + CHECK_FALSE(table.insert(earlier)); + table_statement_order().reset(); + CHECK(table.find("tor1") == later); + // an item added after the load, at no statement, is the newest one + auto *added{new named{"tor1"}}; + CHECK_FALSE(table.insert(added)); + CHECK(table.find("tor1") == added); +} diff --git a/utilities/Names.h b/utilities/Names.h index 52d438f6b..cfa09179f 100644 --- a/utilities/Names.h +++ b/utilities/Names.h @@ -12,6 +12,18 @@ http://mozilla.org/MPL/2.0/. #include #include #include +#include +#include +#include + +// the place in the scenery file of the statement being loaded, while the statements of a scenery load in another order than the +// file's (docs/world-format-eu07.md, D4): a repeated name then goes to the item of the later statement, as a load in the order of the +// file gives it, whatever order the statements load in. none otherwise: the item inserted later takes the name, as it always did +inline std::optional & +table_statement_order() { + static std::optional order; + return order; +} template class basic_table { @@ -27,6 +39,11 @@ public: bool insert( Type_ *Item, std::string itemname ) { m_items.emplace_back( Item ); + // the order of the item: its statement in the file while statements load out of order, after every item before otherwise + auto const order { table_statement_order().value_or( m_lastorder + 1 ) }; + m_lastorder = std::max( m_lastorder, order ); + m_orders.resize( m_items.size(), 0 ); + m_orders.back() = order; if( true == itemname.empty() || itemname == "none" ) { return true; } @@ -36,8 +53,11 @@ public: if( true == mapping.second ) { return true; } - // item with this name already exists; update mapping to point to the new one, for backward compatibility - mapping.first->second = itemhandle; + // item with this name already exists; update mapping to point to the later one, for backward compatibility. with the + // statements loading in the order of the file the later one is the one inserted now, as it always was + if( order >= m_orders[ mapping.first->second ] ) { + mapping.first->second = itemhandle; + } return false; } bool insert (Type_ *Item) { @@ -117,6 +137,9 @@ protected: // members type_sequence m_items; index_map m_itemmap; + // the order of each item, and the latest one, see insert() + std::vector m_orders; + std::uint64_t m_lastorder { 0 }; public: // data access