Squashed 'deps/nostalgia/' changes from 47a6a410..f624c720

f624c720 [studio] Fix broken readClaw call
204e5bbf [ox/std] Add SmallMap
1ecf197a [nostalgia] Remove .idea directory
74a8a7d7 [ox/std] Remove incomplete writeF32toa
331f7212 [olympic,nostalgia] Cleanup
63486c23 [ox] Cleanup

git-subtree-dir: deps/nostalgia
git-subtree-split: f624c720f9dcc9b44943269adb54d32095bc0e04
This commit is contained in:
2024-05-08 23:55:07 -05:00
parent bd416f82e2
commit 420fa96463
44 changed files with 529 additions and 344 deletions

View File

@@ -117,6 +117,7 @@ install(
ranges.hpp
serialize.hpp
size.hpp
smallmap.hpp
stacktrace.hpp
std.hpp
stddef.hpp

View File

@@ -10,6 +10,7 @@
#include "error.hpp"
#include "reader.hpp"
#include "span.hpp"
#include "vector.hpp"
#include "writer.hpp"
@@ -18,6 +19,7 @@ namespace ox {
extern template class Vector<char>;
using Buffer = Vector<char>;
using BufferView = SpanView<char>;
class BufferWriter {
private:
@@ -174,10 +176,7 @@ class BufferReader {
char const* m_buff = nullptr;
public:
constexpr explicit BufferReader(char const*buff, std::size_t sz) noexcept:
m_size(sz), m_buff(buff) {}
constexpr explicit BufferReader(ox::Buffer const&buffer) noexcept:
constexpr explicit BufferReader(ox::BufferView buffer) noexcept:
m_size(buffer.size()), m_buff(buffer.data()) {}
constexpr ox::Result<char> peek() const noexcept {

View File

@@ -234,6 +234,22 @@ struct [[nodiscard]] Result {
return value;
}
template<typename U = T>
constexpr ox::Result<U> to() & noexcept {
if (error) [[unlikely]] {
return error;
}
return U(value);
}
template<typename U = T>
constexpr ox::Result<U> to() && noexcept {
if (error) [[unlikely]] {
return error;
}
return U(std::move(value));
}
template<typename U = T>
constexpr ox::Result<U> to(auto const&f) & noexcept {
if (error) [[unlikely]] {

View File

@@ -40,12 +40,17 @@ constexpr StringView toStringView(const char *s) noexcept {
template<bool force = false, std::size_t size>
constexpr StringView toStringView(const IString<size> &s) noexcept {
return s.c_str();
return s;
}
template<bool force = false>
constexpr StringView toStringView(ox::StringLiteral s) noexcept {
return s;
}
template<bool force = false, std::size_t size>
constexpr StringView toStringView(const BasicString<size> &s) noexcept {
return s.c_str();
return s;
}
#if __has_include(<string>)

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

@@ -19,8 +19,8 @@ template<typename T>
class SpanView {
private:
const T *m_items{};
const std::size_t m_size{};
T const*m_items{};
std::size_t m_size{};
public:
using value_type = T;
@@ -106,6 +106,16 @@ class SpanView {
return m_items[i];
}
constexpr SpanView operator+(size_t i) const noexcept {
return {m_items + i, m_size - i};
}
constexpr SpanView operator+=(size_t i) noexcept {
m_items += i;
m_size -= i;
return *this;
}
[[nodiscard]]
constexpr T const*data() const noexcept {
return m_items;

View File

@@ -39,6 +39,7 @@
#include "realstd.hpp"
#include "serialize.hpp"
#include "size.hpp"
#include "smallmap.hpp"
#include "stacktrace.hpp"
#include "stddef.hpp"
#include "string.hpp"

View File

@@ -1,7 +1,9 @@
#pragma once
#include "bit.hpp"
#include "error.hpp"
#include "math.hpp"
#include "types.hpp"
#include "writer.hpp"

View File

@@ -192,6 +192,10 @@ class BasicString {
[[nodiscard]]
constexpr BasicString substr(std::size_t begin, std::size_t end) const noexcept;
constexpr void resize(size_t sz) noexcept {
m_buff.resize(sz);
}
[[nodiscard]]
constexpr const char *data() const noexcept {
return m_buff.data();

View File

@@ -109,7 +109,9 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
oxAssert(ox::StringView("Write") != ox::String(""), "String / StringView comparison broken");
oxAssert(ox::String("Write") != ox::StringView(""), "String / StringView comparison broken");
oxAssert(ox::String("Write") == ox::StringView("Write"), "String / StringView comparison broken");
oxAssert(ox::String(ox::StringView("Write")) == ox::StringView("Write"), "String / StringView comparison broken");
oxAssert(
ox::String(ox::StringView("Write")) == ox::StringView("Write"),
"String / StringView comparison broken");
return OxError(0);
}
},
@@ -119,7 +121,7 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
ox::Vector<int> v;
oxAssert(v.size() == 0, "Initial Vector size not 0");
oxAssert(v.empty(), "Vector::empty() is broken");
auto insertTest = [&v](int val, [[maybe_unused]] std::size_t size) {
auto insertTest = [&v](int val, std::size_t size) {
v.push_back(val);
oxReturnError(OxError(v.size() != size, "Vector size incorrect"));
oxReturnError(OxError(v[v.size() - 1] != val, "Vector value wrong"));
@@ -152,7 +154,11 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
[] {
using BA = ox::Array<char, 4>;
const auto actual = ox::serialize<uint32_t>(256).unwrap();
oxOutf("[{}, {}, {}, {}]", static_cast<int>(actual[0]), static_cast<int>(actual[1]), static_cast<int>(actual[2]), static_cast<int>(actual[3]));
oxOutf("[{}, {}, {}, {}]",
static_cast<int>(actual[0]),
static_cast<int>(actual[1]),
static_cast<int>(actual[2]),
static_cast<int>(actual[3]));
oxExpect(ox::serialize<int32_t>(4).unwrap(), BA({4, 0, 0, 0}));
oxExpect(ox::serialize<int32_t>(256).unwrap(), BA({0, 1, 0, 0}));
oxExpect(ox::serialize<int32_t>(257).unwrap(), BA({1, 1, 0, 0}));