Use a deque instead of list for CargoPacketList and StationCargoPacketMap.
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
#include "cargoaction.h"
|
||||
#include "order_type.h"
|
||||
|
||||
#include <vector>
|
||||
|
||||
#include "safeguards.h"
|
||||
|
||||
/* Initialize the cargopacket-pool */
|
||||
@@ -311,17 +313,12 @@ template<class Taction>
|
||||
void VehicleCargoList::PopCargo(Taction action)
|
||||
{
|
||||
if (this->packets.empty()) return;
|
||||
Iterator it(--(this->packets.end()));
|
||||
Iterator begin(this->packets.begin());
|
||||
while (action.MaxMove() > 0) {
|
||||
for (auto it = this->packets.end(); it != this->packets.begin();) {
|
||||
if (action.MaxMove() <= 0) break;
|
||||
--it;
|
||||
CargoPacket *cp = *it;
|
||||
if (action(cp)) {
|
||||
if (it != begin) {
|
||||
this->packets.erase(it--);
|
||||
} else {
|
||||
this->packets.erase(it);
|
||||
break;
|
||||
}
|
||||
it = this->packets.erase(it);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@@ -452,9 +449,10 @@ bool VehicleCargoList::Stage(bool accepted, StationID current_station, StationID
|
||||
this->AssertCountConsistency();
|
||||
assert(this->action_counts[MTA_LOAD] == 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();
|
||||
uint sum = 0;
|
||||
CargoPacketList transfer_deliver;
|
||||
std::vector<CargoPacket *> keep;
|
||||
|
||||
bool force_keep = (order_flags & OUFB_NO_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) {
|
||||
CargoPacket *cp = *it;
|
||||
|
||||
this->packets.erase(it++);
|
||||
it = this->packets.erase(it);
|
||||
StationID cargo_next = INVALID_STATION;
|
||||
MoveToAction action = MTA_LOAD;
|
||||
if (force_keep) {
|
||||
@@ -514,14 +512,13 @@ bool VehicleCargoList::Stage(bool accepted, StationID current_station, StationID
|
||||
Money share;
|
||||
switch (action) {
|
||||
case MTA_KEEP:
|
||||
this->packets.push_back(cp);
|
||||
if (deliver == this->packets.end()) --deliver;
|
||||
keep.push_back(cp);
|
||||
break;
|
||||
case MTA_DELIVER:
|
||||
this->packets.insert(deliver, cp);
|
||||
transfer_deliver.push_back(cp);
|
||||
break;
|
||||
case MTA_TRANSFER:
|
||||
this->packets.push_front(cp);
|
||||
transfer_deliver.push_front(cp);
|
||||
/* Add feeder share here to allow reusing field for next station. */
|
||||
share = payment->PayTransfer(cp, cp->count);
|
||||
cp->AddFeederShare(share);
|
||||
@@ -534,6 +531,9 @@ bool VehicleCargoList::Stage(bool accepted, StationID current_station, StationID
|
||||
this->action_counts[action] += 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();
|
||||
return this->action_counts[MTA_DELIVER] > 0 || this->action_counts[MTA_TRANSFER] > 0;
|
||||
}
|
||||
|
@@ -19,7 +19,7 @@
|
||||
#include "cargo_type.h"
|
||||
#include "vehicle_type.h"
|
||||
#include "core/multimap.hpp"
|
||||
#include <list>
|
||||
#include <deque>
|
||||
|
||||
/** Unique identifier for a single cargo packet. */
|
||||
typedef uint32 CargoPacketID;
|
||||
@@ -275,7 +275,7 @@ public:
|
||||
void InvalidateCache();
|
||||
};
|
||||
|
||||
typedef std::list<CargoPacket *> CargoPacketList;
|
||||
typedef std::deque<CargoPacket *> CargoPacketList;
|
||||
|
||||
/**
|
||||
* 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;
|
||||
|
||||
/**
|
||||
|
@@ -240,7 +240,7 @@ static const SaveLoad _station_speclist_desc[] = {
|
||||
SLE_END()
|
||||
};
|
||||
|
||||
std::list<CargoPacket *> _packets;
|
||||
CargoPacketList _packets;
|
||||
uint32 _num_dests;
|
||||
|
||||
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_INT64, 65, 67),
|
||||
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),
|
||||
SLE_CONDVAR(GoodsEntry, cargo.reserved_count, SLE_UINT, 181, SL_MAX_VERSION),
|
||||
SLE_CONDVAR(GoodsEntry, link_graph, SLE_UINT16, 183, SL_MAX_VERSION),
|
||||
@@ -295,11 +295,11 @@ const SaveLoad *GetGoodsDesc()
|
||||
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[] = {
|
||||
SLE_VAR(StationCargoPair, first, SLE_UINT16),
|
||||
SLE_LST(StationCargoPair, second, REF_CARGO_PACKET),
|
||||
SLE_DEQ(StationCargoPair, second, REF_CARGO_PACKET),
|
||||
SLE_END()
|
||||
};
|
||||
|
||||
@@ -313,7 +313,7 @@ static void SwapPackets(GoodsEntry *ge)
|
||||
StationCargoPacketMap &ge_packets = const_cast<StationCargoPacketMap &>(*ge->cargo.Packets());
|
||||
|
||||
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()) {
|
||||
return;
|
||||
} else {
|
||||
|
@@ -639,7 +639,7 @@ const SaveLoad *GetVehicleDescription(VehicleType vt)
|
||||
SLE_VAR(Vehicle, cargo_cap, SLE_UINT16),
|
||||
SLE_CONDVAR(Vehicle, refit_cap, SLE_UINT16, 182, SL_MAX_VERSION),
|
||||
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_CONDVAR(Vehicle, cargo_age_counter, SLE_UINT16, 162, SL_MAX_VERSION),
|
||||
|
||||
|
Reference in New Issue
Block a user