1
0
Fork 0

Codechange: Use EnumBitSet for EdgeUpdateMode. (#13808)

pull/13779/head
Peter Nelson 2025-03-14 09:01:10 +00:00 committed by GitHub
parent 8191f39649
commit bd5d47836d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 29 additions and 29 deletions

View File

@ -164,7 +164,7 @@ NodeID LinkGraph::AddNode(const Station *st)
* @param usage Usage to be added.
* @param mode Update mode to be used.
*/
void LinkGraph::BaseNode::AddEdge(NodeID to, uint capacity, uint usage, uint32_t travel_time, EdgeUpdateMode mode)
void LinkGraph::BaseNode::AddEdge(NodeID to, uint capacity, uint usage, uint32_t travel_time, EdgeUpdateModes modes)
{
assert(!this->HasEdgeTo(to));
@ -172,8 +172,8 @@ void LinkGraph::BaseNode::AddEdge(NodeID to, uint capacity, uint usage, uint32_t
edge.capacity = capacity;
edge.usage = usage;
edge.travel_time_sum = static_cast<uint64_t>(travel_time) * capacity;
if (mode & EUM_UNRESTRICTED) edge.last_unrestricted_update = TimerGameEconomy::date;
if (mode & EUM_RESTRICTED) edge.last_restricted_update = TimerGameEconomy::date;
if (modes.Test(EdgeUpdateMode::Unrestricted)) edge.last_unrestricted_update = TimerGameEconomy::date;
if (modes.Test(EdgeUpdateMode::Restricted)) edge.last_restricted_update = TimerGameEconomy::date;
}
/**
@ -183,14 +183,14 @@ void LinkGraph::BaseNode::AddEdge(NodeID to, uint capacity, uint usage, uint32_t
* @param usage Usage to be added.
* @param mode Update mode to be used.
*/
void LinkGraph::BaseNode::UpdateEdge(NodeID to, uint capacity, uint usage, uint32_t travel_time, EdgeUpdateMode mode)
void LinkGraph::BaseNode::UpdateEdge(NodeID to, uint capacity, uint usage, uint32_t travel_time, EdgeUpdateModes modes)
{
assert(capacity > 0);
assert(usage <= capacity);
if (!this->HasEdgeTo(to)) {
this->AddEdge(to, capacity, usage, travel_time, mode);
this->AddEdge(to, capacity, usage, travel_time, modes);
} else {
this->GetEdge(to)->Update(capacity, usage, travel_time, mode);
this->GetEdge(to)->Update(capacity, usage, travel_time, modes);
}
}
@ -214,12 +214,12 @@ void LinkGraph::BaseNode::RemoveEdge(NodeID to)
* @param travel_time Travel time to be added, in ticks.
* @param mode Update mode to be applied.
*/
void LinkGraph::BaseEdge::Update(uint capacity, uint usage, uint32_t travel_time, EdgeUpdateMode mode)
void LinkGraph::BaseEdge::Update(uint capacity, uint usage, uint32_t travel_time, EdgeUpdateModes modes)
{
assert(this->capacity > 0);
assert(capacity >= usage);
if (mode & EUM_INCREASE) {
if (modes.Test(EdgeUpdateMode::Increase)) {
if (this->travel_time_sum == 0) {
this->travel_time_sum = static_cast<uint64_t>(this->capacity + capacity) * travel_time;
} else if (travel_time == 0) {
@ -229,7 +229,7 @@ void LinkGraph::BaseEdge::Update(uint capacity, uint usage, uint32_t travel_time
}
this->capacity += capacity;
this->usage += usage;
} else if (mode & EUM_REFRESH) {
} else if (modes.Test(EdgeUpdateMode::Refresh)) {
if (this->travel_time_sum == 0) {
this->capacity = std::max(this->capacity, capacity);
this->travel_time_sum = static_cast<uint64_t>(travel_time) * this->capacity;
@ -239,8 +239,8 @@ void LinkGraph::BaseEdge::Update(uint capacity, uint usage, uint32_t travel_time
}
this->usage = std::max(this->usage, usage);
}
if (mode & EUM_UNRESTRICTED) this->last_unrestricted_update = TimerGameEconomy::date;
if (mode & EUM_RESTRICTED) this->last_restricted_update = TimerGameEconomy::date;
if (modes.Test(EdgeUpdateMode::Unrestricted)) this->last_unrestricted_update = TimerGameEconomy::date;
if (modes.Test(EdgeUpdateMode::Restricted)) this->last_restricted_update = TimerGameEconomy::date;
}
/**

View File

@ -61,7 +61,7 @@ public:
*/
TimerGameEconomy::Date LastUpdate() const { return std::max(this->last_unrestricted_update, this->last_restricted_update); }
void Update(uint capacity, uint usage, uint32_t time, EdgeUpdateMode mode);
void Update(uint capacity, uint usage, uint32_t time, EdgeUpdateModes modes);
void Restrict() { this->last_unrestricted_update = EconomyTime::INVALID_DATE; }
void Release() { this->last_restricted_update = EconomyTime::INVALID_DATE; }
@ -126,8 +126,8 @@ public:
this->demand = demand;
}
void AddEdge(NodeID to, uint capacity, uint usage, uint32_t time, EdgeUpdateMode mode);
void UpdateEdge(NodeID to, uint capacity, uint usage, uint32_t time, EdgeUpdateMode mode);
void AddEdge(NodeID to, uint capacity, uint usage, uint32_t time, EdgeUpdateModes modes);
void UpdateEdge(NodeID to, uint capacity, uint usage, uint32_t time, EdgeUpdateModes modes);
void RemoveEdge(NodeID to);
/**

View File

@ -43,13 +43,13 @@ enum DistributionType : uint8_t {
* Refreshing a link makes just sure a minimum capacity is kept. Increasing
* actually adds the given capacity.
*/
enum EdgeUpdateMode : uint8_t {
EUM_INCREASE = 1, ///< Increase capacity.
EUM_REFRESH = 1 << 1, ///< Refresh capacity.
EUM_RESTRICTED = 1 << 2, ///< Use restricted link.
EUM_UNRESTRICTED = 1 << 3, ///< Use unrestricted link.
enum class EdgeUpdateMode : uint8_t {
Increase, ///< Increase capacity.
Refresh, ///< Refresh capacity.
Restricted, ///< Use restricted link.
Unrestricted, ///< Use unrestricted link.
};
DECLARE_ENUM_AS_BIT_SET(EdgeUpdateMode)
using EdgeUpdateModes = EnumBitSet<EdgeUpdateMode, uint8_t>;
#endif /* LINKGRAPH_TYPE_H */

View File

@ -218,7 +218,7 @@ void LinkRefresher::RefreshStats(const Order *cur, const Order *next)
/* A link is at least partly restricted if a vehicle can't load at its source. */
EdgeUpdateMode restricted_mode = (cur->GetLoadType() & OLFB_NO_LOAD) == 0 ?
EUM_UNRESTRICTED : EUM_RESTRICTED;
EdgeUpdateMode::Unrestricted : EdgeUpdateMode::Restricted;
/* This estimates the travel time of the link as the time needed
* to travel between the stations at half the max speed of the consist.
* The result is in tiles/tick (= 2048 km-ish/h). */
@ -236,14 +236,14 @@ void LinkRefresher::RefreshStats(const Order *cur, const Order *next)
if (effective_capacity > (uint)this->vehicle->orders->GetTotalDuration()) {
IncreaseStats(st, c, next_station, effective_capacity /
this->vehicle->orders->GetTotalDuration(), 0, 0,
EUM_INCREASE | restricted_mode);
{EdgeUpdateMode::Increase, restricted_mode});
} else if (RandomRange(this->vehicle->orders->GetTotalDuration()) < effective_capacity) {
IncreaseStats(st, c, next_station, 1, 0, 0, EUM_INCREASE | restricted_mode);
IncreaseStats(st, c, next_station, 1, 0, 0, {EdgeUpdateMode::Increase, restricted_mode});
} else {
IncreaseStats(st, c, next_station, cargo_quantity, 0, time_estimate, EUM_REFRESH | restricted_mode);
IncreaseStats(st, c, next_station, cargo_quantity, 0, time_estimate, {EdgeUpdateMode::Refresh, restricted_mode});
}
} else {
IncreaseStats(st, c, next_station, cargo_quantity, 0, time_estimate, EUM_REFRESH | restricted_mode);
IncreaseStats(st, c, next_station, cargo_quantity, 0, time_estimate, {EdgeUpdateMode::Refresh, restricted_mode});
}
}
}

View File

@ -4112,7 +4112,7 @@ void DeleteStaleLinks(Station *from)
* @param usage Usage to add to link stat.
* @param mode Update mode to be applied.
*/
void IncreaseStats(Station *st, CargoType cargo, StationID next_station_id, uint capacity, uint usage, uint32_t time, EdgeUpdateMode mode)
void IncreaseStats(Station *st, CargoType cargo, StationID next_station_id, uint capacity, uint usage, uint32_t time, EdgeUpdateModes modes)
{
GoodsEntry &ge1 = st->goods[cargo];
Station *st2 = Station::Get(next_station_id);
@ -4154,7 +4154,7 @@ void IncreaseStats(Station *st, CargoType cargo, StationID next_station_id, uint
}
}
if (lg != nullptr) {
(*lg)[ge1.node].UpdateEdge(ge2.node, capacity, usage, time, mode);
(*lg)[ge1.node].UpdateEdge(ge2.node, capacity, usage, time, modes);
}
}
@ -4175,7 +4175,7 @@ void IncreaseStats(Station *st, const Vehicle *front, StationID next_station_id,
* As usage is not such an important figure anyway we just
* ignore the additional cargo then.*/
IncreaseStats(st, v->cargo_type, next_station_id, v->refit_cap,
std::min<uint>(v->refit_cap, v->cargo.StoredCount()), time, EUM_INCREASE);
std::min<uint>(v->refit_cap, v->cargo.StoredCount()), time, EdgeUpdateMode::Increase);
}
}
}

View File

@ -49,7 +49,7 @@ void UpdateAirportsNoise();
bool SplitGroundSpriteForOverlay(const TileInfo *ti, SpriteID *ground, RailTrackOffset *overlay_offset);
void IncreaseStats(Station *st, const Vehicle *v, StationID next_station_id, uint32_t time);
void IncreaseStats(Station *st, CargoType cargo, StationID next_station_id, uint capacity, uint usage, uint32_t time, EdgeUpdateMode mode);
void IncreaseStats(Station *st, CargoType cargo, StationID next_station_id, uint capacity, uint usage, uint32_t time, EdgeUpdateModes modes);
void RerouteCargo(Station *st, CargoType c, StationID avoid, StationID avoid2);
/**