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:
@@ -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(); }
|
||||
|
||||
Reference in New Issue
Block a user