Fix loading order in EsmLoader

Need to load the last present record from a sequence of loaded records. That
means reverse should be called before unique or unique should be applied for
a reversed range. Since unique keeps only the first element from a sub
sequence of equal elements.

Use forEachUnique with reversed range to avoid redundant container
modifications.
This commit is contained in:
elsid 2022-04-12 17:27:59 +02:00
parent d3d9abede4
commit 194c11f214
No known key found for this signature in database
GPG key ID: D27B8E8D10A2896B
3 changed files with 110 additions and 3 deletions

View file

@ -2,6 +2,7 @@
#define OPENMW_COMPONENTS_ESMLOADER_RECORD_H
#include <components/esm3/loadcell.hpp>
#include <components/misc/algorithm.hpp>
#include <algorithm>
#include <utility>
@ -31,12 +32,12 @@ namespace EsmLoader
const auto greaterByKey = [&] (const auto& l, const auto& r) { return getKey(r) < getKey(l); };
const auto equalByKey = [&] (const auto& l, const auto& r) { return getKey(l) == getKey(r); };
std::stable_sort(records.begin(), records.end(), greaterByKey);
records.erase(std::unique(records.begin(), records.end(), equalByKey), records.end());
std::reverse(records.begin(), records.end());
std::vector<T> result;
for (Record<T>& v : records)
Misc::forEachUnique(records.rbegin(), records.rend(), equalByKey, [&] (const auto& v)
{
if (!v.mDeleted)
result.emplace_back(std::move(v.mValue));
});
return result;
}
}