[ox/std] Remove dedicated keys array from SmallMap
All checks were successful
Build / build (push) Successful in 2m31s

This commit is contained in:
Gary Talent 2024-05-11 01:59:40 -05:00
parent 30797c710b
commit 7233da75ea

View File

@ -23,14 +23,13 @@ class SmallMap {
public: public:
using key_t = K; using key_t = K;
using value_t = T; using value_t = T;
private:
struct Pair { struct Pair {
K key = {}; K key = {};
T value{}; T value{};
}; };
private:
using PairVector = Vector<Pair, SmallSz>; using PairVector = Vector<Pair, SmallSz>;
Vector<K> m_keys;
PairVector m_pairs; PairVector m_pairs;
public: public:
@ -63,7 +62,22 @@ class SmallMap {
constexpr std::size_t size() const noexcept; constexpr std::size_t size() const noexcept;
[[nodiscard]] [[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(); 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> template<typename K, typename T, size_t SmallSz>
constexpr SmallMap<K, T, SmallSz>::SmallMap(SmallMap<K, T, SmallSz> &&other) noexcept { 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); 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> template<typename K, typename T, size_t SmallSz>
constexpr bool SmallMap<K, T, SmallSz>::operator==(SmallMap const&other) const { constexpr bool SmallMap<K, T, SmallSz>::operator==(SmallMap const&other) const {
if (m_keys != other.m_keys) { return m_pairs == other.m_pairs;
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;
} }
template<typename K, typename T, size_t SmallSz> template<typename K, typename T, size_t SmallSz>
constexpr SmallMap<K, T, SmallSz> &SmallMap<K, T, SmallSz>::operator=(SmallMap<K, T, SmallSz> const&other) { constexpr SmallMap<K, T, SmallSz> &SmallMap<K, T, SmallSz>::operator=(SmallMap<K, T, SmallSz> const&other) {
if (this != &other) { if (this != &other) {
clear(); clear();
m_keys = other.m_keys;
m_pairs = other.m_pairs; m_pairs = other.m_pairs;
} }
return *this; 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 { constexpr SmallMap<K, T, SmallSz> &SmallMap<K, T, SmallSz>::operator=(SmallMap<K, T, SmallSz> &&other) noexcept {
if (this != &other) { if (this != &other) {
clear(); clear();
m_keys = std::move(other.m_keys);
m_pairs = std::move(other.m_pairs); m_pairs = std::move(other.m_pairs);
} }
return *this; return *this;
@ -164,7 +166,6 @@ constexpr void SmallMap<K, T, SmallSz>::erase(MaybeView_t<K> const&k) {
++i; ++i;
} }
std::ignore = m_pairs.erase(i); std::ignore = m_pairs.erase(i);
std::ignore = m_keys.erase(i);
} }
template<typename K, typename T, size_t SmallSz> 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> template<typename K, typename T, size_t SmallSz>
constexpr std::size_t SmallMap<K, T, SmallSz>::size() const noexcept { 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> template<typename K, typename T, size_t SmallSz>
constexpr Vector<K> const&SmallMap<K, T, SmallSz>::keys() const noexcept { constexpr Vector<K> SmallMap<K, T, SmallSz>::keys() const noexcept {
return m_keys; 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> 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; isNew = true;
m_keys.emplace_back(K(k));
return pairs.emplace_back(); return pairs.emplace_back();
} }
@ -213,7 +243,6 @@ constexpr typename SmallMap<K, T, SmallSz>::Pair &SmallMap<K, T, SmallSz>::acces
} }
} }
isNew = true; isNew = true;
m_keys.emplace_back(K(k));
return pairs.emplace_back(); return pairs.emplace_back();
} }