1
0
Fork 0

Codechange: Use std::ranges::find where possible.

Replace `std::find(range.begin(), range.end(), ...)` with `std::ranges::find(range, ...)`.
pull/13119/head
Peter Nelson 2024-11-10 10:51:26 +00:00 committed by Peter Nelson
parent 1f18894408
commit 3be0166801
31 changed files with 45 additions and 46 deletions

View File

@ -24,7 +24,7 @@ std::vector<TileIndex> _animated_tiles;
*/
void DeleteAnimatedTile(TileIndex tile)
{
auto to_remove = std::find(_animated_tiles.begin(), _animated_tiles.end(), tile);
auto to_remove = std::ranges::find(_animated_tiles, tile);
if (to_remove != _animated_tiles.end()) {
/* The order of the remaining elements must stay the same, otherwise the animation loop may miss a tile. */
_animated_tiles.erase(to_remove);

View File

@ -167,7 +167,7 @@ class ReplaceVehicleWindow : public Window {
if (side == 1) {
/* ensure primary engine of variant group is in list */
for (const auto &variant : variants) {
if (std::find(list.begin(), list.end(), variant) == list.end()) {
if (std::ranges::find(list, variant, &GUIEngineListItem::engine_id) == list.end()) {
const Engine *e = Engine::Get(variant);
list.emplace_back(variant, e->info.variant_id, e->display_flags | EngineDisplayFlags::Shaded, 0);
}

View File

@ -23,7 +23,7 @@
template <typename Container>
inline bool include(Container &container, typename Container::const_reference &item)
{
const bool is_member = std::find(container.begin(), container.end(), item) != container.end();
const bool is_member = std::ranges::find(container, item) != container.end();
if (!is_member) container.emplace_back(item);
return is_member;
}
@ -40,7 +40,7 @@ inline bool include(Container &container, typename Container::const_reference &i
template <typename Container>
int find_index(Container const &container, typename Container::const_reference item)
{
auto const it = std::find(container.begin(), container.end(), item);
auto const it = std::ranges::find(container, item);
if (it != container.end()) return std::distance(container.begin(), it);
return -1;

View File

@ -19,7 +19,7 @@
/* virtual */ PoolBase::~PoolBase()
{
PoolVector *pools = PoolBase::GetPools();
pools->erase(std::find(pools->begin(), pools->end(), this));
pools->erase(std::ranges::find(*pools, this));
if (pools->empty()) delete pools;
}

View File

@ -267,7 +267,7 @@ HotkeyList::HotkeyList(const std::string &ini_group, const std::vector<Hotkey> &
HotkeyList::~HotkeyList()
{
_hotkey_lists->erase(std::find(_hotkey_lists->begin(), _hotkey_lists->end(), this));
_hotkey_lists->erase(std::ranges::find(*_hotkey_lists, this));
}
/**

View File

@ -424,10 +424,10 @@ static void AddAcceptedCargo_Industry(TileIndex tile, CargoArray &acceptance, Ca
if (itspec->special_flags & INDTILE_SPECIAL_ACCEPTS_ALL_CARGO) {
/* Copy all accepted cargoes from industry itself */
for (const auto &a : ind->accepted) {
auto pos = std::find(std::begin(accepts_cargo), std::end(accepts_cargo), a.cargo);
auto pos = std::ranges::find(accepts_cargo, a.cargo);
if (pos == std::end(accepts_cargo)) {
/* Not found, insert */
pos = std::find(std::begin(accepts_cargo), std::end(accepts_cargo), INVALID_CARGO);
pos = std::ranges::find(accepts_cargo, INVALID_CARGO);
if (pos == std::end(accepts_cargo)) continue; // nowhere to place, give up on this one
*pos = a.cargo;
}
@ -1879,7 +1879,7 @@ static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type,
continue;
}
/* Verify valid cargo */
if (std::find(std::begin(indspec->accepts_cargo), std::end(indspec->accepts_cargo), cargo) == std::end(indspec->accepts_cargo)) {
if (std::ranges::find(indspec->accepts_cargo, cargo) == std::end(indspec->accepts_cargo)) {
/* Cargo not in spec, error in NewGRF */
ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_INPUT_CARGO_TYPES, res);
break;
@ -1915,7 +1915,7 @@ static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type,
continue;
}
/* Verify valid cargo */
if (std::find(std::begin(indspec->produced_cargo), std::end(indspec->produced_cargo), cargo) == std::end(indspec->produced_cargo)) {
if (std::ranges::find(indspec->produced_cargo, cargo) == std::end(indspec->produced_cargo)) {
/* Cargo not in spec, error in NewGRF */
ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_OUTPUT_CARGO_TYPES, res);
break;

View File

@ -162,8 +162,8 @@ IniGroup &IniLoadFile::GetOrCreateGroup(std::string_view name)
IniGroup &IniLoadFile::CreateGroup(std::string_view name)
{
IniGroupType type = IGT_VARIABLES;
if (std::find(this->list_group_names.begin(), this->list_group_names.end(), name) != this->list_group_names.end()) type = IGT_LIST;
if (std::find(this->seq_group_names.begin(), this->seq_group_names.end(), name) != this->seq_group_names.end()) type = IGT_SEQUENCE;
if (std::ranges::find(this->list_group_names, name) != this->list_group_names.end()) type = IGT_LIST;
if (std::ranges::find(this->seq_group_names, name) != this->seq_group_names.end()) type = IGT_SEQUENCE;
return this->groups.emplace_back(name, type);
}

View File

@ -262,7 +262,7 @@ public:
*/
inline size_t FindIndex(const T &item) const
{
auto it = std::find(this->data.begin(), this->data.end(), &item);
auto it = std::ranges::find(this->data, &item);
return it == this->data.end() ? 0 : std::distance(this->data.begin(), it);
}

View File

@ -178,8 +178,8 @@ static CommandQueue _local_execution_queue;
*/
static size_t FindCallbackIndex(CommandCallback *callback)
{
if (auto it = std::find(std::cbegin(_callback_table), std::cend(_callback_table), callback); it != std::cend(_callback_table)) {
return static_cast<size_t>(std::distance(std::cbegin(_callback_table), it));
if (auto it = std::ranges::find(_callback_table, callback); it != std::end(_callback_table)) {
return static_cast<size_t>(std::distance(std::begin(_callback_table), it));
}
return std::numeric_limits<size_t>::max();

View File

@ -838,7 +838,7 @@ void ClientNetworkContentSocketHandler::SendReceive()
void ClientNetworkContentSocketHandler::DownloadContentInfo(ContentID cid)
{
/* When we tried to download it already, don't try again */
if (std::find(this->requested.begin(), this->requested.end(), cid) != this->requested.end()) return;
if (std::ranges::find(this->requested, cid) != this->requested.end()) return;
this->requested.push_back(cid);
this->RequestContentList(1, &cid);

View File

@ -146,7 +146,7 @@ public:
/** Add a callback to this class */
void AddCallback(ContentCallback *cb) { include(this->callbacks, cb); }
/** Remove a callback */
void RemoveCallback(ContentCallback *cb) { this->callbacks.erase(std::find(this->callbacks.begin(), this->callbacks.end(), cb)); }
void RemoveCallback(ContentCallback *cb) { this->callbacks.erase(std::ranges::find(this->callbacks, cb)); }
};
extern ClientNetworkContentSocketHandler _network_content_client;

View File

@ -45,7 +45,7 @@ const SpriteGroup *GetWagonOverrideSpriteSet(EngineID engine, CargoID cargo, Eng
for (const WagonOverride &wo : e->overrides) {
if (wo.cargo != cargo && wo.cargo != SpriteGroupCargo::SG_DEFAULT) continue;
if (std::find(wo.engines.begin(), wo.engines.end(), overriding_engine) != wo.engines.end()) return wo.group;
if (std::ranges::find(wo.engines, overriding_engine) != wo.engines.end()) return wo.group;
}
return nullptr;
}

View File

@ -373,7 +373,7 @@ static inline void VisitAdjacentWaterRegionPatchNeighbors(const WaterRegionPatch
const TileIndex neighbor_edge_tile = GetEdgeTileCoordinate(nx, ny, opposite_side, x_or_y);
const TWaterRegionPatchLabel neighbor_label = neighboring_region.GetLabel(neighbor_edge_tile);
assert(neighbor_label != INVALID_WATER_REGION_PATCH);
if (std::find(unique_labels.begin(), unique_labels.end(), neighbor_label) == unique_labels.end()) unique_labels.push_back(neighbor_label);
if (std::ranges::find(unique_labels, neighbor_label) == unique_labels.end()) unique_labels.push_back(neighbor_label);
}
for (TWaterRegionPatchLabel unique_label : unique_labels) func(WaterRegionPatchDesc{ nx, ny, unique_label });
}

View File

@ -151,8 +151,7 @@ public:
TrackFollower F(Yapf().GetVehicle());
if (F.Follow(old_node.key.tile, old_node.key.td)) {
if (this->water_region_corridor.empty()
|| std::find(this->water_region_corridor.begin(), this->water_region_corridor.end(),
GetWaterRegionInfo(F.new_tile)) != this->water_region_corridor.end()) {
|| std::ranges::find(this->water_region_corridor, GetWaterRegionInfo(F.new_tile)) != this->water_region_corridor.end()) {
Yapf().AddMultipleNodes(&old_node, F);
}
}
@ -255,7 +254,7 @@ public:
while (node->parent) {
const WaterRegionPatchDesc node_water_patch = GetWaterRegionPatchInfo(node->GetTile());
const bool node_water_patch_on_high_level_path = std::find(high_level_path.begin(), high_level_path.end(), node_water_patch) != high_level_path.end();
const bool node_water_patch_on_high_level_path = std::ranges::find(high_level_path, node_water_patch) != high_level_path.end();
const bool add_full_path = !is_intermediate_destination && node_water_patch != end_water_patch;
/* The cached path must always lead to a region patch that's on the high level path.

View File

@ -95,7 +95,7 @@ public:
bool HasOrigin(const WaterRegionPatchDesc &water_region_patch)
{
return std::find(this->origin_keys.begin(), this->origin_keys.end(), CYapfRegionPatchNodeKey{ water_region_patch }) != this->origin_keys.end();
return std::ranges::find(this->origin_keys, CYapfRegionPatchNodeKey{ water_region_patch }) != this->origin_keys.end();
}
void PfSetStartupNodes()

View File

@ -49,7 +49,7 @@ PickerCallbacks::PickerCallbacks(const std::string &ini_group) : ini_group(ini_g
PickerCallbacks::~PickerCallbacks()
{
auto &callbacks = GetPickerCallbacks();
callbacks.erase(std::find(callbacks.begin(), callbacks.end(), this));
callbacks.erase(std::ranges::find(callbacks, this));
}
/**
@ -500,7 +500,7 @@ void PickerWindow::EnsureSelectedClassIsVisible()
if (!this->has_class_picker) return;
if (this->classes.empty()) return;
auto it = std::find(std::begin(this->classes), std::end(this->classes), this->callbacks.GetSelectedClass());
auto it = std::ranges::find(this->classes, this->callbacks.GetSelectedClass());
if (it == std::end(this->classes)) return;
int pos = static_cast<int>(std::distance(std::begin(this->classes), it));

View File

@ -322,7 +322,7 @@ RailType GetRailTypeByLabel(RailTypeLabel label, bool allow_alternate_labels)
/* Test if any rail type defines the label as an alternate. */
for (RailType r = RAILTYPE_BEGIN; r != RAILTYPE_END; r++) {
const RailTypeInfo *rti = GetRailTypeInfo(r);
if (std::find(rti->alternate_labels.begin(), rti->alternate_labels.end(), label) != rti->alternate_labels.end()) return r;
if (std::ranges::find(rti->alternate_labels, label) != rti->alternate_labels.end()) return r;
}
}

View File

@ -490,7 +490,7 @@ struct BuildRailToolbarWindow : Window {
bool can_build = CanBuildVehicleInfrastructure(VEH_TRAIN);
if (can_build) return false;
if (std::find(std::begin(can_build_widgets), std::end(can_build_widgets), widget) == std::end(can_build_widgets)) return false;
if (std::ranges::find(can_build_widgets, widget) == std::end(can_build_widgets)) return false;
GuiShowTooltips(this, STR_TOOLBAR_DISABLED_NO_VEHICLE_AVAILABLE, close_cond);
return true;

View File

@ -265,7 +265,7 @@ RoadType GetRoadTypeByLabel(RoadTypeLabel label, bool allow_alternate_labels)
/* Test if any road type defines the label as an alternate. */
for (RoadType r = ROADTYPE_BEGIN; r != ROADTYPE_END; r++) {
const RoadTypeInfo *rti = GetRoadTypeInfo(r);
if (std::find(rti->alternate_labels.begin(), rti->alternate_labels.end(), label) != rti->alternate_labels.end()) return r;
if (std::ranges::find(rti->alternate_labels, label) != rti->alternate_labels.end()) return r;
}
}

View File

@ -168,7 +168,7 @@ std::string ScriptText::GetEncodedText()
void ScriptText::_FillParamList(ParamList &params, ScriptTextList &seen_texts)
{
if (std::find(seen_texts.begin(), seen_texts.end(), this) != seen_texts.end()) throw Script_FatalError(fmt::format("{}: Circular reference detected", GetGameStringName(this->string)));
if (std::ranges::find(seen_texts, this) != seen_texts.end()) throw Script_FatalError(fmt::format("{}: Circular reference detected", GetGameStringName(this->string)));
seen_texts.push_back(this);
for (int i = 0; i < this->paramc; i++) {

View File

@ -86,7 +86,7 @@ static const void *ResolveObject(const GameSettings *settings_ptr, const IntSett
*/
static uint GetCurrentResolutionIndex()
{
auto it = std::find(_resolutions.begin(), _resolutions.end(), Dimension(_screen.width, _screen.height));
auto it = std::ranges::find(_resolutions, Dimension(_screen.width, _screen.height));
return std::distance(_resolutions.begin(), it);
}

View File

@ -301,7 +301,7 @@ static void DumpSections(const IniLoadFile &ifile)
/* Output every group, using its name as template name. */
for (const IniGroup &grp : ifile.groups) {
/* Exclude special group names. */
if (std::find(std::begin(special_group_names), std::end(special_group_names), grp.name) != std::end(special_group_names)) continue;
if (std::ranges::find(special_group_names, grp.name) != std::end(special_group_names)) continue;
const IniItem *template_item = templates_grp->GetItem(grp.name); // Find template value.
if (template_item == nullptr || !template_item->value.has_value()) {

View File

@ -2218,7 +2218,7 @@ static bool AddNearbyStation(TileIndex tile, void *user_data)
if (!T::IsValidID(sid)) return false;
BaseStation *st = BaseStation::Get(sid);
if (st->owner != _local_company || std::find(_stations_nearby_list.begin(), _stations_nearby_list.end(), sid) != _stations_nearby_list.end()) return false;
if (st->owner != _local_company || std::ranges::find(_stations_nearby_list, sid) != _stations_nearby_list.end()) return false;
if (st->rect.BeforeAddRect(ctx->tile, ctx->w, ctx->h, StationRect::ADD_TEST).Succeeded()) {
_stations_nearby_list.push_back(sid);

View File

@ -287,7 +287,7 @@ static void GetCargoSummaryOfArticulatedVehicle(const Train *v, CargoSummary &su
new_item.subtype = GetCargoSubtypeText(v);
if (!IsValidCargoID(new_item.cargo) && new_item.subtype == STR_EMPTY) continue;
auto item = std::find(std::begin(summary), std::end(summary), new_item);
auto item = std::ranges::find(summary, new_item);
if (item == std::end(summary)) {
item = summary.emplace(std::end(summary));
item->cargo = new_item.cargo;

View File

@ -379,7 +379,7 @@ static std::tuple<CommandCost, uint, uint16_t, CargoArray> RefitVehicle(Vehicle
/* Reset actual_subtype for every new vehicle */
if (!v->IsArticulatedPart()) actual_subtype = new_subtype;
if (v->type == VEH_TRAIN && std::find(vehicles_to_refit.begin(), vehicles_to_refit.end(), v->index) == vehicles_to_refit.end() && !only_this) continue;
if (v->type == VEH_TRAIN && std::ranges::find(vehicles_to_refit, v->index) == vehicles_to_refit.end() && !only_this) continue;
const Engine *e = v->GetEngine();
if (!e->CanCarryCargo()) continue;

View File

@ -443,7 +443,7 @@ void BaseVehicleListWindow::FilterVehicleList()
if (this->vehicles.empty()) {
/* No vehicle passed through the filter, invalidate the previously selected vehicle */
this->vehicle_sel = INVALID_VEHICLE;
} else if (this->vehicle_sel != INVALID_VEHICLE && std::find(this->vehicles.begin(), this->vehicles.end(), Vehicle::Get(this->vehicle_sel)) == this->vehicles.end()) { // previously selected engine didn't pass the filter, remove selection
} else if (this->vehicle_sel != INVALID_VEHICLE && std::ranges::find(this->vehicles, Vehicle::Get(this->vehicle_sel)) == this->vehicles.end()) { // previously selected engine didn't pass the filter, remove selection
this->vehicle_sel = INVALID_VEHICLE;
}
}
@ -636,7 +636,7 @@ uint8_t GetBestFittingSubType(Vehicle *v_from, Vehicle *v_for, CargoID dest_carg
StringID subtype = GetCargoSubtypeText(v);
if (subtype == STR_EMPTY) break;
if (std::find(subtypes.begin(), subtypes.end(), subtype) == subtypes.end()) continue;
if (std::ranges::find(subtypes, subtype) == subtypes.end()) continue;
/* We found something matching. */
ret_refit_cyc = refit_cyc;
@ -787,7 +787,7 @@ struct RefitWindow : public Window {
GetVehicleSet(vehicles_to_refit, Vehicle::Get(this->selected_vehicle), this->num_vehicles);
do {
if (v->type == VEH_TRAIN && std::find(vehicles_to_refit.begin(), vehicles_to_refit.end(), v->index) == vehicles_to_refit.end()) continue;
if (v->type == VEH_TRAIN && std::ranges::find(vehicles_to_refit, v->index) == vehicles_to_refit.end()) continue;
const Engine *e = v->GetEngine();
CargoTypes cmask = e->info.refit_mask;
uint8_t callback_mask = e->info.callback_mask;
@ -1081,7 +1081,7 @@ struct RefitWindow : public Window {
for (Train *u = Train::From(v); u != nullptr; u = u->Next()) {
/* Start checking. */
const bool contained = std::find(vehicles_to_refit.begin(), vehicles_to_refit.end(), u->index) != vehicles_to_refit.end();
const bool contained = std::ranges::find(vehicles_to_refit, u->index) != vehicles_to_refit.end();
if (contained && left == INT32_MIN) {
left = x - this->hscroll->GetPosition() + r.left + this->vehicle_margin;
width = 0;

View File

@ -152,7 +152,7 @@ static void GetVideoModes()
uint w = modes[i].width;
uint h = modes[i].height;
if (w < 640 || h < 480) continue;
if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(w, h)) != _resolutions.end()) continue;
if (std::ranges::find(_resolutions, Dimension(w, h)) != _resolutions.end()) continue;
_resolutions.emplace_back(w, h);
}
@ -167,7 +167,7 @@ static void GetAvailableVideoMode(uint *w, uint *h)
if (_resolutions.empty()) return;
/* is the wanted mode among the available modes? */
if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(*w, *h)) != _resolutions.end()) return;
if (std::ranges::find(_resolutions, Dimension(*w, *h)) != _resolutions.end()) return;
/* use the closest possible resolution */
uint best = 0;

View File

@ -65,7 +65,7 @@ static void FindResolutions()
SDL_GetDisplayMode(display, i, &mode);
if (mode.w < 640 || mode.h < 480) continue;
if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(mode.w, mode.h)) != _resolutions.end()) continue;
if (std::ranges::find(_resolutions, Dimension(mode.w, mode.h)) != _resolutions.end()) continue;
_resolutions.emplace_back(mode.w, mode.h);
}
}
@ -84,7 +84,7 @@ static void GetAvailableVideoMode(uint *w, uint *h)
if (!_fullscreen || _resolutions.empty()) return;
/* Is the wanted mode among the available modes? */
if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(*w, *h)) != _resolutions.end()) return;
if (std::ranges::find(_resolutions, Dimension(*w, *h)) != _resolutions.end()) return;
/* Use the closest possible resolution */
uint best = 0;

View File

@ -192,7 +192,7 @@ static void GetVideoModes()
uint w = modes[i]->w;
uint h = modes[i]->h;
if (w < 640 || h < 480) continue; // reject too small resolutions
if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(w, h)) != _resolutions.end()) continue;
if (std::ranges::find(_resolutions, Dimension(w, h)) != _resolutions.end()) continue;
_resolutions.emplace_back(w, h);
}
if (_resolutions.empty()) UserError("No usable screen resolutions found!\n");
@ -206,7 +206,7 @@ static void GetAvailableVideoMode(uint *w, uint *h)
if (_all_modes || _resolutions.empty()) return;
/* Is the wanted mode among the available modes? */
if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(*w, *h)) != _resolutions.end()) return;
if (std::ranges::find(_resolutions, Dimension(*w, *h)) != _resolutions.end()) return;
/* Use the closest possible resolution */
uint best = 0;

View File

@ -800,7 +800,7 @@ static void FindResolutions(uint8_t bpp)
DEVMODE dm;
for (uint i = 0; EnumDisplaySettings(nullptr, i, &dm) != 0; i++) {
if (dm.dmBitsPerPel != bpp || dm.dmPelsWidth < 640 || dm.dmPelsHeight < 480) continue;
if (std::find(_resolutions.begin(), _resolutions.end(), Dimension(dm.dmPelsWidth, dm.dmPelsHeight)) != _resolutions.end()) continue;
if (std::ranges::find(_resolutions, Dimension(dm.dmPelsWidth, dm.dmPelsHeight)) != _resolutions.end()) continue;
_resolutions.emplace_back(dm.dmPelsWidth, dm.dmPelsHeight);
}

View File

@ -126,7 +126,7 @@ WindowDesc::WindowDesc(WindowPosition def_pos, const char *ini_key, int16_t def_
WindowDesc::~WindowDesc()
{
_window_descs->erase(std::find(_window_descs->begin(), _window_descs->end(), this));
_window_descs->erase(std::ranges::find(*_window_descs, this));
}
/**