Performance improvements for getting random value from libraries.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
dd668f2b1c
commit
3ad21fecc2
|
@ -3,18 +3,19 @@
|
|||
|
||||
#include <Arbutils/Assert.hpp>
|
||||
#include <Arbutils/Collections/Dictionary.hpp>
|
||||
#include <Arbutils/Collections/List.hpp>
|
||||
#include <Arbutils/ConstString.hpp>
|
||||
#include <Arbutils/Random.hpp>
|
||||
#include <algorithm>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace CreatureLib::Library {
|
||||
template <class T> class BaseLibrary {
|
||||
Arbutils::Collections::Dictionary<uint32_t, const T*> _values;
|
||||
Arbutils::Collections::List<uint32_t> _listValues;
|
||||
|
||||
public:
|
||||
BaseLibrary(size_t initialCapacity = 32) : _values(initialCapacity) {}
|
||||
BaseLibrary(size_t initialCapacity = 32) : _values(initialCapacity), _listValues(initialCapacity) {}
|
||||
|
||||
virtual ~BaseLibrary() {
|
||||
for (const auto& v : _values) {
|
||||
|
@ -26,14 +27,24 @@ namespace CreatureLib::Library {
|
|||
inline void Insert(const Arbutils::CaseInsensitiveConstString& key, const T* value) {
|
||||
AssertNotNull(value)
|
||||
_values.Insert(key.GetHash(), value);
|
||||
_listValues.Append(key);
|
||||
}
|
||||
inline void Insert(uint32_t hashedKey, const T* value) {
|
||||
AssertNotNull(value)
|
||||
_values.Insert(hashedKey, value);
|
||||
_listValues.Append(hashedKey);
|
||||
}
|
||||
|
||||
inline void Delete(const Arbutils::CaseInsensitiveConstString& key) { _values.erase(key.GetHash()); }
|
||||
inline void Delete(uint32_t hashedKey) { _values.Remove(hashedKey); }
|
||||
inline void Delete(const Arbutils::CaseInsensitiveConstString& key) {
|
||||
_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 {
|
||||
return TryGet(name.GetHash(), out);
|
||||
|
@ -56,14 +67,12 @@ namespace CreatureLib::Library {
|
|||
[[nodiscard]] size_t GetCount() const { return _values.Count(); }
|
||||
|
||||
inline const T* GetRandomValue(Arbutils::Random rand = Arbutils::Random()) const {
|
||||
auto i = rand.Get(_values.Count());
|
||||
auto& map = _values.GetStdMap();
|
||||
return std::next(std::begin(map), i)->second;
|
||||
auto i = rand.Get(_listValues.Count());
|
||||
return _values[_listValues[i]];
|
||||
}
|
||||
inline const T* GetRandomValue(Arbutils::Random* rand) const {
|
||||
auto i = rand->Get(_values.Count());
|
||||
auto& map = _values.GetStdMap();
|
||||
return std::next(std::begin(map), i)->second;
|
||||
auto i = rand->Get(_listValues.Count());
|
||||
return _values[_listValues[i]];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue