91 lines
3.6 KiB
C++
91 lines
3.6 KiB
C++
#ifndef CREATURELIB_BASELIBRARY_HPP
|
|
#define CREATURELIB_BASELIBRARY_HPP
|
|
|
|
#include <Arbutils/Random.hpp>
|
|
|
|
namespace CreatureLib::Library {
|
|
template <class T> class BaseLibrary {
|
|
protected:
|
|
ArbUt::Dictionary<uint32_t, std::unique_ptr<const T>> _values;
|
|
ArbUt::List<uint32_t> _listValues;
|
|
|
|
public:
|
|
BaseLibrary(size_t initialCapacity = 32) : _values(initialCapacity), _listValues(initialCapacity) {}
|
|
|
|
virtual ~BaseLibrary() noexcept { _values.Clear(); }
|
|
|
|
inline virtual void Insert(const ArbUt::StringView& key, const T* value) {
|
|
EnsureNotNull(value)
|
|
_values.GetStdMap().insert({key.GetHash(), std::unique_ptr<const T>(value)});
|
|
_listValues.Append(key);
|
|
}
|
|
inline virtual void Insert(uint32_t hashedKey, const T* value) {
|
|
EnsureNotNull(value)
|
|
_values.GetStdMap().insert({hashedKey, std::unique_ptr<const T>(value)});
|
|
_listValues.Append(hashedKey);
|
|
}
|
|
|
|
inline void Delete(const ArbUt::StringView& key) noexcept {
|
|
_values.erase(key.GetHash());
|
|
auto k = _listValues.IndexOf(key);
|
|
_listValues.Remove(k);
|
|
}
|
|
inline void Delete(uint32_t hashedKey) noexcept {
|
|
_values.Remove(hashedKey);
|
|
auto k = _listValues.IndexOf(hashedKey);
|
|
_listValues.Remove(k);
|
|
}
|
|
|
|
std::optional<ArbUt::BorrowedPtr<const T>> TryGet(const ArbUt::BasicStringView& name) const noexcept {
|
|
return TryGet(name.GetHash());
|
|
}
|
|
std::optional<ArbUt::BorrowedPtr<const T>> TryGet(uint32_t hashedKey) const noexcept {
|
|
auto find = _values.GetStdMap().find(hashedKey);
|
|
if (find == _values.GetStdMap().end())
|
|
return {};
|
|
return std::get<1>(*find);
|
|
}
|
|
|
|
[[nodiscard]] inline ArbUt::BorrowedPtr<const T> Get(const ArbUt::BasicStringView& name) const {
|
|
return _values.Get(name.GetHash());
|
|
}
|
|
[[nodiscard]] inline ArbUt::BorrowedPtr<const T> Get(uint32_t hashedKey) const {
|
|
return _values.Get(hashedKey);
|
|
}
|
|
|
|
[[nodiscard]] inline ArbUt::BorrowedPtr<const T> operator[](const ArbUt::BasicStringView& name) const {
|
|
return Get(name);
|
|
}
|
|
[[nodiscard]] inline ArbUt::BorrowedPtr<const T> operator[](uint32_t hashedKey) const { return Get(hashedKey); }
|
|
[[nodiscard]] inline const ArbUt::Dictionary<uint32_t, const std::unique_ptr<const T>>&
|
|
GetIterator() const noexcept {
|
|
return _values;
|
|
}
|
|
|
|
using const_iterator = typename std::unordered_map<uint32_t, ArbUt::BorrowedPtr<const T>>::const_iterator;
|
|
|
|
inline const_iterator begin() const {
|
|
return reinterpret_cast<const std::unordered_map<uint32_t, ArbUt::BorrowedPtr<const T>>&>(
|
|
_values.GetStdMap())
|
|
.begin();
|
|
}
|
|
inline const_iterator end() const {
|
|
return reinterpret_cast<const std::unordered_map<uint32_t, ArbUt::BorrowedPtr<const T>>&>(
|
|
_values.GetStdMap())
|
|
.end();
|
|
}
|
|
|
|
[[nodiscard]] size_t GetCount() const noexcept { return _values.Count(); }
|
|
ArbUt::BorrowedPtr<const T> GetAtIndex(size_t index) const {
|
|
return std::next(std::begin(_values), index)->second;
|
|
}
|
|
|
|
inline ArbUt::BorrowedPtr<const T> GetRandomValue(ArbUt::Random& rand) const noexcept {
|
|
auto i = rand.Get(_listValues.Count());
|
|
return _values[_listValues[i]];
|
|
}
|
|
};
|
|
}
|
|
|
|
#endif // CREATURELIB_BASELIBRARY_HPP
|