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

@@ -5,10 +5,10 @@ CreatureLib::Library::AttackData::AttackData(std::string name, uint8_t type,
CreatureLib::Library::AttackCategory category, uint8_t power,
uint8_t accuracy, uint8_t baseUsage,
CreatureLib::Library::AttackTarget target, int8_t priority,
std::unordered_set<std::string> flags)
std::unordered_set<Arbutils::CaseInsensitiveConstString> flags)
: _name(std::move(name)), _type(type), _category(category), _basePower(power), _accuracy(accuracy),
_baseUsages(baseUsage), _target(target), _priority(priority), _flags(std::move(flags)) {}
bool CreatureLib::Library::AttackData::HasFlag(const std::string& key) const {
bool CreatureLib::Library::AttackData::HasFlag(const Arbutils::CaseInsensitiveConstString& key) const {
return this->_flags.find(key) != this->_flags.end();
}

View File

@@ -1,6 +1,7 @@
#ifndef CREATURELIB_ATTACKDATA_HPP
#define CREATURELIB_ATTACKDATA_HPP
#include <Arbutils/ConstString.hpp>
#include <string>
#include <unordered_set>
#include "AttackCategory.hpp"
@@ -17,11 +18,12 @@ namespace CreatureLib::Library {
uint8_t _baseUsages;
AttackTarget _target;
int8_t _priority;
std::unordered_set<std::string> _flags;
std::unordered_set<Arbutils::CaseInsensitiveConstString> _flags;
public:
AttackData(std::string name, uint8_t type, AttackCategory category, uint8_t power, uint8_t accuracy,
uint8_t baseUsage, AttackTarget target, int8_t priority, std::unordered_set<std::string> flags);
uint8_t baseUsage, AttackTarget target, int8_t priority,
std::unordered_set<Arbutils::CaseInsensitiveConstString> flags);
virtual ~AttackData() = default;
inline const std::string& GetName() const { return _name; }
@@ -33,7 +35,7 @@ namespace CreatureLib::Library {
inline AttackTarget GetTarget() const { return _target; }
inline int8_t GetPriority() const { return _priority; }
bool HasFlag(const std::string& key) const;
bool HasFlag(const Arbutils::CaseInsensitiveConstString& key) const;
};
}

View File

@@ -1,16 +1,13 @@
#ifndef CREATURELIB_BASELIBRARY_HPP
#define CREATURELIB_BASELIBRARY_HPP
#include <Arbutils/ConstString.hpp>
#include <algorithm>
#include <string>
#include <unordered_map>
namespace CreatureLib::Library {
template <class T> class BaseLibrary {
inline static constexpr char charToLower(const char c) { return (c >= 'A' && c <= 'Z') ? c + ('a' - 'A') : c; }
inline static uint32_t constexpr Hash(char const* input) {
return charToLower(*input) ? static_cast<uint32_t>(charToLower(*input)) + 33 * Hash(input + 1) : 5381;
}
std::unordered_map<uint32_t, const T*> _values;
public:
@@ -23,14 +20,13 @@ namespace CreatureLib::Library {
_values.clear();
}
inline void Insert(const char* key, const T* value) { _values.insert({Hash(key), value}); }
inline void Insert(const std::string& key, const T* value) { Insert(key.c_str(), value); }
inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) {
_values.insert({key.GetHash(), value});
}
inline void Delete(const Arbutils::CaseInsensitiveConstString& key) { _values.erase(key.GetHash()); }
inline void Delete(const char* key) { _values.erase(Hash(key)); }
inline void Delete(const std::string& key) { Delete(key.c_str()); }
bool TryGet(const char* name, const T*& out) const {
auto find = this->_values.find(Hash(name));
bool TryGet(const Arbutils::CaseInsensitiveConstString& name, const T*& out) const {
auto find = this->_values.find(name.GetHash());
if (find == this->_values.end()) {
out = nullptr;
return false;
@@ -38,16 +34,10 @@ namespace CreatureLib::Library {
out = find->second;
return true;
}
bool TryGet(const std::string& name, const T*& out) const {
return TryGet(name.c_str(), out);
inline const T* Get(const Arbutils::CaseInsensitiveConstString& name) const {
return _values.at(name.GetHash());
}
inline const T* Get(const char* name) const { return _values.at(Hash(name)); }
inline const T* Get(const std::string& name) const { return Get(name.c_str()); }
inline const T* operator[](const char* name) const { return Get(name); }
inline const T* operator[](const std::string& name) const { return Get(name.c_str()); }
inline const T* operator[](const Arbutils::CaseInsensitiveConstString& name) const { return Get(name); }
inline const std::unordered_map<uint32_t, const T*>& GetIterator() const { return _values; }
size_t GetCount() const { return _values.size(); }

View File

@@ -2,21 +2,19 @@
using namespace CreatureLib::Library;
CreatureSpecies::CreatureSpecies(uint16_t id, std::string name, const SpeciesVariant* defaultVariant, float genderRatio,
std::string growthRate, uint8_t captureRate)
: _id(id), _genderRate(genderRatio), _growthRate(std::move(growthRate)), _captureRate(captureRate),
_variants({{"default", defaultVariant}}), _name(std::move(name)) {}
CreatureSpecies::CreatureSpecies(uint16_t id, const Arbutils::CaseInsensitiveConstString& name,
const SpeciesVariant* defaultVariant, float genderRatio,
const Arbutils::CaseInsensitiveConstString& growthRate, uint8_t captureRate)
: _id(id), _genderRate(genderRatio), _growthRate(growthRate), _captureRate(captureRate),
_variants({{"default"_cnc, defaultVariant}}), _name(name) {}
bool CreatureSpecies::HasVariant(const std::string& name) const {
auto key = name;
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
return _variants.find(key) != _variants.end();
bool CreatureSpecies::HasVariant(const Arbutils::CaseInsensitiveConstString& name) const {
return _variants.find(name) != _variants.end();
}
bool CreatureSpecies::TryGetVariant(const std::string& name, const SpeciesVariant*& out) const {
auto key = name;
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
auto find = _variants.find(key);
bool CreatureSpecies::TryGetVariant(const Arbutils::CaseInsensitiveConstString& name,
const SpeciesVariant*& out) const {
auto find = _variants.find(name);
if (find != _variants.end()) {
out = find->second;
return true;
@@ -24,20 +22,17 @@ bool CreatureSpecies::TryGetVariant(const std::string& name, const SpeciesVarian
return false;
}
const SpeciesVariant* CreatureSpecies::GetVariant(const std::string& name) const {
const SpeciesVariant* CreatureSpecies::GetVariant(const Arbutils::CaseInsensitiveConstString& name) const {
auto key = name;
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
return _variants.at(key);
}
void CreatureSpecies::SetVariant(const std::string& name, const SpeciesVariant* variant) {
auto key = name;
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
auto find = _variants.find(key);
void CreatureSpecies::SetVariant(const Arbutils::CaseInsensitiveConstString& name, const SpeciesVariant* variant) {
auto find = _variants.find(name);
if (find != _variants.end()) {
delete find->second;
}
_variants[key] = variant;
_variants[name] = variant;
}
Gender CreatureSpecies::GetRandomGender(Arbutils::Random& rand) const {
@@ -46,6 +41,4 @@ Gender CreatureSpecies::GetRandomGender(Arbutils::Random& rand) const {
if (val >= this->_genderRate)
return Gender ::Female;
return Gender ::Male;
}
const std::string& CreatureSpecies::GetName() const { return _name; }
}

View File

@@ -1,6 +1,7 @@
#ifndef CREATURELIB_CREATURESPECIES_HPP
#define CREATURELIB_CREATURESPECIES_HPP
#include <Arbutils/ConstString.hpp>
#include <string>
#include <unordered_map>
#include "../Gender.hpp"
@@ -14,16 +15,17 @@ namespace CreatureLib::Library {
class CreatureSpecies {
uint16_t _id;
float _genderRate;
std::string _growthRate;
const Arbutils::CaseInsensitiveConstString _growthRate;
uint8_t _captureRate;
private:
std::unordered_map<std::string, const SpeciesVariant*> _variants;
std::string _name;
std::unordered_map<Arbutils::CaseInsensitiveConstString, const SpeciesVariant*> _variants;
Arbutils::CaseInsensitiveConstString _name;
public:
CreatureSpecies(uint16_t id, std::string name, const SpeciesVariant* defaultVariant, float genderRatio,
std::string growthRate, uint8_t captureRate);
CreatureSpecies(uint16_t id, const Arbutils::CaseInsensitiveConstString& name,
const SpeciesVariant* defaultVariant, float genderRatio,
const Arbutils::CaseInsensitiveConstString& growthRate, uint8_t captureRate);
virtual ~CreatureSpecies() {
for (auto v : _variants)
@@ -33,18 +35,22 @@ namespace CreatureLib::Library {
inline uint16_t GetId() const { return _id; }
inline float GetGenderRate() const { return _genderRate; }
inline const std::string& GetGrowthRate() const { return _growthRate; }
inline const Arbutils::CaseInsensitiveConstString& GetGrowthRate() const { return _growthRate; }
inline uint8_t GetCaptureRate() const { return _captureRate; }
[[nodiscard]] bool HasVariant(const std::string& key) const;
[[nodiscard]] bool TryGetVariant(const std::string& name, const SpeciesVariant*& out) const;
[[nodiscard]] const SpeciesVariant* GetVariant(const std::string& key) const;
[[nodiscard]] bool HasVariant(const Arbutils::CaseInsensitiveConstString& key) const;
[[nodiscard]] bool TryGetVariant(const Arbutils::CaseInsensitiveConstString& name,
const SpeciesVariant*& out) const;
[[nodiscard]] const SpeciesVariant* GetVariant(const Arbutils::CaseInsensitiveConstString& key) const;
[[nodiscard]] Gender GetRandomGender(Arbutils::Random& rand) const;
[[nodiscard]] const std::string& GetName() const;
[[nodiscard]] const Arbutils::CaseInsensitiveConstString& GetName() const { return _name; }
void SetVariant(const std::string& name, const SpeciesVariant* variant);
void SetVariant(const Arbutils::CaseInsensitiveConstString& name, const SpeciesVariant* variant);
const std::unordered_map<std::string, const SpeciesVariant*>& GetVariantsIterator() const { return _variants; }
const std::unordered_map<Arbutils::CaseInsensitiveConstString, const SpeciesVariant*>&
GetVariantsIterator() const {
return _variants;
}
};
}

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

View File

@@ -1,9 +1,9 @@
#include "Item.hpp"
bool CreatureLib::Library::Item::HasFlag(const std::string& flag) const {
bool CreatureLib::Library::Item::HasFlag(const Arbutils::CaseInsensitiveConstString& flag) const {
return this->_flags.find(flag) != this->_flags.end();
}
CreatureLib::Library::Item::Item(std::string name, CreatureLib::Library::ItemCategory category,
CreatureLib::Library::BattleItemCategory battleCategory, int32_t price,
std::unordered_set<std::string> flags)
std::unordered_set<Arbutils::CaseInsensitiveConstString> flags)
: _name(name), _category(category), _battleCategory(battleCategory), _price(price), _flags(flags) {}

View File

@@ -1,6 +1,7 @@
#ifndef CREATURELIB_ITEM_HPP
#define CREATURELIB_ITEM_HPP
#include <Arbutils/ConstString.hpp>
#include <string>
#include <unordered_set>
#include "BattleItemCategory.hpp"
@@ -13,18 +14,18 @@ namespace CreatureLib::Library {
ItemCategory _category;
BattleItemCategory _battleCategory;
int32_t _price;
std::unordered_set<std::string> _flags;
std::unordered_set<Arbutils::CaseInsensitiveConstString> _flags;
public:
Item(std::string name, ItemCategory category, BattleItemCategory battleCategory, int32_t price,
std::unordered_set<std::string> flags);
std::unordered_set<Arbutils::CaseInsensitiveConstString> flags);
inline const std::string& GetName() const { return _name; }
inline ItemCategory GetCategory() const { return _category; }
inline BattleItemCategory GetBattleCategory() const { return _battleCategory; }
inline const int32_t GetPrice() const { return _price; }
bool HasFlag(const std::string& flag) const;
bool HasFlag(const Arbutils::CaseInsensitiveConstString& flag) const;
};
}