1
0
Fork 0

Codechange: Use projection-based std::range::find where possible.

This simplifies matching by class members and avoids wordy lambdas.
pull/13119/head
Peter Nelson 2024-11-10 10:56:37 +00:00 committed by Peter Nelson
parent 876d53282e
commit 059a4b22f7
16 changed files with 31 additions and 44 deletions

View File

@ -1353,7 +1353,7 @@ struct BuildVehicleWindow : Window {
this->eng_list.Filter(this->cargo_filter_criteria);
if (0 == this->eng_list.size()) { // no engine passed through the filter, invalidate the previously selected engine
this->SelectEngine(INVALID_ENGINE);
} else if (std::find(this->eng_list.begin(), this->eng_list.end(), this->sel_engine) == this->eng_list.end()) { // previously selected engine didn't pass the filter, select the first engine of the list
} else if (std::ranges::find(this->eng_list, this->sel_engine, &GUIEngineListItem::engine_id) == this->eng_list.end()) { // previously selected engine didn't pass the filter, select the first engine of the list
this->SelectEngine(this->eng_list[0].engine_id);
}
}
@ -1426,7 +1426,7 @@ struct BuildVehicleWindow : Window {
/* 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);
if (e->u.rail.railveh_type != RAILVEH_WAGON) num_engines++;
@ -1568,7 +1568,7 @@ struct BuildVehicleWindow : Window {
}
for (const auto &variant : variants) {
if (std::find(this->eng_list.begin(), this->eng_list.end(), variant) == this->eng_list.end()) {
if (std::ranges::find(this->eng_list, variant, &GUIEngineListItem::engine_id) == this->eng_list.end()) {
const Engine *e = Engine::Get(variant);
this->eng_list.emplace_back(variant, e->info.variant_id, e->display_flags | EngineDisplayFlags::Shaded, 0);
}

View File

@ -2454,7 +2454,7 @@ DEF_CONSOLE_CMD(ConNewGRFProfile)
IConsolePrint(CC_INFO, "Loaded GRF files:");
int i = 1;
for (GRFFile *grf : files) {
auto profiler = std::find_if(_newgrf_profilers.begin(), _newgrf_profilers.end(), [&](NewGRFProfiler &pr) { return pr.grffile == grf; });
auto profiler = std::ranges::find(_newgrf_profilers, grf, &NewGRFProfiler::grffile);
bool selected = profiler != _newgrf_profilers.end();
bool active = selected && profiler->active;
TextColour tc = active ? TC_LIGHT_BLUE : selected ? TC_GREEN : CC_INFO;
@ -2496,8 +2496,7 @@ DEF_CONSOLE_CMD(ConNewGRFProfile)
continue;
}
GRFFile *grf = files[grfnum - 1];
auto pos = std::find_if(_newgrf_profilers.begin(), _newgrf_profilers.end(), [&](NewGRFProfiler &pr) { return pr.grffile == grf; });
if (pos != _newgrf_profilers.end()) _newgrf_profilers.erase(pos);
_newgrf_profilers.erase(std::ranges::find(_newgrf_profilers, grf, &NewGRFProfiler::grffile));
}
return true;
}

View File

@ -25,9 +25,6 @@ struct GUIEngineListItem {
uint16_t level_mask; ///< Mask of level continuations.
GUIEngineListItem(EngineID engine_id, EngineID variant_id, EngineDisplayFlags flags, uint8_t indent) : engine_id(engine_id), variant_id(variant_id), flags(flags), indent(indent), level_mask(0) {}
/* Used when searching list only by engine_id. */
bool operator == (const EngineID &other) const { return this->engine_id == other; }
};
typedef GUIList<GUIEngineListItem, std::nullptr_t, CargoID> GUIEngineList;

View File

@ -1131,7 +1131,7 @@ public:
if (g_id != ALL_GROUP && g_id != DEFAULT_GROUP) {
const Group *g = Group::Get(g_id);
auto found = std::find_if(std::begin(this->groups), std::end(this->groups), [g](const auto &item) { return item.group == g; });
auto found = std::ranges::find(this->groups, g, &GUIGroupListItem::group);
if (found == std::end(this->groups)) {
/* The group's branch is maybe collapsed, so try to expand it. */
for (auto pg = Group::GetIfValid(g->parent); pg != nullptr; pg = Group::GetIfValid(pg->parent)) {
@ -1140,7 +1140,7 @@ public:
this->groups.ForceRebuild();
this->BuildGroupList(this->owner);
this->group_sb->SetCount(this->groups.size());
found = std::find_if(std::begin(this->groups), std::end(this->groups), [g](const auto &item) { return item.group == g; });
found = std::ranges::find(this->groups, g, &GUIGroupListItem::group);
}
if (found != std::end(this->groups)) this->group_sb->ScrollTowards(std::distance(std::begin(this->groups), found));
}

View File

@ -167,7 +167,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> {
inline ProducedCargoes::iterator GetCargoProduced(CargoID cargo)
{
if (!IsValidCargoID(cargo)) return std::end(this->produced);
return std::find_if(std::begin(this->produced), std::end(this->produced), [&cargo](const auto &p) { return p.cargo == cargo; });
return std::ranges::find(this->produced, cargo, &ProducedCargo::cargo);
}
/**
@ -178,7 +178,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> {
inline ProducedCargoes::const_iterator GetCargoProduced(CargoID cargo) const
{
if (!IsValidCargoID(cargo)) return std::end(this->produced);
return std::find_if(std::begin(this->produced), std::end(this->produced), [&cargo](const auto &p) { return p.cargo == cargo; });
return std::ranges::find(this->produced, cargo, &ProducedCargo::cargo);
}
/**
@ -189,7 +189,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> {
inline AcceptedCargoes::iterator GetCargoAccepted(CargoID cargo)
{
if (!IsValidCargoID(cargo)) return std::end(this->accepted);
return std::find_if(std::begin(this->accepted), std::end(this->accepted), [&cargo](const auto &a) { return a.cargo == cargo; });
return std::ranges::find(this->accepted, cargo, &AcceptedCargo::cargo);
}
/**
@ -200,7 +200,7 @@ struct Industry : IndustryPool::PoolItem<&_industry_pool> {
inline AcceptedCargoes::const_iterator GetCargoAccepted(CargoID cargo) const
{
if (!IsValidCargoID(cargo)) return std::end(this->accepted);
return std::find_if(std::begin(this->accepted), std::end(this->accepted), [&cargo](const auto &a) { return a.cargo == cargo; });
return std::ranges::find(this->accepted, cargo, &AcceptedCargo::cargo);
}
/**

View File

@ -1624,9 +1624,7 @@ protected:
* because this is the one the player interested in, and that way it is not hidden in the 'n' more cargos */
const CargoID cid = this->produced_cargo_filter_criteria;
if (cid != CargoFilterCriteria::CF_ANY && cid != CargoFilterCriteria::CF_NONE) {
auto filtered_ci = std::find_if(cargos.begin(), cargos.end(), [cid](const CargoInfo &ci) -> bool {
return ci.cargo_id == cid;
});
auto filtered_ci = std::ranges::find(cargos, cid, &CargoInfo::cargo_id);
if (filtered_ci != cargos.end()) {
std::rotate(cargos.begin(), filtered_ci, filtered_ci + 1);
}

View File

@ -70,11 +70,6 @@ struct DLSFile {
WSMPL wave_sample;
std::vector<WLOOP> wave_loops;
bool operator ==(long offset) const
{
return this->file_offset == offset;
}
};
std::vector<DLSInstrument> instruments;
@ -503,7 +498,7 @@ bool DLSFile::LoadFile(const std::string &file)
/* Resolve wave pool table. */
for (std::vector<POOLCUE>::iterator cue = this->pool_cues.begin(); cue != this->pool_cues.end(); cue++) {
std::vector<DLSWave>::iterator w = std::find(this->waves.begin(), this->waves.end(), cue->ulOffset);
std::vector<DLSWave>::iterator w = std::ranges::find(this->waves, cue->ulOffset, &DLSWave::file_offset);
if (w != this->waves.end()) {
cue->ulOffset = (ULONG)(w - this->waves.begin());
} else {

View File

@ -188,7 +188,7 @@ void MusicSystem::ChangeMusicSet(const std::string &set_name)
*/
void MusicSystem::SetPositionBySetIndex(uint set_index)
{
auto it = std::find_if(std::begin(this->active_playlist), std::end(this->active_playlist), [&set_index](const PlaylistEntry &ple) { return ple.set_index == set_index; });
auto it = std::ranges::find(this->active_playlist, set_index, &PlaylistEntry::set_index);
if (it != std::end(this->active_playlist)) this->playlist_position = std::distance(std::begin(this->active_playlist), it);
}

View File

@ -31,7 +31,7 @@ void NewGRFClass<Tspec, Tindex, Tmax>::Reset()
template <typename Tspec, typename Tindex, Tindex Tmax>
Tindex NewGRFClass<Tspec, Tindex, Tmax>::Allocate(uint32_t global_id)
{
auto found = std::find_if(std::begin(NewGRFClass::classes), std::end(NewGRFClass::classes), [global_id](const auto &cls) { return cls.global_id == global_id; });
auto found = std::ranges::find(NewGRFClass::classes, global_id, &NewGRFClass::global_id);
/* Id is already allocated, so reuse it. */
if (found != std::end(NewGRFClass::classes)) return found->Index();

View File

@ -36,7 +36,7 @@ TemporaryStorageArray<int32_t, 0x110> _temp_store;
if (group == nullptr) return nullptr;
const GRFFile *grf = object.grffile;
auto profiler = std::find_if(_newgrf_profilers.begin(), _newgrf_profilers.end(), [&](const NewGRFProfiler &pr) { return pr.grffile == grf; });
auto profiler = std::ranges::find(_newgrf_profilers, grf, &NewGRFProfiler::grffile);
if (profiler == _newgrf_profilers.end() || !profiler->active) {
if (top_level) _temp_store.ClearChanges();

View File

@ -27,7 +27,7 @@ static std::vector<StringID> _grf_townname_names;
GRFTownName *GetGRFTownName(uint32_t grfid)
{
auto found = std::find_if(std::begin(_grf_townnames), std::end(_grf_townnames), [&grfid](const GRFTownName &t) { return t.grfid == grfid; });
auto found = std::ranges::find(_grf_townnames, grfid, &GRFTownName::grfid);
if (found != std::end(_grf_townnames)) return &*found;
return nullptr;
}
@ -44,7 +44,7 @@ GRFTownName *AddGRFTownName(uint32_t grfid)
void DelGRFTownName(uint32_t grfid)
{
_grf_townnames.erase(std::find_if(std::begin(_grf_townnames), std::end(_grf_townnames), [&grfid](const GRFTownName &t) { return t.grfid == grfid; }));
_grf_townnames.erase(std::ranges::find(_grf_townnames, grfid, &GRFTownName::grfid));
}
static void RandomPart(StringBuilder &builder, const GRFTownName *t, uint32_t seed, uint8_t id)

View File

@ -2935,7 +2935,7 @@ SaveOrLoadResult SaveWithFilter(std::shared_ptr<SaveFilter> writer, bool threade
*/
static const SaveLoadFormat *DetermineSaveLoadFormat(uint32_t tag, uint32_t raw_version)
{
auto fmt = std::find_if(std::begin(_saveload_formats), std::end(_saveload_formats), [tag](const auto &fmt) { return fmt.tag == tag; });
auto fmt = std::ranges::find(_saveload_formats, tag, &SaveLoadFormat::tag);
if (fmt != std::end(_saveload_formats)) {
/* Check version number */
_sl_version = (SaveLoadVersion)(TO_BE32(raw_version) >> 16);
@ -2958,7 +2958,7 @@ static const SaveLoadFormat *DetermineSaveLoadFormat(uint32_t tag, uint32_t raw_
_sl_minor_version = 0;
/* Try to find the LZO savegame format; it uses 'OTTD' as tag. */
fmt = std::find_if(std::begin(_saveload_formats), std::end(_saveload_formats), [](const auto &fmt) { return fmt.tag == SAVEGAME_TAG_LZO; });
fmt = std::ranges::find(_saveload_formats, SAVEGAME_TAG_LZO, &SaveLoadFormat::tag);
if (fmt == std::end(_saveload_formats)) {
/* Who removed the LZO savegame format definition? When built without LZO support,
* the formats must still list it just without a method to read the file.

View File

@ -388,7 +388,7 @@ void Station::AddIndustryToDeliver(Industry *ind, TileIndex tile)
uint distance = DistanceMax(this->xy, tile);
/* Don't check further if this industry is already in the list but update the distance if it's closer */
auto pos = std::find_if(this->industries_near.begin(), this->industries_near.end(), [&](const IndustryListEntry &e) { return e.industry->index == ind->index; });
auto pos = std::ranges::find(this->industries_near, ind, &IndustryListEntry::industry);
if (pos != this->industries_near.end()) {
if (pos->distance > distance) {
auto node = this->industries_near.extract(pos);
@ -410,7 +410,7 @@ void Station::AddIndustryToDeliver(Industry *ind, TileIndex tile)
*/
void Station::RemoveIndustryToDeliver(Industry *ind)
{
auto pos = std::find_if(this->industries_near.begin(), this->industries_near.end(), [&](const IndustryListEntry &e) { return e.industry->index == ind->index; });
auto pos = std::ranges::find(this->industries_near, ind, &IndustryListEntry::industry);
if (pos != this->industries_near.end()) {
this->industries_near.erase(pos);
}

View File

@ -42,7 +42,7 @@ TextEffectID AddTextEffect(StringID msg, int center, int y, uint8_t duration, Te
{
if (_game_mode == GM_MENU) return INVALID_TE_ID;
auto it = std::find_if(std::begin(_text_effects), std::end(_text_effects), [](const TextEffect &te) { return te.string_id == INVALID_STRING_ID; });
auto it = std::ranges::find(_text_effects, INVALID_STRING_ID, &TextEffect::string_id);
if (it == std::end(_text_effects)) {
/* _text_effects.size() is the maximum ID + 1 that has been allocated. We should not allocate INVALID_TE_ID or beyond. */
if (_text_effects.size() >= INVALID_TE_ID) return INVALID_TE_ID;

View File

@ -403,7 +403,7 @@ void TextfileWindow::NavigateHistory(int delta)
switch (ClassifyHyperlink(link.destination, this->trusted)) {
case HyperlinkType::Internal:
{
auto it = std::find_if(this->link_anchors.cbegin(), this->link_anchors.cend(), [&](const Hyperlink &other) { return link.destination == other.destination; });
auto it = std::ranges::find(this->link_anchors, link.destination, &Hyperlink::destination);
if (it != this->link_anchors.cend()) {
this->AppendHistory(this->filepath);
this->ScrollToLine(it->line);
@ -484,7 +484,7 @@ void TextfileWindow::NavigateToFile(std::string newfile, size_t line)
if (anchor.empty() || line != 0) {
this->ScrollToLine(line);
} else {
auto anchor_dest = std::find_if(this->link_anchors.cbegin(), this->link_anchors.cend(), [&](const Hyperlink &other) { return anchor == other.destination; });
auto anchor_dest = std::ranges::find(this->link_anchors, anchor, &Hyperlink::destination);
if (anchor_dest != this->link_anchors.cend()) {
this->ScrollToLine(anchor_dest->line);
this->UpdateHistoryScrollpos();

View File

@ -65,8 +65,6 @@ struct TouchBarButton {
SpriteID sprite;
MainToolbarHotkeys hotkey;
NSString *fallback_text;
bool operator ==(const NSTouchBarItemIdentifier other) const { return this->key == other; }
};
/* 9 items can be displayed on the touch bar when using default buttons. */
@ -493,7 +491,7 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
- (void)touchBarButtonAction:(id)sender
{
NSButton *btn = (NSButton *)sender;
if (auto item = std::find(_touchbar_buttons.cbegin(), _touchbar_buttons.cend(), (NSTouchBarItemIdentifier)btn.identifier); item != _touchbar_buttons.cend()) {
if (auto item = std::ranges::find(_touchbar_buttons, (NSTouchBarItemIdentifier)btn.identifier, &TouchBarButton::key); item != _touchbar_buttons.end()) {
HandleToolbarHotkey(item->hotkey);
}
}
@ -519,8 +517,8 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
- (nullable NSTouchBarItem *)touchBar:(NSTouchBar *)touchBar makeItemForIdentifier:(NSTouchBarItemIdentifier)identifier
{
auto item = std::find(_touchbar_buttons.cbegin(), _touchbar_buttons.cend(), identifier);
assert(item != _touchbar_buttons.cend());
auto item = std::ranges::find(_touchbar_buttons, identifier, &TouchBarButton::key);
assert(item != _touchbar_buttons.end());
NSButton *button = [ NSButton buttonWithTitle:item->fallback_text target:self action:@selector(touchBarButtonAction:) ];
button.identifier = identifier;
@ -540,8 +538,8 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
/* Re-create button images from OTTD sprites. */
for (NSTouchBarItemIdentifier ident in self.touchBar.itemIdentifiers) {
auto item = std::find(_touchbar_buttons.cbegin(), _touchbar_buttons.cend(), ident);
if (item == _touchbar_buttons.cend()) continue;
auto item = std::ranges::find(_touchbar_buttons, ident, &TouchBarButton::key);
if (item == _touchbar_buttons.end()) continue;
NSCustomTouchBarItem *tb_item = [ self.touchBar itemForIdentifier:ident ];
NSButton *button = tb_item.view;
@ -830,7 +828,7 @@ void CocoaDialog(const char *title, const char *message, const char *buttonLabel
BOOL interpret_keys = YES;
if (down) {
/* Map keycode to OTTD code. */
auto vk = std::find_if(std::begin(_vk_mapping), std::end(_vk_mapping), [=](const CocoaVkMapping &m) { return m.vk_from == keycode; });
auto vk = std::ranges::find(_vk_mapping, keycode, &CocoaVkMapping::vk_from);
uint32_t pressed_key = vk != std::end(_vk_mapping) ? vk->map_to : 0;
if (modifiers & NSEventModifierFlagShift) pressed_key |= WKC_SHIFT;