diff --git a/deps/ox/src/ox/std/smallmap.hpp b/deps/ox/src/ox/std/smallmap.hpp new file mode 100644 index 00000000..76ad9370 --- /dev/null +++ b/deps/ox/src/ox/std/smallmap.hpp @@ -0,0 +1,238 @@ +/* + * Copyright 2015 - 2024 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 https://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#include "algorithm.hpp" +#include "hash.hpp" +#include "ignore.hpp" +#include "stringview.hpp" +#include "strops.hpp" +#include "vector.hpp" + +namespace ox { + +template +class SmallMap { + + public: + using key_t = K; + using value_t = T; + + private: + struct Pair { + K key = {}; + T value{}; + }; + Vector m_keys; + Vector m_pairs; + + public: + explicit constexpr SmallMap(std::size_t size = 127); + + constexpr SmallMap(SmallMap const&other); + + constexpr SmallMap(SmallMap &&other) noexcept; + + constexpr ~SmallMap(); + + constexpr bool operator==(SmallMap const&other) const; + + constexpr SmallMap &operator=(SmallMap const&other); + + constexpr SmallMap &operator=(SmallMap &&other) noexcept; + + constexpr T &operator[](MaybeView_t const&key); + + constexpr Result at(MaybeView_t const&key) noexcept; + + constexpr Result at(MaybeView_t const&key) const noexcept; + + constexpr void erase(MaybeView_t const&key); + + [[nodiscard]] + constexpr bool contains(MaybeView_t const&key) const noexcept; + + [[nodiscard]] + constexpr std::size_t size() const noexcept; + + [[nodiscard]] + constexpr Vector const&keys() const noexcept; + + constexpr void clear(); + + private: + constexpr void expand(); + + template + constexpr Pair *const&access(Vector const&pairs, KK const&key) const; + + template + constexpr Pair *&access(Vector &pairs, KK const&key); + +}; + +template +constexpr SmallMap::SmallMap(std::size_t size): m_pairs(size) { +} + +template +constexpr SmallMap::SmallMap(SmallMap const&other) { + m_pairs = other.m_pairs; +} + +template +constexpr SmallMap::SmallMap(SmallMap &&other) noexcept { + m_keys = std::move(other.m_keys); + m_pairs = std::move(other.m_pairs); +} + +template +constexpr SmallMap::~SmallMap() { + clear(); +} + +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; +} + +template +constexpr SmallMap &SmallMap::operator=(SmallMap const&other) { + if (this != &other) { + clear(); + m_keys = other.m_keys; + m_pairs = other.m_pairs; + } + return *this; +} + +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; +} + +template +constexpr T &SmallMap::operator[](MaybeView_t const&k) { + auto &p = access(m_pairs, k); + if (p == nullptr) { + if (static_cast(m_pairs.size()) * 0.7 < + static_cast(m_keys.size())) { + expand(); + } + p = new Pair; + p->key = k; + m_keys.emplace_back(k); + } + return p->value; +} + +template +constexpr Result SmallMap::at(MaybeView_t const&k) noexcept { + auto p = access(m_pairs, k); + if (!p) { + return {nullptr, OxError(1, "value not found for given key")}; + } + return &p->value; +} + +template +constexpr Result SmallMap::at(MaybeView_t const&k) const noexcept { + auto p = access(m_pairs, k); + if (!p) { + return {nullptr, OxError(1, "value not found for given key")}; + } + return &p->value; +} + +template +constexpr void SmallMap::erase(MaybeView_t const&k) { + size_t i{}; + for (auto const&p : m_pairs) { + if (k == p.key) { + break; + } + ++i; + } + std::ignore = m_pairs.erase(i); + std::ignore = m_keys.erase(i); +} + +template +constexpr bool SmallMap::contains(MaybeView_t const&k) const noexcept { + return access(m_pairs, k) != nullptr; +} + +template +constexpr std::size_t SmallMap::size() const noexcept { + return m_keys.size(); +} + +template +constexpr Vector const&SmallMap::keys() const noexcept { + return m_keys; +} + +template +constexpr void SmallMap::clear() { + for (std::size_t i = 0; i < m_pairs.size(); i++) { + delete m_pairs[i]; + } + m_pairs.clear(); + m_pairs.resize(127); +} + +template +constexpr void SmallMap::expand() { + Vector 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 +template +constexpr typename SmallMap::Pair *const&SmallMap::access( + Vector const&pairs, KK const&k) const { + for (auto const&p : pairs) { + if (p.key == k) { + return &p; + } + } + return nullptr; +} + +template +template +constexpr typename SmallMap::Pair *&SmallMap::access( + Vector &pairs, KK const&k) { + for (auto &p : pairs) { + if (p.key == k) { + return &p; + } + } + return nullptr; +} + +} diff --git a/deps/ox/src/ox/std/strconv.hpp b/deps/ox/src/ox/std/strconv.hpp index 2a18ec3d..6d8a25b3 100644 --- a/deps/ox/src/ox/std/strconv.hpp +++ b/deps/ox/src/ox/std/strconv.hpp @@ -41,19 +41,6 @@ constexpr ox::Error writeItoa(Integer v, ox::Writer_c auto &writer) noexcept { return {}; } -constexpr ox::Error writeF32toa(float const v, ox::Writer_c auto &writer) noexcept { - auto const raw = std::bit_cast(v); - if (raw >> 31) { - oxReturnError(writer.put('-')); - } - auto const mantissa = raw & onMask(23); - auto const exp = ((raw >> 23) & 0xff) - 126; - auto const p = ox::pow(2u, exp); - auto const out = p * mantissa; - oxReturnError(writeItoa(out, writer)); - return {}; -} - } #include "istring.hpp" diff --git a/deps/ox/src/ox/std/test/CMakeLists.txt b/deps/ox/src/ox/std/test/CMakeLists.txt index 567cf7db..d0c7da23 100644 --- a/deps/ox/src/ox/std/test/CMakeLists.txt +++ b/deps/ox/src/ox/std/test/CMakeLists.txt @@ -11,7 +11,6 @@ add_test("[ox/std] ox_memcmp ABCDEFG != HIJKLMN" ${CMAKE_RUNTIME_OUTPUT_DIRECTOR add_test("[ox/std] ox_memcmp HIJKLMN != ABCDEFG" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "HIJKLMN != ABCDEFG") add_test("[ox/std] ox_memcmp ABCDEFG == ABCDEFG" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "ABCDEFG == ABCDEFG") add_test("[ox/std] ox_memcmp ABCDEFGHI == ABCDEFG" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "ABCDEFGHI == ABCDEFG") -#add_test("[ox/std] ftoa" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "ftoa") add_test("[ox/std] itoa" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "itoa") add_test("[ox/std] BString" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "BString") add_test("[ox/std] String" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "String") diff --git a/deps/ox/src/ox/std/test/tests.cpp b/deps/ox/src/ox/std/test/tests.cpp index 745ea2f8..2a72f17a 100644 --- a/deps/ox/src/ox/std/test/tests.cpp +++ b/deps/ox/src/ox/std/test/tests.cpp @@ -26,16 +26,6 @@ static std::map tests = { return OxError(0); } }, - { - "ftoa", - []() { - ox::Array buff; - ox::CharBuffWriter bw(buff); - oxAssert(ox::writeF32toa(5, bw), "ox::writeItoa returned Error"); - oxExpect(ox::StringView(buff.data()), ox::StringView("5")); - return ox::Error{}; - } - }, { "itoa", []() {