From 4341efb54c55a39871fd911807d378fb6a8b446d Mon Sep 17 00:00:00 2001 From: Deukhoofd Date: Sat, 29 Feb 2020 15:41:43 +0100 Subject: [PATCH] Support ConstString in TypeLibrary. --- src/Library/TypeLibrary.cpp | 10 ++-------- src/Library/TypeLibrary.hpp | 10 ++++++---- tests/TestLibrary/TestLibrary.cpp | 6 +++--- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/src/Library/TypeLibrary.cpp b/src/Library/TypeLibrary.cpp index 045cb3e..b14dbff 100644 --- a/src/Library/TypeLibrary.cpp +++ b/src/Library/TypeLibrary.cpp @@ -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) { diff --git a/src/Library/TypeLibrary.hpp b/src/Library/TypeLibrary.hpp index f5aaa14..1ea0d0d 100644 --- a/src/Library/TypeLibrary.hpp +++ b/src/Library/TypeLibrary.hpp @@ -1,22 +1,24 @@ #ifndef CREATURELIB_TYPELIBRARY_HPP #define CREATURELIB_TYPELIBRARY_HPP +#include #include #include +using ConstString = Arbutils::CaseInsensitiveConstString; namespace CreatureLib::Library { class TypeLibrary { - std::unordered_map _types; + std::unordered_map _types; std::vector> _effectiveness; public: - TypeLibrary(size_t initialCapacity = 20) : _types(std::unordered_map(initialCapacity)) {} + TypeLibrary(size_t initialCapacity = 20) : _types(std::unordered_map(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& 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); }; } diff --git a/tests/TestLibrary/TestLibrary.cpp b/tests/TestLibrary/TestLibrary.cpp index 85c2c1e..8f939d1 100644 --- a/tests/TestLibrary/TestLibrary.cpp +++ b/tests/TestLibrary/TestLibrary.cpp @@ -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; }