diff options
author | Shauren <shauren.trinity@gmail.com> | 2025-07-27 14:22:54 +0200 |
---|---|---|
committer | Shauren <shauren.trinity@gmail.com> | 2025-07-27 14:22:54 +0200 |
commit | a59dada96caca2aa95eb723b9d7d58dab1e072de (patch) | |
tree | f3a54519fab3c880fcd31aeddca387d26d7fa6f6 | |
parent | c850e2f7801fecd9a56d88dda1fc60e27fd30bdf (diff) |
Core/Commands: Remove temporary set with custom comparator from .debug objectcount command
-rw-r--r-- | src/server/scripts/Commands/cs_debug.cpp | 43 |
1 files changed, 23 insertions, 20 deletions
diff --git a/src/server/scripts/Commands/cs_debug.cpp b/src/server/scripts/Commands/cs_debug.cpp index 6f1d61e451e..0b9f9599e74 100644 --- a/src/server/scripts/Commands/cs_debug.cpp +++ b/src/server/scripts/Commands/cs_debug.cpp @@ -1716,37 +1716,40 @@ public: class CreatureCountWorker { public: - CreatureCountWorker() { } - - void Visit(std::unordered_map<ObjectGuid, Creature*>& creatureMap) + void Visit(std::unordered_map<ObjectGuid, Creature*> const& creatureMap) { - for (auto const& p : creatureMap) - { - uint32& count = creatureIds[p.second->GetEntry()]; - ++count; - } + for (auto const& [_, creature] : creatureMap) + ++creatureCountsById[creature->GetEntry()]; } template<class T> - void Visit(std::unordered_map<ObjectGuid, T*>&) { } + static void Visit(std::unordered_map<ObjectGuid, T*> const&) { } - std::vector<std::pair<uint32, uint32>> GetTopCreatureCount(uint32 count) + std::vector<std::pair<uint32, uint32>> GetTopCreatureCount(std::size_t count) const { - auto comp = [](std::pair<uint32, uint32> const& a, std::pair<uint32, uint32> const& b) + count = std::min(count, creatureCountsById.size()); + std::vector<std::pair<uint32, uint32>> result; + if (!count) + return result; + + result.reserve(count + 1); + + for (auto const& [creatureId, creatureCount] : creatureCountsById) { - return a.second > b.second; - }; - std::set<std::pair<uint32, uint32>, decltype(comp)> set(creatureIds.begin(), creatureIds.end(), comp); + if (result.size() >= count && result.back().second > creatureCount) + continue; - count = std::min(count, uint32(set.size())); - std::vector<std::pair<uint32, uint32>> result(count); - std::copy_n(set.begin(), count, result.begin()); + auto where = std::ranges::lower_bound(result, creatureCount, std::ranges::greater(), Trinity::TupleElement<1>); + result.emplace(where, creatureId, creatureCount); + if (result.size() > count) + result.pop_back(); + } return result; } private: - std::unordered_map<uint32, uint32> creatureIds; + std::unordered_map<uint32, uint32> creatureCountsById; }; static void HandleDebugObjectCountMap(ChatHandler* handler, Map* map) @@ -1763,8 +1766,8 @@ public: handler->PSendSysMessage("Top Creatures count:"); - for (auto&& p : worker.GetTopCreatureCount(5)) - handler->PSendSysMessage("Entry: %u Count: %u", p.first, p.second); + for (auto const& [creatureId, count] : worker.GetTopCreatureCount(5)) + handler->PSendSysMessage("Entry: %u Count: %u", creatureId, count); } static bool HandleDebugBecomePersonalClone(ChatHandler* handler) |