Implements ConstString in several core places in the library, improving performance.
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -2,30 +2,25 @@
|
||||
#include <algorithm>
|
||||
#include "../../Core/Exceptions/CreatureException.hpp"
|
||||
|
||||
uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(const std::string& growthRate,
|
||||
uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(const Arbutils::CaseInsensitiveConstString& growthRate,
|
||||
uint32_t experience) const {
|
||||
auto g = growthRate;
|
||||
std::transform(g.begin(), g.end(), g.begin(), ::tolower);
|
||||
auto find = _growthRates.find(g);
|
||||
auto find = _growthRates.find(growthRate);
|
||||
if (find == _growthRates.end()) {
|
||||
throw CreatureException("Invalid growth rate was requested.");
|
||||
}
|
||||
return find->second->CalculateLevel(experience);
|
||||
}
|
||||
|
||||
uint32_t CreatureLib::Library::GrowthRateLibrary::CalculateExperience(const std::string& growthRate,
|
||||
uint8_t level) const {
|
||||
auto g = growthRate;
|
||||
std::transform(g.begin(), g.end(), g.begin(), ::tolower);
|
||||
auto find = _growthRates.find(g);
|
||||
uint32_t
|
||||
CreatureLib::Library::GrowthRateLibrary::CalculateExperience(const Arbutils::CaseInsensitiveConstString& 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 std::string& name,
|
||||
void CreatureLib::Library::GrowthRateLibrary::AddGrowthRate(const Arbutils::CaseInsensitiveConstString& name,
|
||||
CreatureLib::Library::GrowthRate* rate) {
|
||||
auto g = name;
|
||||
std::transform(g.begin(), g.end(), g.begin(), ::tolower);
|
||||
_growthRates.insert({g, rate});
|
||||
_growthRates.insert({name, rate});
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#ifndef CREATURELIB_GROWTHRATELIBRARY_HPP
|
||||
#define CREATURELIB_GROWTHRATELIBRARY_HPP
|
||||
|
||||
#include <Arbutils/ConstString.hpp>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
@@ -9,11 +10,11 @@
|
||||
namespace CreatureLib::Library {
|
||||
class GrowthRateLibrary {
|
||||
private:
|
||||
std::unordered_map<std::string, GrowthRate*> _growthRates;
|
||||
std::unordered_map<Arbutils::CaseInsensitiveConstString, GrowthRate*> _growthRates;
|
||||
|
||||
public:
|
||||
GrowthRateLibrary(size_t initialCapacity = 10)
|
||||
: _growthRates(std::unordered_map<std::string, GrowthRate*>(initialCapacity)) {}
|
||||
: _growthRates(std::unordered_map<Arbutils::CaseInsensitiveConstString, GrowthRate*>(initialCapacity)) {}
|
||||
|
||||
virtual ~GrowthRateLibrary() {
|
||||
for (auto gr : _growthRates) {
|
||||
@@ -21,10 +22,12 @@ namespace CreatureLib::Library {
|
||||
}
|
||||
}
|
||||
|
||||
[[nodiscard]] uint8_t CalculateLevel(const std::string& growthRate, uint32_t experience) const;
|
||||
[[nodiscard]] uint32_t CalculateExperience(const std::string& growthRate, uint8_t level) const;
|
||||
[[nodiscard]] uint8_t CalculateLevel(const Arbutils::CaseInsensitiveConstString& growthRate,
|
||||
uint32_t experience) const;
|
||||
[[nodiscard]] uint32_t CalculateExperience(const Arbutils::CaseInsensitiveConstString& growthRate,
|
||||
uint8_t level) const;
|
||||
|
||||
void AddGrowthRate(const std::string& name, GrowthRate* rate);
|
||||
void AddGrowthRate(const Arbutils::CaseInsensitiveConstString& name, GrowthRate* rate);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user