#ifndef CREATURELIB_BASELIBRARY_HPP #define CREATURELIB_BASELIBRARY_HPP #include namespace CreatureLib::Library { template class BaseLibrary { protected: ArbUt::Dictionary> _values; ArbUt::List _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(value)}); _listValues.Append(key); } inline virtual void Insert(uint32_t hashedKey, const T* value) { EnsureNotNull(value) _values.GetStdMap().insert({hashedKey, std::unique_ptr(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> TryGet(const ArbUt::BasicStringView& name) const noexcept { return TryGet(name.GetHash()); } std::optional> 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 Get(const ArbUt::BasicStringView& name) const { return _values.Get(name.GetHash()); } [[nodiscard]] inline ArbUt::BorrowedPtr Get(uint32_t hashedKey) const { return _values.Get(hashedKey); } [[nodiscard]] inline ArbUt::BorrowedPtr operator[](const ArbUt::BasicStringView& name) const { return Get(name); } [[nodiscard]] inline ArbUt::BorrowedPtr operator[](uint32_t hashedKey) const { return Get(hashedKey); } [[nodiscard]] inline const ArbUt::Dictionary>& GetIterator() const noexcept { return _values; } using const_iterator = typename std::unordered_map>::const_iterator; inline const_iterator begin() const { return reinterpret_cast>&>( _values.GetStdMap()) .begin(); } inline const_iterator end() const { return reinterpret_cast>&>( _values.GetStdMap()) .end(); } [[nodiscard]] size_t GetCount() const noexcept { return _values.Count(); } ArbUt::BorrowedPtr GetAtIndex(size_t index) const { return std::next(std::begin(_values), index)->second; } inline ArbUt::BorrowedPtr GetRandomValue(ArbUt::Random& rand) const noexcept { auto i = rand.Get(_listValues.Count()); return _values[_listValues[i]]; } }; } #endif // CREATURELIB_BASELIBRARY_HPP