mirror of
https://github.com/JGRennison/OpenTTD-patches.git
synced 2024-11-11 13:10:45 +00:00
(svn r17735) -Codechange: update the cache one inserting/removing CargoPackets from the CargoList via Append/Truncate instead of rebuilding the whole cache. For Append this changes the O(n) cache rebuild into a O(1) cache update. For Truncate no temporary list is needed anymore (based on patch by fonsinchen)
This commit is contained in:
parent
446363aac4
commit
705615fd91
@ -76,6 +76,20 @@ CargoList::~CargoList()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CargoList::RemoveFromCache(const CargoPacket *cp)
|
||||||
|
{
|
||||||
|
this->count -= cp->count;
|
||||||
|
this->feeder_share -= cp->feeder_share;
|
||||||
|
this->cargo_days_in_transit -= cp->days_in_transit * cp->count;
|
||||||
|
}
|
||||||
|
|
||||||
|
void CargoList::AddToCache(const CargoPacket *cp)
|
||||||
|
{
|
||||||
|
this->count += cp->count;
|
||||||
|
this->feeder_share += cp->feeder_share;
|
||||||
|
this->cargo_days_in_transit += cp->days_in_transit * cp->count;
|
||||||
|
}
|
||||||
|
|
||||||
void CargoList::AgeCargo()
|
void CargoList::AgeCargo()
|
||||||
{
|
{
|
||||||
for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
|
for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
|
||||||
@ -92,43 +106,47 @@ void CargoList::Append(CargoPacket *cp)
|
|||||||
assert(cp != NULL);
|
assert(cp != NULL);
|
||||||
|
|
||||||
for (List::iterator it = this->packets.begin(); it != this->packets.end(); it++) {
|
for (List::iterator it = this->packets.begin(); it != this->packets.end(); it++) {
|
||||||
if ((*it)->SameSource(cp) && (*it)->count + cp->count <= CargoPacket::MAX_COUNT) {
|
CargoPacket *icp = *it;
|
||||||
(*it)->count += cp->count;
|
if (icp->SameSource(cp) && icp->count + cp->count <= CargoPacket::MAX_COUNT) {
|
||||||
(*it)->feeder_share += cp->feeder_share;
|
icp->count += cp->count;
|
||||||
delete cp;
|
icp->feeder_share += cp->feeder_share;
|
||||||
|
|
||||||
this->InvalidateCache();
|
this->AddToCache(cp);
|
||||||
|
delete cp;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* The packet could not be merged with another one */
|
/* The packet could not be merged with another one */
|
||||||
this->packets.push_back(cp);
|
this->packets.push_back(cp);
|
||||||
this->InvalidateCache();
|
this->AddToCache(cp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void CargoList::Truncate(uint count)
|
void CargoList::Truncate(uint max_remaining)
|
||||||
{
|
{
|
||||||
for (List::iterator it = this->packets.begin(); it != this->packets.end(); it++) {
|
for (List::iterator it = packets.begin(); it != packets.end(); /* done during loop*/) {
|
||||||
uint local_count = (*it)->count;
|
CargoPacket *cp = *it;
|
||||||
if (local_count <= count) {
|
if (max_remaining == 0) {
|
||||||
count -= local_count;
|
/* Nothing should remain, just remove the packets. */
|
||||||
|
packets.erase(it++);
|
||||||
|
this->RemoveFromCache(cp);
|
||||||
|
delete cp;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
(*it)->count = count;
|
uint local_count = cp->count;
|
||||||
count = 0;
|
if (local_count > max_remaining) {
|
||||||
|
uint diff = local_count - max_remaining;
|
||||||
|
this->count -= diff;
|
||||||
|
this->cargo_days_in_transit -= cp->days_in_transit * diff;
|
||||||
|
cp->count = max_remaining;
|
||||||
|
max_remaining = 0;
|
||||||
|
} else {
|
||||||
|
max_remaining -= local_count;
|
||||||
|
}
|
||||||
|
++it;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!this->packets.empty()) {
|
|
||||||
CargoPacket *cp = this->packets.back();
|
|
||||||
if (cp->count != 0) break;
|
|
||||||
delete cp;
|
|
||||||
this->packets.pop_back();
|
|
||||||
}
|
|
||||||
|
|
||||||
this->InvalidateCache();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CargoList::MoveTo(CargoList *dest, uint count, CargoList::MoveToAction mta, CargoPayment *payment, uint data)
|
bool CargoList::MoveTo(CargoList *dest, uint count, CargoList::MoveToAction mta, CargoPayment *payment, uint data)
|
||||||
@ -218,8 +236,6 @@ void CargoList::InvalidateCache()
|
|||||||
this->cargo_days_in_transit = 0;
|
this->cargo_days_in_transit = 0;
|
||||||
|
|
||||||
for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
|
for (List::const_iterator it = this->packets.begin(); it != this->packets.end(); it++) {
|
||||||
this->count += (*it)->count;
|
this->AddToCache(*it);
|
||||||
this->cargo_days_in_transit += (*it)->days_in_transit * (*it)->count;
|
|
||||||
this->feeder_share += (*it)->feeder_share;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -169,6 +169,20 @@ private:
|
|||||||
|
|
||||||
List packets; ///< The cargo packets in this list
|
List packets; ///< The cargo packets in this list
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the cache to reflect adding of this packet.
|
||||||
|
* Increases count, feeder share and days_in_transit
|
||||||
|
* @param cp a new packet to be inserted
|
||||||
|
*/
|
||||||
|
void AddToCache(const CargoPacket *cp);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the cached values to reflect the removal of this packet.
|
||||||
|
* Decreases count, feeder share and days_in_transit
|
||||||
|
* @param cp Packet to be removed from cache
|
||||||
|
*/
|
||||||
|
void RemoveFromCache(const CargoPacket *cp);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
/** The stations, via GoodsEntry, have a CargoList. */
|
/** The stations, via GoodsEntry, have a CargoList. */
|
||||||
friend const struct SaveLoad *GetGoodsDesc();
|
friend const struct SaveLoad *GetGoodsDesc();
|
||||||
|
Loading…
Reference in New Issue
Block a user