Make GrowthRate library be key based on uint32, instead of on the ConstString, to save memory.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-02-29 15:07:05 +01:00
parent b7b54c04e8
commit 4866edebab
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 35 additions and 13 deletions

View File

@ -1,8 +1,7 @@
#include "GrowthRateLibrary.hpp"
#include <algorithm>
#include "../../Core/Exceptions/CreatureException.hpp"
uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(const Arbutils::CaseInsensitiveConstString& growthRate,
uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(const ConstString& growthRate,
uint32_t experience) const {
auto find = _growthRates.find(growthRate);
if (find == _growthRates.end()) {
@ -11,16 +10,36 @@ uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(const Arbutils::
return find->second->CalculateLevel(experience);
}
uint32_t
CreatureLib::Library::GrowthRateLibrary::CalculateExperience(const Arbutils::CaseInsensitiveConstString& growthRate,
uint8_t level) const {
uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(uint32_t hash, uint32_t experience) const {
auto find = _growthRates.find(hash);
if (find == _growthRates.end()) {
throw CreatureException("Invalid growth rate was requested.");
}
return find->second->CalculateLevel(experience);
}
uint32_t CreatureLib::Library::GrowthRateLibrary::CalculateExperience(const ConstString& growthRate,
uint8_t level) const {
auto find = _growthRates.find(growthRate);
if (find == _growthRates.end()) {
throw CreatureException("Invalid growth rate was requested.");
}
return find->second->CalculateExperience(level);
}
void CreatureLib::Library::GrowthRateLibrary::AddGrowthRate(const Arbutils::CaseInsensitiveConstString& name,
uint32_t CreatureLib::Library::GrowthRateLibrary::CalculateExperience(uint32_t hash, uint8_t level) const {
auto find = _growthRates.find(hash);
if (find == _growthRates.end()) {
throw CreatureException("Invalid growth rate was requested.");
}
return find->second->CalculateExperience(level);
}
void CreatureLib::Library::GrowthRateLibrary::AddGrowthRate(const ConstString& name,
CreatureLib::Library::GrowthRate* rate) {
_growthRates.insert({name, rate});
}
void CreatureLib::Library::GrowthRateLibrary::AddGrowthRate(uint32_t hash, CreatureLib::Library::GrowthRate* rate) {
_growthRates.insert({hash, rate});
}

View File

@ -7,14 +7,16 @@
#include <unordered_map>
#include "GrowthRate.hpp"
using ConstString = Arbutils::CaseInsensitiveConstString;
namespace CreatureLib::Library {
class GrowthRateLibrary {
private:
std::unordered_map<Arbutils::CaseInsensitiveConstString, GrowthRate*> _growthRates;
std::unordered_map<uint32_t, GrowthRate*> _growthRates;
public:
GrowthRateLibrary(size_t initialCapacity = 10)
: _growthRates(std::unordered_map<Arbutils::CaseInsensitiveConstString, GrowthRate*>(initialCapacity)) {}
: _growthRates(std::unordered_map<uint32_t, GrowthRate*>(initialCapacity)) {}
virtual ~GrowthRateLibrary() {
for (auto gr : _growthRates) {
@ -22,12 +24,13 @@ namespace CreatureLib::Library {
}
}
[[nodiscard]] uint8_t CalculateLevel(const Arbutils::CaseInsensitiveConstString& growthRate,
uint32_t experience) const;
[[nodiscard]] uint32_t CalculateExperience(const Arbutils::CaseInsensitiveConstString& growthRate,
uint8_t level) const;
[[nodiscard]] uint8_t CalculateLevel(const ConstString& growthRate, uint32_t experience) const;
[[nodiscard]] uint8_t CalculateLevel(uint32_t hash, uint32_t experience) const;
[[nodiscard]] uint32_t CalculateExperience(const ConstString& growthRate, uint8_t level) const;
[[nodiscard]] uint32_t CalculateExperience(uint32_t hash, uint8_t level) const;
void AddGrowthRate(const Arbutils::CaseInsensitiveConstString& name, GrowthRate* rate);
void AddGrowthRate(uint32_t hash, GrowthRate* rate);
void AddGrowthRate(const ConstString& name, GrowthRate* rate);
};
}