From a59dada96caca2aa95eb723b9d7d58dab1e072de Mon Sep 17 00:00:00 2001 From: Shauren Date: Sun, 27 Jul 2025 14:22:54 +0200 Subject: Core/Commands: Remove temporary set with custom comparator from .debug objectcount command --- src/server/scripts/Commands/cs_debug.cpp | 43 +++++++++++++++++--------------- 1 file changed, 23 insertions(+), 20 deletions(-) (limited to 'src/server/scripts') 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& creatureMap) + void Visit(std::unordered_map const& creatureMap) { - for (auto const& p : creatureMap) - { - uint32& count = creatureIds[p.second->GetEntry()]; - ++count; - } + for (auto const& [_, creature] : creatureMap) + ++creatureCountsById[creature->GetEntry()]; } template - void Visit(std::unordered_map&) { } + static void Visit(std::unordered_map const&) { } - std::vector> GetTopCreatureCount(uint32 count) + std::vector> GetTopCreatureCount(std::size_t count) const { - auto comp = [](std::pair const& a, std::pair const& b) + count = std::min(count, creatureCountsById.size()); + std::vector> result; + if (!count) + return result; + + result.reserve(count + 1); + + for (auto const& [creatureId, creatureCount] : creatureCountsById) { - return a.second > b.second; - }; - std::set, 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> 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 creatureIds; + std::unordered_map 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) -- cgit v1.2.3