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
24 typedef Pool<LinkGraphJob, LinkGraphJobID, 32, 0xFFFF> LinkGraphJobPool;
26 extern LinkGraphJobPool _link_graph_job_pool;
27
31 class LinkGraphJob : public LinkGraphJobPool::PoolItem<&_link_graph_job_pool>{
32 private:
36 struct EdgeAnnotation {
37 uint demand;
38 uint unsatisfied_demand;
39 uint flow;
40 void Init();
41 };
42
46 struct NodeAnnotation {
47 uint undelivered_supply;
48 PathList paths;
49 FlowStatMap flows;
50 void Init(uint supply);
51 };
52
53 typedef SmallVector<NodeAnnotation, 16> NodeAnnotationVector;
54 typedef SmallMatrix<EdgeAnnotation> EdgeAnnotationMatrix;
55
56 friend const SaveLoad *GetLinkGraphJobDesc();
57 friend class LinkGraphSchedule;
58
59 protected:
60 const LinkGraph link_graph;
61 const LinkGraphSettings settings;
62 ThreadObject *thread;
63 Date join_date;
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:
79 EdgeAnnotation &anno;
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
137 void SatisfyDemand(uint demand)
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> {
148 EdgeAnnotation *base_anno;
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
165 SmallPair<NodeID, Edge> operator*() const
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:
186 NodeAnnotation &node_anno;
187 EdgeAnnotation *edge_annos;
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:
346 static Path *invalid_path;
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
420 enum PathCapacityBoundaries {
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;
428 int free_capacity;
429 uint flow;
430 NodeID node;
431 NodeID origin;
432 uint num_children;
433 Path *parent;
434 };
435
436 #endif /* LINKGRAPHJOB_H */
LinkGraph::ConstNode
Constant node class.
Definition: linkgraph.h:339
_settings_game
GameSettings _settings_game
Game settings of a running game or the scenario editor.
Definition: settings.cpp:77
LinkGraphJob::EdgeAnnotation::Init
void Init()
Initialize a linkgraph job edge.
Definition: linkgraphjob.cpp:196
_link_graph_job_pool
LinkGraphJobPool _link_graph_job_pool
The actual pool with link graph jobs.
Path::free_capacity
int free_capacity
This capacity is min(edge.capacity - edge.flow) for the current run of Dijkstra.
Definition: linkgraphjob.h:428
LinkGraphJob::Edge
A job edge.
Definition: linkgraphjob.h:77
LinkGraph::BaseEdge
An edge in the link graph.
Definition: linkgraph.h:63
LinkGraphJob::EdgeAnnotation
Annotation for a link graph edge.
Definition: linkgraphjob.h:36
LinkGraphJob::EdgeIterator::operator->
FakePointer operator->() const
Dereference.
Definition: linkgraphjob.h:175
LinkGraphJob::NodeAnnotation::undelivered_supply
uint undelivered_supply
Amount of supply that hasn&#39;t been distributed yet.
Definition: linkgraphjob.h:47
LinkGraphJob::Edge::Flow
uint Flow() const
Get the total flow on the edge.
Definition: linkgraphjob.h:105
LinkGraph::Cargo
CargoID Cargo() const
Get the cargo ID this component&#39;s link graph refers to.
Definition: linkgraph.h:511
LinkGraphJob::operator[]
Node operator[](NodeID num)
Get a node abstraction with the specified id.
Definition: linkgraphjob.h:306
LinkGraphJobPool
Pool< LinkGraphJob, LinkGraphJobID, 32, 0xFFFF > LinkGraphJobPool
Type of the pool for link graph jobs.
Definition: linkgraphjob.h:24
LinkGraphJob::Node::edge_annos
EdgeAnnotation * edge_annos
Edge annotations belonging to this node.
Definition: linkgraphjob.h:187
Path::GetCapacity
uint GetCapacity() const
Get the overall capacity of the path.
Definition: linkgraphjob.h:360
LinkGraphJob::EdgeAnnotation::demand
uint demand
Transport demand between the nodes.
Definition: linkgraphjob.h:37
LinkGraphJob::NodeAnnotation::flows
FlowStatMap flows
Planned flows to other nodes.
Definition: linkgraphjob.h:49
LinkGraphJob::EdgeAnnotation::flow
uint flow
Planned flow over this edge.
Definition: linkgraphjob.h:39
Pool::PoolItem<&_link_graph_job_pool >::index
Tindex index
Index of this pool item.
Definition: pool_type.hpp:147
LinkGraphJob::Node::Paths
const PathList & Paths() const
Get a constant version of the paths this node is part of.
Definition: linkgraphjob.h:251
LinkGraphSchedule
Definition: linkgraphschedule.h:38
Path::AddFlow
void AddFlow(uint f)
Increase the flow on this leg only by the specified amount.
Definition: linkgraphjob.h:393
SmallVector< NodeAnnotation, 16 >
LinkGraphJob::ShiftJoinDate
void ShiftJoinDate(int interval)
Change the join date on date cheating.
Definition: linkgraphjob.h:293
LinkGraphJob::LastCompression
Date LastCompression() const
Get the date when the underlying link graph was last compressed.
Definition: linkgraphjob.h:324
max
static T max(const T a, const T b)
Returns the maximum of two values.
Definition: math_func.hpp:26
Path::GetOrigin
NodeID GetOrigin() const
Get the overall origin of the path.
Definition: linkgraphjob.h:354
LinkGraph
A connected component of a link graph.
Definition: linkgraph.h:40
LinkGraphJob::Edge::Edge
Edge(const LinkGraph::BaseEdge &edge, EdgeAnnotation &anno)
Constructor.
Definition: linkgraphjob.h:86
LinkGraphJob::Settings
const LinkGraphSettings & Settings() const
Get the link graph settings for this component.
Definition: linkgraphjob.h:299
Path::GetNumChildren
uint GetNumChildren() const
Get the number of "forked off" child legs of this one.
Definition: linkgraphjob.h:399
LinkGraph::Size
uint Size() const
Get the current size of the component.
Definition: linkgraph.h:499
LinkGraphJob::EdgeAnnotation::unsatisfied_demand
uint unsatisfied_demand
Demand over this edge that hasn&#39;t been satisfied yet.
Definition: linkgraphjob.h:38
SmallPair
Simple pair of data.
Definition: smallmap_type.hpp:24
Path::GetFlow
uint GetFlow() const
Get the flow on this leg.
Definition: linkgraphjob.h:396
LinkGraphJob::settings
const LinkGraphSettings settings
Copy of _settings_game.linkgraph at spawn time.
Definition: linkgraphjob.h:61
Path::distance
uint distance
Sum(distance of all legs up to this one).
Definition: linkgraphjob.h:426
Path::GetNode
NodeID GetNode() const
Get the node this leg passes.
Definition: linkgraphjob.h:351
INVALID_DATE
static const Date INVALID_DATE
Representation of an invalid date.
Definition: date_type.h:110
Path
A leg of a path in the link graph.
Definition: linkgraphjob.h:344
Path::num_children
uint num_children
Number of child legs that have been forked from this path.
Definition: linkgraphjob.h:432
LinkGraphJob::Edge::SatisfyDemand
void SatisfyDemand(uint demand)
Satisfy some demand.
Definition: linkgraphjob.h:137
Path::GetCapacityRatio
int GetCapacityRatio() const
Get capacity ratio of this path.
Definition: linkgraphjob.h:381
LinkGraphJob::thread
ThreadObject * thread
Thread the job is running in or NULL if it&#39;s running in the main thread.
Definition: linkgraphjob.h:62
LinkGraphJob::Node
Link graph job node.
Definition: linkgraphjob.h:184
LinkGraphJob::Node::Paths
PathList & Paths()
Get the paths this node is part of.
Definition: linkgraphjob.h:245
LinkGraphJob::Node::Node
Node(LinkGraphJob *lgj, NodeID node)
Constructor.
Definition: linkgraphjob.h:195
Path::invalid_path
static Path * invalid_path
Static instance of an invalid path.
Definition: linkgraphjob.h:346
LinkGraphJob::Node::DeliverSupply
void DeliverSupply(NodeID to, uint amount)
Deliver some supply, adding demand to the respective edge.
Definition: linkgraphjob.h:258
Path::ReduceFlow
void ReduceFlow(uint f)
Reduce the flow on this leg only by the specified amount.
Definition: linkgraphjob.h:390
Path::capacity
uint capacity
This capacity is min(capacity) fom all edges.
Definition: linkgraphjob.h:427
LinkGraphJob::JoinDate
Date JoinDate() const
Get the date when the job should be finished.
Definition: linkgraphjob.h:287
LinkGraphJob::SpawnThread
void SpawnThread()
Spawn a thread if possible and run the link graph job in the thread.
Definition: linkgraphjob.cpp:62
LinkGraph::EdgeWrapper
Wrapper for an edge (const or not) allowing retrieval, but no modification.
Definition: linkgraph.h:77
LinkGraph::LastCompression
Date LastCompression() const
Get date of last compression.
Definition: linkgraph.h:505
Path::origin
NodeID origin
Link graph node this path originates from.
Definition: linkgraphjob.h:431
LinkGraphJob::Node::operator[]
Edge operator[](NodeID to) const
Retrieve an edge starting at this node.
Definition: linkgraphjob.h:206
Path::GetCapacityRatio
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
LinkGraphJob::Edge::AddFlow
void AddFlow(uint flow)
Add some flow.
Definition: linkgraphjob.h:111
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:146
LinkGraphJob::Edge::UnsatisfiedDemand
uint UnsatisfiedDemand() const
Get the transport demand that hasn&#39;t been satisfied by flows, yet.
Definition: linkgraphjob.h:99
LinkGraph::BaseEdgeIterator
Base class for iterating across outgoing edges of a node.
Definition: linkgraph.h:182
Path::GetFreeCapacity
int GetFreeCapacity() const
Get the free capacity of the path.
Definition: linkgraphjob.h:363
Pool
Base class for all pools.
Definition: pool_type.hpp:83
LinkGraphJob::Cargo
CargoID Cargo() const
Get the cargo of the underlying link graph.
Definition: linkgraphjob.h:318
Clamp
static T Clamp(const T a, const T min, const T max)
Clamp a value between an interval.
Definition: math_func.hpp:139
LinkGraphJob::Edge::RemoveFlow
void RemoveFlow(uint flow)
Remove some flow.
Definition: linkgraphjob.h:117
Path::node
NodeID node
Link graph node this leg passes.
Definition: linkgraphjob.h:430
LinkGraphJob::link_graph
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
LinkGraphJob::Node::Flows
FlowStatMap & Flows()
Get the flows running through this node.
Definition: linkgraphjob.h:232
Path::GetDistance
uint GetDistance() const
Get the overall distance of the path.
Definition: linkgraphjob.h:387
LinkGraphJob::IsFinished
bool IsFinished() const
Check if job is supposed to be finished.
Definition: linkgraphjob.h:281
LinkGraphJob::EdgeIterator::EdgeIterator
EdgeIterator(const LinkGraph::BaseEdge *base, EdgeAnnotation *base_anno, NodeID current)
Constructor.
Definition: linkgraphjob.h:156
LinkGraphJob::NodeAnnotation::paths
PathList paths
Paths through this node, sorted so that those with flow == 0 are in the back.
Definition: linkgraphjob.h:48
LinkGraphJob::EraseFlows
void EraseFlows(NodeID from)
Erase all flows originating at a specific node.
Definition: linkgraphjob.cpp:51
LinkGraphJob::JoinThread
void JoinThread()
Join the calling thread with this job&#39;s thread if threading is enabled.
Definition: linkgraphjob.cpp:80
LinkGraphJob::Node::node_anno
NodeAnnotation & node_anno
Annotation being wrapped.
Definition: linkgraphjob.h:186
Path::Detach
void Detach()
Detach this path from its parent.
Definition: linkgraphjob.h:404
Path::flow
uint flow
Flow the current run of the mcf solver assigns.
Definition: linkgraphjob.h:429
Path::PathCapacityBoundaries
PathCapacityBoundaries
Some boundaries to clamp agains in order to avoid integer overflows.
Definition: linkgraphjob.h:420
LinkGraphJob::Node::End
EdgeIterator End() const
Iterator for the "end" of the edge array.
Definition: linkgraphjob.h:220
LinkGraphJob::join_date
Date join_date
Date when the job is to be joined.
Definition: linkgraphjob.h:63
LinkGraphJob::EdgeIterator
Iterator for job edges.
Definition: linkgraphjob.h:147
LinkGraphJob::Node::Flows
const FlowStatMap & Flows() const
Get a constant version of the flows running through this node.
Definition: linkgraphjob.h:238
LinkGraphJob::Size
uint Size() const
Get the size of the underlying link graph.
Definition: linkgraphjob.h:312
LinkGraphSettings
Definition: settings_type.h:494
LinkGraphJob::Graph
const LinkGraph & Graph() const
Get a reference to the underlying link graph.
Definition: linkgraphjob.h:336
LinkGraphJob::LinkGraphJob
LinkGraphJob()
Bare constructor, only for save/load.
Definition: linkgraphjob.h:269
LinkGraphJob::GetLinkGraphJobDesc
friend const SaveLoad * GetLinkGraphJobDesc()
Get a SaveLoad array for a link graph job.
Definition: linkgraph_sl.cpp:52
LinkGraphJob::LinkGraphIndex
LinkGraphID LinkGraphIndex() const
Get the ID of the underlying link graph.
Definition: linkgraphjob.h:330
FlowStatMap
Flow descriptions by origin stations.
Definition: station_base.h:152
linkgraph.h
Declaration of link graph classes used for cargo distribution.
Date
int32 Date
The type to store our dates in.
Definition: date_type.h:16
free
static void free(const void *ptr)
Version of the standard free that accepts const pointers.
Definition: depend.cpp:114
LinkGraphJob::edges
EdgeAnnotationMatrix edges
Extra edge data necessary for link graph calculation.
Definition: linkgraphjob.h:65
LinkGraphJob::Edge::anno
EdgeAnnotation & anno
Annotation being wrapped.
Definition: linkgraphjob.h:79
SaveLoad
SaveLoad type struct.
Definition: saveload.h:208
LinkGraphJob::Node::UndeliveredSupply
uint UndeliveredSupply() const
Get amount of supply that hasn&#39;t been delivered, yet.
Definition: linkgraphjob.h:226
SmallMatrix< EdgeAnnotation >
CargoID
byte CargoID
Cargo slots to indicate a cargo type within a game.
Definition: cargo_type.h:22
Path::GetParent
Path * GetParent()
Get the parent leg of this one.
Definition: linkgraphjob.h:357
LinkGraphJob::EdgeIterator::operator*
SmallPair< NodeID, Edge > operator*() const
Dereference.
Definition: linkgraphjob.h:165
_date
Date _date
Current date in days (day counter)
Definition: date.cpp:28
LinkGraphJob::EdgeIterator::base_anno
EdgeAnnotation * base_anno
Array of annotations to be (indirectly) iterated.
Definition: linkgraphjob.h:148
LinkGraphJob::Edge::AddDemand
void AddDemand(uint demand)
Add some (not yet satisfied) demand.
Definition: linkgraphjob.h:127
Path::parent
Path * parent
Parent leg of this one.
Definition: linkgraphjob.h:433
LinkGraphJob::nodes
NodeAnnotationVector nodes
Extra node data necessary for link graph calculation.
Definition: linkgraphjob.h:64
ThreadObject
A Thread Object which works on all our supported OSes.
Definition: thread.h:24
LinkGraphJob::Node::Begin
EdgeIterator Begin() const
Iterator for the "begin" of the edge array.
Definition: linkgraphjob.h:213
LinkGraphJob::~LinkGraphJob
~LinkGraphJob()
Join the link graph job and destroy it.
Definition: linkgraphjob.cpp:92
LinkGraphJob::Edge::Demand
uint Demand() const
Get the transport demand between end the points of the edge.
Definition: linkgraphjob.h:93
LinkGraphJob
Class for calculation jobs to be run on link graphs.
Definition: linkgraphjob.h:31
LinkGraphJob::NodeAnnotation
Annotation for a link graph node.
Definition: linkgraphjob.h:46