diff --git a/MissionEditor/IniFile.h b/MissionEditor/IniFile.h index 5a9ffe8..d1d576e 100644 --- a/MissionEditor/IniFile.h +++ b/MissionEditor/IniFile.h @@ -137,12 +137,13 @@ public: return this->FindValue(val) >= 0; } - size_t LowerBound(const CString& key) const { + // + std::pair LowerBound(const CString& key) const { auto const it = value_pos.lower_bound(key); if (it != value_pos.end()) { - return it->second; + return { it->second, it->first == key }; } - return value_pairs.size(); + return { value_pairs.size(), false }; } // ==================== Modify @@ -176,7 +177,7 @@ public: value_pairs.push_back({ key, value }); value_pos.insert_or_assign(key, value_pairs.size() - 1); } - + // not recommended to call it directly void InsertAt(size_t idx, CString&& key, CString&& value) { if (idx > value_pairs.size()) { idx = value_pairs.size() - 1; @@ -188,9 +189,13 @@ public: it->second++; } } - void Insert(CString&& key, CString&& value) { - auto const pos = LowerBound(key); - InsertAt(pos, std::move(key), std::move(value)); + void InsertOrAssign(CString&& key, CString&& value) { + auto const [pos, found] = LowerBound(key); + if (!found) { + InsertAt(pos, std::move(key), std::move(value)); + } + // existed, assign + value_pairs[pos].second = std::move(value); } // ==================== Delete diff --git a/MissionEditor/MapData.cpp b/MissionEditor/MapData.cpp index 80c0d9f..6700458 100644 --- a/MissionEditor/MapData.cpp +++ b/MissionEditor/MapData.cpp @@ -2060,7 +2060,7 @@ BOOL CMapData::AddWaypoint(CString id, DWORD dwPos) pSec = &m_mapfile.AddSection("Waypoints"); } - pSec->Insert(std::move(id), k); + pSec->InsertOrAssign(std::move(id), k); if (!m_noAutoObjectUpdate) { UpdateWaypoints(FALSE); diff --git a/UnitTest/CIni_Test.cpp b/UnitTest/CIni_Test.cpp index 1895cac..c2dfa04 100644 --- a/UnitTest/CIni_Test.cpp +++ b/UnitTest/CIni_Test.cpp @@ -260,15 +260,19 @@ TEST(CIniFileClass, IniLowerBoundInsertTest) { auto const pSec = file.TryGetSection("Waypoints"); EXPECT_NE(pSec, nullptr); auto const pos = pSec->LowerBound("4"); - EXPECT_LE(pos, pSec->Size()); - pSec->InsertAt(pos, "4", "432156"); + EXPECT_LE(pos.first, pSec->Size()); + // not existed + EXPECT_EQ(pos.second, false); + pSec->InsertAt(pos.first, "4", "432156"); EXPECT_EQ(432156, file.GetInteger("Waypoints", "4")); EXPECT_EQ("432156", file["Waypoints"].Nth(3).second); EXPECT_EQ("789654", file["Waypoints"].Nth(4).second); - pSec->Insert("9", "149367"); - pSec->Insert("11", "987654"); + pSec->InsertOrAssign("9", "149367"); + pSec->InsertOrAssign("11", "987654"); + pSec->InsertOrAssign("10", "159356"); // existed replace EXPECT_EQ(149367, file.GetInteger("Waypoints", "9")); + EXPECT_EQ(159356, file.GetInteger("Waypoints", "10")); EXPECT_EQ(987654, file.GetInteger("Waypoints", "11")); EXPECT_EQ("987654", file["Waypoints"].Nth(pSec->Size() - 1).second); - EXPECT_EQ("159357", file["Waypoints"].Nth(pSec->Size() - 2).second); + EXPECT_EQ("159356", file["Waypoints"].Nth(pSec->Size() - 2).second); } \ No newline at end of file