Support ConstString in TypeLibrary.

This commit is contained in:
Deukhoofd 2020-02-29 15:41:43 +01:00
parent 4866edebab
commit 4341efb54c
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
3 changed files with 11 additions and 15 deletions

View File

@ -15,15 +15,9 @@ float TypeLibrary::GetSingleEffectiveness(uint8_t attacking, uint8_t defensive)
return _effectiveness[attacking][defensive];
}
uint8_t TypeLibrary::GetTypeId(const std::string& s) const {
std::string key = s;
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
return _types.at(key);
}
uint8_t TypeLibrary::GetTypeId(const ConstString& key) const { return _types.at(key); }
uint8_t TypeLibrary::RegisterType(const std::string& typeName) {
std::string key = typeName;
std::transform(key.begin(), key.end(), key.begin(), ::tolower);
uint8_t TypeLibrary::RegisterType(const ConstString& key) {
_types.insert({key, _types.size()});
_effectiveness.resize(_types.size());
for (auto& eff : _effectiveness) {

View File

@ -1,22 +1,24 @@
#ifndef CREATURELIB_TYPELIBRARY_HPP
#define CREATURELIB_TYPELIBRARY_HPP
#include <Arbutils/ConstString.hpp>
#include <unordered_map>
#include <vector>
using ConstString = Arbutils::CaseInsensitiveConstString;
namespace CreatureLib::Library {
class TypeLibrary {
std::unordered_map<std::string, uint8_t> _types;
std::unordered_map<ConstString, uint8_t> _types;
std::vector<std::vector<float>> _effectiveness;
public:
TypeLibrary(size_t initialCapacity = 20) : _types(std::unordered_map<std::string, uint8_t>(initialCapacity)) {}
TypeLibrary(size_t initialCapacity = 20) : _types(std::unordered_map<ConstString, uint8_t>(initialCapacity)) {}
uint8_t GetTypeId(const std::string& s) const;
uint8_t GetTypeId(const ConstString& s) const;
float GetSingleEffectiveness(uint8_t attacking, uint8_t defensive) const;
float GetEffectiveness(uint8_t attacking, const std::vector<uint8_t>& defensive) const;
uint8_t RegisterType(const std::string& typeName);
uint8_t RegisterType(const ConstString& typeName);
void SetEffectiveness(uint8_t attacking, uint8_t defensive, float effectiveness);
};
}

View File

@ -57,9 +57,9 @@ GrowthRateLibrary* TestLibrary::BuildGrowthRateLibrary() {
TypeLibrary* TestLibrary::BuildTypeLibrary() {
auto l = new TypeLibrary();
l->RegisterType("testType1");
l->RegisterType("testType2");
l->RegisterType("testType3");
l->RegisterType("testType1"_cnc);
l->RegisterType("testType2"_cnc);
l->RegisterType("testType3"_cnc);
return l;
}