From 1d112134cf6227c76b63aeb3312bba61a56f236a Mon Sep 17 00:00:00 2001 From: Lioncash Date: Sun, 12 Apr 2020 14:00:31 -0400 Subject: [PATCH] CScriptPlayerHint: Make use of std::any_of/std::find_if where applicable Same behavior, can be simplified even further with ranges in the future. --- Runtime/World/CScriptPlayerHint.cpp | 30 +++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/Runtime/World/CScriptPlayerHint.cpp b/Runtime/World/CScriptPlayerHint.cpp index f369b0a12..a8733e28f 100644 --- a/Runtime/World/CScriptPlayerHint.cpp +++ b/Runtime/World/CScriptPlayerHint.cpp @@ -1,5 +1,7 @@ #include "Runtime/World/CScriptPlayerHint.hpp" +#include + #include "Runtime/CStateManager.hpp" #include "Runtime/MP1/World/CMetroidPrimeRelay.hpp" #include "Runtime/World/CActorParameters.hpp" @@ -19,20 +21,28 @@ CScriptPlayerHint::CScriptPlayerHint(TUniqueId uid, std::string_view name, const void CScriptPlayerHint::Accept(IVisitor& visit) { visit.Visit(this); } void CScriptPlayerHint::AddToObjectList(TUniqueId uid) { - for (TUniqueId existId : xe8_objectList) - if (uid == existId) - return; - if (xe8_objectList.size() != 8) - xe8_objectList.push_back(uid); + const bool inList = + std::any_of(xe8_objectList.cbegin(), xe8_objectList.cend(), [uid](const auto id) { return id == uid; }); + if (inList) { + return; + } + + if (xe8_objectList.size() == xe8_objectList.capacity()) { + return; + } + + xe8_objectList.push_back(uid); } void CScriptPlayerHint::RemoveFromObjectList(TUniqueId uid) { - for (auto it = xe8_objectList.begin(); it != xe8_objectList.end(); ++it) { - if (*it == uid) { - xe8_objectList.erase(it); - return; - } + const auto iter = + std::find_if(xe8_objectList.cbegin(), xe8_objectList.cend(), [uid](const auto id) { return id == uid; }); + + if (iter == xe8_objectList.cend()) { + return; } + + xe8_objectList.erase(iter); } void CScriptPlayerHint::AcceptScriptMsg(EScriptObjectMessage msg, TUniqueId sender, CStateManager& mgr) {