OpenTTD Source  1.11.2
linkgraph.h
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 #ifndef LINKGRAPH_H
11 #define LINKGRAPH_H
12 
13 #include "../core/pool_type.hpp"
14 #include "../core/smallmap_type.hpp"
15 #include "../core/smallmatrix_type.hpp"
16 #include "../station_base.h"
17 #include "../cargotype.h"
18 #include "../date_func.h"
19 #include "linkgraph_type.h"
20 #include <utility>
21 
22 struct SaveLoad;
23 class LinkGraph;
24 
32 
39 class LinkGraph : public LinkGraphPool::PoolItem<&_link_graph_pool> {
40 public:
41 
47  struct BaseNode {
48  uint supply;
49  uint demand;
50  StationID station;
53  void Init(TileIndex xy = INVALID_TILE, StationID st = INVALID_STATION, uint demand = 0);
54  };
55 
62  struct BaseEdge {
63  uint capacity;
64  uint usage;
67  NodeID next_edge;
68  void Init();
69  };
70 
75  template<typename Tedge>
76  class EdgeWrapper {
77  protected:
78  Tedge &edge;
79 
80  public:
81 
86  EdgeWrapper (Tedge &edge) : edge(edge) {}
87 
92  uint Capacity() const { return this->edge.capacity; }
93 
98  uint Usage() const { return this->edge.usage; }
99 
104  Date LastUnrestrictedUpdate() const { return this->edge.last_unrestricted_update; }
105 
110  Date LastRestrictedUpdate() const { return this->edge.last_restricted_update; }
111 
116  Date LastUpdate() const { return std::max(this->edge.last_unrestricted_update, this->edge.last_restricted_update); }
117  };
118 
124  template<typename Tnode, typename Tedge>
125  class NodeWrapper {
126  protected:
127  Tnode &node;
128  Tedge *edges;
129  NodeID index;
130 
131  public:
132 
139  NodeWrapper(Tnode &node, Tedge *edges, NodeID index) : node(node),
140  edges(edges), index(index) {}
141 
146  uint Supply() const { return this->node.supply; }
147 
152  uint Demand() const { return this->node.demand; }
153 
158  StationID Station() const { return this->node.station; }
159 
164  Date LastUpdate() const { return this->node.last_update; }
165 
170  TileIndex XY() const { return this->node.xy; }
171  };
172 
180  template <class Tedge, class Tedge_wrapper, class Titer>
182  protected:
183  Tedge *base;
184  NodeID current;
185 
192  class FakePointer : public std::pair<NodeID, Tedge_wrapper> {
193  public:
194 
199  FakePointer(const std::pair<NodeID, Tedge_wrapper> &pair) : std::pair<NodeID, Tedge_wrapper>(pair) {}
200 
205  std::pair<NodeID, Tedge_wrapper> *operator->() { return this; }
206  };
207 
208  public:
214  BaseEdgeIterator (Tedge *base, NodeID current) :
215  base(base),
216  current(current == INVALID_NODE ? current : base[current].next_edge)
217  {}
218 
223  Titer &operator++()
224  {
225  this->current = this->base[this->current].next_edge;
226  return static_cast<Titer &>(*this);
227  }
228 
233  Titer operator++(int)
234  {
235  Titer ret(static_cast<Titer &>(*this));
236  this->current = this->base[this->current].next_edge;
237  return ret;
238  }
239 
247  template<class Tother>
248  bool operator==(const Tother &other)
249  {
250  return this->base == other.base && this->current == other.current;
251  }
252 
260  template<class Tother>
261  bool operator!=(const Tother &other)
262  {
263  return this->base != other.base || this->current != other.current;
264  }
265 
270  std::pair<NodeID, Tedge_wrapper> operator*() const
271  {
272  return std::pair<NodeID, Tedge_wrapper>(this->current, Tedge_wrapper(this->base[this->current]));
273  }
274 
279  FakePointer operator->() const {
280  return FakePointer(this->operator*());
281  }
282  };
283 
288 
292  class Edge : public EdgeWrapper<BaseEdge> {
293  public:
299  void Update(uint capacity, uint usage, EdgeUpdateMode mode);
300  void Restrict() { this->edge.last_unrestricted_update = INVALID_DATE; }
301  void Release() { this->edge.last_restricted_update = INVALID_DATE; }
302  };
303 
308  class ConstEdgeIterator : public BaseEdgeIterator<const BaseEdge, ConstEdge, ConstEdgeIterator> {
309  public:
317  };
318 
323  class EdgeIterator : public BaseEdgeIterator<BaseEdge, Edge, EdgeIterator> {
324  public:
332  };
333 
338  class ConstNode : public NodeWrapper<const BaseNode, const BaseEdge> {
339  public:
345  ConstNode(const LinkGraph *lg, NodeID node) :
346  NodeWrapper<const BaseNode, const BaseEdge>(lg->nodes[node], lg->edges[node], node)
347  {}
348 
355  ConstEdge operator[](NodeID to) const { return ConstEdge(this->edges[to]); }
356 
361  ConstEdgeIterator Begin() const { return ConstEdgeIterator(this->edges, this->index); }
362 
367  ConstEdgeIterator End() const { return ConstEdgeIterator(this->edges, INVALID_NODE); }
368  };
369 
373  class Node : public NodeWrapper<BaseNode, BaseEdge> {
374  public:
380  Node(LinkGraph *lg, NodeID node) :
382  {}
383 
390  Edge operator[](NodeID to) { return Edge(this->edges[to]); }
391 
396  EdgeIterator Begin() { return EdgeIterator(this->edges, this->index); }
397 
402  EdgeIterator End() { return EdgeIterator(this->edges, INVALID_NODE); }
403 
408  void UpdateSupply(uint supply)
409  {
410  this->node.supply += supply;
411  this->node.last_update = _date;
412  }
413 
419  {
420  this->node.xy = xy;
421  }
422 
427  void SetDemand(uint demand)
428  {
429  this->node.demand = demand;
430  }
431 
432  void AddEdge(NodeID to, uint capacity, uint usage, EdgeUpdateMode mode);
433  void UpdateEdge(NodeID to, uint capacity, uint usage, EdgeUpdateMode mode);
434  void RemoveEdge(NodeID to);
435  };
436 
437  typedef std::vector<BaseNode> NodeVector;
438  typedef SmallMatrix<BaseEdge> EdgeMatrix;
439 
441  static const uint MIN_TIMEOUT_DISTANCE = 32;
442 
444  static const uint COMPRESSION_INTERVAL = 256;
445 
454  inline static uint Scale(uint val, uint target_age, uint orig_age)
455  {
456  return val > 0 ? std::max(1U, val * target_age / orig_age) : 0;
457  }
458 
466 
467  void Init(uint size);
468  void ShiftDates(int interval);
469  void Compress();
470  void Merge(LinkGraph *other);
471 
472  /* Splitting link graphs is intentionally not implemented.
473  * The overhead in determining connectedness would probably outweigh the
474  * benefit of having to deal with smaller graphs. In real world examples
475  * networks generally grow. Only rarely a network is permanently split.
476  * Reacting to temporary splits here would obviously create performance
477  * problems and detecting the temporary or permanent nature of splits isn't
478  * trivial. */
479 
485  inline Node operator[](NodeID num) { return Node(this, num); }
486 
492  inline ConstNode operator[](NodeID num) const { return ConstNode(this, num); }
493 
498  inline uint Size() const { return (uint)this->nodes.size(); }
499 
504  inline Date LastCompression() const { return this->last_compression; }
505 
510  inline CargoID Cargo() const { return this->cargo; }
511 
517  inline uint Monthly(uint base) const
518  {
519  return base * 30 / (_date - this->last_compression + 1);
520  }
521 
522  NodeID AddNode(const Station *st);
523  void RemoveNode(NodeID id);
524 
525 protected:
526  friend class LinkGraph::ConstNode;
527  friend class LinkGraph::Node;
528  friend const SaveLoad *GetLinkGraphDesc();
529  friend const SaveLoad *GetLinkGraphJobDesc();
530  friend void SaveLoad_LinkGraph(LinkGraph &lg);
531 
534  NodeVector nodes;
536 };
537 
538 #endif /* LINKGRAPH_H */
LinkGraph::NodeWrapper::edges
Tedge * edges
Outgoing edges for wrapped node.
Definition: linkgraph.h:128
INVALID_CARGO
static const byte INVALID_CARGO
Constant representing invalid cargo.
Definition: cargotype.h:52
TileIndex
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:83
LinkGraph::EdgeWrapper::Usage
uint Usage() const
Get edge's usage.
Definition: linkgraph.h:98
LinkGraph::COMPRESSION_INTERVAL
static const uint COMPRESSION_INTERVAL
Minimum number of days between subsequent compressions of a LG.
Definition: linkgraph.h:444
LinkGraph::Edge::Edge
Edge(BaseEdge &edge)
Constructor.
Definition: linkgraph.h:298
LinkGraph::edges
EdgeMatrix edges
Edges in the component.
Definition: linkgraph.h:535
LinkGraph::Node::AddEdge
void AddEdge(NodeID to, uint capacity, uint usage, EdgeUpdateMode mode)
Fill an edge with values from a link.
Definition: linkgraph.cpp:191
LinkGraph
A connected component of a link graph.
Definition: linkgraph.h:39
LinkGraphPool
Pool< LinkGraph, LinkGraphID, 32, 0xFFFF > LinkGraphPool
Type of the pool for link graph components.
Definition: linkgraph.h:23
LinkGraph::Node::UpdateEdge
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:211
LinkGraph::Node
Updatable node class.
Definition: linkgraph.h:373
LinkGraph::BaseEdge::usage
uint usage
Usage of the link.
Definition: linkgraph.h:64
LinkGraph::nodes
NodeVector nodes
Nodes in the component.
Definition: linkgraph.h:534
LinkGraph::EdgeWrapper::LastRestrictedUpdate
Date LastRestrictedUpdate() const
Get the date of the last update to the edge's restricted capacity.
Definition: linkgraph.h:110
LinkGraph::BaseNode::demand
uint demand
Acceptance at the station.
Definition: linkgraph.h:49
Station
Station data structure.
Definition: station_base.h:450
LinkGraph::EdgeWrapper::LastUpdate
Date LastUpdate() const
Get the date of the last update to any part of the edge's capacity.
Definition: linkgraph.h:116
LinkGraph::Node::UpdateSupply
void UpdateSupply(uint supply)
Update the node's supply and set last_update to the current date.
Definition: linkgraph.h:408
LinkGraph::Node::Begin
EdgeIterator Begin()
Get an iterator pointing to the start of the edges array.
Definition: linkgraph.h:396
LinkGraph::EdgeIterator
An iterator for non-const edges.
Definition: linkgraph.h:323
LinkGraph::ConstNode
Constant node class.
Definition: linkgraph.h:338
LinkGraph::BaseNode::supply
uint supply
Supply at the station.
Definition: linkgraph.h:48
LinkGraph::BaseEdge::Init
void Init()
Create an edge.
Definition: linkgraph.cpp:38
LinkGraph::Scale
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:454
LinkGraph::Node::End
EdgeIterator End()
Get an iterator pointing beyond the end of the edges array.
Definition: linkgraph.h:402
LinkGraph::BaseNode::station
StationID station
Station ID.
Definition: linkgraph.h:50
LinkGraph::LinkGraph
LinkGraph()
Bare constructor, only for save/load.
Definition: linkgraph.h:460
LinkGraph::RemoveNode
void RemoveNode(NodeID id)
Remove a node from the link graph by overwriting it with the last node.
Definition: linkgraph.cpp:119
LinkGraph::Init
void Init(uint size)
Resize the component and fill it with empty nodes and edges.
Definition: linkgraph.cpp:280
EdgeUpdateMode
EdgeUpdateMode
Special modes for updating links.
Definition: linkgraph_type.h:52
LinkGraph::Edge
An updatable edge class.
Definition: linkgraph.h:292
LinkGraph::EdgeWrapper
Wrapper for an edge (const or not) allowing retrieval, but no modification.
Definition: linkgraph.h:76
LinkGraph::ConstNode::ConstNode
ConstNode(const LinkGraph *lg, NodeID node)
Constructor.
Definition: linkgraph.h:345
LinkGraph::BaseNode::last_update
Date last_update
When the supply was last updated.
Definition: linkgraph.h:52
LinkGraph::GetLinkGraphDesc
const friend SaveLoad * GetLinkGraphDesc()
Get a SaveLoad array for a link graph.
Definition: linkgraph_sl.cpp:31
LinkGraph::BaseEdgeIterator::BaseEdgeIterator
BaseEdgeIterator(Tedge *base, NodeID current)
Constructor.
Definition: linkgraph.h:214
LinkGraph::BaseEdgeIterator::operator++
Titer & operator++()
Prefix-increment.
Definition: linkgraph.h:223
LinkGraph::NodeWrapper::LastUpdate
Date LastUpdate() const
Get node's last update.
Definition: linkgraph.h:164
LinkGraph::EdgeWrapper::LastUnrestrictedUpdate
Date LastUnrestrictedUpdate() const
Get the date of the last update to the edge's unrestricted capacity.
Definition: linkgraph.h:104
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
LinkGraph::BaseEdge::capacity
uint capacity
Capacity of the link.
Definition: linkgraph.h:63
LinkGraph::ConstEdge
EdgeWrapper< const BaseEdge > ConstEdge
A constant edge class.
Definition: linkgraph.h:287
LinkGraph::operator[]
ConstNode operator[](NodeID num) const
Get a const reference to a node with the specified id.
Definition: linkgraph.h:492
LinkGraph::EdgeWrapper::edge
Tedge & edge
Actual edge to be used.
Definition: linkgraph.h:78
LinkGraph::SaveLoad_LinkGraph
friend void SaveLoad_LinkGraph(LinkGraph &lg)
Save/load a link graph.
Definition: linkgraph_sl.cpp:140
LinkGraph::BaseEdgeIterator::operator*
std::pair< NodeID, Tedge_wrapper > operator*() const
Dereference with operator*.
Definition: linkgraph.h:270
SmallMatrix< BaseEdge >
LinkGraph::NodeWrapper::Demand
uint Demand() const
Get demand of wrapped node.
Definition: linkgraph.h:152
_link_graph_pool
LinkGraphPool _link_graph_pool
The actual pool with link graphs.
Date
int32 Date
The type to store our dates in.
Definition: date_type.h:14
LinkGraph::BaseEdge
An edge in the link graph.
Definition: linkgraph.h:62
LinkGraph::Size
uint Size() const
Get the current size of the component.
Definition: linkgraph.h:498
LinkGraph::NodeWrapper
Wrapper for a node (const or not) allowing retrieval, but no modification.
Definition: linkgraph.h:125
LinkGraph::Node::UpdateLocation
void UpdateLocation(TileIndex xy)
Update the node's location on the map.
Definition: linkgraph.h:418
LinkGraph::Cargo
CargoID Cargo() const
Get the cargo ID this component's link graph refers to.
Definition: linkgraph.h:510
LinkGraph::operator[]
Node operator[](NodeID num)
Get a node with the specified id.
Definition: linkgraph.h:485
LinkGraph::EdgeWrapper::Capacity
uint Capacity() const
Get edge's capacity.
Definition: linkgraph.h:92
LinkGraph::BaseEdgeIterator
Base class for iterating across outgoing edges of a node.
Definition: linkgraph.h:181
LinkGraph::ConstNode::operator[]
ConstEdge operator[](NodeID to) const
Get a ConstEdge.
Definition: linkgraph.h:355
LinkGraph::EdgeIterator::EdgeIterator
EdgeIterator(BaseEdge *edges, NodeID current)
Constructor.
Definition: linkgraph.h:330
LinkGraph::NodeWrapper::Station
StationID Station() const
Get ID of station belonging to wrapped node.
Definition: linkgraph.h:158
LinkGraph::LastCompression
Date LastCompression() const
Get date of last compression.
Definition: linkgraph.h:504
LinkGraph::ShiftDates
void ShiftDates(int interval)
Shift all dates by given interval.
Definition: linkgraph.cpp:52
LinkGraph::BaseEdge::last_unrestricted_update
Date last_unrestricted_update
When the unrestricted part of the link was last updated.
Definition: linkgraph.h:65
Pool
Base class for all pools.
Definition: pool_type.hpp:81
LinkGraph::BaseNode::Init
void Init(TileIndex xy=INVALID_TILE, StationID st=INVALID_STATION, uint demand=0)
Create a node or clear it.
Definition: linkgraph.cpp:26
LinkGraph::BaseEdgeIterator::current
NodeID current
Current offset in edges array.
Definition: linkgraph.h:184
LinkGraph::AddNode
NodeID AddNode(const Station *st)
Add a node to the component and create empty edges associated with it.
Definition: linkgraph.cpp:158
LinkGraph::BaseEdgeIterator::operator->
FakePointer operator->() const
Dereference with operator->.
Definition: linkgraph.h:279
LinkGraph::Node::RemoveEdge
void RemoveEdge(NodeID to)
Remove an outgoing edge from this node.
Definition: linkgraph.cpp:226
LinkGraph::NodeWrapper::NodeWrapper
NodeWrapper(Tnode &node, Tedge *edges, NodeID index)
Wrap a node.
Definition: linkgraph.h:139
INVALID_DATE
static const Date INVALID_DATE
Representation of an invalid date.
Definition: date_type.h:110
LinkGraph::cargo
CargoID cargo
Cargo of this component's link graph.
Definition: linkgraph.h:532
LinkGraph::GetLinkGraphJobDesc
const friend SaveLoad * GetLinkGraphJobDesc()
Get a SaveLoad array for a link graph job.
Definition: linkgraph_sl.cpp:51
LinkGraph::MIN_TIMEOUT_DISTANCE
static const uint MIN_TIMEOUT_DISTANCE
Minimum effective distance for timeout calculation.
Definition: linkgraph.h:441
LinkGraph::Edge::Update
void Update(uint capacity, uint usage, EdgeUpdateMode mode)
Update an edge.
Definition: linkgraph.cpp:259
LinkGraph::Node::SetDemand
void SetDemand(uint demand)
Set the node's demand.
Definition: linkgraph.h:427
LinkGraph::last_compression
Date last_compression
Last time the capacities and supplies were compressed.
Definition: linkgraph.h:533
LinkGraph::ConstEdgeIterator
An iterator for const edges.
Definition: linkgraph.h:308
LinkGraph::BaseEdgeIterator::base
Tedge * base
Array of edges being iterated.
Definition: linkgraph.h:183
LinkGraph::BaseNode::xy
TileIndex xy
Location of the station referred to by the node.
Definition: linkgraph.h:51
LinkGraph::Merge
void Merge(LinkGraph *other)
Merge a link graph with another one.
Definition: linkgraph.cpp:85
LinkGraph::ConstEdgeIterator::ConstEdgeIterator
ConstEdgeIterator(const BaseEdge *edges, NodeID current)
Constructor.
Definition: linkgraph.h:315
linkgraph_type.h
LinkGraph::BaseEdge::last_restricted_update
Date last_restricted_update
When the restricted part of the link was last updated.
Definition: linkgraph.h:66
LinkGraph::Node::Node
Node(LinkGraph *lg, NodeID node)
Constructor.
Definition: linkgraph.h:380
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:20
INVALID_TILE
static const TileIndex INVALID_TILE
The very nice invalid tile marker.
Definition: tile_type.h:88
LinkGraph::LinkGraph
LinkGraph(CargoID cargo)
Real constructor.
Definition: linkgraph.h:465
LinkGraph::BaseEdgeIterator::operator++
Titer operator++(int)
Postfix-increment.
Definition: linkgraph.h:233
LinkGraph::BaseEdgeIterator::FakePointer::operator->
std::pair< NodeID, Tedge_wrapper > * operator->()
Retrieve the pair by operator->.
Definition: linkgraph.h:205
LinkGraph::EdgeWrapper::EdgeWrapper
EdgeWrapper(Tedge &edge)
Wrap a an edge.
Definition: linkgraph.h:86
LinkGraph::NodeWrapper::XY
TileIndex XY() const
Get the location of the station associated with the node.
Definition: linkgraph.h:170
LinkGraph::BaseNode
Node of the link graph.
Definition: linkgraph.h:47
LinkGraph::BaseEdgeIterator::FakePointer
A "fake" pointer to enable operator-> on temporaries.
Definition: linkgraph.h:192
SaveLoad
SaveLoad type struct.
Definition: saveload.h:517
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:226
LinkGraph::ConstNode::End
ConstEdgeIterator End() const
Get an iterator pointing beyond the end of the edges array.
Definition: linkgraph.h:367
LinkGraph::Node::operator[]
Edge operator[](NodeID to)
Get an Edge.
Definition: linkgraph.h:390
LinkGraph::NodeWrapper::index
NodeID index
ID of wrapped node.
Definition: linkgraph.h:129
LinkGraph::BaseEdgeIterator::FakePointer::FakePointer
FakePointer(const std::pair< NodeID, Tedge_wrapper > &pair)
Construct a fake pointer from a pair of NodeID and edge.
Definition: linkgraph.h:199
LinkGraph::ConstNode::Begin
ConstEdgeIterator Begin() const
Get an iterator pointing to the start of the edges array.
Definition: linkgraph.h:361
LinkGraph::BaseEdgeIterator::operator!=
bool operator!=(const Tother &other)
Compare for inequality with some other edge iterator.
Definition: linkgraph.h:261
LinkGraph::Monthly
uint Monthly(uint base) const
Scale a value to its monthly equivalent, based on last compression.
Definition: linkgraph.h:517
LinkGraph::BaseEdgeIterator::operator==
bool operator==(const Tother &other)
Compare with some other edge iterator.
Definition: linkgraph.h:248
LinkGraph::NodeWrapper::node
Tnode & node
Node being wrapped.
Definition: linkgraph.h:127
LinkGraph::BaseEdge::next_edge
NodeID next_edge
Destination of next valid edge starting at the same source node.
Definition: linkgraph.h:67
LinkGraph::NodeWrapper::Supply
uint Supply() const
Get supply of wrapped node.
Definition: linkgraph.h:146