diff --git a/deps/ox/src/ox/std/CMakeLists.txt b/deps/ox/src/ox/std/CMakeLists.txt index 5c797fff..26aa613c 100644 --- a/deps/ox/src/ox/std/CMakeLists.txt +++ b/deps/ox/src/ox/std/CMakeLists.txt @@ -63,6 +63,7 @@ target_link_libraries( install( FILES + algorithm.hpp assert.hpp bit.hpp bstring.hpp diff --git a/deps/ox/src/ox/std/algorithm.hpp b/deps/ox/src/ox/std/algorithm.hpp new file mode 100644 index 00000000..f97e8c7c --- /dev/null +++ b/deps/ox/src/ox/std/algorithm.hpp @@ -0,0 +1,23 @@ +/* + * Copyright 2015 - 2021 gary@drinkingtea.net + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +namespace ox { + +template +constexpr It find(It begin, It end, const T &value) { + for (; begin != end; ++begin) { + if (*begin == value) { + return begin; + } + } + return end; +} + +} \ No newline at end of file diff --git a/deps/ox/src/ox/std/hashmap.hpp b/deps/ox/src/ox/std/hashmap.hpp index 87f5cf19..69b6e352 100644 --- a/deps/ox/src/ox/std/hashmap.hpp +++ b/deps/ox/src/ox/std/hashmap.hpp @@ -8,6 +8,7 @@ #pragma once +#include "algorithm.hpp" #include "strops.hpp" #include "vector.hpp" @@ -57,6 +58,8 @@ class HashMap { */ Result 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 HashMap::at(K k) noexcept { return p->value; } +template +void HashMap::erase(const K &k) { + if (!contains(k)) { + return; + } + auto h = hash(k) % m_pairs.size(); + auto hashStr = reinterpret_cast(&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 Result HashMap::at(K k) const noexcept { auto p = access(m_pairs, k); @@ -211,7 +233,7 @@ template typename HashMap::Pair *const&HashMap::access(const Vector &pairs, K k) const { auto h = hash(k) % pairs.size(); auto hashStr = reinterpret_cast(&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 HashMap::Pair *&HashMap::access(Vector &pairs, K k) { auto h = hash(k) % pairs.size(); auto hashStr = reinterpret_cast(&h); - while (1) { + while (true) { auto &p = pairs[h]; if (p == nullptr || ox_strcmp(p->key, k) == 0) { return p;