From 97dfe3cb736bdd58ed9a6d9b6017915830619dc6 Mon Sep 17 00:00:00 2001 From: Ted John Date: Tue, 8 Mar 2022 19:49:43 +0000 Subject: [PATCH] Refactor std::lower_bound to binary_find --- src/openrct2/core/Algorithm.hpp | 20 ++++++++++++++++++++ src/openrct2/entity/EntityRegistry.cpp | 13 +++++++------ src/openrct2/entity/PatrolArea.cpp | 5 +++-- src/openrct2/libopenrct2.vcxproj | 1 + 4 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 src/openrct2/core/Algorithm.hpp diff --git a/src/openrct2/core/Algorithm.hpp b/src/openrct2/core/Algorithm.hpp new file mode 100644 index 0000000000..1dbeb32cda --- /dev/null +++ b/src/openrct2/core/Algorithm.hpp @@ -0,0 +1,20 @@ +/***************************************************************************** + * Copyright (c) 2014-2022 OpenRCT2 developers + * + * For a complete list of all authors, please refer to contributors.md + * Interested in contributing? Visit https://github.com/OpenRCT2/OpenRCT2 + * + * OpenRCT2 is licensed under the GNU General Public License version 3. + *****************************************************************************/ + +#pragma once + +#include +#include + +template> +ForwardIt binary_find(ForwardIt first, ForwardIt last, const T& value, Compare comp = {}) +{ + first = std::lower_bound(first, last, value, comp); + return first != last && !comp(value, *first) ? first : last; +} diff --git a/src/openrct2/entity/EntityRegistry.cpp b/src/openrct2/entity/EntityRegistry.cpp index c2d30c6713..0744889ba6 100644 --- a/src/openrct2/entity/EntityRegistry.cpp +++ b/src/openrct2/entity/EntityRegistry.cpp @@ -10,6 +10,7 @@ #include "EntityRegistry.h" #include "../Game.h" +#include "../core/Algorithm.hpp" #include "../core/ChecksumStream.h" #include "../core/Crypt.h" #include "../core/DataSerialiser.h" @@ -291,8 +292,8 @@ static void AddToFreeList(EntityId index) static void RemoveFromEntityList(EntityBase* entity) { auto& list = gEntityLists[EnumValue(entity->Type)]; - auto ptr = std::lower_bound(std::begin(list), std::end(list), entity->sprite_index); - if (ptr != std::end(list) && *ptr == entity->sprite_index) + auto ptr = binary_find(std::begin(list), std::end(list), entity->sprite_index); + if (ptr != std::end(list)) { list.erase(ptr); } @@ -364,8 +365,8 @@ EntityBase* CreateEntity(EntityType type) EntityBase* CreateEntityAt(const EntityId index, const EntityType type) { - auto id = std::lower_bound(std::rbegin(_freeIdList), std::rend(_freeIdList), index); - if (id == std::rend(_freeIdList) || *id != index) + auto id = binary_find(std::rbegin(_freeIdList), std::rend(_freeIdList), index); + if (id == std::rend(_freeIdList)) { return nullptr; } @@ -421,8 +422,8 @@ static void EntitySpatialRemove(EntityBase* entity) { size_t currentIndex = GetSpatialIndexOffset({ entity->x, entity->y }); auto& spatialVector = gEntitySpatialIndex[currentIndex]; - auto index = std::lower_bound(std::begin(spatialVector), std::end(spatialVector), entity->sprite_index); - if (index != std::end(spatialVector) && *index == entity->sprite_index) + auto index = binary_find(std::begin(spatialVector), std::end(spatialVector), entity->sprite_index); + if (index != std::end(spatialVector)) { spatialVector.erase(index, index + 1); } diff --git a/src/openrct2/entity/PatrolArea.cpp b/src/openrct2/entity/PatrolArea.cpp index 92ca2739bf..95405958c5 100644 --- a/src/openrct2/entity/PatrolArea.cpp +++ b/src/openrct2/entity/PatrolArea.cpp @@ -9,6 +9,7 @@ #include "PatrolArea.h" +#include "../core/Algorithm.hpp" #include "EntityList.h" #include "Staff.h" @@ -56,8 +57,8 @@ bool PatrolArea::Get(const TileCoordsXY& pos) const if (area == nullptr) return false; - auto it = std::lower_bound(area->SortedTiles.begin(), area->SortedTiles.end(), pos, CompareTileCoordsXY); - auto found = it != area->SortedTiles.end() && *it == pos; + auto it = binary_find(area->SortedTiles.begin(), area->SortedTiles.end(), pos, CompareTileCoordsXY); + auto found = it != area->SortedTiles.end(); return found; } diff --git a/src/openrct2/libopenrct2.vcxproj b/src/openrct2/libopenrct2.vcxproj index 94e062dd66..de3aa49e00 100644 --- a/src/openrct2/libopenrct2.vcxproj +++ b/src/openrct2/libopenrct2.vcxproj @@ -149,6 +149,7 @@ +