From e844b82d9e62cb991794b3ff81e0ec09fe2ef14f Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Tue, 13 Sep 2016 18:31:55 +0100 Subject: [PATCH] Use vector instead of list for Station::loading_vehicles. --- src/economy.cpp | 8 ++------ src/saveload/afterload.cpp | 15 ++++++--------- src/saveload/station_sl.cpp | 4 ++-- src/station_base.h | 3 ++- src/station_cmd.cpp | 4 ++-- src/vehicle.cpp | 6 ++++-- 6 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/economy.cpp b/src/economy.cpp index b8d17ac6f2..a0171a2158 100644 --- a/src/economy.cpp +++ b/src/economy.cpp @@ -1969,12 +1969,9 @@ void LoadUnloadStation(Station *st) if (st->loading_vehicles.empty()) return; Vehicle *last_loading = NULL; - 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); @@ -1990,8 +1987,7 @@ void LoadUnloadStation(Station *st) */ if (last_loading == NULL) 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/saveload/afterload.cpp b/src/saveload/afterload.cpp index 79d9c7fc97..bf3948477c 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -62,6 +62,7 @@ #include "saveload_internal.h" #include +#include #include "../safeguards.h" @@ -1658,12 +1659,10 @@ bool AfterLoadGame() Station *st; FOR_ALL_STATIONS(st) { - std::list::iterator iter; - for (iter = st->loading_vehicles.begin(); iter != st->loading_vehicles.end();) { - Vehicle *v = *iter; - iter++; - if (!v->current_order.IsType(OT_LOADING)) st->loading_vehicles.remove(v); - } + st->loading_vehicles.erase(std::remove_if(st->loading_vehicles.begin(), st->loading_vehicles.end(), + [](Vehicle *v) { + return !v->current_order.IsType(OT_LOADING); + }), st->loading_vehicles.end()); } } @@ -2248,13 +2247,11 @@ bool AfterLoadGame() */ Station *st; FOR_ALL_STATIONS(st) { - 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(). */ assert_compile(CargoPaymentPool::MAX_SIZE == VehiclePool::MAX_SIZE); assert(CargoPayment::CanAllocateItem()); - Vehicle *v = *iter; if (v->cargo_payment == NULL) v->cargo_payment = new CargoPayment(v); } } diff --git a/src/saveload/station_sl.cpp b/src/saveload/station_sl.cpp index 19cc6e2e8f..dee5be33a2 100644 --- a/src/saveload/station_sl.cpp +++ b/src/saveload/station_sl.cpp @@ -218,7 +218,7 @@ static const SaveLoad _old_station_desc[] = { SLE_CONDVAR(Station, waiting_triggers, SLE_UINT8, 27, SL_MAX_VERSION), SLE_CONDVAR(Station, num_specs, SLE_UINT8, 27, SL_MAX_VERSION), - SLE_CONDLST(Station, loading_vehicles, REF_VEHICLE, 57, SL_MAX_VERSION), + SLE_CONDVEC(Station, loading_vehicles, REF_VEHICLE, 57, SL_MAX_VERSION), /* reserve extra space in savegame here. (currently 32 bytes) */ SLE_CONDNULL(32, 2, SL_MAX_VERSION), @@ -440,7 +440,7 @@ static const SaveLoad _station_desc[] = { SLE_VAR(Station, time_since_unload, SLE_UINT8), SLE_VAR(Station, last_vehicle_type, SLE_UINT8), SLE_VAR(Station, had_vehicle_of_type, SLE_UINT8), - SLE_LST(Station, loading_vehicles, REF_VEHICLE), + SLE_VEC(Station, loading_vehicles, REF_VEHICLE), SLE_CONDVAR(Station, always_accepted, SLE_UINT32, 127, SL_MAX_VERSION), SLE_END() diff --git a/src/station_base.h b/src/station_base.h index c4743bf88f..2bbc1003a8 100644 --- a/src/station_base.h +++ b/src/station_base.h @@ -20,6 +20,7 @@ #include "linkgraph/linkgraph_type.h" #include "newgrf_storage.h" #include +#include typedef Pool StationPool; extern StationPool _station_pool; @@ -468,7 +469,7 @@ public: byte time_since_unload; byte last_vehicle_type; - std::list loading_vehicles; + std::vector loading_vehicles; GoodsEntry goods[NUM_CARGO]; ///< Goods at this station uint32 always_accepted; ///< Bitmask of always accepted cargo types (by houses, HQs, industry tiles when industry doesn't accept cargo) diff --git a/src/station_cmd.cpp b/src/station_cmd.cpp index 4fbcb073c9..a3f62fedbe 100644 --- a/src/station_cmd.cpp +++ b/src/station_cmd.cpp @@ -3558,8 +3558,8 @@ 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 transfered. */ - for (std::list::iterator it(st->loading_vehicles.begin()); it != st->loading_vehicles.end(); ++it) { - for (Vehicle *v = *it; v != NULL; v = v->Next()) { + for (Vehicle *v : st->loading_vehicles) { + for (; v != NULL; v = v->Next()) { if (v->cargo_type != c) continue; v->cargo.Reroute(UINT_MAX, &v->cargo, avoid, avoid2, &ge); } diff --git a/src/vehicle.cpp b/src/vehicle.cpp index b8b550e593..dcdef4af9a 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -60,6 +60,8 @@ #include "table/strings.h" +#include + #include "safeguards.h" #define GEN_HASH(x, y) ((GB((y), 6 + ZOOM_LVL_SHIFT, 6) << 6) + GB((x), 7 + ZOOM_LVL_SHIFT, 6)) @@ -830,7 +832,7 @@ void Vehicle::PreDestructor() if (Station::IsValidID(this->last_station_visited)) { Station *st = Station::Get(this->last_station_visited); - st->loading_vehicles.remove(this); + st->loading_vehicles.erase(std::remove(st->loading_vehicles.begin(), st->loading_vehicles.end(), this), st->loading_vehicles.end()); HideFillingPercent(&this->fill_percent_te_id); this->CancelReservation(INVALID_STATION, st); @@ -2592,7 +2594,7 @@ void Vehicle::LeaveStation() this->current_order.MakeLeaveStation(); Station *st = Station::Get(this->last_station_visited); this->CancelReservation(INVALID_STATION, st); - st->loading_vehicles.remove(this); + st->loading_vehicles.erase(std::remove(st->loading_vehicles.begin(), st->loading_vehicles.end(), this), st->loading_vehicles.end()); HideFillingPercent(&this->fill_percent_te_id); trip_occupancy = CalcPercentVehicleFilled(this, NULL);