flowmapper.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 "flowmapper.h"
00014 
00019 void FlowMapper::Run(LinkGraphComponent *component)
00020 {
00021   for (NodeID node_id = 0; node_id < component->GetSize(); ++node_id) {
00022     Node &prev_node = component->GetNode(node_id);
00023     StationID prev = prev_node.station;
00024     PathSet &paths = prev_node.paths;
00025     for (PathSet::iterator i = paths.begin(); i != paths.end(); ++i) {
00026       Path *path = *i;
00027       uint flow = path->GetFlow();
00028       if (flow == 0) continue;
00029       Node &node = component->GetNode(path->GetNode());
00030       StationID via = node.station;
00031       assert(prev != via);
00032       StationID origin = component->GetNode(path->GetOrigin()).station;
00033       assert(via != origin);
00034       /* Mark all of the flow for local consumption at "first". */
00035       node.flows[origin][via] += flow;
00036       /* Pass some of the flow marked for local consumption at "prev" on
00037        * to this node. */
00038       prev_node.flows[origin][via] += flow;
00039       /* Find simple circular flows ... */
00040       assert(node.flows[origin][prev] == 0);
00041       if (prev != origin) {
00042         prev_node.flows[origin][prev] -= flow;
00043       }
00044     }
00045   }
00046   for (NodeID node_id = 0; node_id < component->GetSize(); ++node_id) {
00047     PathSet &paths = component->GetNode(node_id).paths;
00048     for (PathSet::iterator i = paths.begin(); i != paths.end(); ++i) {
00049       delete *i;
00050     }
00051     paths.clear();
00052   }
00053 }