Make Dictionary use pointer to map
continuous-integration/drone/push Build is failing Details

This commit is contained in:
Deukhoofd 2022-03-20 11:34:24 +01:00
parent 97babe79e1
commit 74e3cb36a7
Signed by: Deukhoofd
GPG Key ID: F63E044490819F6F
1 changed files with 22 additions and 21 deletions

View File

@ -9,7 +9,7 @@ namespace ArbUt {
/// @brief Wrapper around unordered_map, allowing safer access and adding several helper methods.
template <class KeyT, class ValueT> class Dictionary {
private:
std::unordered_map<KeyT, ValueT> _map;
std::unordered_map<KeyT, ValueT>* _map;
using iterator = typename std::unordered_map<KeyT, ValueT>::iterator;
using const_iterator = typename std::unordered_map<KeyT, ValueT>::const_iterator;
@ -20,18 +20,19 @@ namespace ArbUt {
/// @brief The type used for the value in the dictionary.
typedef ValueT valueType;
Dictionary() : _map() {}
Dictionary() : _map(new std::unordered_map<KeyT, ValueT>()) {}
/// @brief Initialises a dictionary with a certain capacity.
explicit Dictionary(size_t capacity) : _map(capacity) {}
explicit Dictionary(size_t capacity) : _map(new std::unordered_map<KeyT, ValueT>(capacity)) {}
/// @brief Initialises a dictionary from an initializer_list.
Dictionary(const std::initializer_list<std::pair<const KeyT, ValueT>>& l) : _map(l) {}
Dictionary(const std::initializer_list<std::pair<const KeyT, ValueT>>& l)
: _map(new std::unordered_map<KeyT, ValueT>(l)) {}
/// @brief Removes all items from the dictionary.
inline void Clear() noexcept { _map.clear(); }
inline void Clear() noexcept { _map->clear(); }
/// @brief Inserts a new item in the dictionary. This will throw if the dictionary already contains the key.
inline void Insert(const KeyT& key, const ValueT& value) {
[[maybe_unused]] const auto& v = _map.insert({key, value});
[[maybe_unused]] const auto& v = _map->insert({key, value});
#ifndef NO_ASSERT
if (!v.second)
throw ArbUt::Exception("Key already exists");
@ -39,37 +40,37 @@ namespace ArbUt {
}
/// @brief Sets a key in the dictionary to a specific value.
inline void Set(const KeyT& key, const ValueT& value) { _map[key] = value; }
inline void Set(const KeyT& key, const ValueT& value) { (*_map)[key] = value; }
/// @brief Gets a value from the dictionary.
[[nodiscard]] inline ValueT& Get(const KeyT& key) {
#ifndef NO_ASSERT
return _map.at(key);
return _map->at(key);
#else
return _map[key];
return (*_map)[key];
#endif
}
/// @brief Gets a value from the dictionary.
[[nodiscard]] inline const ValueT& Get(const KeyT& key) const { return _map.at(key); }
[[nodiscard]] inline const ValueT& Get(const KeyT& key) const { return _map->at(key); }
/// @brief Try to get an item from the dictionary using a key. Returns false if no item is found, and out will
/// not be touched in that case.
inline std::optional<std::reference_wrapper<const ValueT>> TryGet(const KeyT& key) const noexcept {
const auto& find = _map.find(key);
if (find == _map.end()) {
const auto& find = _map->find(key);
if (find == _map->end()) {
return {};
}
return std::ref(find->second);
}
/// @brief Removes an item with a certain key from the dictionary
inline void Remove(const KeyT& key) { _map.erase(key); }
inline void Remove(const KeyT& key) { _map->erase(key); }
/// @brief Returns the number of items in the dictionary.
[[nodiscard]] inline size_t Count() const noexcept { return _map.size(); }
[[nodiscard]] inline size_t Count() const noexcept { return _map->size(); }
/// @brief Checks whether the dictionary contains a specific key.
inline bool Has(const KeyT& key) const noexcept { return _map.find(key) != _map.end(); }
inline bool Has(const KeyT& key) const noexcept { return _map->find(key) != _map->end(); }
/// @brief Indexing operator to get a value from the dictionary using a key.
inline ValueT& operator[](const KeyT& key) { return Get(key); }
@ -77,19 +78,19 @@ namespace ArbUt {
inline const ValueT& operator[](const KeyT& key) const { return Get(key); }
/// @brief returns an iterator to the beginning of the specified bucket
iterator begin() noexcept { return _map.begin(); }
iterator begin() noexcept { return _map->begin(); }
/// @brief returns an iterator to the beginning of the specified bucket
const_iterator begin() const noexcept { return _map.begin(); }
const_iterator begin() const noexcept { return _map->begin(); }
/// @brief returns an iterator to the end of the specified bucket
iterator end() noexcept { return _map.end(); }
iterator end() noexcept { return _map->end(); }
/// @brief returns an iterator to the end of the specified bucket
const_iterator end() const { return _map.end(); }
const_iterator end() const { return _map->end(); }
/// @brief returns the backing unordered_map.
const std::unordered_map<KeyT, ValueT>& GetStdMap() const noexcept { return _map; }
const std::unordered_map<KeyT, ValueT>& GetStdMap() const noexcept { return *_map; }
/// @brief returns the backing unordered_map.
std::unordered_map<KeyT, ValueT>& GetStdMap() noexcept { return _map; }
std::unordered_map<KeyT, ValueT>& GetStdMap() noexcept { return *_map; }
};
}