Implements ConstString in several core places in the library, improving performance.
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2020-02-27 18:23:23 +01:00
parent 1d3a8da99e
commit 412e0a4d63
17 changed files with 161 additions and 148 deletions

View File

@@ -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});
}

View File

@@ -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);
};
}