From 00de540a31f4bf64c0302b9b2b77c1666dc28612 Mon Sep 17 00:00:00 2001 From: fredzio Date: Wed, 5 May 2021 20:27:22 +0200 Subject: [PATCH 1/2] Remove unused function. --- apps/openmw/mwworld/store.cpp | 11 ----------- apps/openmw/mwworld/store.hpp | 4 ---- 2 files changed, 15 deletions(-) diff --git a/apps/openmw/mwworld/store.cpp b/apps/openmw/mwworld/store.cpp index 9d6552106..94795b841 100644 --- a/apps/openmw/mwworld/store.cpp +++ b/apps/openmw/mwworld/store.cpp @@ -186,17 +186,6 @@ namespace MWWorld return ptr; } template - const T *Store::findRandom(const std::string &id) const - { - const T *ptr = searchRandom(id); - if(ptr == nullptr) - { - const std::string msg = T::getRecordType() + " starting with '" + id + "' not found"; - throw std::runtime_error(msg); - } - return ptr; - } - template RecordId Store::load(ESM::ESMReader &esm) { T record; diff --git a/apps/openmw/mwworld/store.hpp b/apps/openmw/mwworld/store.hpp index 9cb1c7473..e37152431 100644 --- a/apps/openmw/mwworld/store.hpp +++ b/apps/openmw/mwworld/store.hpp @@ -179,10 +179,6 @@ namespace MWWorld const T *find(const std::string &id) const; - /** Returns a random record that starts with the named ID. An exception is thrown if none - * are found. */ - const T *findRandom(const std::string &id) const; - iterator begin() const; iterator end() const; From e99b61d362d6a9802c81a4427f7d10d6ef56f091 Mon Sep 17 00:00:00 2001 From: fredzio Date: Wed, 5 May 2021 20:28:46 +0200 Subject: [PATCH 2/2] Simplify the code --- apps/openmw/mwworld/store.cpp | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/apps/openmw/mwworld/store.cpp b/apps/openmw/mwworld/store.cpp index 94795b841..fb66b0a1d 100644 --- a/apps/openmw/mwworld/store.cpp +++ b/apps/openmw/mwworld/store.cpp @@ -8,28 +8,11 @@ #include #include +#include #include namespace { - template - class GetRecords - { - const std::string mFind; - std::vector *mRecords; - - public: - GetRecords(const std::string &str, std::vector *records) - : mFind(Misc::StringUtils::lowerCase(str)), mRecords(records) - { } - - void operator()(const T *item) - { - if(Misc::StringUtils::ciCompareLen(mFind, item->mId, mFind.size()) == 0) - mRecords->push_back(item); - } - }; - struct Compare { bool operator()(const ESM::Land *x, const ESM::Land *y) { @@ -169,7 +152,11 @@ namespace MWWorld const T *Store::searchRandom(const std::string &id) const { std::vector results; - std::for_each(mShared.begin(), mShared.end(), GetRecords(id, &results)); + std::copy_if(mShared.begin(), mShared.end(), std::back_inserter(results), + [&id](const T* item) + { + return Misc::StringUtils::ciCompareLen(id, item->mId, id.size()) == 0; + }); if(!results.empty()) return results[Misc::Rng::rollDice(results.size())]; return nullptr;