linkgraphjob.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "../stdafx.h"
00013 #include "../core/pool_func.hpp"
00014 #include "../window_func.h"
00015 #include "linkgraphjob.h"
00016 
00017 /* Initialize the link-graph-job-pool */
00018 LinkGraphJobPool _link_graph_job_pool("LinkGraphJob");
00019 INSTANTIATE_POOL_METHODS(LinkGraphJob)
00020 
00021 
00027 LinkGraphJob::LinkGraphJob(const LinkGraph &orig) :
00028     /* Copying the link graph here also copies its index member.
00029      * This is on purpose. */
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   /* Link graph has been merged into another one. */
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     Node from = (*this)[node_id];
00050 
00051     /* The station can have been deleted. */
00052     Station *st = Station::GetIfValid(from.Station());
00053     if (st == NULL) continue;
00054 
00055     /* Link graph merging and station deletion may change around IDs. Make
00056      * sure that everything is still consistent or ignore it otherwise. */
00057     GoodsEntry &ge = st->goods[this->Cargo()];
00058     if (ge.link_graph != this->link_graph.index || ge.node != node_id) continue;
00059 
00060     LinkGraph *lg = LinkGraph::Get(ge.link_graph);
00061     FlowStatMap &flows = from.Flows();
00062 
00063     for (EdgeIterator it(from.Begin()); it != from.End(); ++it) {
00064       if (from[it->first].Flow() == 0) continue;
00065       StationID to = (*this)[it->first].Station();
00066       Station *st2 = Station::GetIfValid(to);
00067       if (st2 == NULL || st2->goods[this->Cargo()].link_graph != this->link_graph.index ||
00068           st2->goods[this->Cargo()].node != it->first ||
00069           (*lg)[node_id][it->first].LastUpdate() == INVALID_DATE) {
00070         /* Edge has been removed. Delete flows. */
00071         flows.DeleteFlows(to);
00072       }
00073     }
00074 
00075     ge.flows.swap(flows);
00076     InvalidateWindowData(WC_STATION_VIEW, st->index, this->Cargo());
00077   }
00078 }
00079 
00085 void LinkGraphJob::Init()
00086 {
00087   uint size = this->Size();
00088   this->nodes.Resize(size);
00089   this->edges.Resize(size, size);
00090   for (uint i = 0; i < size; ++i) {
00091     this->nodes[i].Init(this->link_graph[i].Supply());
00092     EdgeAnnotation *node_edges = this->edges[i];
00093     for (uint j = 0; j < size; ++j) {
00094       node_edges[j].Init();
00095     }
00096   }
00097 }
00098 
00102 void LinkGraphJob::EdgeAnnotation::Init()
00103 {
00104   this->demand = 0;
00105   this->flow = 0;
00106   this->unsatisfied_demand = 0;
00107 }
00108 
00114 void LinkGraphJob::NodeAnnotation::Init(uint supply)
00115 {
00116   this->undelivered_supply = supply;
00117   new (&this->flows) FlowStatMap;
00118   new (&this->paths) PathList;
00119 }
00120 
00129 void Path::Fork(Path *base, uint cap, int free_cap, uint dist)
00130 {
00131   this->capacity = min(base->capacity, cap);
00132   this->free_capacity = min(base->free_capacity, free_cap);
00133   this->distance = base->distance + dist;
00134   assert(this->distance > 0);
00135   if (this->parent != base) {
00136     this->Detach();
00137     this->parent = base;
00138     this->parent->num_children++;
00139   }
00140   this->origin = base->origin;
00141 }
00142 
00151 uint Path::AddFlow(uint new_flow, LinkGraphJob &job, uint max_saturation)
00152 {
00153   if (this->parent != NULL) {
00154     LinkGraphJob::Edge edge = job[this->parent->node][this->node];
00155     if (max_saturation != UINT_MAX) {
00156       uint usable_cap = edge.Capacity() * max_saturation / 100;
00157       if (usable_cap > edge.Flow()) {
00158         new_flow = min(new_flow, usable_cap - edge.Flow());
00159       } else {
00160         return 0;
00161       }
00162     }
00163     new_flow = this->parent->AddFlow(new_flow, job, max_saturation);
00164     if (this->flow == 0 && new_flow > 0) {
00165       job[this->parent->node].Paths().push_back(this);
00166     }
00167     edge.AddFlow(new_flow);
00168   }
00169   this->flow += new_flow;
00170   return new_flow;
00171 }
00172 
00178 Path::Path(NodeID n, bool source) :
00179   distance(source ? 0 : UINT_MAX),
00180   capacity(0),
00181   free_capacity(source ? INT_MAX : INT_MIN),
00182   flow(0), node(n), origin(source ? n : INVALID_NODE),
00183   num_children(0), parent(NULL)
00184 {}
00185