From ba4def93826dd31fbf79f2c2b6ef03957a75d034 Mon Sep 17 00:00:00 2001 From: Gleb Mazovetskiy Date: Sat, 19 Aug 2023 19:32:16 +0900 Subject: [PATCH] Use utils/algorithm/container.hpp in more places --- Source/engine/render/scrollrt.cpp | 11 +++++----- Source/error.cpp | 3 ++- Source/monster.cpp | 2 +- Source/objects.cpp | 4 ++-- Source/options.cpp | 20 ++++++++--------- Source/qol/itemlabels.cpp | 3 ++- Source/utils/algorithm/container.hpp | 32 ++++++++++++++++++++++++++++ 7 files changed, 53 insertions(+), 22 deletions(-) diff --git a/Source/engine/render/scrollrt.cpp b/Source/engine/render/scrollrt.cpp index 82dee7fc8..fc647face 100644 --- a/Source/engine/render/scrollrt.cpp +++ b/Source/engine/render/scrollrt.cpp @@ -1076,12 +1076,11 @@ void DrawGame(const Surface &fullOut, Point position, Displacement offset) #ifdef DUN_RENDER_STATS std::vector> sortedStats(DunRenderStats.begin(), DunRenderStats.end()); - std::sort(sortedStats.begin(), sortedStats.end(), - [](const std::pair &a, const std::pair &b) { - return a.first.maskType == b.first.maskType - ? static_cast(a.first.tileType) < static_cast(b.first.tileType) - : static_cast(a.first.maskType) < static_cast(b.first.maskType); - }); + c_sort(sortedStats, [](const std::pair &a, const std::pair &b) { + return a.first.maskType == b.first.maskType + ? static_cast(a.first.tileType) < static_cast(b.first.tileType) + : static_cast(a.first.maskType) < static_cast(b.first.maskType); + }); Point pos { 100, 20 }; for (size_t i = 0; i < sortedStats.size(); ++i) { const auto &stat = sortedStats[i]; diff --git a/Source/error.cpp b/Source/error.cpp index f46fb9f42..c3ac72abc 100644 --- a/Source/error.cpp +++ b/Source/error.cpp @@ -14,6 +14,7 @@ #include "engine/render/text_render.hpp" #include "panels/info_box.hpp" #include "stores.h" +#include "utils/algorithm/container.hpp" #include "utils/language.h" namespace devilution { @@ -117,7 +118,7 @@ void InitDiabloMsg(std::string_view msg) if (DiabloMessages.size() >= MAX_SEND_STR_LEN) return; - if (std::find(DiabloMessages.begin(), DiabloMessages.end(), msg) != DiabloMessages.end()) + if (c_find(DiabloMessages, msg) != DiabloMessages.end()) return; DiabloMessages.push_back(std::string(msg)); diff --git a/Source/monster.cpp b/Source/monster.cpp index 60320bd2f..90fcb9d2f 100644 --- a/Source/monster.cpp +++ b/Source/monster.cpp @@ -4451,7 +4451,7 @@ bool IsTileAvailable(const Monster &monster, Point position) bool IsSkel(_monster_id mt) { - return std::find(std::begin(SkeletonTypes), std::end(SkeletonTypes), mt) != std::end(SkeletonTypes); + return c_find(SkeletonTypes, mt) != SkeletonTypes.end(); } bool IsGoat(_monster_id mt) diff --git a/Source/objects.cpp b/Source/objects.cpp index 1192eafaa..b94508343 100644 --- a/Source/objects.cpp +++ b/Source/objects.cpp @@ -678,7 +678,7 @@ void SetupObject(Object &object, Point position, _object_id ot) object.position = position; if (!HeadlessMode) { - const auto &found = std::find(std::begin(ObjFileList), std::end(ObjFileList), ofi); + const auto &found = c_find(ObjFileList, ofi); if (found == std::end(ObjFileList)) { LogCritical("Unable to find object_graphic_id {} in list of objects to load, level generation error.", static_cast(ofi)); return; @@ -4712,7 +4712,7 @@ void SyncObjectAnim(Object &object) object_graphic_id index = AllObjects[object._otype].ofindex; if (!HeadlessMode) { - const auto &found = std::find(std::begin(ObjFileList), std::end(ObjFileList), index); + const auto &found = c_find(ObjFileList, index); if (found == std::end(ObjFileList)) { LogCritical("Unable to find object_graphic_id {} in list of objects to load, level generation error.", static_cast(index)); return; diff --git a/Source/options.cpp b/Source/options.cpp index 7c8794a76..ea3a0b301 100644 --- a/Source/options.cpp +++ b/Source/options.cpp @@ -494,7 +494,7 @@ std::string_view OptionEntryEnumBase::GetListDescription(size_t index) const } size_t OptionEntryEnumBase::GetActiveListIndex() const { - auto iterator = std::find(entryValues.begin(), entryValues.end(), value); + auto iterator = c_find(entryValues, value); if (iterator == entryValues.end()) return 0; return std::distance(entryValues.begin(), iterator); @@ -508,9 +508,8 @@ void OptionEntryEnumBase::SetActiveListIndex(size_t index) void OptionEntryIntBase::LoadFromIni(std::string_view category) { value = GetIniInt(category.data(), key.data(), defaultValue); - if (std::find(entryValues.begin(), entryValues.end(), value) == entryValues.end()) { - entryValues.push_back(value); - std::sort(entryValues.begin(), entryValues.end()); + if (c_find(entryValues, value) == entryValues.end()) { + entryValues.insert(c_lower_bound(entryValues, value), value); entryNames.clear(); } } @@ -542,7 +541,7 @@ std::string_view OptionEntryIntBase::GetListDescription(size_t index) const } size_t OptionEntryIntBase::GetActiveListIndex() const { - auto iterator = std::find(entryValues.begin(), entryValues.end(), value); + auto iterator = c_find(entryValues, value); if (iterator == entryValues.end()) return 0; return std::distance(entryValues.begin(), iterator); @@ -775,12 +774,11 @@ void OptionEntryResolution::CheckResolutionsAreInitialized() const #endif // Sort by width then by height - std::sort(sizes.begin(), sizes.end(), - [](const Size &x, const Size &y) -> bool { - if (x.width == y.width) - return x.height > y.height; - return x.width > y.width; - }); + c_sort(sizes, [](const Size &x, const Size &y) -> bool { + if (x.width == y.width) + return x.height > y.height; + return x.width > y.width; + }); // Remove duplicate entries sizes.erase(std::unique(sizes.begin(), sizes.end()), sizes.end()); diff --git a/Source/qol/itemlabels.cpp b/Source/qol/itemlabels.cpp index 330331d2e..231c1ba2c 100644 --- a/Source/qol/itemlabels.cpp +++ b/Source/qol/itemlabels.cpp @@ -18,6 +18,7 @@ #include "options.h" #include "qol/stash.h" #include "stores.h" +#include "utils/algorithm/container.hpp" #include "utils/format_int.hpp" #include "utils/language.h" @@ -50,7 +51,7 @@ class UsedX { public: [[nodiscard]] bool contains(int val) const { - return std::find(data_.begin(), data_.end(), val) != data_.end(); + return c_find(data_, val) != data_.end(); } void insert(int val) diff --git a/Source/utils/algorithm/container.hpp b/Source/utils/algorithm/container.hpp index 03ff2a7af..9d9212e98 100644 --- a/Source/utils/algorithm/container.hpp +++ b/Source/utils/algorithm/container.hpp @@ -56,6 +56,15 @@ bool c_none_of(const C &c, Predicate &&predicate) std::forward(predicate)); } +template +container_internal::Iterator +c_find(C &c, T &&value) +{ + return std::find(container_internal::begin(c), + container_internal::end(c), + std::forward(value)); +} + template container_internal::Iterator c_find_if(C &c, Predicate &&predicate) @@ -83,4 +92,27 @@ c_count(const C &c, T &&value) std::forward(value)); } +template +void c_sort(C &c) +{ + std::sort(container_internal::c_begin(c), + container_internal::c_end(c)); +} + +template +void c_sort(C &c, LessThan &&comp) +{ + std::sort(container_internal::c_begin(c), + container_internal::c_end(c), + std::forward(comp)); +} + +template +container_internal::Iterator c_lower_bound(C &c, T &&value) +{ + return std::lower_bound(container_internal::c_begin(c), + container_internal::c_end(c), + std::forward(value)); +} + } // namespace devilution