[ox/std] Remove dedicated keys array from SmallMap
All checks were successful
Build / build (push) Successful in 2m31s
All checks were successful
Build / build (push) Successful in 2m31s
This commit is contained in:
parent
30797c710b
commit
7233da75ea
75
deps/ox/src/ox/std/smallmap.hpp
vendored
75
deps/ox/src/ox/std/smallmap.hpp
vendored
@ -23,14 +23,13 @@ class SmallMap {
|
||||
public:
|
||||
using key_t = K;
|
||||
using value_t = T;
|
||||
|
||||
private:
|
||||
struct Pair {
|
||||
K key = {};
|
||||
T value{};
|
||||
};
|
||||
|
||||
private:
|
||||
using PairVector = Vector<Pair, SmallSz>;
|
||||
Vector<K> m_keys;
|
||||
PairVector m_pairs;
|
||||
|
||||
public:
|
||||
@ -63,7 +62,22 @@ class SmallMap {
|
||||
constexpr std::size_t size() const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector<K> const&keys() const noexcept;
|
||||
constexpr Vector<K> keys() const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr K const&key(size_t i) const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr T const&value(size_t i) const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr T &value(size_t i) noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Pair const&get(size_t i) const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Pair &get(size_t i) noexcept;
|
||||
|
||||
constexpr void clear();
|
||||
|
||||
@ -83,7 +97,6 @@ constexpr SmallMap<K, T, SmallSz>::SmallMap(SmallMap<K, T, SmallSz> const&other)
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr SmallMap<K, T, SmallSz>::SmallMap(SmallMap<K, T, SmallSz> &&other) noexcept {
|
||||
m_keys = std::move(other.m_keys);
|
||||
m_pairs = std::move(other.m_pairs);
|
||||
}
|
||||
|
||||
@ -94,23 +107,13 @@ constexpr SmallMap<K, T, SmallSz>::~SmallMap() {
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr bool SmallMap<K, T, SmallSz>::operator==(SmallMap const&other) const {
|
||||
if (m_keys != other.m_keys) {
|
||||
return false;
|
||||
}
|
||||
for (int i = 0; i < m_keys.size(); ++i) {
|
||||
auto &k = m_keys[i];
|
||||
if (at(k) != other.at(k)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
return m_pairs == other.m_pairs;
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr SmallMap<K, T, SmallSz> &SmallMap<K, T, SmallSz>::operator=(SmallMap<K, T, SmallSz> const&other) {
|
||||
if (this != &other) {
|
||||
clear();
|
||||
m_keys = other.m_keys;
|
||||
m_pairs = other.m_pairs;
|
||||
}
|
||||
return *this;
|
||||
@ -120,7 +123,6 @@ template<typename K, typename T, size_t SmallSz>
|
||||
constexpr SmallMap<K, T, SmallSz> &SmallMap<K, T, SmallSz>::operator=(SmallMap<K, T, SmallSz> &&other) noexcept {
|
||||
if (this != &other) {
|
||||
clear();
|
||||
m_keys = std::move(other.m_keys);
|
||||
m_pairs = std::move(other.m_pairs);
|
||||
}
|
||||
return *this;
|
||||
@ -164,7 +166,6 @@ constexpr void SmallMap<K, T, SmallSz>::erase(MaybeView_t<K> const&k) {
|
||||
++i;
|
||||
}
|
||||
std::ignore = m_pairs.erase(i);
|
||||
std::ignore = m_keys.erase(i);
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
@ -174,12 +175,42 @@ constexpr bool SmallMap<K, T, SmallSz>::contains(MaybeView_t<K> const&k) const n
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr std::size_t SmallMap<K, T, SmallSz>::size() const noexcept {
|
||||
return m_keys.size();
|
||||
return m_pairs.size();
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr Vector<K> const&SmallMap<K, T, SmallSz>::keys() const noexcept {
|
||||
return m_keys;
|
||||
constexpr Vector<K> SmallMap<K, T, SmallSz>::keys() const noexcept {
|
||||
ox::Vector<K> keys;
|
||||
keys.reserve(m_pairs.size());
|
||||
for (auto const&p : m_pairs) {
|
||||
keys.emplace_back(p.key);
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr K const&SmallMap<K, T, SmallSz>::key(size_t i) const noexcept {
|
||||
return m_pairs[i].key;
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr T const&SmallMap<K, T, SmallSz>::value(size_t i) const noexcept {
|
||||
return m_pairs[i].value;
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr T &SmallMap<K, T, SmallSz>::value(size_t i) noexcept {
|
||||
return m_pairs[i].value;
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr SmallMap<K, T, SmallSz>::Pair const&SmallMap<K, T, SmallSz>::get(size_t i) const noexcept {
|
||||
return m_pairs[i];
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr SmallMap<K, T, SmallSz>::Pair &SmallMap<K, T, SmallSz>::get(size_t i) noexcept {
|
||||
return m_pairs[i];
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
@ -198,7 +229,6 @@ constexpr typename SmallMap<K, T, SmallSz>::Pair const&SmallMap<K, T, SmallSz>::
|
||||
}
|
||||
}
|
||||
isNew = true;
|
||||
m_keys.emplace_back(K(k));
|
||||
return pairs.emplace_back();
|
||||
}
|
||||
|
||||
@ -213,7 +243,6 @@ constexpr typename SmallMap<K, T, SmallSz>::Pair &SmallMap<K, T, SmallSz>::acces
|
||||
}
|
||||
}
|
||||
isNew = true;
|
||||
m_keys.emplace_back(K(k));
|
||||
return pairs.emplace_back();
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user