#ifndef CREATURELIB_BASELIBRARY_HPP #define CREATURELIB_BASELIBRARY_HPP #include #include #include #include #include #include #include namespace CreatureLib::Library { template class BaseLibrary { Arbutils::Collections::Dictionary _values; Arbutils::Collections::List _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& 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