linkgraph.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 "linkgraph.h"
00015 
00016 /* Initialize the link-graph-pool */
00017 LinkGraphPool _link_graph_pool("LinkGraph");
00018 INSTANTIATE_POOL_METHODS(LinkGraph)
00019 
00020 
00025 inline void LinkGraph::BaseNode::Init(StationID st, uint demand)
00026 {
00027   this->supply = 0;
00028   this->demand = demand;
00029   this->station = st;
00030   this->last_update = INVALID_DATE;
00031 }
00032 
00037 inline void LinkGraph::BaseEdge::Init(uint distance)
00038 {
00039   this->distance = distance;
00040   this->capacity = 0;
00041   this->usage = 0;
00042   this->last_update = INVALID_DATE;
00043   this->next_edge = INVALID_NODE;
00044 }
00045 
00046 void LinkGraph::Compress()
00047 {
00048   this->last_compression = (_date + this->last_compression) / 2;
00049   for (NodeID node1 = 0; node1 < this->Size(); ++node1) {
00050     this->nodes[node1].supply /= 2;
00051     for (NodeID node2 = 0; node2 < this->Size(); ++node2) {
00052       this->edges[node1][node2].capacity /= 2;
00053       this->edges[node1][node2].usage /= 2;
00054     }
00055   }
00056 }
00057 
00062 void LinkGraph::Merge(LinkGraph *other)
00063 {
00064   Date age = _date - this->last_compression + 1;
00065   Date other_age = _date - other->last_compression + 1;
00066   NodeID first = this->Size();
00067   for (NodeID node1 = 0; node1 < other->Size(); ++node1) {
00068     Station *st = Station::Get(other->nodes[node1].station);
00069     NodeID new_node = this->AddNode(st);
00070     this->nodes[new_node].supply = LinkGraph::Scale(other->nodes[node1].supply, age, other_age);
00071     st->goods[this->cargo].link_graph = this->index;
00072     st->goods[this->cargo].node = new_node;
00073     for (NodeID node2 = 0; node2 < node1; ++node2) {
00074       BaseEdge &forward = this->edges[new_node][first + node2];
00075       BaseEdge &backward = this->edges[first + node2][new_node];
00076       forward = other->edges[node1][node2];
00077       backward = other->edges[node2][node1];
00078       forward.capacity = LinkGraph::Scale(forward.capacity, age, other_age);
00079       forward.usage = LinkGraph::Scale(forward.usage, age, other_age);
00080       if (forward.next_edge != INVALID_NODE) forward.next_edge += first;
00081       backward.capacity = LinkGraph::Scale(backward.capacity, age, other_age);
00082       backward.usage = LinkGraph::Scale(backward.usage, age, other_age);
00083       if (backward.next_edge != INVALID_NODE) backward.next_edge += first;
00084     }
00085     BaseEdge &new_start = this->edges[new_node][new_node];
00086     new_start = other->edges[node1][node1];
00087     if (new_start.next_edge != INVALID_NODE) new_start.next_edge += first;
00088   }
00089   delete other;
00090 }
00091 
00096 void LinkGraph::RemoveNode(NodeID id)
00097 {
00098   assert(id < this->Size());
00099 
00100   NodeID last_node = this->Size() - 1;
00101   for (NodeID i = 0; i <= last_node; ++i) {
00102     (*this)[i].RemoveEdge(id);
00103     BaseEdge *node_edges = this->edges[i];
00104     NodeID prev = i;
00105     NodeID next = node_edges[i].next_edge;
00106     while (next != INVALID_NODE) {
00107       if (next == last_node) {
00108         node_edges[prev].next_edge = id;
00109         break;
00110       }
00111       prev = next;
00112       next = node_edges[prev].next_edge;
00113     }
00114     node_edges[id] = node_edges[last_node];
00115   }
00116   Station::Get(this->nodes[last_node].station)->goods[this->cargo].node = id;
00117   this->nodes.Erase(this->nodes.Get(id));
00118   this->edges.EraseColumn(id);
00119   /* Not doing EraseRow here, as having the extra invalid row doesn't hurt
00120    * and removing it would trigger a lot of memmove. The data has already
00121    * been copied around in the loop above. */
00122 }
00123 
00132 NodeID LinkGraph::AddNode(const Station *st)
00133 {
00134   const GoodsEntry &good = st->goods[this->cargo];
00135 
00136   NodeID new_node = this->Size();
00137   this->nodes.Append();
00138   /* Avoid reducing the height of the matrix as that is expensive and we
00139    * most likely will increase it again later which is again expensive. */
00140   this->edges.Resize(new_node + 1U,
00141       max(new_node + 1U, this->edges.Height()));
00142 
00143   this->nodes[new_node].Init(st->index,
00144       HasBit(good.acceptance_pickup, GoodsEntry::GES_ACCEPTANCE));
00145 
00146   BaseEdge *new_edges = this->edges[new_node];
00147 
00148   /* Reset the first edge starting at the new node */
00149   new_edges[new_node].next_edge = INVALID_NODE;
00150 
00151   for (NodeID i = 0; i <= new_node; ++i) {
00152     uint distance = DistanceManhattan(st->xy, Station::Get(this->nodes[i].station)->xy);
00153     new_edges[i].Init(distance);
00154     this->edges[i][new_node].Init(distance);
00155   }
00156   return new_node;
00157 }
00158 
00164 void LinkGraph::Node::AddEdge(NodeID to, uint capacity, uint usage)
00165 {
00166   assert(this->index != to);
00167   BaseEdge &edge = this->edges[to];
00168   BaseEdge &first = this->edges[this->index];
00169   edge.capacity = capacity;
00170   edge.usage = usage == UINT_MAX ? 0 : usage;
00171   edge.next_edge = first.next_edge;
00172   first.next_edge = to;
00173   edge.last_update = _date;
00174 }
00175 
00176 void LinkGraph::Node::UpdateEdge(NodeID to, uint capacity, uint usage)
00177 {
00178   if (this->edges[to].capacity == 0) {
00179     this->AddEdge(to, capacity, usage);
00180   } else {
00181     (*this)[to].Update(capacity, usage);
00182   }
00183 }
00184 
00189 void LinkGraph::Node::RemoveEdge(NodeID to)
00190 {
00191   if (this->index == to) return;
00192   BaseEdge &edge = this->edges[to];
00193   edge.capacity = 0;
00194   edge.last_update = INVALID_DATE;
00195   edge.usage = 0;
00196 
00197   NodeID prev = this->index;
00198   NodeID next = this->edges[this->index].next_edge;
00199   while (next != INVALID_NODE) {
00200     if (next == to) {
00201       /* Will be removed, skip it. */
00202       next = this->edges[prev].next_edge = edge.next_edge;
00203       break;
00204     } else {
00205       prev = next;
00206       next = this->edges[next].next_edge;
00207     }
00208   }
00209 }
00210 
00219 void LinkGraph::Edge::Update(uint capacity, uint usage)
00220 {
00221   assert(this->edge.capacity > 0);
00222   if (usage == UINT_MAX) {
00223     this->edge.capacity = max(this->edge.capacity, capacity);
00224   } else {
00225     assert(capacity >= usage);
00226     this->edge.capacity += capacity;
00227     this->edge.usage += usage;
00228   }
00229   this->edge.last_update = _date;
00230 }
00231 
00237 void LinkGraph::Init(uint size)
00238 {
00239   assert(this->Size() == 0);
00240   this->edges.Resize(size, size);
00241   this->nodes.Resize(size);
00242 
00243   for (uint i = 0; i < size; ++i) {
00244     this->nodes[i].Init();
00245     BaseEdge *column = this->edges[i];
00246     for (uint j = 0; j < size; ++j) column[j].Init();
00247   }
00248 }