1
0
Fork 0

Codechange: explicitly initialise LinkGraph and LinkGraphJob member variables

pull/13608/head
Rubidium 2025-02-18 17:46:48 +01:00 committed by rubidium42
parent 35e7255a5a
commit 6eb2dec338
3 changed files with 42 additions and 46 deletions

View File

@ -40,12 +40,12 @@ public:
* An edge in the link graph. Corresponds to a link between two stations. * An edge in the link graph. Corresponds to a link between two stations.
*/ */
struct BaseEdge { struct BaseEdge {
uint capacity; ///< Capacity of the link. uint capacity = 0; ///< Capacity of the link.
uint usage; ///< Usage of the link. uint usage = 0; ///< Usage of the link.
uint64_t travel_time_sum; ///< Sum of the travel times of the link, in ticks. uint64_t travel_time_sum = 0; ///< Sum of the travel times of the link, in ticks.
TimerGameEconomy::Date last_unrestricted_update; ///< When the unrestricted part of the link was last updated. TimerGameEconomy::Date last_unrestricted_update{}; ///< When the unrestricted part of the link was last updated.
TimerGameEconomy::Date last_restricted_update; ///< When the restricted part of the link was last updated. TimerGameEconomy::Date last_restricted_update{}; ///< When the restricted part of the link was last updated.
NodeID dest_node; ///< Destination of the edge. NodeID dest_node = INVALID_NODE; ///< Destination of the edge.
BaseEdge(NodeID dest_node = INVALID_NODE); BaseEdge(NodeID dest_node = INVALID_NODE);
@ -88,11 +88,11 @@ public:
* in a separate thread. * in a separate thread.
*/ */
struct BaseNode { struct BaseNode {
uint supply; ///< Supply at the station. uint supply = 0; ///< Supply at the station.
uint demand; ///< Acceptance at the station. uint demand = 0; ///< Acceptance at the station.
StationID station; ///< Station ID. StationID station = StationID::Invalid(); ///< Station ID.
TileIndex xy; ///< Location of the station referred to by the node. TileIndex xy = INVALID_TILE; ///< Location of the station referred to by the node.
TimerGameEconomy::Date last_update; ///< When the supply was last updated. TimerGameEconomy::Date last_update{}; ///< When the supply was last updated.
std::vector<BaseEdge> edges; ///< Sorted list of outgoing edges from this node. std::vector<BaseEdge> edges; ///< Sorted list of outgoing edges from this node.
@ -189,7 +189,7 @@ public:
} }
/** Bare constructor, only for save/load. */ /** Bare constructor, only for save/load. */
LinkGraph() : cargo(INVALID_CARGO), last_compression(0) {} LinkGraph() {}
/** /**
* Real constructor. * Real constructor.
* @param cargo Cargo the link graph is about. * @param cargo Cargo the link graph is about.
@ -261,9 +261,9 @@ protected:
friend class SlLinkgraphEdge; friend class SlLinkgraphEdge;
friend class LinkGraphJob; friend class LinkGraphJob;
CargoType cargo; ///< Cargo of this component's link graph. CargoType cargo = INVALID_CARGO; ///< Cargo of this component's link graph.
TimerGameEconomy::Date last_compression; ///< Last time the capacities and supplies were compressed. TimerGameEconomy::Date last_compression{}; ///< Last time the capacities and supplies were compressed.
NodeVector nodes; ///< Nodes in the component. NodeVector nodes{}; ///< Nodes in the component.
}; };
#endif /* LINKGRAPH_H */ #endif /* LINKGRAPH_H */

View File

@ -37,9 +37,7 @@ LinkGraphJob::LinkGraphJob(const LinkGraph &orig) :
* This is on purpose. */ * This is on purpose. */
link_graph(orig), link_graph(orig),
settings(_settings_game.linkgraph), settings(_settings_game.linkgraph),
join_date(TimerGameEconomy::date + (_settings_game.linkgraph.recalc_time / EconomyTime::SECONDS_PER_DAY)), join_date(TimerGameEconomy::date + (_settings_game.linkgraph.recalc_time / EconomyTime::SECONDS_PER_DAY))
job_completed(false),
job_aborted(false)
{ {
} }

View File

@ -32,8 +32,8 @@ public:
* Demand between two nodes. * Demand between two nodes.
*/ */
struct DemandAnnotation { struct DemandAnnotation {
uint demand; ///< Transport demand between the nodes. uint demand = 0; ///< Transport demand between the nodes.
uint unsatisfied_demand; ///< Demand over this edge that hasn't been satisfied yet. uint unsatisfied_demand = 0; ///< Demand over this edge that hasn't been satisfied yet.
}; };
/** /**
@ -41,10 +41,9 @@ public:
*/ */
struct EdgeAnnotation { struct EdgeAnnotation {
const LinkGraph::BaseEdge &base; ///< Reference to the edge that is annotated. const LinkGraph::BaseEdge &base; ///< Reference to the edge that is annotated.
uint flow = 0; ///< Planned flow over this edge.
uint flow; ///< Planned flow over this edge. EdgeAnnotation(const LinkGraph::BaseEdge &base) : base(base) {}
EdgeAnnotation(const LinkGraph::BaseEdge &base) : base(base), flow(0) {}
/** /**
* Get the total flow on the edge. * Get the total flow on the edge.
@ -80,14 +79,14 @@ public:
struct NodeAnnotation { struct NodeAnnotation {
const LinkGraph::BaseNode &base; ///< Reference to the node that is annotated. const LinkGraph::BaseNode &base; ///< Reference to the node that is annotated.
uint undelivered_supply; ///< Amount of supply that hasn't been distributed yet. uint undelivered_supply = 0; ///< Amount of supply that hasn't been distributed yet.
PathList paths; ///< Paths through this node, sorted so that those with flow == 0 are in the back. PathList paths{}; ///< Paths through this node, sorted so that those with flow == 0 are in the back.
FlowStatMap flows; ///< Planned flows to other nodes. FlowStatMap flows{}; ///< Planned flows to other nodes.
std::vector<EdgeAnnotation> edges; ///< Annotations for all edges originating at this node. std::vector<EdgeAnnotation> edges{}; ///< Annotations for all edges originating at this node.
std::vector<DemandAnnotation> demands; ///< Annotations for the demand to all other nodes. std::vector<DemandAnnotation> demands{}; ///< Annotations for the demand to all other nodes.
NodeAnnotation(const LinkGraph::BaseNode &node, size_t size) : base(node), undelivered_supply(node.supply), paths(), flows() NodeAnnotation(const LinkGraph::BaseNode &node, size_t size) : base(node), undelivered_supply(node.supply)
{ {
this->edges.reserve(node.edges.size()); this->edges.reserve(node.edges.size());
for (auto &e : node.edges) this->edges.emplace_back(e); for (auto &e : node.edges) this->edges.emplace_back(e);
@ -160,13 +159,13 @@ private:
friend class LinkGraphSchedule; friend class LinkGraphSchedule;
protected: protected:
const LinkGraph link_graph; ///< Link graph to by analyzed. Is copied when job is started and mustn't be modified later. 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. 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. std::thread thread{}; ///< Thread the job is running in or a default-constructed thread if it's running in the main thread.
TimerGameEconomy::Date join_date; ///< Date when the job is to be joined. TimerGameEconomy::Date join_date = EconomyTime::INVALID_DATE; ///< Date when the job is to be joined.
NodeAnnotationVector nodes; ///< Extra node data necessary for link graph calculation. NodeAnnotationVector nodes{}; ///< Extra node 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_completed = false; ///< 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. std::atomic<bool> job_aborted = false; ///< Has the job been aborted. This is accessed by multiple threads and reads may be stale.
void EraseFlows(NodeID from); void EraseFlows(NodeID from);
void JoinThread(); void JoinThread();
@ -177,8 +176,7 @@ public:
* Bare constructor, only for save/load. link_graph, join_date and actually * Bare constructor, only for save/load. link_graph, join_date and actually
* settings have to be brutally const-casted in order to populate them. * settings have to be brutally const-casted in order to populate them.
*/ */
LinkGraphJob() : settings(_settings_game.linkgraph), LinkGraphJob() : settings(_settings_game.linkgraph) {}
join_date(EconomyTime::INVALID_DATE), job_completed(false), job_aborted(false) {}
LinkGraphJob(const LinkGraph &orig); LinkGraphJob(const LinkGraph &orig);
~LinkGraphJob(); ~LinkGraphJob();
@ -353,14 +351,14 @@ protected:
static constexpr int PATH_CAP_MIN_FREE = (INT_MIN + 1) / PATH_CAP_MULTIPLIER; static constexpr int PATH_CAP_MIN_FREE = (INT_MIN + 1) / PATH_CAP_MULTIPLIER;
static constexpr int PATH_CAP_MAX_FREE = (INT_MAX - 1) / PATH_CAP_MULTIPLIER; static constexpr int PATH_CAP_MAX_FREE = (INT_MAX - 1) / PATH_CAP_MULTIPLIER;
uint distance; ///< Sum(distance of all legs up to this one). uint distance = 0; ///< Sum(distance of all legs up to this one).
uint capacity; ///< This capacity is min(capacity) fom all edges. uint capacity = 0; ///< This capacity is min(capacity) fom all edges.
int free_capacity; ///< This capacity is min(edge.capacity - edge.flow) for the current run of Dijkstra. int free_capacity = 0; ///< This capacity is min(edge.capacity - edge.flow) for the current run of Dijkstra.
uint flow; ///< Flow the current run of the mcf solver assigns. uint flow = 0; ///< Flow the current run of the mcf solver assigns.
NodeID node; ///< Link graph node this leg passes. NodeID node = INVALID_NODE; ///< Link graph node this leg passes.
NodeID origin; ///< Link graph node this path originates from. NodeID origin = INVALID_NODE; ///< Link graph node this path originates from.
uint num_children; ///< Number of child legs that have been forked from this path. uint num_children = 0; ///< Number of child legs that have been forked from this path.
Path *parent; ///< Parent leg of this one. Path *parent = nullptr; ///< Parent leg of this one.
}; };
#endif /* LINKGRAPHJOB_H */ #endif /* LINKGRAPHJOB_H */