Codechange: Use std::range::find_if where possible.

This commit is contained in:
Peter Nelson 2024-11-10 10:57:48 +00:00 committed by Peter Nelson
parent 059a4b22f7
commit fa1849b855
10 changed files with 12 additions and 12 deletions

View File

@ -1256,7 +1256,7 @@ struct BuildVehicleWindow : Window {
/* Select the first unshaded engine in the list as default when opening the window */
EngineID engine = INVALID_ENGINE;
auto it = std::find_if(this->eng_list.begin(), this->eng_list.end(), [&](GUIEngineListItem &item) { return !HasFlag(item.flags, EngineDisplayFlags::Shaded); });
auto it = std::ranges::find_if(this->eng_list, [&](GUIEngineListItem &item) { return !HasFlag(item.flags, EngineDisplayFlags::Shaded); });
if (it != this->eng_list.end()) engine = it->engine_id;
this->SelectEngine(engine);
}

View File

@ -59,7 +59,7 @@ int8_t SaveHighScoreValue(const Company *c)
auto &highscores = _highscore_table[SP_CUSTOM];
uint16_t score = c->old_economy[0].performance_history;
auto it = std::find_if(highscores.begin(), highscores.end(), [&score](auto &highscore) { return highscore.score <= score; });
auto it = std::ranges::find_if(highscores, [&score](auto &highscore) { return highscore.score <= score; });
/* If we cannot find it, our score is not high enough. */
if (it == highscores.end()) return -1;

View File

@ -101,7 +101,7 @@ public:
*/
EdgeAnnotation &operator[](NodeID to)
{
auto it = std::find_if(this->edges.begin(), this->edges.end(), [=] (const EdgeAnnotation &e) { return e.base.dest_node == to; });
auto it = std::ranges::find_if(this->edges, [=] (const EdgeAnnotation &e) { return e.base.dest_node == to; });
assert(it != this->edges.end());
return *it;
}
@ -113,7 +113,7 @@ public:
*/
const EdgeAnnotation &operator[](NodeID to) const
{
auto it = std::find_if(this->edges.begin(), this->edges.end(), [=] (const EdgeAnnotation &e) { return e.base.dest_node == to; });
auto it = std::ranges::find_if(this->edges, [=] (const EdgeAnnotation &e) { return e.base.dest_node == to; });
assert(it != this->edges.end());
return *it;
}

View File

@ -167,7 +167,7 @@ bool NetworkCanJoinCompany(CompanyID company_id)
*/
static auto FindKey(auto *authorized_keys, std::string_view authorized_key)
{
return std::find_if(authorized_keys->begin(), authorized_keys->end(), [authorized_key](auto &value) { return StrEqualsIgnoreCase(value, authorized_key); });
return std::ranges::find_if(*authorized_keys, [authorized_key](auto &value) { return StrEqualsIgnoreCase(value, authorized_key); });
}
/**

View File

@ -363,7 +363,7 @@ NetworkAuthenticationServerHandler::ResponseResult X25519AuthenticationHandler::
NetworkAuthenticationMethod method = static_cast<NetworkAuthenticationMethod>(p.Recv_uint8());
auto is_of_method = [method](Handler &handler) { return handler->GetAuthenticationMethod() == method; };
auto it = std::find_if(handlers.begin(), handlers.end(), is_of_method);
auto it = std::ranges::find_if(handlers, is_of_method);
if (it == handlers.end()) return INVALID;
this->current_handler = it->get();

View File

@ -560,7 +560,7 @@ StringID AddGRFString(uint32_t grfid, uint16_t stringid, uint8_t langid_to_add,
}
}
auto it = std::find_if(std::begin(_grf_text), std::end(_grf_text), [&grfid, &stringid](const GRFTextEntry &grf_text) { return grf_text.grfid == grfid && grf_text.stringid == stringid; });
auto it = std::ranges::find_if(_grf_text, [&grfid, &stringid](const GRFTextEntry &grf_text) { return grf_text.grfid == grfid && grf_text.stringid == stringid; });
if (it == std::end(_grf_text)) {
/* Too many strings allocated, return empty. */
if (_grf_text.size() == TAB_SIZE_NEWGRF) return STR_EMPTY;
@ -586,7 +586,7 @@ StringID AddGRFString(uint32_t grfid, uint16_t stringid, uint8_t langid_to_add,
*/
StringID GetGRFStringID(uint32_t grfid, StringID stringid)
{
auto it = std::find_if(std::begin(_grf_text), std::end(_grf_text), [&grfid, &stringid](const GRFTextEntry &grf_text) { return grf_text.grfid == grfid && grf_text.stringid == stringid; });
auto it = std::ranges::find_if(_grf_text, [&grfid, &stringid](const GRFTextEntry &grf_text) { return grf_text.grfid == grfid && grf_text.stringid == stringid; });
if (it != std::end(_grf_text)) {
uint id = static_cast<uint>(it - std::begin(_grf_text));
return MakeStringID(TEXT_TAB_NEWGRF_START, id);

View File

@ -612,7 +612,7 @@ void PickerWindow::EnsureSelectedTypeIsVisible()
int class_index = this->callbacks.GetSelectedClass();
int index = this->callbacks.GetSelectedType();
auto it = std::find_if(std::begin(this->types), std::end(this->types), [class_index, index](const auto &item) { return item.class_index == class_index && item.index == index; });
auto it = std::ranges::find_if(this->types, [class_index, index](const auto &item) { return item.class_index == class_index && item.index == index; });
if (it == std::end(this->types)) return;
int pos = static_cast<int>(std::distance(std::begin(this->types), it));

View File

@ -87,7 +87,7 @@ void MoveWaypointsToBaseStations()
* from the GRF ID / station index. */
for (OldWaypoint &wp : _old_waypoints) {
const auto specs = StationClass::Get(STAT_CLASS_WAYP)->Specs();
auto found = std::find_if(std::begin(specs), std::end(specs), [&wp](const StationSpec *spec) { return spec != nullptr && spec->grf_prop.grffile->grfid == wp.grfid && spec->grf_prop.local_id == wp.localidx; });
auto found = std::ranges::find_if(specs, [&wp](const StationSpec *spec) { return spec != nullptr && spec->grf_prop.grffile->grfid == wp.grfid && spec->grf_prop.local_id == wp.localidx; });
if (found != std::end(specs)) wp.spec = *found;
}
}

View File

@ -312,7 +312,7 @@ void TextfileWindow::CheckHyperlinkClick(Point pt)
size_t line_index;
size_t subline;
if (IsWidgetLowered(WID_TF_WRAPTEXT)) {
auto it = std::find_if(std::begin(this->lines), std::end(this->lines), [clicked_row](const Line &l) { return l.top <= clicked_row && l.bottom > clicked_row; });
auto it = std::ranges::find_if(this->lines, [clicked_row](const Line &l) { return l.top <= clicked_row && l.bottom > clicked_row; });
if (it == this->lines.cend()) return;
line_index = it - this->lines.cbegin();
subline = clicked_row - it->top;

View File

@ -1562,7 +1562,7 @@ public:
dst.insert(item);
} else {
/* Search for spec by grfid and local index. */
auto it = std::find_if(specs.begin(), specs.end(), [&item](const HouseSpec &spec) { return spec.grf_prop.grffile != nullptr && spec.grf_prop.grffile->grfid == item.grfid && spec.grf_prop.local_id == item.local_id; });
auto it = std::ranges::find_if(specs, [&item](const HouseSpec &spec) { return spec.grf_prop.grffile != nullptr && spec.grf_prop.grffile->grfid == item.grfid && spec.grf_prop.local_id == item.local_id; });
if (it == specs.end()) {
/* Not preset, hide from UI. */
dst.insert({item.grfid, item.local_id, -1, -1});