Performance improvements for getting random value from libraries.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-04-24 18:24:51 +02:00
parent dd668f2b1c
commit 3ad21fecc2
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
1 changed files with 19 additions and 10 deletions

View File

@ -3,18 +3,19 @@
#include <Arbutils/Assert.hpp> #include <Arbutils/Assert.hpp>
#include <Arbutils/Collections/Dictionary.hpp> #include <Arbutils/Collections/Dictionary.hpp>
#include <Arbutils/Collections/List.hpp>
#include <Arbutils/ConstString.hpp> #include <Arbutils/ConstString.hpp>
#include <Arbutils/Random.hpp> #include <Arbutils/Random.hpp>
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <unordered_map>
namespace CreatureLib::Library { namespace CreatureLib::Library {
template <class T> class BaseLibrary { template <class T> class BaseLibrary {
Arbutils::Collections::Dictionary<uint32_t, const T*> _values; Arbutils::Collections::Dictionary<uint32_t, const T*> _values;
Arbutils::Collections::List<uint32_t> _listValues;
public: public:
BaseLibrary(size_t initialCapacity = 32) : _values(initialCapacity) {} BaseLibrary(size_t initialCapacity = 32) : _values(initialCapacity), _listValues(initialCapacity) {}
virtual ~BaseLibrary() { virtual ~BaseLibrary() {
for (const auto& v : _values) { for (const auto& v : _values) {
@ -26,14 +27,24 @@ namespace CreatureLib::Library {
inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) { inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) {
AssertNotNull(value) AssertNotNull(value)
_values.Insert(key.GetHash(), value); _values.Insert(key.GetHash(), value);
_listValues.Append(key);
} }
inline void Insert(uint32_t hashedKey, const T* value) { inline void Insert(uint32_t hashedKey, const T* value) {
AssertNotNull(value) AssertNotNull(value)
_values.Insert(hashedKey, value); _values.Insert(hashedKey, value);
_listValues.Append(hashedKey);
} }
inline void Delete(const Arbutils::CaseInsensitiveConstString& key) { _values.erase(key.GetHash()); } inline void Delete(const Arbutils::CaseInsensitiveConstString& key) {
inline void Delete(uint32_t hashedKey) { _values.Remove(hashedKey); } _values.erase(key.GetHash());
auto k = _listValues.IndexOf(key);
_listValues.Remove(k);
}
inline void Delete(uint32_t hashedKey) {
_values.Remove(hashedKey);
auto k = _listValues.IndexOf(hashedKey);
_listValues.Remove(k);
}
bool TryGet(const Arbutils::CaseInsensitiveConstString& name, const T*& out) const { bool TryGet(const Arbutils::CaseInsensitiveConstString& name, const T*& out) const {
return TryGet(name.GetHash(), out); return TryGet(name.GetHash(), out);
@ -56,14 +67,12 @@ namespace CreatureLib::Library {
[[nodiscard]] size_t GetCount() const { return _values.Count(); } [[nodiscard]] size_t GetCount() const { return _values.Count(); }
inline const T* GetRandomValue(Arbutils::Random rand = Arbutils::Random()) const { inline const T* GetRandomValue(Arbutils::Random rand = Arbutils::Random()) const {
auto i = rand.Get(_values.Count()); auto i = rand.Get(_listValues.Count());
auto& map = _values.GetStdMap(); return _values[_listValues[i]];
return std::next(std::begin(map), i)->second;
} }
inline const T* GetRandomValue(Arbutils::Random* rand) const { inline const T* GetRandomValue(Arbutils::Random* rand) const {
auto i = rand->Get(_values.Count()); auto i = rand->Get(_listValues.Count());
auto& map = _values.GetStdMap(); return _values[_listValues[i]];
return std::next(std::begin(map), i)->second;
} }
}; };
} }