OpenTTD
linkgraphjob.h
Go to the documentation of this file.
1 /* $Id: linkgraphjob.h 27178 2015-03-07 18:27:01Z frosch $ */
2 
3 /*
4  * This file is part of OpenTTD.
5  * 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.
6  * 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.
7  * 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/>.
8  */
9 
12 #ifndef LINKGRAPHJOB_H
13 #define LINKGRAPHJOB_H
14 
15 #include "../thread/thread.h"
16 #include "linkgraph.h"
17 #include <list>
18 
19 class LinkGraphJob;
20 class Path;
21 typedef std::list<Path *> PathList;
22 
27 
31 class LinkGraphJob : public LinkGraphJobPool::PoolItem<&_link_graph_job_pool>{
32 private:
36  struct EdgeAnnotation {
37  uint demand;
39  uint flow;
40  void Init();
41  };
42 
46  struct NodeAnnotation {
48  PathList paths;
50  void Init(uint supply);
51  };
52 
55 
56  friend const SaveLoad *GetLinkGraphJobDesc();
57  friend class LinkGraphSchedule;
58 
59 protected:
64  NodeAnnotationVector nodes;
65  EdgeAnnotationMatrix edges;
66 
67  void EraseFlows(NodeID from);
68  void JoinThread();
69  void SpawnThread();
70 
71 public:
72 
77  class Edge : public LinkGraph::ConstEdge {
78  private:
80  public:
86  Edge(const LinkGraph::BaseEdge &edge, EdgeAnnotation &anno) :
87  LinkGraph::ConstEdge(edge), anno(anno) {}
88 
93  uint Demand() const { return this->anno.demand; }
94 
99  uint UnsatisfiedDemand() const { return this->anno.unsatisfied_demand; }
100 
105  uint Flow() const { return this->anno.flow; }
106 
111  void AddFlow(uint flow) { this->anno.flow += flow; }
112 
117  void RemoveFlow(uint flow)
118  {
119  assert(flow <= this->anno.flow);
120  this->anno.flow -= flow;
121  }
122 
127  void AddDemand(uint demand)
128  {
129  this->anno.demand += demand;
130  this->anno.unsatisfied_demand += demand;
131  }
132 
138  {
139  assert(demand <= this->anno.unsatisfied_demand);
140  this->anno.unsatisfied_demand -= demand;
141  }
142  };
143 
147  class EdgeIterator : public LinkGraph::BaseEdgeIterator<const LinkGraph::BaseEdge, Edge, EdgeIterator> {
149  public:
156  EdgeIterator(const LinkGraph::BaseEdge *base, EdgeAnnotation *base_anno, NodeID current) :
157  LinkGraph::BaseEdgeIterator<const LinkGraph::BaseEdge, Edge, EdgeIterator>(base, current),
158  base_anno(base_anno) {}
159 
166  {
167  return SmallPair<NodeID, Edge>(this->current, Edge(this->base[this->current], this->base_anno[this->current]));
168  }
169 
175  FakePointer operator->() const {
176  return FakePointer(this->operator*());
177  }
178  };
179 
184  class Node : public LinkGraph::ConstNode {
185  private:
188  public:
189 
195  Node (LinkGraphJob *lgj, NodeID node) :
196  LinkGraph::ConstNode(&lgj->link_graph, node),
197  node_anno(lgj->nodes[node]), edge_annos(lgj->edges[node])
198  {}
199 
206  Edge operator[](NodeID to) const { return Edge(this->edges[to], this->edge_annos[to]); }
207 
213  EdgeIterator Begin() const { return EdgeIterator(this->edges, this->edge_annos, index); }
214 
220  EdgeIterator End() const { return EdgeIterator(this->edges, this->edge_annos, INVALID_NODE); }
221 
226  uint UndeliveredSupply() const { return this->node_anno.undelivered_supply; }
227 
232  FlowStatMap &Flows() { return this->node_anno.flows; }
233 
238  const FlowStatMap &Flows() const { return this->node_anno.flows; }
239 
245  PathList &Paths() { return this->node_anno.paths; }
246 
251  const PathList &Paths() const { return this->node_anno.paths; }
252 
258  void DeliverSupply(NodeID to, uint amount)
259  {
260  this->node_anno.undelivered_supply -= amount;
261  (*this)[to].AddDemand(amount);
262  }
263  };
264 
269  LinkGraphJob() : settings(_settings_game.linkgraph), thread(NULL),
270  join_date(INVALID_DATE) {}
271 
272  LinkGraphJob(const LinkGraph &orig);
273  ~LinkGraphJob();
274 
275  void Init();
276 
281  inline bool IsFinished() const { return this->join_date <= _date; }
282 
287  inline Date JoinDate() const { return join_date; }
288 
293  inline void ShiftJoinDate(int interval) { this->join_date += interval; }
294 
299  inline const LinkGraphSettings &Settings() const { return this->settings; }
300 
306  inline Node operator[](NodeID num) { return Node(this, num); }
307 
312  inline uint Size() const { return this->link_graph.Size(); }
313 
318  inline CargoID Cargo() const { return this->link_graph.Cargo(); }
319 
324  inline Date LastCompression() const { return this->link_graph.LastCompression(); }
325 
330  inline LinkGraphID LinkGraphIndex() const { return this->link_graph.index; }
331 
336  inline const LinkGraph &Graph() const { return this->link_graph; }
337 };
338 
339 #define FOR_ALL_LINK_GRAPH_JOBS(var) FOR_ALL_ITEMS_FROM(LinkGraphJob, link_graph_job_index, var, 0)
340 
344 class Path {
345 public:
347 
348  Path(NodeID n, bool source = false);
349 
351  inline NodeID GetNode() const { return this->node; }
352 
354  inline NodeID GetOrigin() const { return this->origin; }
355 
357  inline Path *GetParent() { return this->parent; }
358 
360  inline uint GetCapacity() const { return this->capacity; }
361 
363  inline int GetFreeCapacity() const { return this->free_capacity; }
364 
372  inline static int GetCapacityRatio(int free, uint total)
373  {
374  return Clamp(free, PATH_CAP_MIN_FREE, PATH_CAP_MAX_FREE) * PATH_CAP_MULTIPLIER / max(total, 1U);
375  }
376 
381  inline int GetCapacityRatio() const
382  {
383  return Path::GetCapacityRatio(this->free_capacity, this->capacity);
384  }
385 
387  inline uint GetDistance() const { return this->distance; }
388 
390  inline void ReduceFlow(uint f) { this->flow -= f; }
391 
393  inline void AddFlow(uint f) { this->flow += f; }
394 
396  inline uint GetFlow() const { return this->flow; }
397 
399  inline uint GetNumChildren() const { return this->num_children; }
400 
404  inline void Detach()
405  {
406  if (this->parent != NULL) {
407  this->parent->num_children--;
408  this->parent = NULL;
409  }
410  }
411 
412  uint AddFlow(uint f, LinkGraphJob &job, uint max_saturation);
413  void Fork(Path *base, uint cap, int free_cap, uint dist);
414 
415 protected:
416 
421  PATH_CAP_MULTIPLIER = 16,
422  PATH_CAP_MIN_FREE = (INT_MIN + 1) / PATH_CAP_MULTIPLIER,
423  PATH_CAP_MAX_FREE = (INT_MAX - 1) / PATH_CAP_MULTIPLIER
424  };
425 
426  uint distance;
427  uint capacity;
429  uint flow;
430  NodeID node;
431  NodeID origin;
434 };
435 
436 #endif /* LINKGRAPHJOB_H */
Constant node class.
Definition: linkgraph.h:339
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
void Init()
Initialize a linkgraph job edge.
LinkGraphJobPool _link_graph_job_pool
The actual pool with link graph jobs.
int free_capacity
This capacity is min(edge.capacity - edge.flow) for the current run of Dijkstra.
Definition: linkgraphjob.h:428
A job edge.
Definition: linkgraphjob.h:77
An edge in the link graph.
Definition: linkgraph.h:63
Annotation for a link graph edge.
Definition: linkgraphjob.h:36
FakePointer operator->() const
Dereference.
Definition: linkgraphjob.h:175
uint undelivered_supply
Amount of supply that hasn&#39;t been distributed yet.
Definition: linkgraphjob.h:47
uint Flow() const
Get the total flow on the edge.
Definition: linkgraphjob.h:105
CargoID Cargo() const
Get the cargo ID this component&#39;s link graph refers to.
Definition: linkgraph.h:511
Node operator[](NodeID num)
Get a node abstraction with the specified id.
Definition: linkgraphjob.h:306
Pool< LinkGraphJob, LinkGraphJobID, 32, 0xFFFF > LinkGraphJobPool
Type of the pool for link graph jobs.
Definition: linkgraphjob.h:24
EdgeAnnotation * edge_annos
Edge annotations belonging to this node.
Definition: linkgraphjob.h:187
uint GetCapacity() const
Get the overall capacity of the path.
Definition: linkgraphjob.h:360
uint demand
Transport demand between the nodes.
Definition: linkgraphjob.h:37
FlowStatMap flows
Planned flows to other nodes.
Definition: linkgraphjob.h:49
uint flow
Planned flow over this edge.
Definition: linkgraphjob.h:39
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
const PathList & Paths() const
Get a constant version of the paths this node is part of.
Definition: linkgraphjob.h:251
void AddFlow(uint f)
Increase the flow on this leg only by the specified amount.
Definition: linkgraphjob.h:393
void ShiftJoinDate(int interval)
Change the join date on date cheating.
Definition: linkgraphjob.h:293
Date LastCompression() const
Get the date when the underlying link graph was last compressed.
Definition: linkgraphjob.h:324
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
NodeID GetOrigin() const
Get the overall origin of the path.
Definition: linkgraphjob.h:354
A connected component of a link graph.
Definition: linkgraph.h:40
Edge(const LinkGraph::BaseEdge &edge, EdgeAnnotation &anno)
Constructor.
Definition: linkgraphjob.h:86
const LinkGraphSettings & Settings() const
Get the link graph settings for this component.
Definition: linkgraphjob.h:299
uint GetNumChildren() const
Get the number of "forked off" child legs of this one.
Definition: linkgraphjob.h:399
uint Size() const
Get the current size of the component.
Definition: linkgraph.h:499
uint unsatisfied_demand
Demand over this edge that hasn&#39;t been satisfied yet.
Definition: linkgraphjob.h:38
Simple pair of data.
uint GetFlow() const
Get the flow on this leg.
Definition: linkgraphjob.h:396
const LinkGraphSettings settings
Copy of _settings_game.linkgraph at spawn time.
Definition: linkgraphjob.h:61
uint distance
Sum(distance of all legs up to this one).
Definition: linkgraphjob.h:426
NodeID GetNode() const
Get the node this leg passes.
Definition: linkgraphjob.h:351
static const Date INVALID_DATE
Representation of an invalid date.
Definition: date_type.h:110
A leg of a path in the link graph.
Definition: linkgraphjob.h:344
uint num_children
Number of child legs that have been forked from this path.
Definition: linkgraphjob.h:432
void SatisfyDemand(uint demand)
Satisfy some demand.
Definition: linkgraphjob.h:137
int GetCapacityRatio() const
Get capacity ratio of this path.
Definition: linkgraphjob.h:381
ThreadObject * thread
Thread the job is running in or NULL if it&#39;s running in the main thread.
Definition: linkgraphjob.h:62
Link graph job node.
Definition: linkgraphjob.h:184
PathList & Paths()
Get the paths this node is part of.
Definition: linkgraphjob.h:245
Node(LinkGraphJob *lgj, NodeID node)
Constructor.
Definition: linkgraphjob.h:195
static Path * invalid_path
Static instance of an invalid path.
Definition: linkgraphjob.h:346
void DeliverSupply(NodeID to, uint amount)
Deliver some supply, adding demand to the respective edge.
Definition: linkgraphjob.h:258
void ReduceFlow(uint f)
Reduce the flow on this leg only by the specified amount.
Definition: linkgraphjob.h:390
uint capacity
This capacity is min(capacity) fom all edges.
Definition: linkgraphjob.h:427
Date JoinDate() const
Get the date when the job should be finished.
Definition: linkgraphjob.h:287
void SpawnThread()
Spawn a thread if possible and run the link graph job in the thread.
Wrapper for an edge (const or not) allowing retrieval, but no modification.
Definition: linkgraph.h:77
Date LastCompression() const
Get date of last compression.
Definition: linkgraph.h:505
NodeID origin
Link graph node this path originates from.
Definition: linkgraphjob.h:431
Edge operator[](NodeID to) const
Retrieve an edge starting at this node.
Definition: linkgraphjob.h:206
static int GetCapacityRatio(int free, uint total)
Get ratio of free * 16 (so that we get fewer 0) / max(total capacity, 1) (so that we don&#39;t divide by ...
Definition: linkgraphjob.h:372
void AddFlow(uint flow)
Add some flow.
Definition: linkgraphjob.h:111
Base class for all PoolItems.
Definition: pool_type.hpp:146
uint UnsatisfiedDemand() const
Get the transport demand that hasn&#39;t been satisfied by flows, yet.
Definition: linkgraphjob.h:99
Base class for iterating across outgoing edges of a node.
Definition: linkgraph.h:182
int GetFreeCapacity() const
Get the free capacity of the path.
Definition: linkgraphjob.h:363
Base class for all pools.
Definition: pool_type.hpp:83
CargoID Cargo() const
Get the cargo of the underlying link graph.
Definition: linkgraphjob.h:318
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
void RemoveFlow(uint flow)
Remove some flow.
Definition: linkgraphjob.h:117
NodeID node
Link graph node this leg passes.
Definition: linkgraphjob.h:430
const LinkGraph link_graph
Link graph to by analyzed. Is copied when job is started and mustn&#39;t be modified later.
Definition: linkgraphjob.h:60
FlowStatMap & Flows()
Get the flows running through this node.
Definition: linkgraphjob.h:232
uint GetDistance() const
Get the overall distance of the path.
Definition: linkgraphjob.h:387
bool IsFinished() const
Check if job is supposed to be finished.
Definition: linkgraphjob.h:281
EdgeIterator(const LinkGraph::BaseEdge *base, EdgeAnnotation *base_anno, NodeID current)
Constructor.
Definition: linkgraphjob.h:156
PathList paths
Paths through this node, sorted so that those with flow == 0 are in the back.
Definition: linkgraphjob.h:48
void EraseFlows(NodeID from)
Erase all flows originating at a specific node.
void JoinThread()
Join the calling thread with this job&#39;s thread if threading is enabled.
NodeAnnotation & node_anno
Annotation being wrapped.
Definition: linkgraphjob.h:186
void Detach()
Detach this path from its parent.
Definition: linkgraphjob.h:404
uint flow
Flow the current run of the mcf solver assigns.
Definition: linkgraphjob.h:429
PathCapacityBoundaries
Some boundaries to clamp agains in order to avoid integer overflows.
Definition: linkgraphjob.h:420
EdgeIterator End() const
Iterator for the "end" of the edge array.
Definition: linkgraphjob.h:220
Date join_date
Date when the job is to be joined.
Definition: linkgraphjob.h:63
Iterator for job edges.
Definition: linkgraphjob.h:147
const FlowStatMap & Flows() const
Get a constant version of the flows running through this node.
Definition: linkgraphjob.h:238
uint Size() const
Get the size of the underlying link graph.
Definition: linkgraphjob.h:312
const LinkGraph & Graph() const
Get a reference to the underlying link graph.
Definition: linkgraphjob.h:336
LinkGraphJob()
Bare constructor, only for save/load.
Definition: linkgraphjob.h:269
friend const SaveLoad * GetLinkGraphJobDesc()
Get a SaveLoad array for a link graph job.
LinkGraphID LinkGraphIndex() const
Get the ID of the underlying link graph.
Definition: linkgraphjob.h:330
Flow descriptions by origin stations.
Definition: station_base.h:152
Declaration of link graph classes used for cargo distribution.
int32 Date
The type to store our dates in.
Definition: date_type.h:16
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
EdgeAnnotationMatrix edges
Extra edge data necessary for link graph calculation.
Definition: linkgraphjob.h:65
EdgeAnnotation & anno
Annotation being wrapped.
Definition: linkgraphjob.h:79
SaveLoad type struct.
Definition: saveload.h:208
uint UndeliveredSupply() const
Get amount of supply that hasn&#39;t been delivered, yet.
Definition: linkgraphjob.h:226
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
Path * GetParent()
Get the parent leg of this one.
Definition: linkgraphjob.h:357
SmallPair< NodeID, Edge > operator*() const
Dereference.
Definition: linkgraphjob.h:165
Date _date
Current date in days (day counter)
Definition: date.cpp:28
EdgeAnnotation * base_anno
Array of annotations to be (indirectly) iterated.
Definition: linkgraphjob.h:148
void AddDemand(uint demand)
Add some (not yet satisfied) demand.
Definition: linkgraphjob.h:127
Path * parent
Parent leg of this one.
Definition: linkgraphjob.h:433
NodeAnnotationVector nodes
Extra node data necessary for link graph calculation.
Definition: linkgraphjob.h:64
A Thread Object which works on all our supported OSes.
Definition: thread.h:24
EdgeIterator Begin() const
Iterator for the "begin" of the edge array.
Definition: linkgraphjob.h:213
~LinkGraphJob()
Join the link graph job and destroy it.
uint Demand() const
Get the transport demand between end the points of the edge.
Definition: linkgraphjob.h:93
Class for calculation jobs to be run on link graphs.
Definition: linkgraphjob.h:31
Annotation for a link graph node.
Definition: linkgraphjob.h:46