OpenTTD
linkgraph.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
10 #include "../stdafx.h"
11 #include "../core/pool_func.hpp"
12 #include "linkgraph.h"
13 
14 #include "../safeguards.h"
15 
16 /* Initialize the link-graph-pool */
17 LinkGraphPool _link_graph_pool("LinkGraph");
19 
20 
26 inline void LinkGraph::BaseNode::Init(TileIndex xy, StationID st, uint demand)
27 {
28  this->xy = xy;
29  this->supply = 0;
30  this->demand = demand;
31  this->station = st;
32  this->last_update = INVALID_DATE;
33 }
34 
39 {
40  this->capacity = 0;
41  this->usage = 0;
44  this->next_edge = INVALID_NODE;
45 }
46 
52 void LinkGraph::ShiftDates(int interval)
53 {
54  this->last_compression += interval;
55  for (NodeID node1 = 0; node1 < this->Size(); ++node1) {
56  BaseNode &source = this->nodes[node1];
57  if (source.last_update != INVALID_DATE) source.last_update += interval;
58  for (NodeID node2 = 0; node2 < this->Size(); ++node2) {
59  BaseEdge &edge = this->edges[node1][node2];
61  if (edge.last_restricted_update != INVALID_DATE) edge.last_restricted_update += interval;
62  }
63  }
64 }
65 
66 void LinkGraph::Compress()
67 {
68  this->last_compression = (_date + this->last_compression) / 2;
69  for (NodeID node1 = 0; node1 < this->Size(); ++node1) {
70  this->nodes[node1].supply /= 2;
71  for (NodeID node2 = 0; node2 < this->Size(); ++node2) {
72  BaseEdge &edge = this->edges[node1][node2];
73  if (edge.capacity > 0) {
74  edge.capacity = max(1U, edge.capacity / 2);
75  edge.usage /= 2;
76  }
77  }
78  }
79 }
80 
86 {
87  Date age = _date - this->last_compression + 1;
88  Date other_age = _date - other->last_compression + 1;
89  NodeID first = this->Size();
90  for (NodeID node1 = 0; node1 < other->Size(); ++node1) {
91  Station *st = Station::Get(other->nodes[node1].station);
92  NodeID new_node = this->AddNode(st);
93  this->nodes[new_node].supply = LinkGraph::Scale(other->nodes[node1].supply, age, other_age);
94  st->goods[this->cargo].link_graph = this->index;
95  st->goods[this->cargo].node = new_node;
96  for (NodeID node2 = 0; node2 < node1; ++node2) {
97  BaseEdge &forward = this->edges[new_node][first + node2];
98  BaseEdge &backward = this->edges[first + node2][new_node];
99  forward = other->edges[node1][node2];
100  backward = other->edges[node2][node1];
101  forward.capacity = LinkGraph::Scale(forward.capacity, age, other_age);
102  forward.usage = LinkGraph::Scale(forward.usage, age, other_age);
103  if (forward.next_edge != INVALID_NODE) forward.next_edge += first;
104  backward.capacity = LinkGraph::Scale(backward.capacity, age, other_age);
105  backward.usage = LinkGraph::Scale(backward.usage, age, other_age);
106  if (backward.next_edge != INVALID_NODE) backward.next_edge += first;
107  }
108  BaseEdge &new_start = this->edges[new_node][new_node];
109  new_start = other->edges[node1][node1];
110  if (new_start.next_edge != INVALID_NODE) new_start.next_edge += first;
111  }
112  delete other;
113 }
114 
119 void LinkGraph::RemoveNode(NodeID id)
120 {
121  assert(id < this->Size());
122 
123  NodeID last_node = this->Size() - 1;
124  for (NodeID i = 0; i <= last_node; ++i) {
125  (*this)[i].RemoveEdge(id);
126  BaseEdge *node_edges = this->edges[i];
127  NodeID prev = i;
128  NodeID next = node_edges[i].next_edge;
129  while (next != INVALID_NODE) {
130  if (next == last_node) {
131  node_edges[prev].next_edge = id;
132  break;
133  }
134  prev = next;
135  next = node_edges[prev].next_edge;
136  }
137  node_edges[id] = node_edges[last_node];
138  }
139  Station::Get(this->nodes[last_node].station)->goods[this->cargo].node = id;
140  /* Erase node by swapping with the last element. Node index is referenced
141  * directly from station goods entries so the order and position must remain. */
142  this->nodes[id] = this->nodes.back();
143  this->nodes.pop_back();
144  this->edges.EraseColumn(id);
145  /* Not doing EraseRow here, as having the extra invalid row doesn't hurt
146  * and removing it would trigger a lot of memmove. The data has already
147  * been copied around in the loop above. */
148 }
149 
158 NodeID LinkGraph::AddNode(const Station *st)
159 {
160  const GoodsEntry &good = st->goods[this->cargo];
161 
162  NodeID new_node = this->Size();
163  this->nodes.emplace_back();
164  /* Avoid reducing the height of the matrix as that is expensive and we
165  * most likely will increase it again later which is again expensive. */
166  this->edges.Resize(new_node + 1U,
167  max(new_node + 1U, this->edges.Height()));
168 
169  this->nodes[new_node].Init(st->xy, st->index,
171 
172  BaseEdge *new_edges = this->edges[new_node];
173 
174  /* Reset the first edge starting at the new node */
175  new_edges[new_node].next_edge = INVALID_NODE;
176 
177  for (NodeID i = 0; i <= new_node; ++i) {
178  new_edges[i].Init();
179  this->edges[i][new_node].Init();
180  }
181  return new_node;
182 }
183 
192 void LinkGraph::Node::AddEdge(NodeID to, uint capacity, uint usage, EdgeUpdateMode mode)
193 {
194  assert(this->index != to);
195  BaseEdge &edge = this->edges[to];
196  BaseEdge &first = this->edges[this->index];
197  edge.capacity = capacity;
198  edge.usage = usage;
199  edge.next_edge = first.next_edge;
200  first.next_edge = to;
202  if (mode & EUM_RESTRICTED) edge.last_restricted_update = _date;
203 }
204 
212 void LinkGraph::Node::UpdateEdge(NodeID to, uint capacity, uint usage, EdgeUpdateMode mode)
213 {
214  assert(capacity > 0);
215  assert(usage <= capacity);
216  if (this->edges[to].capacity == 0) {
217  this->AddEdge(to, capacity, usage, mode);
218  } else {
219  (*this)[to].Update(capacity, usage, mode);
220  }
221 }
222 
228 {
229  if (this->index == to) return;
230  BaseEdge &edge = this->edges[to];
231  edge.capacity = 0;
234  edge.usage = 0;
235 
236  NodeID prev = this->index;
237  NodeID next = this->edges[this->index].next_edge;
238  while (next != INVALID_NODE) {
239  if (next == to) {
240  /* Will be removed, skip it. */
241  this->edges[prev].next_edge = edge.next_edge;
242  edge.next_edge = INVALID_NODE;
243  break;
244  } else {
245  prev = next;
246  next = this->edges[next].next_edge;
247  }
248  }
249 }
250 
261 {
262  assert(this->edge.capacity > 0);
263  assert(capacity >= usage);
264 
265  if (mode & EUM_INCREASE) {
266  this->edge.capacity += capacity;
267  this->edge.usage += usage;
268  } else if (mode & EUM_REFRESH) {
269  this->edge.capacity = max(this->edge.capacity, capacity);
270  this->edge.usage = max(this->edge.usage, usage);
271  }
272  if (mode & EUM_UNRESTRICTED) this->edge.last_unrestricted_update = _date;
273  if (mode & EUM_RESTRICTED) this->edge.last_restricted_update = _date;
274 }
275 
281 void LinkGraph::Init(uint size)
282 {
283  assert(this->Size() == 0);
284  this->edges.Resize(size, size);
285  this->nodes.resize(size);
286 
287  for (uint i = 0; i < size; ++i) {
288  this->nodes[i].Init();
289  BaseEdge *column = this->edges[i];
290  for (uint j = 0; j < size; ++j) column[j].Init();
291  }
292 }
NodeID AddNode(const Station *st)
Add a node to the component and create empty edges associated with it.
Definition: linkgraph.cpp:158
Increase capacity.
An edge in the link graph.
Definition: linkgraph.h:61
LinkGraphPool _link_graph_pool("LinkGraph")
The actual pool with link graphs.
Stores station stats for a single cargo.
Definition: station_base.h:170
Tindex index
Index of this pool item.
Definition: pool_type.hpp:189
void AddEdge(NodeID to, uint capacity, uint usage, EdgeUpdateMode mode)
Fill an edge with values from a link.
Definition: linkgraph.cpp:192
void ShiftDates(int interval)
Shift all dates by given interval.
Definition: linkgraph.cpp:52
CargoID cargo
Cargo of this component&#39;s link graph.
Definition: linkgraph.h:531
uint usage
Usage of the link.
Definition: linkgraph.h:63
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:24
GoodsEntry goods[NUM_CARGO]
Goods at this station.
Definition: station_base.h:479
Refresh capacity.
A connected component of a link graph.
Definition: linkgraph.h:38
NodeID next_edge
Destination of next valid edge starting at the same source node.
Definition: linkgraph.h:66
Date last_update
When the supply was last updated.
Definition: linkgraph.h:51
static uint Scale(uint val, uint target_age, uint orig_age)
Scale a value from a link graph of age orig_age for usage in one of age target_age.
Definition: linkgraph.h:453
uint Size() const
Get the current size of the component.
Definition: linkgraph.h:497
void Resize(uint new_width, uint new_height)
Set the size to a specific width and height, preserving item positions as far as possible in the proc...
LinkGraphID link_graph
Link graph this station belongs to.
Definition: station_base.h:257
byte status
Status of this cargo, see GoodsEntryStatus.
Definition: station_base.h:226
static const Date INVALID_DATE
Representation of an invalid date.
Definition: date_type.h:108
NodeID node
ID of node in link graph referring to this goods entry.
Definition: station_base.h:258
Use restricted link.
EdgeMatrix edges
Edges in the component.
Definition: linkgraph.h:534
Date last_restricted_update
When the restricted part of the link was last updated.
Definition: linkgraph.h:65
void Init()
Create an edge.
Definition: linkgraph.cpp:38
void RemoveNode(NodeID id)
Remove a node from the link graph by overwriting it with the last node.
Definition: linkgraph.cpp:119
Base class for all pools.
Definition: pool_type.hpp:82
EdgeUpdateMode
Special modes for updating links.
uint capacity
Capacity of the link.
Definition: linkgraph.h:62
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don&#39;t get linker errors.
Definition: pool_func.hpp:224
void EraseColumn(uint x)
Erase a column, replacing it with the last one.
void Init(uint size)
Resize the component and fill it with empty nodes and edges.
Definition: linkgraph.cpp:281
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:78
Node of the link graph.
Definition: linkgraph.h:46
TileIndex xy
Base tile of the station.
Date last_compression
Last time the capacities and supplies were compressed.
Definition: linkgraph.h:532
Declaration of link graph classes used for cargo distribution.
int32 Date
The type to store our dates in.
Definition: date_type.h:14
void Update(uint capacity, uint usage, EdgeUpdateMode mode)
Update an edge.
Definition: linkgraph.cpp:260
static bool HasBit(const T x, const uint8 y)
Checks if a bit in a value is set.
Use unrestricted link.
NodeVector nodes
Nodes in the component.
Definition: linkgraph.h:533
void RemoveEdge(NodeID to)
Remove an outgoing edge from this node.
Definition: linkgraph.cpp:227
void UpdateEdge(NodeID to, uint capacity, uint usage, EdgeUpdateMode mode)
Creates an edge if none exists yet or updates an existing edge.
Definition: linkgraph.cpp:212
static Station * Get(size_t index)
Gets station with given index.
Date _date
Current date in days (day counter)
Definition: date.cpp:26
void Merge(LinkGraph *other)
Merge a link graph with another one.
Definition: linkgraph.cpp:85
Station data structure.
Definition: station_base.h:450
Set when the station accepts the cargo currently for final deliveries.
Definition: station_base.h:177
Date last_unrestricted_update
When the unrestricted part of the link was last updated.
Definition: linkgraph.h:64