Change: Use bitmap for free unit ID generation. (#12165)

This improves performance of finding the next free unit number for a vehicle.

Based loosely on pool's used slot bitmap.
This commit is contained in:
Peter Nelson
2024-02-25 12:36:13 +00:00
committed by GitHub
parent 8afef45d4e
commit 56cf89d189
7 changed files with 68 additions and 56 deletions

View File

@@ -49,6 +49,19 @@ struct CompanyInfrastructure {
uint32_t GetTramTotal() const;
};
class FreeUnitIDGenerator {
public:
UnitID NextID() const;
UnitID UseID(UnitID index);
void ReleaseID(UnitID index);
private:
using BitmapStorage = size_t;
static constexpr size_t BITMAP_SIZE = std::numeric_limits<BitmapStorage>::digits;
std::vector<BitmapStorage> used_bitmap;
};
typedef Pool<Company, CompanyID, 1, MAX_COMPANIES> CompanyPool;
extern CompanyPool _company_pool;
@@ -129,6 +142,8 @@ struct Company : CompanyProperties, CompanyPool::PoolItem<&_company_pool> {
CompanyInfrastructure infrastructure; ///< NOSAVE: Counts of company owned infrastructure.
FreeUnitIDGenerator freeunits[VEH_COMPANY_END];
Money GetMaxLoan() const;
/**