[ox/std] Fix bugs in HashMap and SmallMap
All checks were successful
Build / build (push) Successful in 2m30s

This commit is contained in:
Gary Talent 2024-05-10 22:10:34 -05:00
parent d1845448c3
commit cd60c4abaf
2 changed files with 32 additions and 42 deletions

View File

@ -133,17 +133,18 @@ constexpr HashMap<K, T> &HashMap<K, T>::operator=(HashMap<K, T> &&other) noexcep
template<typename K, typename T> template<typename K, typename T>
constexpr T &HashMap<K, T>::operator[](MaybeView_t<K> const&k) { constexpr T &HashMap<K, T>::operator[](MaybeView_t<K> const&k) {
auto &p = access(m_pairs, k); auto p = &access(m_pairs, k);
if (p == nullptr) { if (*p == nullptr) {
if (static_cast<double>(m_pairs.size()) * 0.7 < if (static_cast<double>(m_pairs.size()) * 0.7 <
static_cast<double>(m_keys.size())) { static_cast<double>(m_keys.size())) {
expand(); expand();
p = &access(m_pairs, k);
} }
p = new Pair; *p = new Pair;
p->key = k; (*p)->key = k;
m_keys.emplace_back(k); m_keys.emplace_back(k);
} }
return p->value; return (*p)->value;
} }
template<typename K, typename T> template<typename K, typename T>
@ -208,7 +209,7 @@ constexpr void HashMap<K, T>::clear() {
template<typename K, typename T> template<typename K, typename T>
constexpr void HashMap<K, T>::expand() { constexpr void HashMap<K, T>::expand() {
Vector<Pair*> r; Vector<Pair*> r(m_pairs.size() * 2);
for (std::size_t i = 0; i < m_keys.size(); ++i) { for (std::size_t i = 0; i < m_keys.size(); ++i) {
auto const&k = m_keys[i]; auto const&k = m_keys[i];
access(r, k) = std::move(access(m_pairs, k)); access(r, k) = std::move(access(m_pairs, k));
@ -221,7 +222,7 @@ template<typename KK>
constexpr typename HashMap<K, T>::Pair *const&HashMap<K, T>::access(Vector<Pair*> const&pairs, KK const&k) const { constexpr typename HashMap<K, T>::Pair *const&HashMap<K, T>::access(Vector<Pair*> const&pairs, KK const&k) const {
auto h = static_cast<std::size_t>(ox::hash<KK>{}(k) % pairs.size()); auto h = static_cast<std::size_t>(ox::hash<KK>{}(k) % pairs.size());
while (true) { while (true) {
const auto &p = pairs[h]; auto const&p = *pairs.at(h).unwrap();
if (p == nullptr || p->key == k) { if (p == nullptr || p->key == k) {
return p; return p;
} else { } else {
@ -235,7 +236,7 @@ template<typename KK>
constexpr typename HashMap<K, T>::Pair *&HashMap<K, T>::access(Vector<Pair*> &pairs, KK const&k) { constexpr typename HashMap<K, T>::Pair *&HashMap<K, T>::access(Vector<Pair*> &pairs, KK const&k) {
auto h = static_cast<std::size_t>(ox::hash<KK>{}(k) % pairs.size()); auto h = static_cast<std::size_t>(ox::hash<KK>{}(k) % pairs.size());
while (true) { while (true) {
auto &p = pairs[h]; auto &p = *pairs.at(h).unwrap();
if (p == nullptr || p->key == k) { if (p == nullptr || p->key == k) {
return p; return p;
} else { } else {

View File

@ -67,13 +67,11 @@ class SmallMap {
constexpr void clear(); constexpr void clear();
private: private:
constexpr void expand(); template<typename KK>
constexpr Pair const&access(Vector<Pair> const&pairs, KK const&key, bool &isNew) const;
template<typename KK> template<typename KK>
constexpr Pair *const&access(Vector<Pair> const&pairs, KK const&key) const; constexpr Pair &access(Vector<Pair> &pairs, KK const&key, bool &isNew);
template<typename KK>
constexpr Pair *&access(Vector<Pair> &pairs, KK const&key);
}; };
@ -133,15 +131,10 @@ constexpr SmallMap<K, T> &SmallMap<K, T>::operator=(SmallMap<K, T> &&other) noex
template<typename K, typename T> template<typename K, typename T>
constexpr T &SmallMap<K, T>::operator[](MaybeView_t<K> const&k) { constexpr T &SmallMap<K, T>::operator[](MaybeView_t<K> const&k) {
auto &p = access(m_pairs, k); bool isNew{};
if (p == nullptr) { auto p = &access(m_pairs, k, isNew);
if (static_cast<double>(m_pairs.size()) * 0.7 < if (isNew) {
static_cast<double>(m_keys.size())) {
expand();
}
p = new Pair;
p->key = k; p->key = k;
m_keys.emplace_back(k);
} }
return p->value; return p->value;
} }
@ -194,45 +187,41 @@ constexpr Vector<K> const&SmallMap<K, T>::keys() const noexcept {
template<typename K, typename T> template<typename K, typename T>
constexpr void SmallMap<K, T>::clear() { constexpr void SmallMap<K, T>::clear() {
for (std::size_t i = 0; i < m_pairs.size(); i++) {
delete m_pairs[i];
}
m_pairs.clear(); m_pairs.clear();
m_pairs.resize(127); m_pairs.resize(127);
} }
template<typename K, typename T>
constexpr void SmallMap<K, T>::expand() {
Vector<Pair*> r;
for (std::size_t i = 0; i < m_keys.size(); ++i) {
auto const&k = m_keys[i];
access(r, k) = std::move(access(m_pairs, k));
}
m_pairs = std::move(r);
}
template<typename K, typename T> template<typename K, typename T>
template<typename KK> template<typename KK>
constexpr typename SmallMap<K, T>::Pair *const&SmallMap<K, T>::access( constexpr typename SmallMap<K, T>::Pair const&SmallMap<K, T>::access(
Vector<Pair> const&pairs, KK const&k) const { Vector<Pair> const&pairs, KK const&k, bool &isNew) const {
for (auto const&p : pairs) { for (auto const&p : pairs) {
if (p.key == k) { if (p.key == k) {
return &p; isNew = false;
return p;
} }
} }
return nullptr; isNew = true;
m_keys.emplace_back(K(k));
return pairs.emplace_back();
} }
template<typename K, typename T> template<typename K, typename T>
template<typename KK> template<typename KK>
constexpr typename SmallMap<K, T>::Pair *&SmallMap<K, T>::access( constexpr typename SmallMap<K, T>::Pair &SmallMap<K, T>::access(
Vector<Pair> &pairs, KK const&k) { Vector<Pair> &pairs, KK const&k, bool &isNew) {
for (auto &p : pairs) { for (auto &p : pairs) {
if (p.key == k) { if (p.key == k) {
return &p; isNew = false;
return p;
} }
} }
return nullptr; isNew = true;
if (static_cast<double>(m_pairs.size()) * 0.7 <
static_cast<double>(m_keys.size())) {
}
m_keys.emplace_back(K(k));
return pairs.emplace_back();
} }
} }