OpenTTD Source  12.0-beta2
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 "../saveload/saveload.h"
20 #include "linkgraph_type.h"
21 #include <utility>
22 
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;
65  uint64 travel_time_sum;
68  NodeID next_edge;
69  void Init();
70  };
71 
76  template<typename Tedge>
77  class EdgeWrapper {
78  protected:
79  Tedge &edge;
80 
81  public:
82 
87  EdgeWrapper (Tedge &edge) : edge(edge) {}
88 
93  uint Capacity() const { return this->edge.capacity; }
94 
99  uint Usage() const { return this->edge.usage; }
100 
105  uint32 TravelTime() const { return this->edge.travel_time_sum / this->edge.capacity; }
106 
111  Date LastUnrestrictedUpdate() const { return this->edge.last_unrestricted_update; }
112 
117  Date LastRestrictedUpdate() const { return this->edge.last_restricted_update; }
118 
123  Date LastUpdate() const { return std::max(this->edge.last_unrestricted_update, this->edge.last_restricted_update); }
124  };
125 
131  template<typename Tnode, typename Tedge>
132  class NodeWrapper {
133  protected:
134  Tnode &node;
135  Tedge *edges;
136  NodeID index;
137 
138  public:
139 
146  NodeWrapper(Tnode &node, Tedge *edges, NodeID index) : node(node),
147  edges(edges), index(index) {}
148 
153  uint Supply() const { return this->node.supply; }
154 
159  uint Demand() const { return this->node.demand; }
160 
165  StationID Station() const { return this->node.station; }
166 
171  Date LastUpdate() const { return this->node.last_update; }
172 
177  TileIndex XY() const { return this->node.xy; }
178  };
179 
187  template <class Tedge, class Tedge_wrapper, class Titer>
189  protected:
190  Tedge *base;
191  NodeID current;
192 
199  class FakePointer : public std::pair<NodeID, Tedge_wrapper> {
200  public:
201 
206  FakePointer(const std::pair<NodeID, Tedge_wrapper> &pair) : std::pair<NodeID, Tedge_wrapper>(pair) {}
207 
212  std::pair<NodeID, Tedge_wrapper> *operator->() { return this; }
213  };
214 
215  public:
221  BaseEdgeIterator (Tedge *base, NodeID current) :
222  base(base),
223  current(current == INVALID_NODE ? current : base[current].next_edge)
224  {}
225 
230  Titer &operator++()
231  {
232  this->current = this->base[this->current].next_edge;
233  return static_cast<Titer &>(*this);
234  }
235 
240  Titer operator++(int)
241  {
242  Titer ret(static_cast<Titer &>(*this));
243  this->current = this->base[this->current].next_edge;
244  return ret;
245  }
246 
254  template<class Tother>
255  bool operator==(const Tother &other)
256  {
257  return this->base == other.base && this->current == other.current;
258  }
259 
267  template<class Tother>
268  bool operator!=(const Tother &other)
269  {
270  return this->base != other.base || this->current != other.current;
271  }
272 
277  std::pair<NodeID, Tedge_wrapper> operator*() const
278  {
279  return std::pair<NodeID, Tedge_wrapper>(this->current, Tedge_wrapper(this->base[this->current]));
280  }
281 
286  FakePointer operator->() const {
287  return FakePointer(this->operator*());
288  }
289  };
290 
295 
299  class Edge : public EdgeWrapper<BaseEdge> {
300  public:
306  void Update(uint capacity, uint usage, uint32 time, EdgeUpdateMode mode);
307  void Restrict() { this->edge.last_unrestricted_update = INVALID_DATE; }
308  void Release() { this->edge.last_restricted_update = INVALID_DATE; }
309  };
310 
315  class ConstEdgeIterator : public BaseEdgeIterator<const BaseEdge, ConstEdge, ConstEdgeIterator> {
316  public:
324  };
325 
330  class EdgeIterator : public BaseEdgeIterator<BaseEdge, Edge, EdgeIterator> {
331  public:
339  };
340 
345  class ConstNode : public NodeWrapper<const BaseNode, const BaseEdge> {
346  public:
352  ConstNode(const LinkGraph *lg, NodeID node) :
353  NodeWrapper<const BaseNode, const BaseEdge>(lg->nodes[node], lg->edges[node], node)
354  {}
355 
362  ConstEdge operator[](NodeID to) const { return ConstEdge(this->edges[to]); }
363 
368  ConstEdgeIterator Begin() const { return ConstEdgeIterator(this->edges, this->index); }
369 
374  ConstEdgeIterator End() const { return ConstEdgeIterator(this->edges, INVALID_NODE); }
375  };
376 
380  class Node : public NodeWrapper<BaseNode, BaseEdge> {
381  public:
387  Node(LinkGraph *lg, NodeID node) :
389  {}
390 
397  Edge operator[](NodeID to) { return Edge(this->edges[to]); }
398 
403  EdgeIterator Begin() { return EdgeIterator(this->edges, this->index); }
404 
409  EdgeIterator End() { return EdgeIterator(this->edges, INVALID_NODE); }
410 
415  void UpdateSupply(uint supply)
416  {
417  this->node.supply += supply;
418  this->node.last_update = _date;
419  }
420 
426  {
427  this->node.xy = xy;
428  }
429 
434  void SetDemand(uint demand)
435  {
436  this->node.demand = demand;
437  }
438 
439  void AddEdge(NodeID to, uint capacity, uint usage, uint32 time, EdgeUpdateMode mode);
440  void UpdateEdge(NodeID to, uint capacity, uint usage, uint32 time, EdgeUpdateMode mode);
441  void RemoveEdge(NodeID to);
442  };
443 
444  typedef std::vector<BaseNode> NodeVector;
445  typedef SmallMatrix<BaseEdge> EdgeMatrix;
446 
448  static const uint MIN_TIMEOUT_DISTANCE = 32;
449 
451  static const uint COMPRESSION_INTERVAL = 256;
452 
461  inline static uint Scale(uint val, uint target_age, uint orig_age)
462  {
463  return val > 0 ? std::max(1U, val * target_age / orig_age) : 0;
464  }
465 
473 
474  void Init(uint size);
475  void ShiftDates(int interval);
476  void Compress();
477  void Merge(LinkGraph *other);
478 
479  /* Splitting link graphs is intentionally not implemented.
480  * The overhead in determining connectedness would probably outweigh the
481  * benefit of having to deal with smaller graphs. In real world examples
482  * networks generally grow. Only rarely a network is permanently split.
483  * Reacting to temporary splits here would obviously create performance
484  * problems and detecting the temporary or permanent nature of splits isn't
485  * trivial. */
486 
492  inline Node operator[](NodeID num) { return Node(this, num); }
493 
499  inline ConstNode operator[](NodeID num) const { return ConstNode(this, num); }
500 
505  inline NodeID Size() const { return (NodeID)this->nodes.size(); }
506 
511  inline Date LastCompression() const { return this->last_compression; }
512 
517  inline CargoID Cargo() const { return this->cargo; }
518 
524  inline uint Monthly(uint base) const
525  {
526  return base * 30 / (_date - this->last_compression + 1);
527  }
528 
529  NodeID AddNode(const Station *st);
530  void RemoveNode(NodeID id);
531 
532 protected:
533  friend class LinkGraph::ConstNode;
534  friend class LinkGraph::Node;
537  friend class SlLinkgraphNode;
538  friend class SlLinkgraphEdge;
539 
542  NodeVector nodes;
544 };
545 
546 #endif /* LINKGRAPH_H */
LinkGraph::NodeWrapper::edges
Tedge * edges
Outgoing edges for wrapped node.
Definition: linkgraph.h:135
INVALID_CARGO
static const byte INVALID_CARGO
Constant representing invalid cargo.
Definition: cargotype.h:54
SlLinkgraphNode
Definition: linkgraph_sl.cpp:83
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:99
LinkGraph::COMPRESSION_INTERVAL
static const uint COMPRESSION_INTERVAL
Minimum number of days between subsequent compressions of a LG.
Definition: linkgraph.h:451
LinkGraph::Edge::Edge
Edge(BaseEdge &edge)
Constructor.
Definition: linkgraph.h:305
LinkGraph::edges
EdgeMatrix edges
Edges in the component.
Definition: linkgraph.h:543
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
Updatable node class.
Definition: linkgraph.h:380
LinkGraph::BaseEdge::usage
uint usage
Usage of the link.
Definition: linkgraph.h:64
LinkGraph::nodes
NodeVector nodes
Nodes in the component.
Definition: linkgraph.h:542
LinkGraph::EdgeWrapper::LastRestrictedUpdate
Date LastRestrictedUpdate() const
Get the date of the last update to the edge's restricted capacity.
Definition: linkgraph.h:117
LinkGraph::BaseNode::demand
uint demand
Acceptance at the station.
Definition: linkgraph.h:49
Station
Station data structure.
Definition: station_base.h:447
LinkGraph::EdgeWrapper::LastUpdate
Date LastUpdate() const
Get the date of the last update to any part of the edge's capacity.
Definition: linkgraph.h:123
LinkGraph::EdgeWrapper::TravelTime
uint32 TravelTime() const
Get edge's average travel time.
Definition: linkgraph.h:105
LinkGraph::Node::UpdateSupply
void UpdateSupply(uint supply)
Update the node's supply and set last_update to the current date.
Definition: linkgraph.h:415
LinkGraph::Node::Begin
EdgeIterator Begin()
Get an iterator pointing to the start of the edges array.
Definition: linkgraph.h:403
LinkGraph::EdgeIterator
An iterator for non-const edges.
Definition: linkgraph.h:330
LinkGraph::ConstNode
Constant node class.
Definition: linkgraph.h:345
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:461
LinkGraph::Node::End
EdgeIterator End()
Get an iterator pointing beyond the end of the edges array.
Definition: linkgraph.h:409
LinkGraph::BaseNode::station
StationID station
Station ID.
Definition: linkgraph.h:50
LinkGraph::Size
NodeID Size() const
Get the current size of the component.
Definition: linkgraph.h:505
LinkGraph::LinkGraph
LinkGraph()
Bare constructor, only for save/load.
Definition: linkgraph.h:467
LinkGraph::RemoveNode
void RemoveNode(NodeID id)
Remove a node from the link graph by overwriting it with the last node.
Definition: linkgraph.cpp:125
LinkGraph::Node::UpdateEdge
void UpdateEdge(NodeID to, uint capacity, uint usage, uint32 time, EdgeUpdateMode mode)
Creates an edge if none exists yet or updates an existing edge.
Definition: linkgraph.cpp:218
LinkGraph::Init
void Init(uint size)
Resize the component and fill it with empty nodes and edges.
Definition: linkgraph.cpp:302
EdgeUpdateMode
EdgeUpdateMode
Special modes for updating links.
Definition: linkgraph_type.h:52
LinkGraph::Edge
An updatable edge class.
Definition: linkgraph.h:299
LinkGraph::GetLinkGraphDesc
friend SaveLoadTable GetLinkGraphDesc()
Get a SaveLoad array for a link graph.
Definition: linkgraph_sl.cpp:123
LinkGraph::EdgeWrapper
Wrapper for an edge (const or not) allowing retrieval, but no modification.
Definition: linkgraph.h:77
LinkGraph::ConstNode::ConstNode
ConstNode(const LinkGraph *lg, NodeID node)
Constructor.
Definition: linkgraph.h:352
LinkGraph::BaseNode::last_update
Date last_update
When the supply was last updated.
Definition: linkgraph.h:52
LinkGraph::BaseEdgeIterator::BaseEdgeIterator
BaseEdgeIterator(Tedge *base, NodeID current)
Constructor.
Definition: linkgraph.h:221
LinkGraph::BaseEdgeIterator::operator++
Titer & operator++()
Prefix-increment.
Definition: linkgraph.h:230
LinkGraph::NodeWrapper::LastUpdate
Date LastUpdate() const
Get node's last update.
Definition: linkgraph.h:171
LinkGraph::EdgeWrapper::LastUnrestrictedUpdate
Date LastUnrestrictedUpdate() const
Get the date of the last update to the edge's unrestricted capacity.
Definition: linkgraph.h:111
SlLinkgraphEdge
Definition: linkgraph_sl.cpp:31
_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:294
LinkGraph::operator[]
ConstNode operator[](NodeID num) const
Get a const reference to a node with the specified id.
Definition: linkgraph.h:499
span
A trimmed down version of what std::span will be in C++20.
Definition: span_type.hpp:60
LinkGraph::EdgeWrapper::edge
Tedge & edge
Actual edge to be used.
Definition: linkgraph.h:79
LinkGraph::BaseEdgeIterator::operator*
std::pair< NodeID, Tedge_wrapper > operator*() const
Dereference with operator*.
Definition: linkgraph.h:277
SmallMatrix< BaseEdge >
LinkGraph::NodeWrapper::Demand
uint Demand() const
Get demand of wrapped node.
Definition: linkgraph.h:159
_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::NodeWrapper
Wrapper for a node (const or not) allowing retrieval, but no modification.
Definition: linkgraph.h:132
LinkGraph::Node::UpdateLocation
void UpdateLocation(TileIndex xy)
Update the node's location on the map.
Definition: linkgraph.h:425
LinkGraph::Cargo
CargoID Cargo() const
Get the cargo ID this component's link graph refers to.
Definition: linkgraph.h:517
LinkGraph::operator[]
Node operator[](NodeID num)
Get a node with the specified id.
Definition: linkgraph.h:492
LinkGraph::EdgeWrapper::Capacity
uint Capacity() const
Get edge's capacity.
Definition: linkgraph.h:93
LinkGraph::BaseEdgeIterator
Base class for iterating across outgoing edges of a node.
Definition: linkgraph.h:188
LinkGraph::ConstNode::operator[]
ConstEdge operator[](NodeID to) const
Get a ConstEdge.
Definition: linkgraph.h:362
LinkGraph::EdgeIterator::EdgeIterator
EdgeIterator(BaseEdge *edges, NodeID current)
Constructor.
Definition: linkgraph.h:337
LinkGraph::NodeWrapper::Station
StationID Station() const
Get ID of station belonging to wrapped node.
Definition: linkgraph.h:165
LinkGraph::Edge::Update
void Update(uint capacity, uint usage, uint32 time, EdgeUpdateMode mode)
Update an edge.
Definition: linkgraph.cpp:268
LinkGraph::LastCompression
Date LastCompression() const
Get date of last compression.
Definition: linkgraph.h:511
LinkGraph::Node::AddEdge
void AddEdge(NodeID to, uint capacity, uint usage, uint32 time, EdgeUpdateMode mode)
Fill an edge with values from a link.
Definition: linkgraph.cpp:197
LinkGraph::ShiftDates
void ShiftDates(int interval)
Shift all dates by given interval.
Definition: linkgraph.cpp:53
LinkGraph::BaseEdge::last_unrestricted_update
Date last_unrestricted_update
When the unrestricted part of the link was last updated.
Definition: linkgraph.h:66
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:191
LinkGraph::AddNode
NodeID AddNode(const Station *st)
Add a node to the component and create empty edges associated with it.
Definition: linkgraph.cpp:164
LinkGraph::BaseEdgeIterator::operator->
FakePointer operator->() const
Dereference with operator->.
Definition: linkgraph.h:286
LinkGraph::Node::RemoveEdge
void RemoveEdge(NodeID to)
Remove an outgoing edge from this node.
Definition: linkgraph.cpp:233
LinkGraph::NodeWrapper::NodeWrapper
NodeWrapper(Tnode &node, Tedge *edges, NodeID index)
Wrap a node.
Definition: linkgraph.h:146
INVALID_DATE
static const Date INVALID_DATE
Representation of an invalid date.
Definition: date_type.h:111
LinkGraph::cargo
CargoID cargo
Cargo of this component's link graph.
Definition: linkgraph.h:540
LinkGraph::MIN_TIMEOUT_DISTANCE
static const uint MIN_TIMEOUT_DISTANCE
Minimum effective distance for timeout calculation.
Definition: linkgraph.h:448
LinkGraph::Node::SetDemand
void SetDemand(uint demand)
Set the node's demand.
Definition: linkgraph.h:434
LinkGraph::last_compression
Date last_compression
Last time the capacities and supplies were compressed.
Definition: linkgraph.h:541
LinkGraph::ConstEdgeIterator
An iterator for const edges.
Definition: linkgraph.h:315
LinkGraph::BaseEdgeIterator::base
Tedge * base
Array of edges being iterated.
Definition: linkgraph.h:190
LinkGraph::BaseEdge::travel_time_sum
uint64 travel_time_sum
Sum of the travel times of the link, in ticks.
Definition: linkgraph.h:65
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:89
LinkGraph::ConstEdgeIterator::ConstEdgeIterator
ConstEdgeIterator(const BaseEdge *edges, NodeID current)
Constructor.
Definition: linkgraph.h:322
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:67
LinkGraph::GetLinkGraphJobDesc
friend SaveLoadTable GetLinkGraphJobDesc()
Get a SaveLoad array for a link graph job.
Definition: linkgraph_sl.cpp:167
LinkGraph::Node::Node
Node(LinkGraph *lg, NodeID node)
Constructor.
Definition: linkgraph.h:387
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:472
LinkGraph::BaseEdgeIterator::operator++
Titer operator++(int)
Postfix-increment.
Definition: linkgraph.h:240
LinkGraph::BaseEdgeIterator::FakePointer::operator->
std::pair< NodeID, Tedge_wrapper > * operator->()
Retrieve the pair by operator->.
Definition: linkgraph.h:212
LinkGraph::EdgeWrapper::EdgeWrapper
EdgeWrapper(Tedge &edge)
Wrap a an edge.
Definition: linkgraph.h:87
LinkGraph::NodeWrapper::XY
TileIndex XY() const
Get the location of the station associated with the node.
Definition: linkgraph.h:177
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:199
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:234
LinkGraph::ConstNode::End
ConstEdgeIterator End() const
Get an iterator pointing beyond the end of the edges array.
Definition: linkgraph.h:374
LinkGraph::Node::operator[]
Edge operator[](NodeID to)
Get an Edge.
Definition: linkgraph.h:397
LinkGraph::NodeWrapper::index
NodeID index
ID of wrapped node.
Definition: linkgraph.h:136
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:206
LinkGraph::ConstNode::Begin
ConstEdgeIterator Begin() const
Get an iterator pointing to the start of the edges array.
Definition: linkgraph.h:368
LinkGraph::BaseEdgeIterator::operator!=
bool operator!=(const Tother &other)
Compare for inequality with some other edge iterator.
Definition: linkgraph.h:268
LinkGraph::Monthly
uint Monthly(uint base) const
Scale a value to its monthly equivalent, based on last compression.
Definition: linkgraph.h:524
LinkGraph::BaseEdgeIterator::operator==
bool operator==(const Tother &other)
Compare with some other edge iterator.
Definition: linkgraph.h:255
LinkGraph::NodeWrapper::node
Tnode & node
Node being wrapped.
Definition: linkgraph.h:134
LinkGraph::BaseEdge::next_edge
NodeID next_edge
Destination of next valid edge starting at the same source node.
Definition: linkgraph.h:68
LinkGraph::NodeWrapper::Supply
uint Supply() const
Get supply of wrapped node.
Definition: linkgraph.h:153