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
00046 if (CleaningPool()) return;
00047
00048
00049 if (!LinkGraph::IsValidID(this->link_graph.index)) return;
00050
00051 uint size = this->Size();
00052 for (NodeID node_id = 0; node_id < size; ++node_id) {
00053 Node from = (*this)[node_id];
00054
00055
00056 Station *st = Station::GetIfValid(from.Station());
00057 if (st == NULL) continue;
00058
00059
00060
00061 GoodsEntry &ge = st->goods[this->Cargo()];
00062 if (ge.link_graph != this->link_graph.index || ge.node != node_id) continue;
00063
00064 LinkGraph *lg = LinkGraph::Get(ge.link_graph);
00065 FlowStatMap &flows = from.Flows();
00066
00067 for (EdgeIterator it(from.Begin()); it != from.End(); ++it) {
00068 if (from[it->first].Flow() == 0) continue;
00069 StationID to = (*this)[it->first].Station();
00070 Station *st2 = Station::GetIfValid(to);
00071 if (st2 == NULL || st2->goods[this->Cargo()].link_graph != this->link_graph.index ||
00072 st2->goods[this->Cargo()].node != it->first ||
00073 (*lg)[node_id][it->first].LastUpdate() == INVALID_DATE) {
00074
00075 StationIDStack erased = flows.DeleteFlows(to);
00076
00077
00078
00079 while (!erased.IsEmpty()) ge.flows.erase(erased.Pop());
00080 } else if ((*lg)[node_id][it->first].LastUnrestrictedUpdate() == INVALID_DATE) {
00081
00082 flows.RestrictFlows(to);
00083 }
00084 }
00085
00086
00087
00088
00089
00090 for (FlowStatMap::iterator it(ge.flows.begin()); it != ge.flows.end();) {
00091 FlowStatMap::iterator new_it = flows.find(it->first);
00092 if (new_it == flows.end()) {
00093 if (_settings_game.linkgraph.GetDistributionType(this->Cargo()) != DT_MANUAL) {
00094 it->second.Invalidate();
00095 ++it;
00096 } else {
00097 ge.flows.erase(it++);
00098 }
00099 } else {
00100 it->second.SwapShares(new_it->second);
00101 flows.erase(new_it);
00102 ++it;
00103 }
00104 }
00105 ge.flows.insert(flows.begin(), flows.end());
00106 InvalidateWindowData(WC_STATION_VIEW, st->index, this->Cargo());
00107 }
00108 }
00109
00115 void LinkGraphJob::Init()
00116 {
00117 uint size = this->Size();
00118 this->nodes.Resize(size);
00119 this->edges.Resize(size, size);
00120 for (uint i = 0; i < size; ++i) {
00121 this->nodes[i].Init(this->link_graph[i].Supply());
00122 EdgeAnnotation *node_edges = this->edges[i];
00123 for (uint j = 0; j < size; ++j) {
00124 node_edges[j].Init();
00125 }
00126 }
00127 }
00128
00132 void LinkGraphJob::EdgeAnnotation::Init()
00133 {
00134 this->demand = 0;
00135 this->flow = 0;
00136 this->unsatisfied_demand = 0;
00137 }
00138
00144 void LinkGraphJob::NodeAnnotation::Init(uint supply)
00145 {
00146 this->undelivered_supply = supply;
00147 new (&this->flows) FlowStatMap;
00148 new (&this->paths) PathList;
00149 }
00150
00159 void Path::Fork(Path *base, uint cap, int free_cap, uint dist)
00160 {
00161 this->capacity = min(base->capacity, cap);
00162 this->free_capacity = min(base->free_capacity, free_cap);
00163 this->distance = base->distance + dist;
00164 assert(this->distance > 0);
00165 if (this->parent != base) {
00166 this->Detach();
00167 this->parent = base;
00168 this->parent->num_children++;
00169 }
00170 this->origin = base->origin;
00171 }
00172
00181 uint Path::AddFlow(uint new_flow, LinkGraphJob &job, uint max_saturation)
00182 {
00183 if (this->parent != NULL) {
00184 LinkGraphJob::Edge edge = job[this->parent->node][this->node];
00185 if (max_saturation != UINT_MAX) {
00186 uint usable_cap = edge.Capacity() * max_saturation / 100;
00187 if (usable_cap > edge.Flow()) {
00188 new_flow = min(new_flow, usable_cap - edge.Flow());
00189 } else {
00190 return 0;
00191 }
00192 }
00193 new_flow = this->parent->AddFlow(new_flow, job, max_saturation);
00194 if (this->flow == 0 && new_flow > 0) {
00195 job[this->parent->node].Paths().push_front(this);
00196 }
00197 edge.AddFlow(new_flow);
00198 }
00199 this->flow += new_flow;
00200 return new_flow;
00201 }
00202
00208 Path::Path(NodeID n, bool source) :
00209 distance(source ? 0 : UINT_MAX),
00210 capacity(source ? UINT_MAX : 0),
00211 free_capacity(source ? INT_MAX : INT_MIN),
00212 flow(0), node(n), origin(source ? n : INVALID_NODE),
00213 num_children(0), parent(NULL)
00214 {}
00215