Refactor std::lower_bound to binary_find

This commit is contained in:
Ted John 2022-03-08 19:49:43 +00:00
parent 0aa6ceb2aa
commit 97dfe3cb73
4 changed files with 31 additions and 8 deletions

View File

@ -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 <algorithm>
#include <functional>
template<class ForwardIt, class T, class Compare = std::less<>>
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;
}

View File

@ -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);
}

View File

@ -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;
}

View File

@ -149,6 +149,7 @@
<ClInclude Include="config\IniReader.hpp" />
<ClInclude Include="config\IniWriter.hpp" />
<ClInclude Include="Context.h" />
<ClInclude Include="core\Algorithm.hpp" />
<ClInclude Include="core\BitSet.hpp" />
<ClInclude Include="core\ChecksumStream.h" />
<ClInclude Include="core\CircularBuffer.h" />