Use a deque instead of list for CargoPacketList and StationCargoPacketMap.

pull/8/head
Jonathan G Rennison 8 years ago
parent ae8e19cf9a
commit a422cb6753

@ -17,6 +17,8 @@
#include "cargoaction.h" #include "cargoaction.h"
#include "order_type.h" #include "order_type.h"
#include <vector>
#include "safeguards.h" #include "safeguards.h"
/* Initialize the cargopacket-pool */ /* Initialize the cargopacket-pool */
@ -311,17 +313,12 @@ template<class Taction>
void VehicleCargoList::PopCargo(Taction action) void VehicleCargoList::PopCargo(Taction action)
{ {
if (this->packets.empty()) return; if (this->packets.empty()) return;
Iterator it(--(this->packets.end())); for (auto it = this->packets.end(); it != this->packets.begin();) {
Iterator begin(this->packets.begin()); if (action.MaxMove() <= 0) break;
while (action.MaxMove() > 0) { --it;
CargoPacket *cp = *it; CargoPacket *cp = *it;
if (action(cp)) { if (action(cp)) {
if (it != begin) { it = this->packets.erase(it);
this->packets.erase(it--);
} else {
this->packets.erase(it);
break;
}
} else { } else {
break; break;
} }
@ -452,9 +449,10 @@ bool VehicleCargoList::Stage(bool accepted, StationID current_station, StationID
this->AssertCountConsistency(); this->AssertCountConsistency();
assert(this->action_counts[MTA_LOAD] == 0); assert(this->action_counts[MTA_LOAD] == 0);
this->action_counts[MTA_TRANSFER] = this->action_counts[MTA_DELIVER] = this->action_counts[MTA_KEEP] = 0; this->action_counts[MTA_TRANSFER] = this->action_counts[MTA_DELIVER] = this->action_counts[MTA_KEEP] = 0;
Iterator deliver = this->packets.end();
Iterator it = this->packets.begin(); Iterator it = this->packets.begin();
uint sum = 0; uint sum = 0;
CargoPacketList transfer_deliver;
std::vector<CargoPacket *> keep;
bool force_keep = (order_flags & OUFB_NO_UNLOAD) != 0; bool force_keep = (order_flags & OUFB_NO_UNLOAD) != 0;
bool force_unload = (order_flags & OUFB_UNLOAD) != 0; bool force_unload = (order_flags & OUFB_UNLOAD) != 0;
@ -463,7 +461,7 @@ bool VehicleCargoList::Stage(bool accepted, StationID current_station, StationID
while (sum < this->count) { while (sum < this->count) {
CargoPacket *cp = *it; CargoPacket *cp = *it;
this->packets.erase(it++); it = this->packets.erase(it);
StationID cargo_next = INVALID_STATION; StationID cargo_next = INVALID_STATION;
MoveToAction action = MTA_LOAD; MoveToAction action = MTA_LOAD;
if (force_keep) { if (force_keep) {
@ -514,14 +512,13 @@ bool VehicleCargoList::Stage(bool accepted, StationID current_station, StationID
Money share; Money share;
switch (action) { switch (action) {
case MTA_KEEP: case MTA_KEEP:
this->packets.push_back(cp); keep.push_back(cp);
if (deliver == this->packets.end()) --deliver;
break; break;
case MTA_DELIVER: case MTA_DELIVER:
this->packets.insert(deliver, cp); transfer_deliver.push_back(cp);
break; break;
case MTA_TRANSFER: case MTA_TRANSFER:
this->packets.push_front(cp); transfer_deliver.push_front(cp);
/* Add feeder share here to allow reusing field for next station. */ /* Add feeder share here to allow reusing field for next station. */
share = payment->PayTransfer(cp, cp->count); share = payment->PayTransfer(cp, cp->count);
cp->AddFeederShare(share); cp->AddFeederShare(share);
@ -534,6 +531,9 @@ bool VehicleCargoList::Stage(bool accepted, StationID current_station, StationID
this->action_counts[action] += cp->count; this->action_counts[action] += cp->count;
sum += cp->count; sum += cp->count;
} }
assert(this->packets.empty());
this->packets = std::move(transfer_deliver);
this->packets.insert(this->packets.end(), keep.begin(), keep.end());
this->AssertCountConsistency(); this->AssertCountConsistency();
return this->action_counts[MTA_DELIVER] > 0 || this->action_counts[MTA_TRANSFER] > 0; return this->action_counts[MTA_DELIVER] > 0 || this->action_counts[MTA_TRANSFER] > 0;
} }

@ -19,7 +19,7 @@
#include "cargo_type.h" #include "cargo_type.h"
#include "vehicle_type.h" #include "vehicle_type.h"
#include "core/multimap.hpp" #include "core/multimap.hpp"
#include <list> #include <deque>
/** Unique identifier for a single cargo packet. */ /** Unique identifier for a single cargo packet. */
typedef uint32 CargoPacketID; typedef uint32 CargoPacketID;
@ -275,7 +275,7 @@ public:
void InvalidateCache(); void InvalidateCache();
}; };
typedef std::list<CargoPacket *> CargoPacketList; typedef std::deque<CargoPacket *> CargoPacketList;
/** /**
* CargoList that is used for vehicles. * CargoList that is used for vehicles.
@ -454,7 +454,7 @@ public:
} }
}; };
typedef MultiMap<StationID, CargoPacket *> StationCargoPacketMap; typedef MultiMap<StationID, CargoPacket *, CargoPacketList> StationCargoPacketMap;
typedef std::map<StationID, uint> StationCargoAmountMap; typedef std::map<StationID, uint> StationCargoAmountMap;
/** /**

@ -240,7 +240,7 @@ static const SaveLoad _station_speclist_desc[] = {
SLE_END() SLE_END()
}; };
std::list<CargoPacket *> _packets; CargoPacketList _packets;
uint32 _num_dests; uint32 _num_dests;
struct FlowSaveLoad { struct FlowSaveLoad {
@ -282,7 +282,7 @@ const SaveLoad *GetGoodsDesc()
SLEG_CONDVAR( _cargo_feeder_share, SLE_FILE_U32 | SLE_VAR_I64, 14, 64), SLEG_CONDVAR( _cargo_feeder_share, SLE_FILE_U32 | SLE_VAR_I64, 14, 64),
SLEG_CONDVAR( _cargo_feeder_share, SLE_INT64, 65, 67), SLEG_CONDVAR( _cargo_feeder_share, SLE_INT64, 65, 67),
SLE_CONDVAR(GoodsEntry, amount_fract, SLE_UINT8, 150, SL_MAX_VERSION), SLE_CONDVAR(GoodsEntry, amount_fract, SLE_UINT8, 150, SL_MAX_VERSION),
SLEG_CONDLST( _packets, REF_CARGO_PACKET, 68, 182), SLEG_CONDDEQ( _packets, REF_CARGO_PACKET, 68, 182),
SLEG_CONDVAR( _num_dests, SLE_UINT32, 183, SL_MAX_VERSION), SLEG_CONDVAR( _num_dests, SLE_UINT32, 183, SL_MAX_VERSION),
SLE_CONDVAR(GoodsEntry, cargo.reserved_count, SLE_UINT, 181, SL_MAX_VERSION), SLE_CONDVAR(GoodsEntry, cargo.reserved_count, SLE_UINT, 181, SL_MAX_VERSION),
SLE_CONDVAR(GoodsEntry, link_graph, SLE_UINT16, 183, SL_MAX_VERSION), SLE_CONDVAR(GoodsEntry, link_graph, SLE_UINT16, 183, SL_MAX_VERSION),
@ -295,11 +295,11 @@ const SaveLoad *GetGoodsDesc()
return goods_desc; return goods_desc;
} }
typedef std::pair<const StationID, std::list<CargoPacket *> > StationCargoPair; typedef std::pair<const StationID, CargoPacketList> StationCargoPair;
static const SaveLoad _cargo_list_desc[] = { static const SaveLoad _cargo_list_desc[] = {
SLE_VAR(StationCargoPair, first, SLE_UINT16), SLE_VAR(StationCargoPair, first, SLE_UINT16),
SLE_LST(StationCargoPair, second, REF_CARGO_PACKET), SLE_DEQ(StationCargoPair, second, REF_CARGO_PACKET),
SLE_END() SLE_END()
}; };
@ -313,7 +313,7 @@ static void SwapPackets(GoodsEntry *ge)
StationCargoPacketMap &ge_packets = const_cast<StationCargoPacketMap &>(*ge->cargo.Packets()); StationCargoPacketMap &ge_packets = const_cast<StationCargoPacketMap &>(*ge->cargo.Packets());
if (_packets.empty()) { if (_packets.empty()) {
std::map<StationID, std::list<CargoPacket *> >::iterator it(ge_packets.find(INVALID_STATION)); std::map<StationID, CargoPacketList>::iterator it(ge_packets.find(INVALID_STATION));
if (it == ge_packets.end()) { if (it == ge_packets.end()) {
return; return;
} else { } else {

@ -639,7 +639,7 @@ const SaveLoad *GetVehicleDescription(VehicleType vt)
SLE_VAR(Vehicle, cargo_cap, SLE_UINT16), SLE_VAR(Vehicle, cargo_cap, SLE_UINT16),
SLE_CONDVAR(Vehicle, refit_cap, SLE_UINT16, 182, SL_MAX_VERSION), SLE_CONDVAR(Vehicle, refit_cap, SLE_UINT16, 182, SL_MAX_VERSION),
SLEG_CONDVAR( _cargo_count, SLE_UINT16, 0, 67), SLEG_CONDVAR( _cargo_count, SLE_UINT16, 0, 67),
SLE_CONDLST(Vehicle, cargo.packets, REF_CARGO_PACKET, 68, SL_MAX_VERSION), SLE_CONDDEQ(Vehicle, cargo.packets, REF_CARGO_PACKET, 68, SL_MAX_VERSION),
SLE_CONDARR(Vehicle, cargo.action_counts, SLE_UINT, VehicleCargoList::NUM_MOVE_TO_ACTION, 181, SL_MAX_VERSION), SLE_CONDARR(Vehicle, cargo.action_counts, SLE_UINT, VehicleCargoList::NUM_MOVE_TO_ACTION, 181, SL_MAX_VERSION),
SLE_CONDVAR(Vehicle, cargo_age_counter, SLE_UINT16, 162, SL_MAX_VERSION), SLE_CONDVAR(Vehicle, cargo_age_counter, SLE_UINT16, 162, SL_MAX_VERSION),

Loading…
Cancel
Save