# 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   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       /* Station got removed during the link graph run. We have to remove all
    00048        * flows pointing to it now. This is costly but it should be rare. */
    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();) {
    00053             it->second.ChangeShare(station, INT_MIN);
    00054             if (it->second.GetShares()->empty()) {
    00055               flows.erase(it++);
    00056             } else {
    00057               ++it;
    00058             }
    00059           }
    00060         }
    00061       }
    00062       station = INVALID_STATION;
    00063     }
    00064   }
    00065   for (NodeID node_id = 0; node_id < size; ++node_id) {
    00066     StationID station = (*this)[node_id].Station();
    00067     if (Station::IsValidID(station)) {
    00068       InvalidateWindowData(WC_STATION_VIEW, station, this->Cargo());
    00069       Station::Get(station)->goods[this->Cargo()].flows.
    00070           swap(this->nodes[node_id].flows);
    00071     }
    00072   }
    00073 }
    00074
    00080 void LinkGraphJob::Init()
    00081 {
    00082   uint size = this->Size();
    00083   this->nodes.Resize(size);
    00084   this->edges.Resize(size, size);
    00085   for (uint i = 0; i < size; ++i) {
    00086     this->nodes[i].Init(this->link_graph[i].Supply());
    00087     EdgeAnnotation *node_edges = this->edges[i];
    00088     for (uint j = 0; j < size; ++j) {
    00089       node_edges[j].Init();
    00090     }
    00091   }
    00092 }
    00093
    00097 void LinkGraphJob::EdgeAnnotation::Init()
    00098 {
    00099   this->demand = 0;
    00100   this->flow = 0;
    00101   this->unsatisfied_demand = 0;
    00102 }
    00103
    00109 void LinkGraphJob::NodeAnnotation::Init(uint supply)
    00110 {
    00111   this->undelivered_supply = supply;
    00112   new (&this->flows) FlowStatMap;
    00113   new (&this->paths) PathSet;
    00114 }
    00115
    00124 void Path::Fork(Path *base, uint cap, int free_cap, uint dist)
    00125 {
    00126   this->capacity = min(base->capacity, cap);
    00127   this->free_capacity = min(base->free_capacity, free_cap);
    00128   this->distance = base->distance + dist;
    00129   assert(this->distance > 0);
    00130   if (this->parent != base) {
    00131     this->Detach();
    00132     this->parent = base;
    00133     this->parent->num_children++;
    00134   }
    00135   this->origin = base->origin;
    00136 }
    00137
    00146 uint Path::AddFlow(uint new_flow, LinkGraphJob &job, uint max_saturation)
    00147 {
    00148   if (this->parent != NULL) {
    00149     LinkGraphJob::Edge edge = job[this->parent->node][this->node];
    00150     if (max_saturation != UINT_MAX) {
    00151       uint usable_cap = edge.Capacity() * max_saturation / 100;
    00152       if (usable_cap > edge.Flow()) {
    00153         new_flow = min(new_flow, usable_cap - edge.Flow());
    00154       } else {
    00155         return 0;
    00156       }
    00157     }
    00158     new_flow = this->parent->AddFlow(new_flow, job, max_saturation);
    00159     if (new_flow > 0) {
    00160       job[this->parent->node].Paths().insert(this);
    00161     }
    00162     edge.AddFlow(new_flow);
    00163   }
    00164   this->flow += new_flow;
    00165   return new_flow;
    00166 }
    00167
    00173 Path::Path(NodeID n, bool source) :
    00174   distance(source ? 0 : UINT_MAX),
    00175   capacity(0),
    00176   free_capacity(source ? INT_MAX : INT_MIN),
    00177   flow(0), node(n), origin(source ? n : INVALID_NODE),
    00178   num_children(0), parent(NULL)
    00179 {}
    00180