81 lines
3.0 KiB
C++
81 lines
3.0 KiB
C++
#ifndef CREATURELIB_BASELIBRARY_HPP
|
|
#define CREATURELIB_BASELIBRARY_HPP
|
|
|
|
#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>
|
|
|
|
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), _listValues(initialCapacity) {}
|
|
|
|
virtual ~BaseLibrary() {
|
|
for (const auto& v : _values) {
|
|
delete v.second;
|
|
}
|
|
_values.Clear();
|
|
}
|
|
|
|
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());
|
|
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);
|
|
}
|
|
bool TryGet(uint32_t hashedKey, const T*& out) const { return _values.TryGet(hashedKey, out); }
|
|
|
|
[[nodiscard]] inline const T* Get(const Arbutils::CaseInsensitiveConstString& name) const {
|
|
return _values.Get(name.GetHash());
|
|
}
|
|
[[nodiscard]] inline const T* Get(uint32_t hashedKey) const { return _values.Get(hashedKey); }
|
|
|
|
[[nodiscard]] inline const T* operator[](const Arbutils::CaseInsensitiveConstString& name) const {
|
|
return Get(name);
|
|
}
|
|
[[nodiscard]] inline const T* operator[](uint32_t hashedKey) const { return Get(hashedKey); }
|
|
[[nodiscard]] inline const Arbutils::Collections::Dictionary<uint32_t, const T*>& GetIterator() const {
|
|
return _values;
|
|
}
|
|
|
|
[[nodiscard]] size_t GetCount() const { return _values.Count(); }
|
|
|
|
inline const T* GetRandomValue(Arbutils::Random rand = Arbutils::Random()) const {
|
|
auto i = rand.Get(_listValues.Count());
|
|
return _values[_listValues[i]];
|
|
}
|
|
inline const T* GetRandomValue(Arbutils::Random* rand) const {
|
|
auto i = rand->Get(_listValues.Count());
|
|
return _values[_listValues[i]];
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif // CREATURELIB_BASELIBRARY_HPP
|