flowmapper.cpp

Go to the documentation of this file.
00001 /* $Id: flowmapper.cpp 26166 2013-12-20 14:57:44Z 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 "flowmapper.h"
00014 
00019 void FlowMapper::Run(LinkGraphJob &job) const
00020 {
00021   for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
00022     Node prev_node = job[node_id];
00023     StationID prev = prev_node.Station();
00024     PathList &paths = prev_node.Paths();
00025     for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
00026       Path *path = *i;
00027       uint flow = path->GetFlow();
00028       if (flow == 0) break;
00029       Node node = job[path->GetNode()];
00030       StationID via = node.Station();
00031       StationID origin = job[path->GetOrigin()].Station();
00032       assert(prev != via && via != origin);
00033       /* Mark all of the flow for local consumption at "first". */
00034       node.Flows().AddFlow(origin, via, flow);
00035       if (prev != origin) {
00036         /* Pass some of the flow marked for local consumption at "prev" on
00037          * to this node. */
00038         prev_node.Flows().PassOnFlow(origin, via, flow);
00039       } else {
00040         /* Prev node is origin. Simply add flow. */
00041         prev_node.Flows().AddFlow(origin, via, flow);
00042       }
00043     }
00044   }
00045 
00046   for (NodeID node_id = 0; node_id < job.Size(); ++node_id) {
00047     /* Remove local consumption shares marked as invalid. */
00048     Node node = job[node_id];
00049     FlowStatMap &flows = node.Flows();
00050     flows.FinalizeLocalConsumption(node.Station());
00051     if (this->scale) {
00052       /* Scale by time the graph has been running without being compressed. */
00053       uint runtime = job.JoinDate() - job.Settings().recalc_time - job.LastCompression();
00054       for (FlowStatMap::iterator i = flows.begin(); i != flows.end(); ++i) {
00055         i->second.ScaleToMonthly(runtime);
00056       }
00057     }
00058     /* Clear paths. */
00059     PathList &paths = node.Paths();
00060     for (PathList::iterator i = paths.begin(); i != paths.end(); ++i) {
00061       delete *i;
00062     }
00063     paths.clear();
00064   }
00065 }