Skip to content

Commit

Permalink
Add hash table index to EngineOverrideManager
Browse files Browse the repository at this point in the history
Use for EngineOverrideManager::GetID lookups
  • Loading branch information
JGRennison committed May 31, 2024
1 parent 5567223 commit f09f412
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ void EngineOverrideManager::ResetToDefaultMapping()
eid.substitute_id = internal_id;
}
}
this->ReIndex();
}

/**
Expand All @@ -592,14 +593,21 @@ void EngineOverrideManager::ResetToDefaultMapping()
*/
EngineID EngineOverrideManager::GetID(VehicleType type, uint16_t grf_local_id, uint32_t grfid)
{
auto iter = this->mapping_index.find(HashKey(type, grf_local_id, grfid));
EngineID id = (iter != this->mapping_index.end()) ? iter->second : INVALID_ENGINE;

#ifdef _DEBUG
EngineID index = 0;
for (const EngineIDMapping &eid : *this) {
if (eid.type == type && eid.grfid == grfid && eid.internal_id == grf_local_id) {
assert(id == index);
return index;
}
index++;
}
return INVALID_ENGINE;
assert(id == INVALID_ENGINE);
#endif
return id;
}

/**
Expand All @@ -620,6 +628,26 @@ bool EngineOverrideManager::ResetToCurrentNewGRFConfig()
return true;
}

void EngineOverrideManager::AddToIndex(EngineID id)
{
this->mapping_index.insert({ HashKey((*this)[id]), id });
}

void EngineOverrideManager::RemoveFromIndex(EngineID id)
{
this->mapping_index.erase(HashKey((*this)[id]));
}

void EngineOverrideManager::ReIndex()
{
this->mapping_index.clear();
EngineID index = 0;
for (const EngineIDMapping &eid : *this) {
this->mapping_index.insert({ HashKey(eid), index });
index++;
}
}

/**
* Initialise the engine pool with the data from the original vehicles.
*/
Expand Down
19 changes: 19 additions & 0 deletions src/engine_override.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "engine_type.h"
#include "vehicle_type.h"

#include "3rdparty/robin_hood/robin_hood.h"
#include <vector>

struct EngineIDMapping {
Expand All @@ -29,9 +30,27 @@ struct EngineIDMapping {
struct EngineOverrideManager : std::vector<EngineIDMapping> {
static const uint NUM_DEFAULT_ENGINES; ///< Number of default entries

private:
static uint64_t HashKey(VehicleType type, uint16_t grf_local_id, uint32_t grfid)
{
return grfid | (static_cast<uint64_t>(grf_local_id) << 32) | (static_cast<uint64_t>(type) << 48);
}

static uint64_t HashKey(const EngineIDMapping &eid)
{
return HashKey(eid.type, eid.internal_id, eid.grfid);
}

robin_hood::unordered_map<uint64_t, EngineID> mapping_index;

public:
void ResetToDefaultMapping();
EngineID GetID(VehicleType type, uint16_t grf_local_id, uint32_t grfid);

void AddToIndex(EngineID id);
void RemoveFromIndex(EngineID id);
void ReIndex();

static bool ResetToCurrentNewGRFConfig();
};

Expand Down
3 changes: 3 additions & 0 deletions src/newgrf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -569,8 +569,10 @@ static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16_t inte

/* Reserve the engine slot */
if (!static_access) {
_engine_mngr.RemoveFromIndex(engine);
EngineIDMapping *eid = _engine_mngr.data() + engine;
eid->grfid = scope_grfid; // Note: this is INVALID_GRFID if dynamic_engines is disabled, so no reservation
_engine_mngr.AddToIndex(engine);
}

return e;
Expand All @@ -597,6 +599,7 @@ static Engine *GetNewEngine(const GRFFile *file, VehicleType type, uint16_t inte
type,
std::min<uint8_t>(internal_id, _engine_counts[type]) // substitute_id == _engine_counts[subtype] means "no substitute"
});
_engine_mngr.AddToIndex(e->index);

if (engine_pool_size != Engine::GetPoolSize()) {
/* Resize temporary engine data ... */
Expand Down
2 changes: 2 additions & 0 deletions src/saveload/engine_sl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ struct EIDSChunkHandler : ChunkHandler {
EngineIDMapping *eid = &_engine_mngr.emplace_back();
SlObject(eid, slt);
}

_engine_mngr.ReIndex();
}
};

Expand Down

0 comments on commit f09f412

Please sign in to comment.