From 16cc1d3eb9011bdf0cfe421cc32fb52d0a132c05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=B6eh=20Matt?= <5415177+ZehMatt@users.noreply.github.com> Date: Tue, 28 Feb 2023 05:09:15 +0200 Subject: [PATCH] Change loop for searching free staff id from O(N^2) to O(N*log(N)) --- src/openrct2/actions/StaffHireNewAction.cpp | 29 +++++++++------------ 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/src/openrct2/actions/StaffHireNewAction.cpp b/src/openrct2/actions/StaffHireNewAction.cpp index ef580fe771..10a979c03c 100644 --- a/src/openrct2/actions/StaffHireNewAction.cpp +++ b/src/openrct2/actions/StaffHireNewAction.cpp @@ -26,6 +26,8 @@ #include "../world/Entrance.h" #include "../world/Park.h" +#include + /* rct2: 0x009929FC */ static constexpr const PeepSpriteType spriteTypes[] = { PeepSpriteType::Handyman, @@ -142,25 +144,20 @@ GameActions::Result StaffHireNewAction::QueryExecute(bool execute) const newPeep->StaffOrders = _staffOrders; // We search for the first available Id for a given staff type - uint32_t newStaffId = 0; - for (;;) + std::set usedStaffIds; + + for (auto searchPeep : EntityList()) { - bool found = false; - ++newStaffId; - for (auto searchPeep : EntityList()) - { - if (static_cast(searchPeep->AssignedStaffType) != _staffType) - continue; + if (static_cast(searchPeep->AssignedStaffType) != _staffType) + continue; - if (searchPeep->PeepId == newStaffId) - { - found = true; - break; - } - } + usedStaffIds.insert(searchPeep->PeepId); + } - if (!found) - break; + uint32_t newStaffId = 1; + while (usedStaffIds.find(newStaffId) != usedStaffIds.end()) + { + newStaffId++; } newPeep->PeepId = newStaffId;