#ifndef ARBUTILS_DICTIONARY_HPP #define ARBUTILS_DICTIONARY_HPP #include #include #include "../Assert.hpp" namespace ArbUt::Collections { template class Dictionary { private: std::unordered_map _map; using iterator = typename std::unordered_map::iterator; using const_iterator = typename std::unordered_map::const_iterator; public: Dictionary() : _map() {} explicit Dictionary(size_t capacity) : _map(capacity) {} explicit Dictionary(const std::initializer_list>& l) : _map(l) {} inline void Clear() { _map.clear(); } inline void Insert(const KeyT& key, const ValueT& value) { [[maybe_unused]] const auto& v = _map.insert({key, value}); #ifndef NO_ASSERT if (!v.second) throw std::logic_error("Key already exists"); #endif } inline void Set(const KeyT& key, const ValueT& value) { _map[key] = value; } [[nodiscard]] inline ValueT& Get(const KeyT& key) { #ifndef NO_ASSERT return _map.at(key); #else return _map[key]; #endif } [[nodiscard]] inline const ValueT& Get(const KeyT& key) const { return _map.at(key); } inline bool TryGet(const KeyT& key, ValueT& out) const { const auto& find = _map.find(key); if (find == _map.end()) { return false; } out = find->second; return true; } inline void Remove(const KeyT& key) { _map.erase(key); } [[nodiscard]] inline size_t Count() const { return _map.size(); } inline bool Has(const KeyT& key) const noexcept { return _map.find(key) != _map.end(); } inline ValueT& operator[](const KeyT& key) { return Get(key); } inline const ValueT& operator[](const KeyT& key) const { return Get(key); } iterator begin() { return _map.begin(); } const_iterator begin() const { return _map.begin(); } iterator end() { return _map.end(); } const_iterator end() const { return _map.end(); } const std::unordered_map& GetStdMap() const { return _map; } std::unordered_map& GetStdMap() { return _map; } }; } #endif // ARBUTILS_DICTIONARY_HPP