OpenTTD Source: LinkGraphJob Class Reference

OpenTTD Source 12.2

Data Structures | Public Member Functions | Protected Member Functions | Protected Attributes | Private Types | Friends
LinkGraphJob Class Reference

Class for calculation jobs to be run on link graphs. More...

`#include <linkgraphjob.h>`

Inheritance diagram for LinkGraphJob:

## Data Structures
class | Edge
| A job edge. More...
struct | EdgeAnnotation
| Annotation for a link graph edge. More...
class | EdgeIterator
| Iterator for job edges. More...
class | Node
| Link graph job node. More...
struct | NodeAnnotation
| Annotation for a link graph node. More...

## Public Member Functions
| LinkGraphJob ()
| Bare constructor, only for save/load. More...
| LinkGraphJob (const LinkGraph &orig)
| Create a link graph job from a link graph. More...
| ~LinkGraphJob ()
| Join the link graph job and destroy it.
void | Init ()
| Initialize the link graph job: Resize nodes and edges and populate them. More...
bool | IsJobCompleted () const
| Check if job has actually finished. More...
bool | IsJobAborted () const
| Check if job has been aborted. More...
void | AbortJob ()
| Abort job. More...
bool | IsScheduledToBeJoined () const
| Check if job is supposed to be finished. More...
Date | JoinDate () const
| Get the date when the job should be finished. More...
void | ShiftJoinDate (int interval)
| Change the join date on date cheating. More...
const LinkGraphSettings & | Settings () const
| Get the link graph settings for this component. More...
Node | operator[] (NodeID num)
| Get a node abstraction with the specified id. More...
NodeID | Size () const
| Get the size of the underlying link graph. More...
CargoID | Cargo () const
| Get the cargo of the underlying link graph. More...
Date | LastCompression () const
| Get the date when the underlying link graph was last compressed. More...
LinkGraphID | LinkGraphIndex () const
| Get the ID of the underlying link graph. More...
const LinkGraph & | Graph () const
| Get a reference to the underlying link graph. More...
Public Member Functions inherited from Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_link_graph_job_pool >
void * | operator new (size_t size)
| Allocates space for new Titem. More...
void * | operator new (size_t size, size_t index)
| Allocates space for new Titem with given index. More...
void * | operator new (size_t size, void *ptr)
| Allocates space for new Titem at given memory address. More...
void | operator delete (void *p)
| Marks Titem as free. More...

## Protected Member Functions
void | EraseFlows (NodeID from)
| Erase all flows originating at a specific node. More...
void | JoinThread ()
| Join the calling thread with this job's thread if threading is enabled.
void | SpawnThread ()
| Spawn a thread if possible and run the link graph job in the thread. More...

## Protected Attributes
const LinkGraph | link_graph
| Link graph to by analyzed. Is copied when job is started and mustn't be modified later.
const LinkGraphSettings | settings
| Copy of _settings_game.linkgraph at spawn time.
std::thread | thread
| Thread the job is running in or a default-constructed thread if it's running in the main thread.
Date | join_date
| Date when the job is to be joined.
NodeAnnotationVector | nodes
| Extra node data necessary for link graph calculation.
EdgeAnnotationMatrix | edges
| Extra edge data necessary for link graph calculation.
std::atomic< bool > | job_completed
| Is the job still running. This is accessed by multiple threads and reads may be stale.
std::atomic< bool > | job_aborted
| Has the job been aborted. This is accessed by multiple threads and reads may be stale.

## Private Types
typedef std::vector< NodeAnnotation > | NodeAnnotationVector
typedef SmallMatrix< EdgeAnnotation > | EdgeAnnotationMatrix

## Friends
class | LinkGraphSchedule
SaveLoadTable | GetLinkGraphJobDesc ()
| Get a SaveLoad array for a link graph job. More...

## Additional Inherited Members
Public Types inherited from Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_link_graph_job_pool >
typedef struct Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero > | Pool
| Type of the pool this item is going to be part of.
Static Public Member Functions inherited from Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_link_graph_job_pool >
static bool | CanAllocateItem (size_t n=1)
| Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function() More...
static bool | CleaningPool ()
| Returns current state of pool cleaning - yes or no. More...
static bool | IsValidID (size_t index)
| Tests whether given index can be used to get valid (non-nullptr) Titem. More...
static Titem * | Get (size_t index)
| Returns Titem with given index. More...
static Titem * | GetIfValid (size_t index)
| Returns Titem with given index. More...
static size_t | GetPoolSize ()
| Returns first unused index. More...
static size_t | GetNumItems ()
| Returns number of valid items in the pool. More...
static void | PostDestructor (size_t index)
| Dummy function called after destructor of each member. More...
static Pool::IterateWrapper< Titem > | Iterate (size_t from=0)
| Returns an iterable ensemble of all valid Titem. More...
Data Fields inherited from Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_link_graph_job_pool >
Tindex | index
| Index of this pool item.

## Detailed Description

Class for calculation jobs to be run on link graphs.

Definition at line 30 of file linkgraphjob.h.

## Constructor & Destructor Documentation

## ◆ LinkGraphJob() [1/2]

LinkGraphJob::LinkGraphJob | ( |  | ) |
| inline

Bare constructor, only for save/load.

link_graph, join_date and actually settings have to be brutally const-casted in order to populate them.

Definition at line 270 of file linkgraphjob.h.

## ◆ LinkGraphJob() [2/2]

LinkGraphJob::LinkGraphJob | ( | const LinkGraph & | orig | ) |

Create a link graph job from a link graph.

The link graph will be copied so that the calculations don't interfer with the normal operations on the original. The job is immediately started.

Parameters

  orig | Original LinkGraph to be copied.

Definition at line 35 of file linkgraphjob.cpp.

## Member Function Documentation

## ◆ AbortJob()

void LinkGraphJob::AbortJob | ( |  | ) |
| inline

Abort job.

The job may exit early at the next available opportunity. After this method has been called the state of the job is undefined, and the only valid operation is to join the thread and discard the job data.

Definition at line 298 of file linkgraphjob.h.

## ◆ Cargo()

CargoID LinkGraphJob::Cargo | ( |  | ) | const
| inline

Get the cargo of the underlying link graph.

Returns
  Cargo.

Definition at line 341 of file linkgraphjob.h.

References LinkGraph::Cargo().

Referenced by DemandCalculator::DemandCalculator(), and ~LinkGraphJob().

## ◆ EraseFlows()

void LinkGraphJob::EraseFlows | ( | NodeID | from | ) |
| protected

Erase all flows originating at a specific node.

Parameters

  from | Node to erase flows for.

Definition at line 50 of file linkgraphjob.cpp.

References Size().

Referenced by ~LinkGraphJob().

## ◆ Graph()

const LinkGraph& LinkGraphJob::Graph | ( |  | ) | const
| inline

Get a reference to the underlying link graph.

Only use this for save/load.

Returns
  Link graph.

Definition at line 359 of file linkgraphjob.h.

References link_graph.

Referenced by AfterLoadLinkGraphs().

## ◆ Init()

void LinkGraphJob::Init | ( |  | ) |

Initialize the link graph job: Resize nodes and edges and populate them.

This is done after the constructor so that we can do it in the calculation thread without delaying the main game.

Definition at line 180 of file linkgraphjob.cpp.

References edges, LinkGraphJob::EdgeAnnotation::Init(), link_graph, nodes, SmallMatrix< T >::Resize(), and Size().

Referenced by InitHandler::Run().

## ◆ IsJobAborted()

bool LinkGraphJob::IsJobAborted | ( |  | ) | const
| inline

Check if job has been aborted.

This is allowed to spuriously return false incorrectly, but is not allowed to incorrectly return true.

Returns
  True if job has been aborted.

Definition at line 290 of file linkgraphjob.h.

Referenced by MCF1stPass::MCF1stPass(), MCF2ndPass::MCF2ndPass(), LinkGraphSchedule::Run(), and ~LinkGraphJob().

## ◆ IsJobCompleted()

bool LinkGraphJob::IsJobCompleted | ( |  | ) | const
| inline

Check if job has actually finished.

This is allowed to spuriously return an incorrect value.

Returns
  True if job has actually finished.

Definition at line 283 of file linkgraphjob.h.

Referenced by LinkGraphSchedule::IsJoinWithUnfinishedJobDue().

## ◆ IsScheduledToBeJoined()

bool LinkGraphJob::IsScheduledToBeJoined | ( |  | ) | const
| inline

Check if job is supposed to be finished.

Returns
  True if job should be finished by now, false if not.

Definition at line 304 of file linkgraphjob.h.

References _date.

Referenced by LinkGraphSchedule::IsJoinWithUnfinishedJobDue(), and LinkGraphSchedule::JoinNext().

## ◆ JoinDate()

Date LinkGraphJob::JoinDate | ( |  | ) | const
| inline

Get the date when the job should be finished.

Returns
  Join date.

Definition at line 310 of file linkgraphjob.h.

References join_date.

Referenced by FlowMapper::Run().

## ◆ LastCompression()

Date LinkGraphJob::LastCompression | ( |  | ) | const
| inline

Get the date when the underlying link graph was last compressed.

Returns
  Compression date.

Definition at line 347 of file linkgraphjob.h.

References LinkGraph::LastCompression().

Referenced by FlowMapper::Run().

## ◆ LinkGraphIndex()

LinkGraphID LinkGraphJob::LinkGraphIndex | ( |  | ) | const
| inline

Get the ID of the underlying link graph.

Returns
  Link graph ID.

Definition at line 353 of file linkgraphjob.h.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index.

Referenced by LinkGraphSchedule::JoinNext().

## ◆ operator[]()

Node LinkGraphJob::operator[] | ( | NodeID | num | ) |
| inline

Get a node abstraction with the specified id.

Parameters

  num | ID of the node.

Returns
  the Requested node.

Definition at line 329 of file linkgraphjob.h.

## ◆ Settings()

const LinkGraphSettings& LinkGraphJob::Settings | ( |  | ) | const
| inline

Get the link graph settings for this component.

Returns
  Settings.

Definition at line 322 of file linkgraphjob.h.

References settings.

Referenced by DemandCalculator::DemandCalculator(), MCF1stPass::MCF1stPass(), MCF2ndPass::MCF2ndPass(), and FlowMapper::Run().

## ◆ ShiftJoinDate()

void LinkGraphJob::ShiftJoinDate | ( | int | interval | ) |
| inline

Change the join date on date cheating.

Parameters

  interval | Number of days to add.

Definition at line 316 of file linkgraphjob.h.

## ◆ Size()

NodeID LinkGraphJob::Size | ( |  | ) | const
| inline

Get the size of the underlying link graph.

Returns
  Size.

Definition at line 335 of file linkgraphjob.h.

References LinkGraph::Size().

Referenced by DemandCalculator::CalcDemand(), MultiCommodityFlow::Dijkstra(), MCF1stPass::EliminateCycles(), EraseFlows(), FlowEdgeIterator::FlowEdgeIterator(), Init(), MCF1stPass::MCF1stPass(), MCF2ndPass::MCF2ndPass(), FlowMapper::Run(), and ~LinkGraphJob().

## ◆ SpawnThread()

void LinkGraphJob::SpawnThread | ( |  | ) |
| protected

Spawn a thread if possible and run the link graph job in the thread.

If that's not possible run the job right now in the current thread.

Definition at line 61 of file linkgraphjob.cpp.

References LinkGraphSchedule::Run(), StartNewThread(), and thread.

Referenced by LinkGraphSchedule::SpawnNext().

## Friends And Related Function Documentation

## ◆ GetLinkGraphJobDesc

SaveLoadTable GetLinkGraphJobDesc | ( |  | ) |
| friend

Get a SaveLoad array for a link graph job.

The settings struct is derived from the global settings saveload array. The exact entries are calculated when the function is called the first time. It's necessary to keep a copy of the settings for each link graph job so that you can change the settings while in-game and still not mess with current link graph runs. Of course the settings have to be saved and loaded, too, to avoid desyncs.

Returns
  Array of SaveLoad structs.

Definition at line 167 of file linkgraph_sl.cpp.

The documentation for this class was generated from the following files:

- src/linkgraph/linkgraphjob.h
- src/linkgraph/linkgraphjob.cpp