From 7233da75eaaea055ed0486895fc85606db86d0f4 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 11 May 2024 01:59:40 -0500 Subject: [PATCH] [ox/std] Remove dedicated keys array from SmallMap --- deps/ox/src/ox/std/smallmap.hpp | 75 +++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 23 deletions(-) diff --git a/deps/ox/src/ox/std/smallmap.hpp b/deps/ox/src/ox/std/smallmap.hpp index 78502faf..28f2d43e 100644 --- a/deps/ox/src/ox/std/smallmap.hpp +++ b/deps/ox/src/ox/std/smallmap.hpp @@ -23,14 +23,13 @@ class SmallMap { public: using key_t = K; using value_t = T; - - private: struct Pair { K key = {}; T value{}; }; + + private: using PairVector = Vector; - Vector m_keys; PairVector m_pairs; public: @@ -63,7 +62,22 @@ class SmallMap { constexpr std::size_t size() const noexcept; [[nodiscard]] - constexpr Vector const&keys() const noexcept; + constexpr Vector 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(); @@ -83,7 +97,6 @@ constexpr SmallMap::SmallMap(SmallMap const&other) template constexpr SmallMap::SmallMap(SmallMap &&other) noexcept { - m_keys = std::move(other.m_keys); m_pairs = std::move(other.m_pairs); } @@ -94,23 +107,13 @@ constexpr SmallMap::~SmallMap() { template constexpr bool SmallMap::operator==(SmallMap const&other) const { - if (m_keys != other.m_keys) { - 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; + return m_pairs == other.m_pairs; } template constexpr SmallMap &SmallMap::operator=(SmallMap const&other) { if (this != &other) { clear(); - m_keys = other.m_keys; m_pairs = other.m_pairs; } return *this; @@ -120,7 +123,6 @@ template constexpr SmallMap &SmallMap::operator=(SmallMap &&other) noexcept { if (this != &other) { clear(); - m_keys = std::move(other.m_keys); m_pairs = std::move(other.m_pairs); } return *this; @@ -164,7 +166,6 @@ constexpr void SmallMap::erase(MaybeView_t const&k) { ++i; } std::ignore = m_pairs.erase(i); - std::ignore = m_keys.erase(i); } template @@ -174,12 +175,42 @@ constexpr bool SmallMap::contains(MaybeView_t const&k) const n template constexpr std::size_t SmallMap::size() const noexcept { - return m_keys.size(); + return m_pairs.size(); } template -constexpr Vector const&SmallMap::keys() const noexcept { - return m_keys; +constexpr Vector SmallMap::keys() const noexcept { + ox::Vector keys; + keys.reserve(m_pairs.size()); + for (auto const&p : m_pairs) { + keys.emplace_back(p.key); + } + return keys; +} + +template +constexpr K const&SmallMap::key(size_t i) const noexcept { + return m_pairs[i].key; +} + +template +constexpr T const&SmallMap::value(size_t i) const noexcept { + return m_pairs[i].value; +} + +template +constexpr T &SmallMap::value(size_t i) noexcept { + return m_pairs[i].value; +} + +template +constexpr SmallMap::Pair const&SmallMap::get(size_t i) const noexcept { + return m_pairs[i]; +} + +template +constexpr SmallMap::Pair &SmallMap::get(size_t i) noexcept { + return m_pairs[i]; } template @@ -198,7 +229,6 @@ constexpr typename SmallMap::Pair const&SmallMap:: } } isNew = true; - m_keys.emplace_back(K(k)); return pairs.emplace_back(); } @@ -213,7 +243,6 @@ constexpr typename SmallMap::Pair &SmallMap::acces } } isNew = true; - m_keys.emplace_back(K(k)); return pairs.emplace_back(); }