Replace some iterator usages by for-range or algorithm.

This commit is contained in:
Jarod42 2023-08-07 11:36:34 +02:00 committed by Joris Dauphin
parent 77e3f63bb8
commit af0f4e38fb
30 changed files with 166 additions and 224 deletions

View file

@ -542,17 +542,15 @@ void FreeAi()
*/ */
static int AiRemoveFromBuilt2(PlayerAi *pai, const CUnitType &type) static int AiRemoveFromBuilt2(PlayerAi *pai, const CUnitType &type)
{ {
std::vector<AiBuildQueue>::iterator i; auto it = ranges::find_if(pai->UnitTypeBuilt,
[&](const AiBuildQueue &q) { return q.Made && q.Type == &type; });
for (i = pai->UnitTypeBuilt.begin(); i != pai->UnitTypeBuilt.end(); ++i) { if (it != pai->UnitTypeBuilt.end())
Assert((*i).Want); {
if (&type == (*i).Type && (*i).Made) { --(*it).Made;
--(*i).Made; if (!--(*it).Want) {
if (!--(*i).Want) { pai->UnitTypeBuilt.erase(it);
pai->UnitTypeBuilt.erase(i);
}
return 1;
} }
return 1;
} }
return 0; return 0;
} }
@ -593,13 +591,13 @@ static void AiRemoveFromBuilt(PlayerAi *pai, const CUnitType &type)
*/ */
static bool AiReduceMadeInBuilt2(PlayerAi &pai, const CUnitType &type) static bool AiReduceMadeInBuilt2(PlayerAi &pai, const CUnitType &type)
{ {
std::vector<AiBuildQueue>::iterator i; auto it = ranges::find_if(pai.UnitTypeBuilt,
[&](const AiBuildQueue &q) { return q.Made && q.Type == &type; });
for (i = pai.UnitTypeBuilt.begin(); i != pai.UnitTypeBuilt.end(); ++i) { if (it != pai.UnitTypeBuilt.end())
if (&type == (*i).Type && (*i).Made) { {
(*i).Made--; (*it).Made--;
return true; return true;
}
} }
return false; return false;
} }

View file

@ -484,8 +484,8 @@ CUnit *AiGetSuitableDepot(const CUnit &worker, const CUnit &oldDepot, CUnit **re
} }
std::sort(depots.begin(), depots.end(), CompareDepotsByDistance(worker)); std::sort(depots.begin(), depots.end(), CompareDepotsByDistance(worker));
for (std::vector<CUnit *>::iterator it = depots.begin(); it != depots.end(); ++it) { for (CUnit* unitPtr : depots) {
CUnit &unit = **it; CUnit &unit = *unitPtr;
const unsigned int tooManyWorkers = 15; const unsigned int tooManyWorkers = 15;
const int range = 15; const int range = 15;

View file

@ -148,11 +148,9 @@ static std::vector<CUnitType *> getSupplyUnits()
} }
} }
sorted_res.push_back(besttype); sorted_res.push_back(besttype);
for (std::vector<CUnitType *>::iterator i = res.begin(); i != res.end(); ++i) { auto it = ranges::find(res, besttype);
if (*i == besttype) { if (it != res.end()) {
i = res.erase(i); res.erase(it);
break;
}
} }
} }
return sorted_res; return sorted_res;
@ -197,11 +195,9 @@ static std::vector<CUnitType *> getRefineryUnits()
} }
} }
sorted_res.push_back(besttype); sorted_res.push_back(besttype);
for (std::vector<CUnitType *>::iterator i = res.begin(); i != res.end(); ++i) { auto it = ranges::find(res, besttype);
if (*i == besttype) { if (it != res.end()) {
i = res.erase(i); res.erase(it);
break;
}
} }
} }
return sorted_res; return sorted_res;

View file

@ -415,9 +415,7 @@ CAnimations *AnimationsByIdent(const std::string &ident)
void FreeAnimations() void FreeAnimations()
{ {
std::map<std::string, CAnimations *>::iterator it; for (auto &[key, anims] : AnimationMap) {
for (it = AnimationMap.begin(); it != AnimationMap.end(); ++it) {
CAnimations *anims = (*it).second;
delete anims; delete anims;
} }
AnimationMap.clear(); AnimationMap.clear();

View file

@ -477,8 +477,8 @@ static int WriteMapSetup(const fs::path &mapSetup, CMap &map, int writeTerrain,
} }
} }
f->printf("\n\n"); f->printf("\n\n");
for (std::vector<CUnit *>::iterator it = teleporters.begin(); it != teleporters.end(); ++it) { for (const CUnit* unitPtr : teleporters) {
CUnit &unit = **it; const CUnit &unit = *unitPtr;
f->printf("SetTeleportDestination(%d, %d)\n", UnitNumber(unit), UnitNumber(*unit.Goal)); f->printf("SetTeleportDestination(%d, %d)\n", UnitNumber(unit), UnitNumber(*unit.Goal));
} }
f->printf("\n\n"); f->printf("\n\n");

View file

@ -172,11 +172,10 @@ public:
} }
} }
#else #else
for (std::vector<CUnit *>::iterator i(Units.begin()), end(Units.end()); i != end; ++i) { auto it = ranges::find(Units, unit);
if ((*i) == unit) { if (it != Units.end()) {
Units.erase(i); Units.erase(it);
return true; return true;
}
} }
#endif #endif
return false; return false;
@ -189,11 +188,9 @@ public:
*/ */
void RemoveS(CUnit *const unit) void RemoveS(CUnit *const unit)
{ {
for (std::vector<CUnit *>::iterator i(Units.begin()), end(Units.end()); i != end; ++i) { auto it = ranges::find(Units, unit);
if ((*i) == unit) { if (it != Units.end()) {
Units.erase(i); Units.erase(it);
return;
}
} }
} }

View file

@ -737,11 +737,9 @@ public:
return buildin[i].key; return buildin[i].key;
} }
} }
for (std::map<std::string, int>::iterator for (const auto &[key, value] : user) {
it(user.begin()), end(user.end()); if (value == index) {
it != end; ++it) { return key.c_str();
if ((*it).second == index) {
return ((*it).first).c_str();
} }
} }
return nullptr; return nullptr;

View file

@ -199,6 +199,13 @@ namespace ranges
return std::find(begin(range), end(range), value); return std::find(begin(range), end(range), value);
} }
template <typename Range, typename Predicate>
auto find_if(Range &range, Predicate &&predicate)
{
return std::find_if(begin(range), end(range), std::forward<Predicate>(predicate));
}
template<typename Range, typename Value> template<typename Range, typename Value>
bool consist(Range& range, const Value& value) bool consist(Range& range, const Value& value)
{ {

View file

@ -103,8 +103,8 @@ void MissileType::LoadMissileSprite()
void LoadMissileSprites() void LoadMissileSprites()
{ {
#ifndef DYNAMIC_LOAD #ifndef DYNAMIC_LOAD
for (MissileTypeMap::iterator it = MissileTypes.begin(); it != MissileTypes.end(); ++it) { for (auto &[key, missileType] : MissileTypes) {
(*it).second->LoadMissileSprite(); missileType->LoadMissileSprite();
} }
#endif #endif
} }
@ -669,8 +669,8 @@ void MissileHandlePierce(Missile &missile, const Vec2i &pos)
} }
std::vector<CUnit *> units; std::vector<CUnit *> units;
Select(pos, pos, units); Select(pos, pos, units);
for (std::vector<CUnit *>::iterator it = units.begin(); it != units.end(); ++it) { for (CUnit *unitPtr : units) {
CUnit &unit = **it; CUnit &unit = *unitPtr;
if (unit.IsAliveOnMap() if (unit.IsAliveOnMap()
&& (missile.Type->FriendlyFire == false || unit.IsEnemy(*missile.SourceUnit->Player))) { && (missile.Type->FriendlyFire == false || unit.IsEnemy(*missile.SourceUnit->Player))) {
@ -695,8 +695,8 @@ bool MissileHandleBlocking(Missile &missile, const PixelPos &position)
std::vector<CUnit *> blockingUnits; std::vector<CUnit *> blockingUnits;
const Vec2i missilePos = Map.MapPixelPosToTilePos(position); const Vec2i missilePos = Map.MapPixelPosToTilePos(position);
Select(missilePos, missilePos, blockingUnits); Select(missilePos, missilePos, blockingUnits);
for (std::vector<CUnit *>::iterator it = blockingUnits.begin(); it != blockingUnits.end(); ++it) { for (CUnit *unitPtr : blockingUnits) {
CUnit &unit = **it; CUnit &unit = *unitPtr;
// If land unit shoots at land unit, missile can be blocked by Wall units // If land unit shoots at land unit, missile can be blocked by Wall units
if (!missile.Type->IgnoreWalls && missile.SourceUnit->Type->UnitType == UnitTypeLand) { if (!missile.Type->IgnoreWalls && missile.SourceUnit->Type->UnitType == UnitTypeLand) {
if (!missile.TargetUnit || missile.TargetUnit->Type->UnitType == UnitTypeLand) { if (!missile.TargetUnit || missile.TargetUnit->Type->UnitType == UnitTypeLand) {
@ -1227,10 +1227,9 @@ int ViewPointDistanceToMissile(const Missile &missile)
*/ */
MissileType *MissileBurningBuilding(int percent) MissileType *MissileBurningBuilding(int percent)
{ {
for (std::vector<BurningBuildingFrame *>::iterator i = BurningBuildingFrames.begin(); for (BurningBuildingFrame *frame : BurningBuildingFrames) {
i != BurningBuildingFrames.end(); ++i) { if (percent > frame->Percent) {
if (percent > (*i)->Percent) { return frame->Missile;
return (*i)->Missile;
} }
} }
return nullptr; return nullptr;
@ -1307,10 +1306,8 @@ void MissileType::Init()
// Resolve impact missiles and sounds. // Resolve impact missiles and sounds.
this->FiredSound.MapSound(); this->FiredSound.MapSound();
this->ImpactSound.MapSound(); this->ImpactSound.MapSound();
for (std::vector<MissileConfig *>::iterator it = this->Impact.begin(); it != this->Impact.end(); ++it) { for (MissileConfig *mc : this->Impact) {
MissileConfig &mc = **it; mc->MapMissile();
mc.MapMissile();
} }
this->Smoke.MapMissile(); this->Smoke.MapMissile();
} }
@ -1320,8 +1317,8 @@ void MissileType::Init()
*/ */
void InitMissileTypes() void InitMissileTypes()
{ {
for (MissileTypeMap::iterator it = MissileTypes.begin(); it != MissileTypes.end(); ++it) { for (auto &[key, value] : MissileTypes) {
(*it).second->Init(); value->Init();
} }
} }
@ -1361,8 +1358,8 @@ MissileType::~MissileType()
*/ */
void CleanMissileTypes() void CleanMissileTypes()
{ {
for (MissileTypeMap::iterator it = MissileTypes.begin(); it != MissileTypes.end(); ++it) { for (auto &[key, value] : MissileTypes) {
delete it->second; delete value;
} }
MissileTypes.clear(); MissileTypes.clear();
} }
@ -1401,9 +1398,8 @@ void CleanMissiles()
void FreeBurningBuildingFrames() void FreeBurningBuildingFrames()
{ {
for (std::vector<BurningBuildingFrame *>::iterator i = BurningBuildingFrames.begin(); for (BurningBuildingFrame *frame : BurningBuildingFrames) {
i != BurningBuildingFrames.end(); ++i) { delete frame;
delete *i;
} }
BurningBuildingFrames.clear(); BurningBuildingFrames.clear();
} }

View file

@ -344,9 +344,8 @@ static int CclMissile(lua_State *l)
*/ */
static int CclDefineBurningBuilding(lua_State *l) static int CclDefineBurningBuilding(lua_State *l)
{ {
for (std::vector<BurningBuildingFrame *>::iterator i = BurningBuildingFrames.begin(); for (BurningBuildingFrame *frame : BurningBuildingFrames) {
i != BurningBuildingFrames.end(); ++i) { delete frame;
delete *i;
} }
BurningBuildingFrames.clear(); BurningBuildingFrames.clear();

View file

@ -664,8 +664,8 @@ void SocketSet::DelSocket(Socket socket)
} }
if (socket == MaxSockFD) { if (socket == MaxSockFD) {
MaxSockFD = 0; MaxSockFD = 0;
for (i = Sockets.begin(); i != Sockets.end(); ++i) { for (auto &socketFd : Sockets) {
MaxSockFD = std::max(this->MaxSockFD, *i); MaxSockFD = std::max(this->MaxSockFD, socketFd);
} }
} }
} }

View file

@ -60,14 +60,13 @@ void CParticleManager::exit()
void CParticleManager::clear() void CParticleManager::clear()
{ {
std::vector<CParticle *>::iterator i; for (auto *p : particles) {
for (i = particles.begin(); i != particles.end(); ++i) { delete p;
delete *i;
} }
particles.clear(); particles.clear();
for (i = new_particles.begin(); i != new_particles.end(); ++i) { for (auto *p : new_particles) {
delete *i; delete p;
} }
new_particles.clear(); new_particles.clear();
} }
@ -81,8 +80,8 @@ void CParticleManager::prepareToDraw(const CViewport &vp, std::vector<CParticle
{ {
this->vp = &vp; this->vp = &vp;
for (std::vector<CParticle *>::iterator it = particles.begin(); it != particles.end(); ++it) { for (CParticle *p : particles) {
CParticle &particle = **it; CParticle &particle = *p;
if (particle.isVisible(vp)) { if (particle.isVisible(vp)) {
table.push_back(&particle); table.push_back(&particle);
} }

View file

@ -235,29 +235,23 @@ inline void ProfilePrint()
return; return;
} }
std::vector<ProfileData *> prof; std::vector<ProfileData *> prof;
for (std::map<const char *const, ProfileData>::iterator i = functionProfiles.begin(); for (auto &[key, data] : functionProfiles) {
i != functionProfiles.end(); ++i) { prof.insert(std::upper_bound(prof.begin(), prof.end(), &data, compProfileData), &data);
ProfileData *data = &i->second;
prof.insert(std::upper_bound(prof.begin(), prof.end(), data, compProfileData), data);
} }
FILE *fd = fopen("profile.txt", "wb"); FILE *fd = fopen("profile.txt", "wb");
fprintf(fd, " total\t calls\t per\tname\n"); fprintf(fd, " total\t calls\t per\tname\n");
for (std::vector<ProfileData *>::iterator i = prof.begin(); i != prof.end(); ++i) { for (ProfileData *data : prof) {
ProfileData *data = (*i);
fprintf(fd, "%9.3f\t%9lu\t%9.3f\t", fprintf(fd, "%9.3f\t%9lu\t%9.3f\t",
(double)data->TotalTime / frequency.QuadPart * 1000.0, (double)data->TotalTime / frequency.QuadPart * 1000.0,
data->Calls, data->Calls,
(double)data->TotalTime / frequency.QuadPart * 1000.0 / data->Calls); (double)data->TotalTime / frequency.QuadPart * 1000.0 / data->Calls);
for (std::map<const char *const, ProfileData>::iterator j = for (const auto &[key, data2] : functionProfiles) {
functionProfiles.begin(); j != functionProfiles.end(); ++j) { if (data == &data2) {
ProfileData *data2 = &j->second; fprintf(fd, "%s\n", key);
if (data == data2) {
fprintf(fd, "%s\n", j->first);
} }
} }
} }
fclose(fd); fclose(fd);

View file

@ -192,8 +192,8 @@ static void EvaluateMissileLocation(const SpellActionMissileLocation &location,
std::vector<CUnit *> table; std::vector<CUnit *> table;
Select(goalPos - offset, goalPos + offset, table); Select(goalPos - offset, goalPos + offset, table);
int count = 0; int count = 0;
for (std::vector<CUnit *>::iterator it = table.begin(); it != table.end(); ++it) { for (CUnit *unitPtr : table) {
CUnit &unit = **it; CUnit &unit = *unitPtr;
if (unit.Type->BoolFlag[ORGANIC_INDEX].value && unit.IsEnemy(caster)) { if (unit.Type->BoolFlag[ORGANIC_INDEX].value && unit.IsEnemy(caster)) {
table[count++] = &unit; table[count++] = &unit;

View file

@ -417,10 +417,9 @@ void InitSpells()
*/ */
SpellType *SpellTypeByIdent(const std::string &ident) SpellType *SpellTypeByIdent(const std::string &ident)
{ {
for (std::vector<SpellType *>::iterator i = SpellTypeTable.begin(); i < SpellTypeTable.end(); ++i) { auto it = ranges::find_if(SpellTypeTable, [&](SpellType* spell) { return spell->Ident == ident; });
if ((*i)->Ident == ident) { if (it != SpellTypeTable.end()) {
return *i; return *it;
}
} }
return nullptr; return nullptr;
} }
@ -585,8 +584,8 @@ SpellType::SpellType(int slot, const std::string &ident) :
*/ */
SpellType::~SpellType() SpellType::~SpellType()
{ {
for (std::vector<SpellActionType *>::iterator act = Action.begin(); act != Action.end(); ++act) { for (auto *act : Action) {
delete *act; delete act;
} }
Action.clear(); Action.clear();
@ -605,8 +604,8 @@ SpellType::~SpellType()
void CleanSpells() void CleanSpells()
{ {
DebugPrint("Cleaning spells.\n"); DebugPrint("Cleaning spells.\n");
for (std::vector<SpellType *>::iterator i = SpellTypeTable.begin(); i < SpellTypeTable.end(); ++i) { for (SpellType *spell : SpellTypeTable) {
delete *i; delete spell;
} }
SpellTypeTable.clear(); SpellTypeTable.clear();
} }

View file

@ -133,10 +133,8 @@ void InitConstructions()
*/ */
void LoadConstructions() void LoadConstructions()
{ {
for (std::vector<CConstruction *>::iterator it = Constructions.begin(); for (CConstruction *c :Constructions) {
it != Constructions.end(); c->Load();
++it) {
(*it)->Load();
} }
} }
@ -146,10 +144,8 @@ void LoadConstructions()
void CleanConstructions() void CleanConstructions()
{ {
// Free the construction table. // Free the construction table.
for (std::vector<CConstruction *>::iterator it = Constructions.begin(); for (CConstruction *c : Constructions) {
it != Constructions.end(); delete c;
++it) {
delete *it;
} }
Constructions.clear(); Constructions.clear();
} }
@ -190,7 +186,6 @@ static int CclDefineConstruction(lua_State *l)
// Slot identifier // Slot identifier
const std::string str = LuaToString(l, 1); const std::string str = LuaToString(l, 1);
CConstruction *construction = ConstructionByIdent(str); CConstruction *construction = ConstructionByIdent(str);
std::vector<CConstruction *>::iterator i;
if (construction == nullptr) { if (construction == nullptr) {
construction = new CConstruction; construction = new CConstruction;

View file

@ -389,11 +389,9 @@ bool IconConfig::Load()
*/ */
void LoadIcons() void LoadIcons()
{ {
for (IconMap::iterator it = Icons.begin(); it != Icons.end(); ++it) { for (auto &[key, icon] : Icons) {
CIcon &icon = *(*it).second; ShowLoadProgress(_("Icons %s"), icon->G->File.c_str());
icon->Load();
ShowLoadProgress(_("Icons %s"), icon.G->File.c_str());
icon.Load();
} }
} }
@ -402,8 +400,7 @@ void LoadIcons()
*/ */
void CleanIcons() void CleanIcons()
{ {
for (IconMap::iterator it = Icons.begin(); it != Icons.end(); ++it) { for (auto &[key, icon] : Icons) {
CIcon *icon = (*it).second;
delete icon; delete icon;
} }
Icons.clear(); Icons.clear();

View file

@ -618,9 +618,8 @@ CPopup::CPopup() :
CPopup::~CPopup() CPopup::~CPopup()
{ {
for (std::vector<CPopupContentType *>::iterator content = Contents.begin(); for (CPopupContentType *content : Contents) {
content != Contents.end(); ++content) { delete content;
delete *content;
} }
} }

View file

@ -609,10 +609,9 @@ static int CclDefinePanelContents(lua_State *l)
LuaError(l, "'%s' invalid for DefinePanelContents" _C_ key); LuaError(l, "'%s' invalid for DefinePanelContents" _C_ key);
} }
} }
for (std::vector<CContentType *>::iterator content = infopanel->Contents.begin(); for (CContentType *content : infopanel->Contents) { // Default value for invalid value.
content != infopanel->Contents.end(); ++content) { // Default value for invalid value. content->Pos.x += infopanel->PosX;
(*content)->Pos.x += infopanel->PosX; content->Pos.y += infopanel->PosY;
(*content)->Pos.y += infopanel->PosY;
} }
size_t j; size_t j;
for (j = 0; j < UI.InfoPanelContents.size(); ++j) { for (j = 0; j < UI.InfoPanelContents.size(); ++j) {

View file

@ -119,9 +119,8 @@ bool IsDemoMode()
CUnitInfoPanel::~CUnitInfoPanel() CUnitInfoPanel::~CUnitInfoPanel()
{ {
for (std::vector<CContentType *>::iterator content = Contents.begin(); for (CContentType *content : Contents) {
content != Contents.end(); ++content) { delete content;
delete *content;
} }
delete Condition; delete Condition;
} }
@ -189,10 +188,10 @@ CUserInterface::CUserInterface() :
*/ */
CPopup *PopupByIdent(const std::string &ident) CPopup *PopupByIdent(const std::string &ident)
{ {
for (std::vector<CPopup *>::iterator i = UI.ButtonPopups.begin(); i < UI.ButtonPopups.end(); ++i) { const auto it =
if ((*i)->Ident == ident) { ranges::find_if(UI.ButtonPopups, [&](CPopup *popup) { return popup->Ident == ident; });
return *i; if (it != UI.ButtonPopups.end()) {
} return *it;
} }
return nullptr; return nullptr;
} }
@ -206,8 +205,8 @@ void InitUserInterface()
UI.Offset480Y = (Video.Height - 480) / 2; UI.Offset480Y = (Video.Height - 480) / 2;
UI.LifeBarColorsInt.clear(); UI.LifeBarColorsInt.clear();
for(std::vector<std::string>::iterator it = UI.LifeBarColorNames.begin(); it != UI.LifeBarColorNames.end(); ++it) { for(const std::string& name : UI.LifeBarColorNames) {
UI.LifeBarColorsInt.push_back(IndexToColor(GetColorIndexByName((*it).c_str()))); UI.LifeBarColorsInt.push_back(IndexToColor(GetColorIndexByName(name.c_str())));
} }
// //
@ -371,9 +370,8 @@ void CleanUserInterface()
// Info Panel // Info Panel
CGraphic::Free(UI.InfoPanel.G); CGraphic::Free(UI.InfoPanel.G);
for (std::vector<CUnitInfoPanel *>::iterator panel = UI.InfoPanelContents.begin(); for (CUnitInfoPanel *panel : UI.InfoPanelContents) {
panel != UI.InfoPanelContents.end(); ++panel) { delete panel;
delete *panel;
} }
UI.InfoPanelContents.clear(); UI.InfoPanelContents.clear();
@ -385,9 +383,8 @@ void CleanUserInterface()
} }
// Button Popups // Button Popups
for (std::vector<CPopup *>::iterator popup = UI.ButtonPopups.begin(); for (CPopup *popup : UI.ButtonPopups) {
popup != UI.ButtonPopups.end(); ++popup) { delete popup;
delete *popup;
} }
UI.ButtonPopups.clear(); UI.ButtonPopups.clear();
@ -424,9 +421,8 @@ void CleanUserInterface()
void FreeButtonStyles() void FreeButtonStyles()
{ {
std::map<std::string, ButtonStyle *>::iterator i; for (auto &[key, value] : ButtonStyleHash) {
for (i = ButtonStyleHash.begin(); i != ButtonStyleHash.end(); ++i) { delete value;
delete(*i).second;
} }
ButtonStyleHash.clear(); ButtonStyleHash.clear();
} }

View file

@ -276,10 +276,8 @@ void PathFinderOutput::Load(lua_State *l)
*/ */
static void CclParseOrders(lua_State *l, CUnit &unit) static void CclParseOrders(lua_State *l, CUnit &unit)
{ {
for (std::vector<COrderPtr>::iterator order = unit.Orders.begin(); for (COrderPtr order : unit.Orders) {
order != unit.Orders.end(); delete order;
++order) {
delete *order;
} }
unit.Orders.clear(); unit.Orders.clear();
const int n = lua_rawlen(l, -1); const int n = lua_rawlen(l, -1);

View file

@ -918,9 +918,8 @@ static int CclDefineUnitType(lua_State *l)
} }
const int subargs = lua_rawlen(l, -1); const int subargs = lua_rawlen(l, -1);
// Free any old restrictions if they are redefined // Free any old restrictions if they are redefined
for (std::vector<CBuildRestriction *>::iterator b = type->BuildingRules.begin(); for (CBuildRestriction *b : type->BuildingRules) {
b != type->BuildingRules.end(); ++b) { delete b;
delete *b;
} }
type->BuildingRules.clear(); type->BuildingRules.clear();
for (int k = 0; k < subargs; ++k) { for (int k = 0; k < subargs; ++k) {
@ -937,9 +936,8 @@ static int CclDefineUnitType(lua_State *l)
} }
const int subargs = lua_rawlen(l, -1); const int subargs = lua_rawlen(l, -1);
// Free any old restrictions if they are redefined // Free any old restrictions if they are redefined
for (std::vector<CBuildRestriction *>::iterator b = type->AiBuildingRules.begin(); for (CBuildRestriction *b : type->AiBuildingRules) {
b != type->AiBuildingRules.end(); ++b) { delete b;
delete *b;
} }
type->AiBuildingRules.clear(); type->AiBuildingRules.clear();
for (int k = 0; k < subargs; ++k) { for (int k = 0; k < subargs; ++k) {

View file

@ -3521,9 +3521,7 @@ void CleanUnits()
// Free memory for all units in unit table. // Free memory for all units in unit table.
std::vector<CUnit *> units(UnitManager->begin(), UnitManager->end()); std::vector<CUnit *> units(UnitManager->begin(), UnitManager->end());
for (std::vector<CUnit *>::iterator it = units.begin(); it != units.end(); ++it) { for (CUnit *unit : units) {
CUnit *unit = *it;
if (unit == nullptr) { if (unit == nullptr) {
continue; continue;
} }

View file

@ -373,11 +373,10 @@ void DecorationCclRegister()
*/ */
void LoadDecorations() void LoadDecorations()
{ {
std::vector<Decoration>::iterator i; for (Decoration& deco : DecoSprite.SpriteArray) {
for (i = DecoSprite.SpriteArray.begin(); i != DecoSprite.SpriteArray.end(); ++i) { ShowLoadProgress(_("Decorations '%s'"), deco.File.c_str());
ShowLoadProgress(_("Decorations '%s'"), (*i).File.c_str()); deco.Sprite = CGraphic::New(deco.File, deco.Width, deco.Height);
(*i).Sprite = CGraphic::New((*i).File, (*i).Width, (*i).Height); deco.Sprite->Load();
(*i).Sprite->Load();
} }
} }

View file

@ -563,14 +563,12 @@ CUnitType::~CUnitType()
BoolFlag.clear(); BoolFlag.clear();
// Free Building Restrictions if there are any // Free Building Restrictions if there are any
for (std::vector<CBuildRestriction *>::iterator b = BuildingRules.begin(); for (CBuildRestriction *b : BuildingRules) {
b != BuildingRules.end(); ++b) { delete b;
delete *b;
} }
BuildingRules.clear(); BuildingRules.clear();
for (std::vector<CBuildRestriction *>::iterator b = AiBuildingRules.begin(); for (CBuildRestriction *b : AiBuildingRules) {
b != AiBuildingRules.end(); ++b) { delete b;
delete *b;
} }
AiBuildingRules.clear(); AiBuildingRules.clear();
@ -964,15 +962,13 @@ void InitUnitTypes(int reset_player_stats)
type.StillFrame = GetStillFrame(type); type.StillFrame = GetStillFrame(type);
// Lookup BuildingTypes // Lookup BuildingTypes
for (std::vector<CBuildRestriction *>::iterator b = type.BuildingRules.begin(); for (CBuildRestriction *b : type.BuildingRules) {
b < type.BuildingRules.end(); ++b) { b->Init();
(*b)->Init();
} }
// Lookup AiBuildingTypes // Lookup AiBuildingTypes
for (std::vector<CBuildRestriction *>::iterator b = type.AiBuildingRules.begin(); for (CBuildRestriction *b : type.AiBuildingRules) {
b < type.AiBuildingRules.end(); ++b) { b->Init();
(*b)->Init();
} }
} }
@ -1103,9 +1099,8 @@ void CUnitTypeVar::Clear()
{ {
Variable.clear(); Variable.clear();
for (std::vector<CDecoVar *>::iterator it = DecoVar.begin(); for (CDecoVar *deco : DecoVar) {
it != DecoVar.end(); ++it) { delete deco;
delete(*it);
} }
DecoVar.clear(); DecoVar.clear();
} }

View file

@ -163,8 +163,8 @@ void CCursor::Reset() {
*/ */
void LoadCursors(const std::string &race) void LoadCursors(const std::string &race)
{ {
for (std::vector<CCursor *>::iterator i = AllCursors.begin(); i != AllCursors.end(); ++i) { for (CCursor *cursorPtr : AllCursors) {
CCursor &cursor = **i; CCursor &cursor = *cursorPtr;
// Only load cursors of this race or universal cursors. // Only load cursors of this race or universal cursors.
if (!cursor.Race.empty() && cursor.Race != race) { if (!cursor.Race.empty() && cursor.Race != race) {
@ -189,8 +189,8 @@ void LoadCursors(const std::string &race)
*/ */
CCursor *CursorByIdent(const std::string &ident) CCursor *CursorByIdent(const std::string &ident)
{ {
for (std::vector<CCursor *>::iterator i = AllCursors.begin(); i != AllCursors.end(); ++i) { for (CCursor *cursorPtr : AllCursors) {
CCursor &cursor = **i; CCursor &cursor = *cursorPtr;
if (cursor.Ident != ident || !cursor.G->IsLoaded()) { if (cursor.Ident != ident || !cursor.G->IsLoaded()) {
continue; continue;
@ -436,9 +436,9 @@ void InitVideoCursors()
*/ */
void CleanCursors() void CleanCursors()
{ {
for (std::vector<CCursor *>::iterator i = AllCursors.begin(); i != AllCursors.end(); ++i) { for (CCursor *cursor : AllCursors) {
CGraphic::Free((**i).G); CGraphic::Free(cursor->G);
delete *i; delete cursor;
} }
AllCursors.clear(); AllCursors.clear();

View file

@ -932,9 +932,8 @@ void CFont::DynamicLoad() const
*/ */
void LoadFonts() void LoadFonts()
{ {
for (FontMap::iterator it = Fonts.begin(); it != Fonts.end(); ++it) { for (auto &[key, font] : Fonts) {
CFont &font = *it->second; font->Load();
font.Load();
} }
// TODO: remove this // TODO: remove this
@ -946,9 +945,7 @@ void CFont::Reload() const
{ {
if (this->G) { if (this->G) {
FontColorGraphicMap &fontColorGraphicMap = FontColorGraphics[this]; FontColorGraphicMap &fontColorGraphicMap = FontColorGraphics[this];
for (FontColorGraphicMap::iterator it = fontColorGraphicMap.begin(); for (auto &[key, g] : fontColorGraphicMap) {
it != fontColorGraphicMap.end(); ++it) {
CGraphic *g = it->second;
delete g; delete g;
} }
fontColorGraphicMap.clear(); fontColorGraphicMap.clear();
@ -961,10 +958,8 @@ void CFont::Reload() const
*/ */
void ReloadFonts() void ReloadFonts()
{ {
for (FontMap::iterator it = Fonts.begin(); it != Fonts.end(); ++it) { for (auto &[key, font] : Fonts) {
CFont &font = *it->second; font->Reload();
font.Reload();
} }
} }
@ -1066,16 +1061,14 @@ void CFont::Clean()
*/ */
void CleanFonts() void CleanFonts()
{ {
for (FontMap::iterator it = Fonts.begin(); it != Fonts.end(); ++it) { for (auto &[key, font] : Fonts) {
CFont *font = it->second;
font->Clean(); font->Clean();
delete font; delete font;
} }
Fonts.clear(); Fonts.clear();
for (FontColorMap::iterator it = FontColors.begin(); it != FontColors.end(); ++it) { for (auto &[key, fontColor] : FontColors) {
delete it->second; delete fontColor;
} }
FontColors.clear(); FontColors.clear();

View file

@ -908,16 +908,14 @@ int Str2SdlKey(const char *str)
{ {
InitKey2Str(); InitKey2Str();
std::map<int, std::string>::iterator i; for (auto &[sdlkey, s] : Key2Str) {
for (i = Key2Str.begin(); i != Key2Str.end(); ++i) { if (!strcasecmp(str, s.c_str())) {
if (!strcasecmp(str, (*i).second.c_str())) { return sdlkey;
return (*i).first;
} }
} }
std::map<std::string, int>::iterator i2; for (auto &[s, sdlkey] : Str2Key) {
for (i2 = Str2Key.begin(); i2 != Str2Key.end(); ++i2) { if (!strcasecmp(str, s.c_str())) {
if (!strcasecmp(str, (*i2).first.c_str())) { return sdlkey;
return (*i2).second;
} }
} }
return 0; return 0;

View file

@ -551,8 +551,7 @@ void ColorCycle()
CColorCycling &colorCycling = CColorCycling::GetInstance(); CColorCycling &colorCycling = CColorCycling::GetInstance();
if (colorCycling.ColorCycleAll) { if (colorCycling.ColorCycleAll) {
++colorCycling.cycleCount; ++colorCycling.cycleCount;
for (std::vector<SDL_Surface *>::iterator it = colorCycling.PaletteList.begin(); it != colorCycling.PaletteList.end(); ++it) { for (SDL_Surface *surface : colorCycling.PaletteList) {
SDL_Surface *surface = (*it);
ColorCycleSurface(*surface); ColorCycleSurface(*surface);
} }
} else if (Map.TileGraphic->Surface->format->BytesPerPixel == 1) { } else if (Map.TileGraphic->Surface->format->BytesPerPixel == 1) {
@ -565,9 +564,7 @@ void RestoreColorCyclingSurface()
{ {
CColorCycling &colorCycling = CColorCycling::GetInstance(); CColorCycling &colorCycling = CColorCycling::GetInstance();
if (colorCycling.ColorCycleAll) { if (colorCycling.ColorCycleAll) {
for (std::vector<SDL_Surface *>::iterator it = colorCycling.PaletteList.begin(); it != colorCycling.PaletteList.end(); ++it) { for (SDL_Surface *surface : colorCycling.PaletteList) {
SDL_Surface *surface = (*it);
ColorCycleSurface_Reverse(*surface, colorCycling.cycleCount); ColorCycleSurface_Reverse(*surface, colorCycling.cycleCount);
} }
} else if (Map.TileGraphic->Surface->format->BytesPerPixel == 1) { } else if (Map.TileGraphic->Surface->format->BytesPerPixel == 1) {

View file

@ -249,19 +249,18 @@ public:
fclose(fp); fclose(fp);
} }
/** swaps color a and color b in both the palette and the image */ /** swaps color a and color b in both the palette and the image */
void swap_colors (int a, int b) void swap_colors(int a, int b)
{ {
std::swap (m_palette[a], m_palette[b]); std::swap(m_palette[a], m_palette[b]);
for (std::vector<int>::iterator i = m_image.begin (); i != m_image.end (); ++i) for (int &e : m_image) {
{ if (e == a)
if (*i == a) e = b;
*i = b; else if (e == b)
else if (*i == b) e = a;
*i = a; }
} }
}
void set_color (int i, Color c) { void set_color (int i, Color c) {
m_palette[i] = c; m_palette[i] = c;