mirror of
https://github.com/OpenTTD/OpenTTD.git
synced 2025-03-09 07:29:44 +00:00
Codechange: Use std::range::find_if where possible.
This commit is contained in:
parent
059a4b22f7
commit
fa1849b855
@ -1256,7 +1256,7 @@ struct BuildVehicleWindow : Window {
|
|||||||
|
|
||||||
/* Select the first unshaded engine in the list as default when opening the window */
|
/* Select the first unshaded engine in the list as default when opening the window */
|
||||||
EngineID engine = INVALID_ENGINE;
|
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;
|
if (it != this->eng_list.end()) engine = it->engine_id;
|
||||||
this->SelectEngine(engine);
|
this->SelectEngine(engine);
|
||||||
}
|
}
|
||||||
|
@ -59,7 +59,7 @@ int8_t SaveHighScoreValue(const Company *c)
|
|||||||
auto &highscores = _highscore_table[SP_CUSTOM];
|
auto &highscores = _highscore_table[SP_CUSTOM];
|
||||||
uint16_t score = c->old_economy[0].performance_history;
|
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 we cannot find it, our score is not high enough. */
|
||||||
if (it == highscores.end()) return -1;
|
if (it == highscores.end()) return -1;
|
||||||
|
@ -101,7 +101,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
EdgeAnnotation &operator[](NodeID to)
|
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());
|
assert(it != this->edges.end());
|
||||||
return *it;
|
return *it;
|
||||||
}
|
}
|
||||||
@ -113,7 +113,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
const EdgeAnnotation &operator[](NodeID to) const
|
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());
|
assert(it != this->edges.end());
|
||||||
return *it;
|
return *it;
|
||||||
}
|
}
|
||||||
|
@ -167,7 +167,7 @@ bool NetworkCanJoinCompany(CompanyID company_id)
|
|||||||
*/
|
*/
|
||||||
static auto FindKey(auto *authorized_keys, std::string_view authorized_key)
|
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); });
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -363,7 +363,7 @@ NetworkAuthenticationServerHandler::ResponseResult X25519AuthenticationHandler::
|
|||||||
NetworkAuthenticationMethod method = static_cast<NetworkAuthenticationMethod>(p.Recv_uint8());
|
NetworkAuthenticationMethod method = static_cast<NetworkAuthenticationMethod>(p.Recv_uint8());
|
||||||
|
|
||||||
auto is_of_method = [method](Handler &handler) { return handler->GetAuthenticationMethod() == method; };
|
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;
|
if (it == handlers.end()) return INVALID;
|
||||||
|
|
||||||
this->current_handler = it->get();
|
this->current_handler = it->get();
|
||||||
|
@ -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)) {
|
if (it == std::end(_grf_text)) {
|
||||||
/* Too many strings allocated, return empty. */
|
/* Too many strings allocated, return empty. */
|
||||||
if (_grf_text.size() == TAB_SIZE_NEWGRF) return STR_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)
|
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)) {
|
if (it != std::end(_grf_text)) {
|
||||||
uint id = static_cast<uint>(it - std::begin(_grf_text));
|
uint id = static_cast<uint>(it - std::begin(_grf_text));
|
||||||
return MakeStringID(TEXT_TAB_NEWGRF_START, id);
|
return MakeStringID(TEXT_TAB_NEWGRF_START, id);
|
||||||
|
@ -612,7 +612,7 @@ void PickerWindow::EnsureSelectedTypeIsVisible()
|
|||||||
int class_index = this->callbacks.GetSelectedClass();
|
int class_index = this->callbacks.GetSelectedClass();
|
||||||
int index = this->callbacks.GetSelectedType();
|
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;
|
if (it == std::end(this->types)) return;
|
||||||
|
|
||||||
int pos = static_cast<int>(std::distance(std::begin(this->types), it));
|
int pos = static_cast<int>(std::distance(std::begin(this->types), it));
|
||||||
|
@ -87,7 +87,7 @@ void MoveWaypointsToBaseStations()
|
|||||||
* from the GRF ID / station index. */
|
* from the GRF ID / station index. */
|
||||||
for (OldWaypoint &wp : _old_waypoints) {
|
for (OldWaypoint &wp : _old_waypoints) {
|
||||||
const auto specs = StationClass::Get(STAT_CLASS_WAYP)->Specs();
|
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;
|
if (found != std::end(specs)) wp.spec = *found;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -312,7 +312,7 @@ void TextfileWindow::CheckHyperlinkClick(Point pt)
|
|||||||
size_t line_index;
|
size_t line_index;
|
||||||
size_t subline;
|
size_t subline;
|
||||||
if (IsWidgetLowered(WID_TF_WRAPTEXT)) {
|
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;
|
if (it == this->lines.cend()) return;
|
||||||
line_index = it - this->lines.cbegin();
|
line_index = it - this->lines.cbegin();
|
||||||
subline = clicked_row - it->top;
|
subline = clicked_row - it->top;
|
||||||
|
@ -1562,7 +1562,7 @@ public:
|
|||||||
dst.insert(item);
|
dst.insert(item);
|
||||||
} else {
|
} else {
|
||||||
/* Search for spec by grfid and local index. */
|
/* 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()) {
|
if (it == specs.end()) {
|
||||||
/* Not preset, hide from UI. */
|
/* Not preset, hide from UI. */
|
||||||
dst.insert({item.grfid, item.local_id, -1, -1});
|
dst.insert({item.grfid, item.local_id, -1, -1});
|
||||||
|
Loading…
Reference in New Issue
Block a user