[ox/std] Fix bugs in HashMap and SmallMap
All checks were successful
Build / build (push) Successful in 2m30s
All checks were successful
Build / build (push) Successful in 2m30s
This commit is contained in:
parent
d1845448c3
commit
cd60c4abaf
17
deps/ox/src/ox/std/hashmap.hpp
vendored
17
deps/ox/src/ox/std/hashmap.hpp
vendored
@ -133,17 +133,18 @@ constexpr HashMap<K, T> &HashMap<K, T>::operator=(HashMap<K, T> &&other) noexcep
|
||||
|
||||
template<typename K, typename T>
|
||||
constexpr T &HashMap<K, T>::operator[](MaybeView_t<K> const&k) {
|
||||
auto &p = access(m_pairs, k);
|
||||
if (p == nullptr) {
|
||||
auto p = &access(m_pairs, k);
|
||||
if (*p == nullptr) {
|
||||
if (static_cast<double>(m_pairs.size()) * 0.7 <
|
||||
static_cast<double>(m_keys.size())) {
|
||||
expand();
|
||||
p = &access(m_pairs, k);
|
||||
}
|
||||
p = new Pair;
|
||||
p->key = k;
|
||||
*p = new Pair;
|
||||
(*p)->key = k;
|
||||
m_keys.emplace_back(k);
|
||||
}
|
||||
return p->value;
|
||||
return (*p)->value;
|
||||
}
|
||||
|
||||
template<typename K, typename T>
|
||||
@ -208,7 +209,7 @@ constexpr void HashMap<K, T>::clear() {
|
||||
|
||||
template<typename K, typename T>
|
||||
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) {
|
||||
auto const&k = m_keys[i];
|
||||
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 {
|
||||
auto h = static_cast<std::size_t>(ox::hash<KK>{}(k) % pairs.size());
|
||||
while (true) {
|
||||
const auto &p = pairs[h];
|
||||
auto const&p = *pairs.at(h).unwrap();
|
||||
if (p == nullptr || p->key == k) {
|
||||
return p;
|
||||
} else {
|
||||
@ -235,7 +236,7 @@ template<typename KK>
|
||||
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());
|
||||
while (true) {
|
||||
auto &p = pairs[h];
|
||||
auto &p = *pairs.at(h).unwrap();
|
||||
if (p == nullptr || p->key == k) {
|
||||
return p;
|
||||
} else {
|
||||
|
57
deps/ox/src/ox/std/smallmap.hpp
vendored
57
deps/ox/src/ox/std/smallmap.hpp
vendored
@ -67,13 +67,11 @@ class SmallMap {
|
||||
constexpr void clear();
|
||||
|
||||
private:
|
||||
constexpr void expand();
|
||||
template<typename KK>
|
||||
constexpr Pair const&access(Vector<Pair> const&pairs, KK const&key, bool &isNew) const;
|
||||
|
||||
template<typename KK>
|
||||
constexpr Pair *const&access(Vector<Pair> const&pairs, KK const&key) const;
|
||||
|
||||
template<typename KK>
|
||||
constexpr Pair *&access(Vector<Pair> &pairs, KK const&key);
|
||||
constexpr Pair &access(Vector<Pair> &pairs, KK const&key, bool &isNew);
|
||||
|
||||
};
|
||||
|
||||
@ -133,15 +131,10 @@ constexpr SmallMap<K, T> &SmallMap<K, T>::operator=(SmallMap<K, T> &&other) noex
|
||||
|
||||
template<typename K, typename T>
|
||||
constexpr T &SmallMap<K, T>::operator[](MaybeView_t<K> const&k) {
|
||||
auto &p = access(m_pairs, k);
|
||||
if (p == nullptr) {
|
||||
if (static_cast<double>(m_pairs.size()) * 0.7 <
|
||||
static_cast<double>(m_keys.size())) {
|
||||
expand();
|
||||
}
|
||||
p = new Pair;
|
||||
bool isNew{};
|
||||
auto p = &access(m_pairs, k, isNew);
|
||||
if (isNew) {
|
||||
p->key = k;
|
||||
m_keys.emplace_back(k);
|
||||
}
|
||||
return p->value;
|
||||
}
|
||||
@ -194,45 +187,41 @@ constexpr Vector<K> const&SmallMap<K, T>::keys() const noexcept {
|
||||
|
||||
template<typename K, typename T>
|
||||
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.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 KK>
|
||||
constexpr typename SmallMap<K, T>::Pair *const&SmallMap<K, T>::access(
|
||||
Vector<Pair> const&pairs, KK const&k) const {
|
||||
constexpr typename SmallMap<K, T>::Pair const&SmallMap<K, T>::access(
|
||||
Vector<Pair> const&pairs, KK const&k, bool &isNew) const {
|
||||
for (auto const&p : pairs) {
|
||||
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 KK>
|
||||
constexpr typename SmallMap<K, T>::Pair *&SmallMap<K, T>::access(
|
||||
Vector<Pair> &pairs, KK const&k) {
|
||||
constexpr typename SmallMap<K, T>::Pair &SmallMap<K, T>::access(
|
||||
Vector<Pair> &pairs, KK const&k, bool &isNew) {
|
||||
for (auto &p : pairs) {
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user