linkgraphjob.cpp

Go to the documentation of this file.
00001 /* $Id: linkgraphjob.cpp 25963 2013-11-10 15:18:49Z fonsinchen $ */
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   /* Don't update stuff from other pools, when everything is being removed.
00045    * Accessing other pools may be invalid. */
00046   if (CleaningPool()) return;
00047 
00048   /* Link graph has been merged into another one. */
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     /* The station can have been deleted. */
00056     Station *st = Station::GetIfValid(from.Station());
00057     if (st == NULL) continue;
00058 
00059     /* Link graph merging and station deletion may change around IDs. Make
00060      * sure that everything is still consistent or ignore it otherwise. */
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         /* Edge has been removed. Delete flows. */
00075         StationIDStack erased = flows.DeleteFlows(to);
00076         /* Delete old flows for source stations which have been deleted
00077          * from the new flows. This avoids flow cycles between old and
00078          * new flows. */
00079         while (!erased.IsEmpty()) ge.flows.erase(erased.Pop());
00080       } else if ((*lg)[node_id][it->first].LastUnrestrictedUpdate() == INVALID_DATE) {
00081         /* Edge is fully restricted. */
00082         flows.RestrictFlows(to);
00083       }
00084     }
00085 
00086     /* Swap shares and invalidate ones that are completely deleted. Don't
00087      * really delete them as we could then end up with unroutable cargo
00088      * somewhere. Do delete them if automatic distribution has been turned
00089      * off for that cargo, though. */
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