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