Revers change to Dictionary using pointer.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
f4a9059150
commit
84dd686b61
|
@ -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,12 +20,11 @@ namespace ArbUt {
|
|||
/// @brief The type used for the value in the dictionary.
|
||||
typedef ValueT valueType;
|
||||
|
||||
Dictionary() : _map(new std::unordered_map<KeyT, ValueT>()) {}
|
||||
Dictionary() : _map() {}
|
||||
/// @brief Initialises a dictionary with a certain capacity.
|
||||
explicit Dictionary(size_t capacity) : _map(new std::unordered_map<KeyT, ValueT>(capacity)) {}
|
||||
explicit Dictionary(size_t capacity) : _map(capacity) {}
|
||||
/// @brief Initialises a dictionary from an initializer_list.
|
||||
Dictionary(const std::initializer_list<std::pair<const KeyT, ValueT>>& l)
|
||||
: _map(new std::unordered_map<KeyT, ValueT>(l)) {}
|
||||
Dictionary(const std::initializer_list<std::pair<const KeyT, ValueT>>& l) : _map(l) {}
|
||||
/// @brief Copy operator
|
||||
Dictionary(const Dictionary& other) noexcept : _map(other._map) {}
|
||||
/// @brief Assignment operator
|
||||
|
@ -37,14 +36,12 @@ namespace ArbUt {
|
|||
return *this;
|
||||
}
|
||||
|
||||
~Dictionary() noexcept { delete _map; }
|
||||
|
||||
/// @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");
|
||||
|
@ -52,37 +49,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); }
|
||||
|
@ -90,19 +87,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; }
|
||||
};
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue