Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "../stdafx.h"
00013 #include "../core/pool_func.hpp"
00014 #include "../window_func.h"
00015 #include "linkgraphjob.h"
00016
00017
00018 LinkGraphJobPool _link_graph_job_pool("LinkGraphJob");
00019 INSTANTIATE_POOL_METHODS(LinkGraphJob)
00020
00021
00027 LinkGraphJob::LinkGraphJob(const LinkGraph &orig) :
00028
00029
00030 link_graph(orig),
00031 settings(_settings_game.linkgraph),
00032 thread(NULL),
00033 join_date(_date + _settings_game.linkgraph.recalc_time)
00034 {
00035 }
00036
00040 LinkGraphJob::~LinkGraphJob()
00041 {
00042 assert(this->thread == NULL);
00043
00044
00045 if (!LinkGraph::IsValidID(this->link_graph.index)) return;
00046
00047 uint size = this->Size();
00048 for (NodeID node_id = 0; node_id < size; ++node_id) {
00049 StationID station = (*this)[node_id].Station();
00050 if (Station::IsValidID(station)) continue;
00051
00052
00053
00054 for (NodeID from_id = 0; from_id < size; ++from_id) {
00055 if ((*this)[from_id][node_id].Capacity() > 0) {
00056 FlowStatMap &flows = this->nodes[from_id].flows;
00057 for (FlowStatMap::iterator it(flows.begin()); it != flows.end();) {
00058 it->second.ChangeShare(station, INT_MIN);
00059 if (it->second.GetShares()->empty()) {
00060 flows.erase(it++);
00061 } else {
00062 ++it;
00063 }
00064 }
00065 }
00066 }
00067 }
00068
00069 for (NodeID node_id = 0; node_id < size; ++node_id) {
00070
00071
00072 Station *st = Station::GetIfValid((*this)[node_id].Station());
00073 if (st == NULL) continue;
00074
00075
00076
00077 GoodsEntry &ge = st->goods[this->Cargo()];
00078 if (ge.link_graph != this->link_graph.index || ge.node != node_id) continue;
00079
00080
00081 FlowStatMap &flows = this->nodes[node_id].flows;
00082 flows.Cleanup(node_id, this->link_graph.index);
00083
00084 ge.flows.swap(flows);
00085 InvalidateWindowData(WC_STATION_VIEW, st->index, this->Cargo());
00086 }
00087 }
00088
00094 void LinkGraphJob::Init()
00095 {
00096 uint size = this->Size();
00097 this->nodes.Resize(size);
00098 this->edges.Resize(size, size);
00099 for (uint i = 0; i < size; ++i) {
00100 this->nodes[i].Init(this->link_graph[i].Supply());
00101 EdgeAnnotation *node_edges = this->edges[i];
00102 for (uint j = 0; j < size; ++j) {
00103 node_edges[j].Init();
00104 }
00105 }
00106 }
00107
00111 void LinkGraphJob::EdgeAnnotation::Init()
00112 {
00113 this->demand = 0;
00114 this->flow = 0;
00115 this->unsatisfied_demand = 0;
00116 }
00117
00123 void LinkGraphJob::NodeAnnotation::Init(uint supply)
00124 {
00125 this->undelivered_supply = supply;
00126 new (&this->flows) FlowStatMap;
00127 new (&this->paths) PathSet;
00128 }
00129
00138 void Path::Fork(Path *base, uint cap, int free_cap, uint dist)
00139 {
00140 this->capacity = min(base->capacity, cap);
00141 this->free_capacity = min(base->free_capacity, free_cap);
00142 this->distance = base->distance + dist;
00143 assert(this->distance > 0);
00144 if (this->parent != base) {
00145 this->Detach();
00146 this->parent = base;
00147 this->parent->num_children++;
00148 }
00149 this->origin = base->origin;
00150 }
00151
00160 uint Path::AddFlow(uint new_flow, LinkGraphJob &job, uint max_saturation)
00161 {
00162 if (this->parent != NULL) {
00163 LinkGraphJob::Edge edge = job[this->parent->node][this->node];
00164 if (max_saturation != UINT_MAX) {
00165 uint usable_cap = edge.Capacity() * max_saturation / 100;
00166 if (usable_cap > edge.Flow()) {
00167 new_flow = min(new_flow, usable_cap - edge.Flow());
00168 } else {
00169 return 0;
00170 }
00171 }
00172 new_flow = this->parent->AddFlow(new_flow, job, max_saturation);
00173 if (new_flow > 0) {
00174 job[this->parent->node].Paths().insert(this);
00175 }
00176 edge.AddFlow(new_flow);
00177 }
00178 this->flow += new_flow;
00179 return new_flow;
00180 }
00181
00187 Path::Path(NodeID n, bool source) :
00188 distance(source ? 0 : UINT_MAX),
00189 capacity(0),
00190 free_capacity(source ? INT_MAX : INT_MIN),
00191 flow(0), node(n), origin(source ? n : INVALID_NODE),
00192 num_children(0), parent(NULL)
00193 {}
00194