diff --git a/src/blitter/factory.hpp b/src/blitter/factory.hpp index ea7793dc11..554f1e4292 100644 --- a/src/blitter/factory.hpp +++ b/src/blitter/factory.hpp @@ -124,9 +124,8 @@ public: if (GetBlitters().size() == 0) return nullptr; const char *bname = name.empty() ? default_blitter : name.c_str(); - Blitters::iterator it = GetBlitters().begin(); - for (; it != GetBlitters().end(); it++) { - BlitterFactory *b = (*it).second; + for (auto &it : GetBlitters()) { + BlitterFactory *b = it.second; if (StrEqualsIgnoreCase(bname, b->name)) { return b->IsUsable() ? b : nullptr; } @@ -151,9 +150,8 @@ public: static char *GetBlittersInfo(char *p, const char *last) { p += seprintf(p, last, "List of blitters:\n"); - Blitters::iterator it = GetBlitters().begin(); - for (; it != GetBlitters().end(); it++) { - BlitterFactory *b = (*it).second; + for (auto &it : GetBlitters()) { + BlitterFactory *b = it.second; p += seprintf(p, last, "%18s: %s\n", b->name.c_str(), b->GetDescription().c_str()); } p += seprintf(p, last, "\n"); diff --git a/src/cargopacket.cpp b/src/cargopacket.cpp index e79de1e14b..28523e2844 100644 --- a/src/cargopacket.cpp +++ b/src/cargopacket.cpp @@ -380,8 +380,7 @@ void VehicleCargoList::AddToMeta(const CargoPacket *cp, MoveToAction action) */ void VehicleCargoList::AgeCargo() { - for (ConstIterator it(this->packets.begin()); it != this->packets.end(); it++) { - CargoPacket *cp = *it; + for (const auto &cp : this->packets) { /* If we're at the maximum, then we can't increase no more. */ if (cp->days_in_transit == UINT16_MAX) continue; diff --git a/src/driver.cpp b/src/driver.cpp index 1e4f5e0bd9..eefcbbcb12 100644 --- a/src/driver.cpp +++ b/src/driver.cpp @@ -105,9 +105,8 @@ bool DriverFactoryBase::SelectDriverImpl(const std::string &name, Driver::Type t if (name.empty()) { /* Probe for this driver, but do not fall back to dedicated/null! */ for (int priority = 10; priority > 0; priority--) { - Drivers::iterator it = GetDrivers().begin(); - for (; it != GetDrivers().end(); ++it) { - DriverFactoryBase *d = (*it).second; + for (auto &it : GetDrivers()) { + DriverFactoryBase *d = it.second; /* Check driver type */ if (d->type != type) continue; @@ -151,9 +150,8 @@ bool DriverFactoryBase::SelectDriverImpl(const std::string &name, Driver::Type t } /* Find this driver */ - Drivers::iterator it = GetDrivers().begin(); - for (; it != GetDrivers().end(); ++it) { - DriverFactoryBase *d = (*it).second; + for (auto &it : GetDrivers()) { + DriverFactoryBase *d = it.second; /* Check driver type */ if (d->type != type) continue; @@ -191,9 +189,8 @@ char *DriverFactoryBase::GetDriversInfo(char *p, const char *last) p += seprintf(p, last, "List of %s drivers:\n", GetDriverTypeName(type)); for (int priority = 10; priority >= 0; priority--) { - Drivers::iterator it = GetDrivers().begin(); - for (; it != GetDrivers().end(); it++) { - DriverFactoryBase *d = (*it).second; + for (auto &it : GetDrivers()) { + DriverFactoryBase *d = it.second; if (d->type != type) continue; if (d->priority != priority) continue; p += seprintf(p, last, "%18s: %s\n", d->name, d->GetDescription()); diff --git a/src/economy.cpp b/src/economy.cpp index b4d9f59aaa..644d6e023c 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -1899,12 +1899,9 @@ void LoadUnloadStation(Station *st) if (st->loading_vehicles.empty()) return; Vehicle *last_loading = nullptr; - std::list::iterator iter; /* Check if anything will be loaded at all. Otherwise we don't need to reserve either. */ - for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) { - Vehicle *v = *iter; - + for (Vehicle *v : st->loading_vehicles) { if ((v->vehstatus & (VS_STOPPED | VS_CRASHED))) continue; assert(v->load_unload_ticks != 0); @@ -1920,8 +1917,7 @@ void LoadUnloadStation(Station *st) */ if (last_loading == nullptr) return; - for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) { - Vehicle *v = *iter; + for (Vehicle *v : st->loading_vehicles) { if (!(v->vehstatus & (VS_STOPPED | VS_CRASHED))) LoadUnloadVehicle(v); if (v == last_loading) break; } diff --git a/src/fileio.cpp b/src/fileio.cpp index 758b2cf2a5..2345f7583b 100644 --- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -288,12 +288,12 @@ FILE *FioFOpenFile(const std::string &filename, const char *mode, Subdirectory s } /* Resolve ONE directory link */ - for (TarLinkList::iterator link = _tar_linklist[subdir].begin(); link != _tar_linklist[subdir].end(); link++) { - const std::string &src = link->first; + for (const auto &link : _tar_linklist[subdir]) { + const std::string &src = link.first; size_t len = src.length(); if (resolved_name.length() >= len && resolved_name[len - 1] == PATHSEPCHAR && src.compare(0, len, resolved_name, 0, len) == 0) { /* Apply link */ - resolved_name.replace(0, len, link->second); + resolved_name.replace(0, len, link.second); break; // Only resolve one level } } @@ -666,10 +666,8 @@ bool TarScanner::AddFile(const std::string &filename, size_t basepath_length, co * The destination path must NOT contain any links. * The source path may contain one directory link. */ - for (TarLinkList::iterator link = links.begin(); link != links.end(); link++) { - const std::string &src = link->first; - const std::string &dest = link->second; - TarAddLink(src, dest, this->subdir); + for (auto &it : links) { + TarAddLink(it.first, it.second, this->subdir); } return true; @@ -705,16 +703,16 @@ bool ExtractTar(const std::string &tar_filename, Subdirectory subdir) Debug(misc, 8, "Extracting {} to directory {}", tar_filename, filename); FioCreateDirectory(filename); - for (TarFileList::iterator it2 = _tar_filelist[subdir].begin(); it2 != _tar_filelist[subdir].end(); it2++) { - if (tar_filename != it2->second.tar_filename) continue; + for (auto &it2 : _tar_filelist[subdir]) { + if (tar_filename != it2.second.tar_filename) continue; - filename.replace(p + 1, std::string::npos, it2->first); + filename.replace(p + 1, std::string::npos, it2.first); Debug(misc, 9, " extracting {}", filename); /* First open the file in the .tar. */ size_t to_copy = 0; - std::unique_ptr in(FioFOpenFileTar(it2->second, &to_copy)); + std::unique_ptr in(FioFOpenFileTar(it2.second, &to_copy)); if (!in) { Debug(misc, 6, "Extracting {} failed; could not open {}", filename, tar_filename); return false; diff --git a/src/linkgraph/flowmapper.cpp b/src/linkgraph/flowmapper.cpp index eb86d5e32e..7ddbafc788 100644 --- a/src/linkgraph/flowmapper.cpp +++ b/src/linkgraph/flowmapper.cpp @@ -51,8 +51,8 @@ void FlowMapper::Run(LinkGraphJob &job) const * division by 0 if spawn date == last compression date. This matches * LinkGraph::Monthly(). */ uint runtime = job.JoinDate() - job.Settings().recalc_time - job.LastCompression() + 1; - for (FlowStatMap::iterator i = flows.begin(); i != flows.end(); ++i) { - i->second.ScaleToMonthly(runtime); + for (auto &it : flows) { + it.second.ScaleToMonthly(runtime); } } /* Clear paths. */ diff --git a/src/linkgraph/linkgraph_gui.cpp b/src/linkgraph/linkgraph_gui.cpp index ad6415e91e..077bf422c0 100644 --- a/src/linkgraph/linkgraph_gui.cpp +++ b/src/linkgraph/linkgraph_gui.cpp @@ -273,14 +273,14 @@ void LinkGraphOverlay::Draw(const DrawPixelInfo *dpi) void LinkGraphOverlay::DrawLinks(const DrawPixelInfo *dpi) const { int width = ScaleGUITrad(this->scale); - for (LinkMap::const_iterator i(this->cached_links.begin()); i != this->cached_links.end(); ++i) { - if (!Station::IsValidID(i->first)) continue; - Point pta = this->GetStationMiddle(Station::Get(i->first)); - for (StationLinkMap::const_iterator j(i->second.begin()); j != i->second.end(); ++j) { - if (!Station::IsValidID(j->first)) continue; - Point ptb = this->GetStationMiddle(Station::Get(j->first)); + for (const auto &i : this->cached_links) { + if (!Station::IsValidID(i.first)) continue; + Point pta = this->GetStationMiddle(Station::Get(i.first)); + for (const auto &j : i.second) { + if (!Station::IsValidID(j.first)) continue; + Point ptb = this->GetStationMiddle(Station::Get(j.first)); if (!this->IsLinkVisible(pta, ptb, dpi, width + 2)) continue; - this->DrawContent(pta, ptb, j->second); + this->DrawContent(pta, ptb, j.second); } } } @@ -319,13 +319,13 @@ void LinkGraphOverlay::DrawContent(Point pta, Point ptb, const LinkProperties &c void LinkGraphOverlay::DrawStationDots(const DrawPixelInfo *dpi) const { int width = ScaleGUITrad(this->scale); - for (StationSupplyList::const_iterator i(this->cached_stations.begin()); i != this->cached_stations.end(); ++i) { - const Station *st = Station::GetIfValid(i->first); + for (const auto &i : this->cached_stations) { + const Station *st = Station::GetIfValid(i.first); if (st == nullptr) continue; Point pt = this->GetStationMiddle(st); if (!this->IsPointVisible(pt, dpi, 3 * width)) continue; - uint r = width * 2 + width * 2 * std::min(200U, i->second) / 200; + uint r = width * 2 + width * 2 * std::min(200U, i.second) / 200; LinkGraphOverlay::DrawVertex(pt.x, pt.y, r, _colour_gradient[st->owner != OWNER_NONE ? diff --git a/src/linkgraph/linkgraphschedule.cpp b/src/linkgraph/linkgraphschedule.cpp index d868c9d25f..3c3a12f4fe 100644 --- a/src/linkgraph/linkgraphschedule.cpp +++ b/src/linkgraph/linkgraphschedule.cpp @@ -110,8 +110,8 @@ void LinkGraphSchedule::JoinNext() */ void LinkGraphSchedule::SpawnAll() { - for (JobList::iterator i = this->running.begin(); i != this->running.end(); ++i) { - (*i)->SpawnThread(); + for (auto &it : this->running) { + it->SpawnThread(); } } @@ -120,8 +120,8 @@ void LinkGraphSchedule::SpawnAll() */ /* static */ void LinkGraphSchedule::Clear() { - for (JobList::iterator i(instance.running.begin()); i != instance.running.end(); ++i) { - (*i)->AbortJob(); + for (auto &it : instance.running) { + it->AbortJob(); } instance.running.clear(); instance.schedule.clear(); diff --git a/src/linkgraph/refresh.cpp b/src/linkgraph/refresh.cpp index f62a4347d3..5ebe157ef8 100644 --- a/src/linkgraph/refresh.cpp +++ b/src/linkgraph/refresh.cpp @@ -144,10 +144,10 @@ bool LinkRefresher::HandleRefit(CargoID refit_cargo) */ void LinkRefresher::ResetRefit() { - for (RefitList::iterator it(this->refit_capacities.begin()); it != this->refit_capacities.end(); ++it) { - if (it->remaining == it->capacity) continue; - this->capacities[it->cargo] += it->capacity - it->remaining; - it->remaining = it->capacity; + for (auto &it : this->refit_capacities) { + if (it.remaining == it.capacity) continue; + this->capacities[it.cargo] += it.capacity - it.remaining; + it.remaining = it.capacity; } } diff --git a/src/music_gui.cpp b/src/music_gui.cpp index 8e4fb39465..1bf66211f1 100644 --- a/src/music_gui.cpp +++ b/src/music_gui.cpp @@ -413,9 +413,9 @@ void MusicSystem::SaveCustomPlaylist(PlaylistChoices pl) size_t num = 0; MemSetT(settings_pl, 0, NUM_SONGS_PLAYLIST); - for (Playlist::const_iterator song = this->standard_playlists[pl].begin(); song != this->standard_playlists[pl].end(); ++song) { + for (const auto &song : this->standard_playlists[pl]) { /* Music set indices in the settings playlist are 1-based, 0 means unused slot */ - settings_pl[num++] = (byte)song->set_index + 1; + settings_pl[num++] = (byte)song.set_index + 1; } } @@ -510,10 +510,10 @@ struct MusicTrackSelectionWindow : public Window { case WID_MTS_LIST_LEFT: case WID_MTS_LIST_RIGHT: { Dimension d = {0, 0}; - for (MusicSystem::Playlist::const_iterator song = _music.music_set.begin(); song != _music.music_set.end(); ++song) { - SetDParam(0, song->tracknr); + for (const auto &song : _music.music_set) { + SetDParam(0, song.tracknr); SetDParam(1, 2); - SetDParamStr(2, song->songname); + SetDParamStr(2, song.songname); Dimension d2 = GetStringBoundingBox(STR_PLAYLIST_TRACK_NAME); d.width = std::max(d.width, d2.width); d.height += d2.height; @@ -533,10 +533,10 @@ struct MusicTrackSelectionWindow : public Window { GfxFillRect(r.Shrink(WidgetDimensions::scaled.bevel), PC_BLACK); Rect tr = r.Shrink(WidgetDimensions::scaled.framerect); - for (MusicSystem::Playlist::const_iterator song = _music.music_set.begin(); song != _music.music_set.end(); ++song) { - SetDParam(0, song->tracknr); + for (const auto &song : _music.music_set) { + SetDParam(0, song.tracknr); SetDParam(1, 2); - SetDParamStr(2, song->songname); + SetDParamStr(2, song.songname); DrawString(tr, STR_PLAYLIST_TRACK_NAME); tr.top += FONT_HEIGHT_SMALL; } @@ -547,10 +547,10 @@ struct MusicTrackSelectionWindow : public Window { GfxFillRect(r.Shrink(WidgetDimensions::scaled.bevel), PC_BLACK); Rect tr = r.Shrink(WidgetDimensions::scaled.framerect); - for (MusicSystem::Playlist::const_iterator song = _music.active_playlist.begin(); song != _music.active_playlist.end(); ++song) { - SetDParam(0, song->tracknr); + for (const auto &song : _music.active_playlist) { + SetDParam(0, song.tracknr); SetDParam(1, 2); - SetDParamStr(2, song->songname); + SetDParamStr(2, song.songname); DrawString(tr, STR_PLAYLIST_TRACK_NAME); tr.top += FONT_HEIGHT_SMALL; } @@ -698,8 +698,8 @@ struct MusicWindow : public Window { case WID_M_TRACK_NAME: { Dimension d = GetStringBoundingBox(STR_MUSIC_TITLE_NONE); - for (MusicSystem::Playlist::const_iterator song = _music.music_set.begin(); song != _music.music_set.end(); ++song) { - SetDParamStr(0, song->songname); + for (const auto &song : _music.music_set) { + SetDParamStr(0, song.songname); d = maxdim(d, GetStringBoundingBox(STR_MUSIC_TITLE_NAME)); } d.width += padding.width; diff --git a/src/newgrf_generic.cpp b/src/newgrf_generic.cpp index b795aff417..f2e15f68bd 100644 --- a/src/newgrf_generic.cpp +++ b/src/newgrf_generic.cpp @@ -170,16 +170,16 @@ static uint16 GetGenericCallbackResult(uint8 feature, ResolverObject &object, ui assert(feature < lengthof(_gcl)); /* Test each feature callback sprite group. */ - for (GenericCallbackList::const_iterator it = _gcl[feature].begin(); it != _gcl[feature].end(); ++it) { - object.grffile = it->file; - object.root_spritegroup = it->group; + for (const auto &it : _gcl[feature]) { + object.grffile = it.file; + object.root_spritegroup = it.group; /* Set callback param based on GRF version. */ - object.callback_param1 = it->file->grf_version >= 8 ? param1_grfv8 : param1_grfv7; + object.callback_param1 = it.file->grf_version >= 8 ? param1_grfv8 : param1_grfv7; uint16 result = object.ResolveCallback(); if (result == CALLBACK_FAILED) continue; /* Return NewGRF file if necessary */ - if (file != nullptr) *file = it->file; + if (file != nullptr) *file = it.file; return result; } diff --git a/src/newgrf_storage.cpp b/src/newgrf_storage.cpp index dded197c5f..a0130f829a 100644 --- a/src/newgrf_storage.cpp +++ b/src/newgrf_storage.cpp @@ -91,9 +91,9 @@ void AddChangedPersistentStorage(BasePersistentStorageArray *storage) } /* Discard all temporary changes */ - for (std::set::iterator it = _changed_storage_arrays->begin(); it != _changed_storage_arrays->end(); it++) { - Debug(desync, 1, "Discarding persistent storage changes: Feature {}, GrfID {:08X}, Tile {}", (*it)->feature, BSWAP32((*it)->grfid), (*it)->tile); - (*it)->ClearChanges(); + for (auto &it : *_changed_storage_arrays) { + Debug(desync, 1, "Discarding persistent storage changes: Feature {}, GrfID {:08X}, Tile {}", it->feature, BSWAP32(it->grfid), it->tile); + it->ClearChanges(); } _changed_storage_arrays->clear(); } diff --git a/src/newgrf_town.cpp b/src/newgrf_town.cpp index 90eeede78d..c13f37eae4 100644 --- a/src/newgrf_town.cpp +++ b/src/newgrf_town.cpp @@ -35,9 +35,8 @@ grfid = this->ro.grffile->grfid; } - std::list::iterator iter; - for (iter = this->t->psa_list.begin(); iter != this->t->psa_list.end(); iter++) { - if ((*iter)->grfid == grfid) return (*iter)->GetValue(parameter); + for (auto &it : this->t->psa_list) { + if (it->grfid == grfid) return it->GetValue(parameter); } return 0; @@ -133,10 +132,9 @@ if (grfid != this->ro.grffile->grfid) return; /* Check if the storage exists. */ - std::list::iterator iter; - for (iter = t->psa_list.begin(); iter != t->psa_list.end(); iter++) { - if ((*iter)->grfid == grfid) { - (*iter)->StoreValue(pos, value); + for (auto &it : t->psa_list) { + if (it->grfid == grfid) { + it->StoreValue(pos, value); return; } } diff --git a/src/roadstop.cpp b/src/roadstop.cpp index a7f25fe0c3..e9f3a9a238 100644 --- a/src/roadstop.cpp +++ b/src/roadstop.cpp @@ -336,8 +336,8 @@ Vehicle *FindVehiclesInRoadStop(Vehicle *v, void *data) if (rv->state < RVSB_IN_ROAD_STOP) return nullptr; /* Do not add duplicates! */ - for (RVList::iterator it = rserh->vehicles.begin(); it != rserh->vehicles.end(); it++) { - if (rv == *it) return nullptr; + for (const auto &it : rserh->vehicles) { + if (rv == it) return nullptr; } rserh->vehicles.push_back(rv); @@ -367,8 +367,8 @@ void RoadStop::Entry::Rebuild(const RoadStop *rs, int side) } this->occupied = 0; - for (RVList::iterator it = rserh.vehicles.begin(); it != rserh.vehicles.end(); it++) { - this->occupied += (*it)->gcache.cached_total_length; + for (const auto &it : rserh.vehicles) { + this->occupied += it->gcache.cached_total_length; } } diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 9faa0c13a3..735490433c 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -283,9 +283,9 @@ static void InitializeWindowsAndCaches() } } for (Town *t : Town::Iterate()) { - for (std::list::iterator it = t->psa_list.begin(); it != t->psa_list.end(); ++it) { - (*it)->feature = GSF_FAKE_TOWNS; - (*it)->tile = t->xy; + for (auto &it : t->psa_list) { + it->feature = GSF_FAKE_TOWNS; + it->tile = t->xy; } } for (RoadVehicle *rv : RoadVehicle::Iterate()) { @@ -2157,13 +2157,11 @@ bool AfterLoadGame() * add cargopayment for the vehicles that don't have it. */ for (Station *st : Station::Iterate()) { - std::list::iterator iter; - for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end(); ++iter) { + for (Vehicle *v : st->loading_vehicles) { /* There are always as many CargoPayments as Vehicles. We need to make the * assert() in Pool::GetNew() happy by calling CanAllocateItem(). */ static_assert(CargoPaymentPool::MAX_SIZE == VehiclePool::MAX_SIZE); assert(CargoPayment::CanAllocateItem()); - Vehicle *v = *iter; if (v->cargo_payment == nullptr) v->cargo_payment = new CargoPayment(v); } } diff --git a/src/saveload/station_sl.cpp b/src/saveload/station_sl.cpp index 93f4db3171..3c1de1290e 100644 --- a/src/saveload/station_sl.cpp +++ b/src/saveload/station_sl.cpp @@ -310,22 +310,22 @@ public: void Save(GoodsEntry *ge) const override { - uint32 num_flows = 0; - for (FlowStatMap::const_iterator it(ge->flows.begin()); it != ge->flows.end(); ++it) { - num_flows += (uint32)it->second.GetShares()->size(); + size_t num_flows = 0; + for (const auto &it : ge->flows) { + num_flows += it.second.GetShares()->size(); } SlSetStructListLength(num_flows); - for (FlowStatMap::const_iterator outer_it(ge->flows.begin()); outer_it != ge->flows.end(); ++outer_it) { - const FlowStat::SharesMap *shares = outer_it->second.GetShares(); + for (const auto &outer_it : ge->flows) { + const FlowStat::SharesMap *shares = outer_it.second.GetShares(); uint32 sum_shares = 0; FlowSaveLoad flow; - flow.source = outer_it->first; - for (FlowStat::SharesMap::const_iterator inner_it(shares->begin()); inner_it != shares->end(); ++inner_it) { - flow.via = inner_it->second; - flow.share = inner_it->first - sum_shares; - flow.restricted = inner_it->first > outer_it->second.GetUnrestricted(); - sum_shares = inner_it->first; + flow.source = outer_it.first; + for (auto &inner_it : *shares) { + flow.via = inner_it.second; + flow.share = inner_it.first - sum_shares; + flow.restricted = inner_it.first > outer_it.second.GetUnrestricted(); + sum_shares = inner_it.first; assert(flow.share > 0); SlObject(&flow, this->GetDescription()); } diff --git a/src/script/api/script_list.cpp b/src/script/api/script_list.cpp index ddee29fbd7..14bb9293d5 100644 --- a/src/script/api/script_list.cpp +++ b/src/script/api/script_list.cpp @@ -564,9 +564,9 @@ void ScriptList::AddList(ScriptList *list) this->modifications++; } else { ScriptListMap *list_items = &list->items; - for (ScriptListMap::iterator iter = list_items->begin(); iter != list_items->end(); iter++) { - this->AddItem((*iter).first); - this->SetValue((*iter).first, (*iter).second); + for (auto &it : *list_items) { + this->AddItem(it.first); + this->SetValue(it.first, it.second); } } } diff --git a/src/settings_gui.cpp b/src/settings_gui.cpp index af0c90e17b..8b14e89229 100644 --- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -1347,8 +1347,8 @@ void SettingEntry::DrawSetting(GameSettings *settings_ptr, int left, int right, */ void SettingsContainer::Init(byte level) { - for (EntryVector::iterator it = this->entries.begin(); it != this->entries.end(); ++it) { - (*it)->Init(level); + for (auto &it : this->entries) { + it->Init(level); } } @@ -1363,16 +1363,16 @@ void SettingsContainer::ResetAll() /** Recursively close all folds of sub-pages */ void SettingsContainer::FoldAll() { - for (EntryVector::iterator it = this->entries.begin(); it != this->entries.end(); ++it) { - (*it)->FoldAll(); + for (auto &it : this->entries) { + it->FoldAll(); } } /** Recursively open all folds of sub-pages */ void SettingsContainer::UnFoldAll() { - for (EntryVector::iterator it = this->entries.begin(); it != this->entries.end(); ++it) { - (*it)->UnFoldAll(); + for (auto &it : this->entries) { + it->UnFoldAll(); } } @@ -1383,8 +1383,8 @@ void SettingsContainer::UnFoldAll() */ void SettingsContainer::GetFoldingState(bool &all_folded, bool &all_unfolded) const { - for (EntryVector::const_iterator it = this->entries.begin(); it != this->entries.end(); ++it) { - (*it)->GetFoldingState(all_folded, all_unfolded); + for (auto &it : this->entries) { + it->GetFoldingState(all_folded, all_unfolded); } } @@ -1415,8 +1415,8 @@ bool SettingsContainer::UpdateFilterState(SettingFilter &filter, bool force_visi */ bool SettingsContainer::IsVisible(const BaseSettingEntry *item) const { - for (EntryVector::const_iterator it = this->entries.begin(); it != this->entries.end(); ++it) { - if ((*it)->IsVisible(item)) return true; + for (const auto &it : this->entries) { + if (it->IsVisible(item)) return true; } return false; } @@ -1425,8 +1425,8 @@ bool SettingsContainer::IsVisible(const BaseSettingEntry *item) const uint SettingsContainer::Length() const { uint length = 0; - for (EntryVector::const_iterator it = this->entries.begin(); it != this->entries.end(); ++it) { - length += (*it)->Length(); + for (const auto &it : this->entries) { + length += it->Length(); } return length; } @@ -1440,8 +1440,8 @@ uint SettingsContainer::Length() const BaseSettingEntry *SettingsContainer::FindEntry(uint row_num, uint *cur_row) { BaseSettingEntry *pe = nullptr; - for (EntryVector::iterator it = this->entries.begin(); it != this->entries.end(); ++it) { - pe = (*it)->FindEntry(row_num, cur_row); + for (const auto &it : this->entries) { + pe = it->FindEntry(row_num, cur_row); if (pe != nullptr) { break; } @@ -1457,8 +1457,8 @@ BaseSettingEntry *SettingsContainer::FindEntry(uint row_num, uint *cur_row) uint SettingsContainer::GetMaxHelpHeight(int maxw) { uint biggest = 0; - for (EntryVector::const_iterator it = this->entries.begin(); it != this->entries.end(); ++it) { - biggest = std::max(biggest, (*it)->GetMaxHelpHeight(maxw)); + for (const auto &it : this->entries) { + biggest = std::max(biggest, it->GetMaxHelpHeight(maxw)); } return biggest; } @@ -1480,11 +1480,9 @@ uint SettingsContainer::GetMaxHelpHeight(int maxw) */ uint SettingsContainer::Draw(GameSettings *settings_ptr, int left, int right, int y, uint first_row, uint max_row, BaseSettingEntry *selected, uint cur_row, uint parent_last) const { - for (EntryVector::const_iterator it = this->entries.begin(); it != this->entries.end(); ++it) { - cur_row = (*it)->Draw(settings_ptr, left, right, y, first_row, max_row, selected, cur_row, parent_last); - if (cur_row >= max_row) { - break; - } + for (const auto &it : this->entries) { + cur_row = it->Draw(settings_ptr, left, right, y, first_row, max_row, selected, cur_row, parent_last); + if (cur_row >= max_row) break; } return cur_row; } diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 5d92b3c963..20b2143f93 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -3781,10 +3781,10 @@ void RerouteCargo(Station *st, CargoID c, StationID avoid, StationID avoid2) ge.cargo.Reroute(UINT_MAX, &ge.cargo, avoid, avoid2, &ge); /* Reroute cargo staged to be transferred. */ - for (std::list::iterator it(st->loading_vehicles.begin()); it != st->loading_vehicles.end(); ++it) { - for (Vehicle *v = *it; v != nullptr; v = v->Next()) { - if (v->cargo_type != c) continue; - v->cargo.Reroute(UINT_MAX, &v->cargo, avoid, avoid2, &ge); + for (Vehicle *v : st->loading_vehicles) { + for (Vehicle *u = v; u != nullptr; u = u->Next()) { + if (u->cargo_type != c) continue; + u->cargo.Reroute(UINT_MAX, &u->cargo, avoid, avoid2, &ge); } } } @@ -4531,11 +4531,11 @@ static CommandCost TerraformTile_Station(TileIndex tile, DoCommandFlag flags, in uint FlowStat::GetShare(StationID st) const { uint32 prev = 0; - for (SharesMap::const_iterator it = this->shares.begin(); it != this->shares.end(); ++it) { - if (it->second == st) { - return it->first - prev; + for (const auto &it : this->shares) { + if (it.second == st) { + return it.first - prev; } else { - prev = it->first; + prev = it.first; } } return 0; @@ -4605,9 +4605,9 @@ void FlowStat::Invalidate() assert(!this->shares.empty()); SharesMap new_shares; uint i = 0; - for (SharesMap::iterator it(this->shares.begin()); it != this->shares.end(); ++it) { - new_shares[++i] = it->second; - if (it->first == this->unrestricted) this->unrestricted = i; + for (const auto &it : this->shares) { + new_shares[++i] = it.second; + if (it.first == this->unrestricted) this->unrestricted = i; } this->shares.swap(new_shares); assert(!this->shares.empty() && this->unrestricted <= (--this->shares.end())->first); @@ -4629,29 +4629,29 @@ void FlowStat::ChangeShare(StationID st, int flow) uint added_shares = 0; uint last_share = 0; SharesMap new_shares; - for (SharesMap::iterator it(this->shares.begin()); it != this->shares.end(); ++it) { - if (it->second == st) { + for (const auto &it : this->shares) { + if (it.second == st) { if (flow < 0) { - uint share = it->first - last_share; + uint share = it.first - last_share; if (flow == INT_MIN || (uint)(-flow) >= share) { removed_shares += share; - if (it->first <= this->unrestricted) this->unrestricted -= share; + if (it.first <= this->unrestricted) this->unrestricted -= share; if (flow != INT_MIN) flow += share; - last_share = it->first; + last_share = it.first; continue; // remove the whole share } removed_shares += (uint)(-flow); } else { added_shares += (uint)(flow); } - if (it->first <= this->unrestricted) this->unrestricted += flow; + if (it.first <= this->unrestricted) this->unrestricted += flow; /* If we don't continue above the whole flow has been added or * removed. */ flow = 0; } - new_shares[it->first + added_shares - removed_shares] = it->second; - last_share = it->first; + new_shares[it.first + added_shares - removed_shares] = it.second; + last_share = it.first; } if (flow > 0) { new_shares[last_share + (uint)flow] = st; @@ -4675,19 +4675,19 @@ void FlowStat::RestrictShare(StationID st) uint flow = 0; uint last_share = 0; SharesMap new_shares; - for (SharesMap::iterator it(this->shares.begin()); it != this->shares.end(); ++it) { + for (auto &it : this->shares) { if (flow == 0) { - if (it->first > this->unrestricted) return; // Not present or already restricted. - if (it->second == st) { - flow = it->first - last_share; + if (it.first > this->unrestricted) return; // Not present or already restricted. + if (it.second == st) { + flow = it.first - last_share; this->unrestricted -= flow; } else { - new_shares[it->first] = it->second; + new_shares[it.first] = it.second; } } else { - new_shares[it->first - flow] = it->second; + new_shares[it.first - flow] = it.second; } - last_share = it->first; + last_share = it.first; } if (flow == 0) return; new_shares[last_share + flow] = st; @@ -4742,10 +4742,10 @@ void FlowStat::ScaleToMonthly(uint runtime) assert(runtime > 0); SharesMap new_shares; uint share = 0; - for (SharesMap::iterator i = this->shares.begin(); i != this->shares.end(); ++i) { - share = std::max(share + 1, i->first * 30 / runtime); - new_shares[share] = i->second; - if (this->unrestricted == i->first) this->unrestricted = share; + for (auto i : this->shares) { + share = std::max(share + 1, i.first * 30 / runtime); + new_shares[share] = i.second; + if (this->unrestricted == i.first) this->unrestricted = share; } this->shares.swap(new_shares); } @@ -4795,8 +4795,8 @@ void FlowStatMap::PassOnFlow(StationID origin, StationID via, uint flow) */ void FlowStatMap::FinalizeLocalConsumption(StationID self) { - for (FlowStatMap::iterator i = this->begin(); i != this->end(); ++i) { - FlowStat &fs = i->second; + for (auto &i : *this) { + FlowStat &fs = i.second; uint local = fs.GetShare(INVALID_STATION); if (local > INT_MAX) { // make sure it fits in an int fs.ChangeShare(self, -INT_MAX); @@ -4840,8 +4840,8 @@ StationIDStack FlowStatMap::DeleteFlows(StationID via) */ void FlowStatMap::RestrictFlows(StationID via) { - for (FlowStatMap::iterator it = this->begin(); it != this->end(); ++it) { - it->second.RestrictShare(via); + for (auto &it : *this) { + it.second.RestrictShare(via); } } @@ -4851,8 +4851,8 @@ void FlowStatMap::RestrictFlows(StationID via) */ void FlowStatMap::ReleaseFlows(StationID via) { - for (FlowStatMap::iterator it = this->begin(); it != this->end(); ++it) { - it->second.ReleaseShare(via); + for (auto &it : *this) { + it.second.ReleaseShare(via); } } @@ -4863,8 +4863,8 @@ void FlowStatMap::ReleaseFlows(StationID via) uint FlowStatMap::GetFlow() const { uint ret = 0; - for (FlowStatMap::const_iterator i = this->begin(); i != this->end(); ++i) { - ret += (--(i->second.GetShares()->end()))->first; + for (const auto &it : *this) { + ret += (--(it.second.GetShares()->end()))->first; } return ret; } @@ -4877,8 +4877,8 @@ uint FlowStatMap::GetFlow() const uint FlowStatMap::GetFlowVia(StationID via) const { uint ret = 0; - for (FlowStatMap::const_iterator i = this->begin(); i != this->end(); ++i) { - ret += i->second.GetShare(via); + for (const auto &it : *this) { + ret += it.second.GetShare(via); } return ret; } diff --git a/src/station_gui.cpp b/src/station_gui.cpp index 13c4fb469b..b8ec485987 100644 --- a/src/station_gui.cpp +++ b/src/station_gui.cpp @@ -1058,9 +1058,9 @@ CargoDataEntry::~CargoDataEntry() void CargoDataEntry::Clear() { if (this->children != nullptr) { - for (CargoDataSet::iterator i = this->children->begin(); i != this->children->end(); ++i) { - assert(*i != this); - delete *i; + for (auto &it : *this->children) { + assert(it != this); + delete it; } this->children->clear(); } @@ -1464,21 +1464,19 @@ struct StationViewWindow : public Window { CargoDataEntry *cargo_entry = cached_destinations.InsertOrRetrieve(i); cargo_entry->Clear(); - const FlowStatMap &flows = st->goods[i].flows; - for (FlowStatMap::const_iterator it = flows.begin(); it != flows.end(); ++it) { - StationID from = it->first; + for (const auto &it : st->goods[i].flows) { + StationID from = it.first; CargoDataEntry *source_entry = cargo_entry->InsertOrRetrieve(from); - const FlowStat::SharesMap *shares = it->second.GetShares(); uint32 prev_count = 0; - for (FlowStat::SharesMap::const_iterator flow_it = shares->begin(); flow_it != shares->end(); ++flow_it) { - StationID via = flow_it->second; + for (const auto &flow_it : *it.second.GetShares()) { + StationID via = flow_it.second; CargoDataEntry *via_entry = source_entry->InsertOrRetrieve(via); if (via == this->window_number) { - via_entry->InsertOrRetrieve(via)->Update(flow_it->first - prev_count); + via_entry->InsertOrRetrieve(via)->Update(flow_it.first - prev_count); } else { - EstimateDestinations(i, from, via, flow_it->first - prev_count, via_entry); + EstimateDestinations(i, from, via, flow_it.first - prev_count, via_entry); } - prev_count = flow_it->first; + prev_count = flow_it.first; } } } diff --git a/src/table/newgrf_debug_data.h b/src/table/newgrf_debug_data.h index b99a11c4df..0e6af1c6e0 100644 --- a/src/table/newgrf_debug_data.h +++ b/src/table/newgrf_debug_data.h @@ -550,9 +550,8 @@ class NIHTown : public NIHelper { { Town *t = Town::Get(index); - std::list::iterator iter; - for (iter = t->psa_list.begin(); iter != t->psa_list.end(); iter++) { - if ((*iter)->grfid == grfid) return (int32 *)(&(*iter)->storage[0]); + for (const auto &it : t->psa_list) { + if (it->grfid == grfid) return &it->storage[0]; } return nullptr; diff --git a/src/terraform_cmd.cpp b/src/terraform_cmd.cpp index 742e8e201c..dbcd3ccbbc 100644 --- a/src/terraform_cmd.cpp +++ b/src/terraform_cmd.cpp @@ -222,9 +222,7 @@ std::tuple CmdTerraformLand(DoCommandFlag flags, * Pass == 0: Collect tileareas which are caused to be auto-cleared. * Pass == 1: Collect the actual cost. */ for (int pass = 0; pass < 2; pass++) { - for (TileIndexSet::const_iterator it = ts.dirty_tiles.begin(); it != ts.dirty_tiles.end(); it++) { - TileIndex t = *it; - + for (const auto &t : ts.dirty_tiles) { assert(t < Map::Size()); /* MP_VOID tiles can be terraformed but as tunnels and bridges * cannot go under / over these tiles they don't need checking. */ @@ -301,18 +299,17 @@ std::tuple CmdTerraformLand(DoCommandFlag flags, if (flags & DC_EXEC) { /* Mark affected areas dirty. */ - for (TileIndexSet::const_iterator it = ts.dirty_tiles.begin(); it != ts.dirty_tiles.end(); it++) { - MarkTileDirtyByTile(*it); - TileIndexToHeightMap::const_iterator new_height = ts.tile_to_new_height.find(*it); + for (const auto &t : ts.dirty_tiles) { + MarkTileDirtyByTile(t); + TileIndexToHeightMap::const_iterator new_height = ts.tile_to_new_height.find(t); if (new_height == ts.tile_to_new_height.end()) continue; - MarkTileDirtyByTile(*it, 0, new_height->second); + MarkTileDirtyByTile(t, 0, new_height->second); } /* change the height */ - for (TileIndexToHeightMap::const_iterator it = ts.tile_to_new_height.begin(); - it != ts.tile_to_new_height.end(); it++) { - TileIndex t = it->first; - int height = it->second; + for (const auto &it : ts.tile_to_new_height) { + TileIndex t = it.first; + int height = it.second; SetTileHeight(t, (uint)height); }