Browse Source

Use utils/algorithm/container.hpp in more places

pull/6488/head
Gleb Mazovetskiy 3 years ago
parent
commit
ba4def9382
  1. 11
      Source/engine/render/scrollrt.cpp
  2. 3
      Source/error.cpp
  3. 2
      Source/monster.cpp
  4. 4
      Source/objects.cpp
  5. 20
      Source/options.cpp
  6. 3
      Source/qol/itemlabels.cpp
  7. 32
      Source/utils/algorithm/container.hpp

11
Source/engine/render/scrollrt.cpp

@ -1076,12 +1076,11 @@ void DrawGame(const Surface &fullOut, Point position, Displacement offset)
#ifdef DUN_RENDER_STATS
std::vector<std::pair<DunRenderType, size_t>> sortedStats(DunRenderStats.begin(), DunRenderStats.end());
std::sort(sortedStats.begin(), sortedStats.end(),
[](const std::pair<DunRenderType, size_t> &a, const std::pair<DunRenderType, size_t> &b) {
return a.first.maskType == b.first.maskType
? static_cast<uint8_t>(a.first.tileType) < static_cast<uint8_t>(b.first.tileType)
: static_cast<uint8_t>(a.first.maskType) < static_cast<uint8_t>(b.first.maskType);
});
c_sort(sortedStats, [](const std::pair<DunRenderType, size_t> &a, const std::pair<DunRenderType, size_t> &b) {
return a.first.maskType == b.first.maskType
? static_cast<uint8_t>(a.first.tileType) < static_cast<uint8_t>(b.first.tileType)
: static_cast<uint8_t>(a.first.maskType) < static_cast<uint8_t>(b.first.maskType);
});
Point pos { 100, 20 };
for (size_t i = 0; i < sortedStats.size(); ++i) {
const auto &stat = sortedStats[i];

3
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));

2
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)

4
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<int>(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<int>(index));
return;

20
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());

3
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)

32
Source/utils/algorithm/container.hpp

@ -56,6 +56,15 @@ bool c_none_of(const C &c, Predicate &&predicate)
std::forward<Predicate>(predicate));
}
template <typename C, typename T>
container_internal::Iterator<C>
c_find(C &c, T &&value)
{
return std::find(container_internal::begin(c),
container_internal::end(c),
std::forward<T>(value));
}
template <typename C, typename Predicate>
container_internal::Iterator<C>
c_find_if(C &c, Predicate &&predicate)
@ -83,4 +92,27 @@ c_count(const C &c, T &&value)
std::forward<T>(value));
}
template <typename C>
void c_sort(C &c)
{
std::sort(container_internal::c_begin(c),
container_internal::c_end(c));
}
template <typename C, typename LessThan>
void c_sort(C &c, LessThan &&comp)
{
std::sort(container_internal::c_begin(c),
container_internal::c_end(c),
std::forward<LessThan>(comp));
}
template <typename C, typename T>
container_internal::Iterator<C> c_lower_bound(C &c, T &&value)
{
return std::lower_bound(container_internal::c_begin(c),
container_internal::c_end(c),
std::forward<T>(value));
}
} // namespace devilution

Loading…
Cancel
Save