[ox/std] Remove incomplete writeF32toa
All checks were successful
Build / build (push) Successful in 2m28s

This commit is contained in:
Gary Talent 2024-05-08 23:43:12 -05:00
parent 331f721292
commit 74a8a7d751
4 changed files with 238 additions and 24 deletions

238
deps/ox/src/ox/std/smallmap.hpp vendored Normal file
View File

@ -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<typename K, typename T>
class SmallMap {
public:
using key_t = K;
using value_t = T;
private:
struct Pair {
K key = {};
T value{};
};
Vector<K> m_keys;
Vector<Pair> 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<K> const&key);
constexpr Result<T*> at(MaybeView_t<K> const&key) noexcept;
constexpr Result<const T*> at(MaybeView_t<K> const&key) const noexcept;
constexpr void erase(MaybeView_t<K> const&key);
[[nodiscard]]
constexpr bool contains(MaybeView_t<K> const&key) const noexcept;
[[nodiscard]]
constexpr std::size_t size() const noexcept;
[[nodiscard]]
constexpr Vector<K> const&keys() const noexcept;
constexpr void clear();
private:
constexpr void expand();
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);
};
template<typename K, typename T>
constexpr SmallMap<K, T>::SmallMap(std::size_t size): m_pairs(size) {
}
template<typename K, typename T>
constexpr SmallMap<K, T>::SmallMap(SmallMap<K, T> const&other) {
m_pairs = other.m_pairs;
}
template<typename K, typename T>
constexpr SmallMap<K, T>::SmallMap(SmallMap<K, T> &&other) noexcept {
m_keys = std::move(other.m_keys);
m_pairs = std::move(other.m_pairs);
}
template<typename K, typename T>
constexpr SmallMap<K, T>::~SmallMap() {
clear();
}
template<typename K, typename T>
constexpr bool SmallMap<K, T>::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<typename K, typename T>
constexpr SmallMap<K, T> &SmallMap<K, T>::operator=(SmallMap<K, T> const&other) {
if (this != &other) {
clear();
m_keys = other.m_keys;
m_pairs = other.m_pairs;
}
return *this;
}
template<typename K, typename T>
constexpr SmallMap<K, T> &SmallMap<K, T>::operator=(SmallMap<K, T> &&other) noexcept {
if (this != &other) {
clear();
m_keys = std::move(other.m_keys);
m_pairs = std::move(other.m_pairs);
}
return *this;
}
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;
p->key = k;
m_keys.emplace_back(k);
}
return p->value;
}
template<typename K, typename T>
constexpr Result<T*> SmallMap<K, T>::at(MaybeView_t<K> 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<typename K, typename T>
constexpr Result<const T*> SmallMap<K, T>::at(MaybeView_t<K> 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<typename K, typename T>
constexpr void SmallMap<K, T>::erase(MaybeView_t<K> 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<typename K, typename T>
constexpr bool SmallMap<K, T>::contains(MaybeView_t<K> const&k) const noexcept {
return access(m_pairs, k) != nullptr;
}
template<typename K, typename T>
constexpr std::size_t SmallMap<K, T>::size() const noexcept {
return m_keys.size();
}
template<typename K, typename T>
constexpr Vector<K> const&SmallMap<K, T>::keys() const noexcept {
return m_keys;
}
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 {
for (auto const&p : pairs) {
if (p.key == k) {
return &p;
}
}
return nullptr;
}
template<typename K, typename T>
template<typename KK>
constexpr typename SmallMap<K, T>::Pair *&SmallMap<K, T>::access(
Vector<Pair> &pairs, KK const&k) {
for (auto &p : pairs) {
if (p.key == k) {
return &p;
}
}
return nullptr;
}
}

View File

@ -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<uint32_t>(v);
if (raw >> 31) {
oxReturnError(writer.put('-'));
}
auto const mantissa = raw & onMask<uint32_t>(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"

View File

@ -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")

View File

@ -26,16 +26,6 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
return OxError(0);
}
},
{
"ftoa",
[]() {
ox::Array<char, 10> 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",
[]() {