Make NO_ASSERT disable a lot of Dictionary safety in exchange for a bit more speed.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-03-22 16:43:31 +01:00
parent 5de26d0866
commit e4c203027b
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
1 changed files with 11 additions and 0 deletions

View File

@ -1,6 +1,7 @@
#ifndef ARBUTILS_DICTIONARY_HPP #ifndef ARBUTILS_DICTIONARY_HPP
#define ARBUTILS_DICTIONARY_HPP #define ARBUTILS_DICTIONARY_HPP
#include <unordered_map> #include <unordered_map>
#include "../Assert.hpp"
namespace Arbutils::Collections { namespace Arbutils::Collections {
template <class KeyT, class ValueT> class Dictionary { template <class KeyT, class ValueT> class Dictionary {
@ -15,26 +16,36 @@ namespace Arbutils::Collections {
inline void Insert(KeyT key, ValueT value) { inline void Insert(KeyT key, ValueT value) {
auto v = _map.insert({key, value}); auto v = _map.insert({key, value});
#ifndef NO_ASSERT
if (!v.second) if (!v.second)
throw std::logic_error("Key already exists"); throw std::logic_error("Key already exists");
#endif
} }
inline void Set(KeyT key, ValueT value) { _map[key] = value; } inline void Set(KeyT key, ValueT value) { _map[key] = value; }
[[nodiscard]] inline ValueT& Get(KeyT key) { [[nodiscard]] inline ValueT& Get(KeyT key) {
#ifndef NO_ASSERT
auto find = _map.find(key); auto find = _map.find(key);
if (find == _map.end()) { if (find == _map.end()) {
throw std::logic_error("Key not found"); throw std::logic_error("Key not found");
} }
return find->second; return find->second;
#else
return _map[key];
#endif
} }
[[nodiscard]] inline const ValueT& Get(KeyT key) const { [[nodiscard]] inline const ValueT& Get(KeyT key) const {
#ifndef NO_ASSERT
auto find = _map.find(key); auto find = _map.find(key);
if (find == _map.end()) { if (find == _map.end()) {
throw std::logic_error("Key not found"); throw std::logic_error("Key not found");
} }
return find->second; return find->second;
#else
return _map[key];
#endif
} }
inline bool TryGet(KeyT key, ValueT& out) const { inline bool TryGet(KeyT key, ValueT& out) const {