Make GrowthRate library be key based on uint32, instead of on the ConstString, to save memory.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
b7b54c04e8
commit
4866edebab
|
@ -1,8 +1,7 @@
|
||||||
#include "GrowthRateLibrary.hpp"
|
#include "GrowthRateLibrary.hpp"
|
||||||
#include <algorithm>
|
|
||||||
#include "../../Core/Exceptions/CreatureException.hpp"
|
#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 {
|
uint32_t experience) const {
|
||||||
auto find = _growthRates.find(growthRate);
|
auto find = _growthRates.find(growthRate);
|
||||||
if (find == _growthRates.end()) {
|
if (find == _growthRates.end()) {
|
||||||
|
@ -11,8 +10,15 @@ uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(const Arbutils::
|
||||||
return find->second->CalculateLevel(experience);
|
return find->second->CalculateLevel(experience);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t
|
uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(uint32_t hash, uint32_t experience) const {
|
||||||
CreatureLib::Library::GrowthRateLibrary::CalculateExperience(const Arbutils::CaseInsensitiveConstString& growthRate,
|
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 {
|
uint8_t level) const {
|
||||||
auto find = _growthRates.find(growthRate);
|
auto find = _growthRates.find(growthRate);
|
||||||
if (find == _growthRates.end()) {
|
if (find == _growthRates.end()) {
|
||||||
|
@ -20,7 +26,20 @@ CreatureLib::Library::GrowthRateLibrary::CalculateExperience(const Arbutils::Cas
|
||||||
}
|
}
|
||||||
return find->second->CalculateExperience(level);
|
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) {
|
CreatureLib::Library::GrowthRate* rate) {
|
||||||
_growthRates.insert({name, rate});
|
_growthRates.insert({name, rate});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void CreatureLib::Library::GrowthRateLibrary::AddGrowthRate(uint32_t hash, CreatureLib::Library::GrowthRate* rate) {
|
||||||
|
_growthRates.insert({hash, rate});
|
||||||
|
}
|
||||||
|
|
|
@ -7,14 +7,16 @@
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include "GrowthRate.hpp"
|
#include "GrowthRate.hpp"
|
||||||
|
|
||||||
|
using ConstString = Arbutils::CaseInsensitiveConstString;
|
||||||
|
|
||||||
namespace CreatureLib::Library {
|
namespace CreatureLib::Library {
|
||||||
class GrowthRateLibrary {
|
class GrowthRateLibrary {
|
||||||
private:
|
private:
|
||||||
std::unordered_map<Arbutils::CaseInsensitiveConstString, GrowthRate*> _growthRates;
|
std::unordered_map<uint32_t, GrowthRate*> _growthRates;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
GrowthRateLibrary(size_t initialCapacity = 10)
|
GrowthRateLibrary(size_t initialCapacity = 10)
|
||||||
: _growthRates(std::unordered_map<Arbutils::CaseInsensitiveConstString, GrowthRate*>(initialCapacity)) {}
|
: _growthRates(std::unordered_map<uint32_t, GrowthRate*>(initialCapacity)) {}
|
||||||
|
|
||||||
virtual ~GrowthRateLibrary() {
|
virtual ~GrowthRateLibrary() {
|
||||||
for (auto gr : _growthRates) {
|
for (auto gr : _growthRates) {
|
||||||
|
@ -22,12 +24,13 @@ namespace CreatureLib::Library {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] uint8_t CalculateLevel(const Arbutils::CaseInsensitiveConstString& growthRate,
|
[[nodiscard]] uint8_t CalculateLevel(const ConstString& growthRate, uint32_t experience) const;
|
||||||
uint32_t experience) const;
|
[[nodiscard]] uint8_t CalculateLevel(uint32_t hash, uint32_t experience) const;
|
||||||
[[nodiscard]] uint32_t CalculateExperience(const Arbutils::CaseInsensitiveConstString& growthRate,
|
[[nodiscard]] uint32_t CalculateExperience(const ConstString& growthRate, uint8_t level) const;
|
||||||
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);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue