[ox/std] Add ox::find(...) and HashMap::erase(K)
This commit is contained in:
26
deps/ox/src/ox/std/hashmap.hpp
vendored
26
deps/ox/src/ox/std/hashmap.hpp
vendored
@@ -8,6 +8,7 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "algorithm.hpp"
|
||||
#include "strops.hpp"
|
||||
#include "vector.hpp"
|
||||
|
||||
@@ -57,6 +58,8 @@ class HashMap {
|
||||
*/
|
||||
Result<const T&> at(K key) const noexcept;
|
||||
|
||||
void erase(const K &key);
|
||||
|
||||
bool contains(K key) const noexcept;
|
||||
|
||||
std::size_t size() const noexcept;
|
||||
@@ -163,6 +166,25 @@ Result<T&> HashMap<K, T>::at(K k) noexcept {
|
||||
return p->value;
|
||||
}
|
||||
|
||||
template<typename K, typename T>
|
||||
void HashMap<K, T>::erase(const K &k) {
|
||||
if (!contains(k)) {
|
||||
return;
|
||||
}
|
||||
auto h = hash(k) % m_pairs.size();
|
||||
auto hashStr = reinterpret_cast<char*>(&h);
|
||||
while (true) {
|
||||
const auto &p = m_pairs[h];
|
||||
if (p == nullptr || ox_strcmp(p->key, k) == 0) {
|
||||
m_pairs.erase(h);
|
||||
break;
|
||||
} else {
|
||||
h = hash(hashStr, 8) % m_pairs.size();
|
||||
}
|
||||
}
|
||||
m_keys.erase(ox::find(m_keys.cbegin(), m_keys.cend(), k));
|
||||
}
|
||||
|
||||
template<typename K, typename T>
|
||||
Result<const T&> HashMap<K, T>::at(K k) const noexcept {
|
||||
auto p = access(m_pairs, k);
|
||||
@@ -211,7 +233,7 @@ template<typename K, typename T>
|
||||
typename HashMap<K, T>::Pair *const&HashMap<K, T>::access(const Vector<Pair*> &pairs, K k) const {
|
||||
auto h = hash(k) % pairs.size();
|
||||
auto hashStr = reinterpret_cast<char*>(&h);
|
||||
while (1) {
|
||||
while (true) {
|
||||
const auto &p = pairs[h];
|
||||
if (p == nullptr || ox_strcmp(p->key, k) == 0) {
|
||||
return p;
|
||||
@@ -225,7 +247,7 @@ template<typename K, typename T>
|
||||
typename HashMap<K, T>::Pair *&HashMap<K, T>::access(Vector<Pair*> &pairs, K k) {
|
||||
auto h = hash(k) % pairs.size();
|
||||
auto hashStr = reinterpret_cast<char*>(&h);
|
||||
while (1) {
|
||||
while (true) {
|
||||
auto &p = pairs[h];
|
||||
if (p == nullptr || ox_strcmp(p->key, k) == 0) {
|
||||
return p;
|
||||
|
Reference in New Issue
Block a user