Performance improvements for collections.
continuous-integration/drone/push Build is passing
Details
continuous-integration/drone/push Build is passing
Details
This commit is contained in:
parent
24aff14745
commit
fbf2e42b8f
|
@ -1,5 +1,6 @@
|
||||||
#ifndef ARBUTILS_DICTIONARY_HPP
|
#ifndef ARBUTILS_DICTIONARY_HPP
|
||||||
#define ARBUTILS_DICTIONARY_HPP
|
#define ARBUTILS_DICTIONARY_HPP
|
||||||
|
#include <cstdint>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include "../Assert.hpp"
|
#include "../Assert.hpp"
|
||||||
|
|
||||||
|
@ -20,7 +21,7 @@ namespace Arbutils::Collections {
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void Insert(const KeyT& key, const ValueT& value) {
|
inline void Insert(const KeyT& key, const ValueT& value) {
|
||||||
[[maybe_unused]] auto v = _map.insert({key, value});
|
[[maybe_unused]] const auto& v = _map.insert({key, value});
|
||||||
#ifndef NO_ASSERT
|
#ifndef NO_ASSERT
|
||||||
if (!v.second)
|
if (!v.second)
|
||||||
throw std::logic_error("Key already exists");
|
throw std::logic_error("Key already exists");
|
||||||
|
@ -32,7 +33,7 @@ namespace Arbutils::Collections {
|
||||||
|
|
||||||
[[nodiscard]] inline ValueT& Get(const KeyT& key) {
|
[[nodiscard]] inline ValueT& Get(const KeyT& key) {
|
||||||
#ifndef NO_ASSERT
|
#ifndef NO_ASSERT
|
||||||
auto find = _map.find(key);
|
const 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");
|
||||||
}
|
}
|
||||||
|
@ -44,7 +45,7 @@ namespace Arbutils::Collections {
|
||||||
|
|
||||||
[[nodiscard]] inline const ValueT& Get(const KeyT& key) const {
|
[[nodiscard]] inline const ValueT& Get(const KeyT& key) const {
|
||||||
#ifndef NO_ASSERT
|
#ifndef NO_ASSERT
|
||||||
auto find = _map.find(key);
|
const 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");
|
||||||
}
|
}
|
||||||
|
@ -55,7 +56,7 @@ namespace Arbutils::Collections {
|
||||||
}
|
}
|
||||||
|
|
||||||
inline bool TryGet(const KeyT& key, ValueT& out) const {
|
inline bool TryGet(const KeyT& key, ValueT& out) const {
|
||||||
auto find = _map.find(key);
|
const auto& find = _map.find(key);
|
||||||
if (find == _map.end()) {
|
if (find == _map.end()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,7 +33,7 @@ namespace Arbutils::Collections {
|
||||||
return _vector.at(index);
|
return _vector.at(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const_reference At(size_t index) const {
|
inline const const_reference& At(size_t index) const {
|
||||||
#ifndef NO_ASSERT
|
#ifndef NO_ASSERT
|
||||||
if (index >= _vector.size() || index < 0) {
|
if (index >= _vector.size() || index < 0) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
@ -44,7 +44,7 @@ namespace Arbutils::Collections {
|
||||||
return _vector.at(index);
|
return _vector.at(index);
|
||||||
}
|
}
|
||||||
|
|
||||||
inline const_reference Set(size_t index, const ValueT& value) {
|
inline const const_reference& Set(size_t index, const ValueT& value) {
|
||||||
#ifndef NO_ASSERT
|
#ifndef NO_ASSERT
|
||||||
if (index >= _vector.size() || index < 0) {
|
if (index >= _vector.size() || index < 0) {
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
|
@ -52,7 +52,7 @@ namespace Arbutils::Collections {
|
||||||
throw std::logic_error(ss.str());
|
throw std::logic_error(ss.str());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
auto prev = _vector.at(index);
|
const auto& prev = _vector.at(index);
|
||||||
_vector[index] = value;
|
_vector[index] = value;
|
||||||
return prev;
|
return prev;
|
||||||
}
|
}
|
||||||
|
@ -65,7 +65,7 @@ namespace Arbutils::Collections {
|
||||||
/// \param value The value we want the index for.
|
/// \param value The value we want the index for.
|
||||||
/// \return The index of the first occurrence of the value in the list, or -1 if none is found.
|
/// \return The index of the first occurrence of the value in the list, or -1 if none is found.
|
||||||
inline size_t IndexOf(const ValueT& value) const {
|
inline size_t IndexOf(const ValueT& value) const {
|
||||||
auto it = std::find(_vector.begin(), _vector.end(), value);
|
const auto& it = std::find(_vector.begin(), _vector.end(), value);
|
||||||
if (it == _vector.end())
|
if (it == _vector.end())
|
||||||
return -1;
|
return -1;
|
||||||
return std::distance(_vector.begin(), it);
|
return std::distance(_vector.begin(), it);
|
||||||
|
@ -76,7 +76,7 @@ namespace Arbutils::Collections {
|
||||||
inline void Insert(size_t index, const ValueT& value) { _vector.insert(index, value); }
|
inline void Insert(size_t index, const ValueT& value) { _vector.insert(index, value); }
|
||||||
|
|
||||||
inline reference operator[](size_t index) { return At(index); }
|
inline reference operator[](size_t index) { return At(index); }
|
||||||
inline const_reference operator[](size_t index) const { return At(index); }
|
inline const const_reference& operator[](size_t index) const { return At(index); }
|
||||||
|
|
||||||
inline size_t Count() const { return _vector.size(); }
|
inline size_t Count() const { return _vector.size(); }
|
||||||
inline void Resize(size_t size) { _vector.resize(size); }
|
inline void Resize(size_t size) { _vector.resize(size); }
|
||||||
|
|
Loading…
Reference in New Issue