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 uint size = this->Size();
00044 for (NodeID node_id = 0; node_id < size; ++node_id) {
00045 StationID station = (*this)[node_id].Station();
00046 if (!Station::IsValidID(station)) {
00047
00048
00049 for (NodeID from_id = 0; from_id < size; ++from_id) {
00050 if ((*this)[from_id][node_id].Capacity() > 0) {
00051 FlowStatMap &flows = this->nodes[from_id].flows;
00052 for (FlowStatMap::iterator it(flows.begin()); it != flows.end(); ++it) {
00053 it->second.ChangeShare(station, INT_MIN);
00054 }
00055 }
00056 }
00057 station = INVALID_STATION;
00058 }
00059 }
00060 for (NodeID node_id = 0; node_id < size; ++node_id) {
00061 StationID station = (*this)[node_id].Station();
00062 if (Station::IsValidID(station)) {
00063 InvalidateWindowData(WC_STATION_VIEW, station, this->Cargo());
00064 Station::Get(station)->goods[this->Cargo()].flows.
00065 swap(this->nodes[node_id].flows);
00066 }
00067 }
00068 }
00069
00075 void LinkGraphJob::Init()
00076 {
00077 uint size = this->Size();
00078 this->nodes.Resize(size);
00079 this->edges.Resize(size, size);
00080 for (uint i = 0; i < size; ++i) {
00081 this->nodes[i].Init(this->link_graph[i].Supply());
00082 EdgeAnnotation *node_edges = this->edges[i];
00083 for (uint j = 0; j < size; ++j) {
00084 node_edges[j].Init();
00085 }
00086 }
00087 }
00088
00092 void LinkGraphJob::EdgeAnnotation::Init()
00093 {
00094 this->demand = 0;
00095 this->flow = 0;
00096 this->unsatisfied_demand = 0;
00097 }
00098
00104 void LinkGraphJob::NodeAnnotation::Init(uint supply)
00105 {
00106 this->undelivered_supply = supply;
00107 new (&this->flows) FlowStatMap;
00108 new (&this->paths) PathSet;
00109 }
00110
00119 void Path::Fork(Path *base, uint cap, int free_cap, uint dist)
00120 {
00121 this->capacity = min(base->capacity, cap);
00122 this->free_capacity = min(base->free_capacity, free_cap);
00123 this->distance = base->distance + dist;
00124 assert(this->distance > 0);
00125 if (this->parent != base) {
00126 this->Detach();
00127 this->parent = base;
00128 this->parent->num_children++;
00129 }
00130 this->origin = base->origin;
00131 }
00132
00141 uint Path::AddFlow(uint new_flow, LinkGraphJob &job, uint max_saturation)
00142 {
00143 if (this->parent != NULL) {
00144 LinkGraphJob::Edge edge = job[this->parent->node][this->node];
00145 if (max_saturation != UINT_MAX) {
00146 uint usable_cap = edge.Capacity() * max_saturation / 100;
00147 if (usable_cap > edge.Flow()) {
00148 new_flow = min(new_flow, usable_cap - edge.Flow());
00149 } else {
00150 return 0;
00151 }
00152 }
00153 new_flow = this->parent->AddFlow(new_flow, job, max_saturation);
00154 if (new_flow > 0) {
00155 job[this->parent->node].Paths().insert(this);
00156 }
00157 edge.AddFlow(new_flow);
00158 }
00159 this->flow += new_flow;
00160 return new_flow;
00161 }
00162
00168 Path::Path(NodeID n, bool source) :
00169 distance(source ? 0 : UINT_MAX),
00170 capacity(0),
00171 free_capacity(source ? INT_MAX : INT_MIN),
00172 flow(0), node(n), origin(source ? n : INVALID_NODE),
00173 num_children(0), parent(NULL)
00174 {}
00175