Merge commit 'f48824793cfce315971fe2e699ece198c7a79407'
This commit is contained in:
commit
095a1135e7
12
deps/ox/src/ox/std/array.hpp
vendored
12
deps/ox/src/ox/std/array.hpp
vendored
@ -137,12 +137,16 @@ constexpr Array<T, ArraySize>::Array(Array &&other) noexcept {
|
||||
|
||||
template<typename T, std::size_t ArraySize>
|
||||
constexpr bool Array<T, ArraySize>::operator==(const Array &other) const {
|
||||
for (std::size_t i = 0; i < ArraySize; i++) {
|
||||
if (!(m_items[i] == other.m_items[i])) {
|
||||
return false;
|
||||
if (std::is_constant_evaluated()) {
|
||||
for (std::size_t i = 0; i < ArraySize; i++) {
|
||||
if (!(m_items[i] == other.m_items[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return memcmp(this, &other, sizeof(*this)) == 0;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename T, std::size_t ArraySize>
|
||||
|
17
deps/ox/src/ox/std/hashmap.hpp
vendored
17
deps/ox/src/ox/std/hashmap.hpp
vendored
@ -133,17 +133,18 @@ constexpr HashMap<K, T> &HashMap<K, T>::operator=(HashMap<K, T> &&other) noexcep
|
||||
|
||||
template<typename K, typename T>
|
||||
constexpr T &HashMap<K, T>::operator[](MaybeView_t<K> const&k) {
|
||||
auto &p = access(m_pairs, k);
|
||||
if (p == nullptr) {
|
||||
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 = &access(m_pairs, k);
|
||||
}
|
||||
p = new Pair;
|
||||
p->key = k;
|
||||
*p = new Pair;
|
||||
(*p)->key = k;
|
||||
m_keys.emplace_back(k);
|
||||
}
|
||||
return p->value;
|
||||
return (*p)->value;
|
||||
}
|
||||
|
||||
template<typename K, typename T>
|
||||
@ -208,7 +209,7 @@ constexpr void HashMap<K, T>::clear() {
|
||||
|
||||
template<typename K, typename T>
|
||||
constexpr void HashMap<K, T>::expand() {
|
||||
Vector<Pair*> r;
|
||||
Vector<Pair*> r(m_pairs.size() * 2);
|
||||
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));
|
||||
@ -221,7 +222,7 @@ template<typename KK>
|
||||
constexpr typename HashMap<K, T>::Pair *const&HashMap<K, T>::access(Vector<Pair*> const&pairs, KK const&k) const {
|
||||
auto h = static_cast<std::size_t>(ox::hash<KK>{}(k) % pairs.size());
|
||||
while (true) {
|
||||
const auto &p = pairs[h];
|
||||
auto const&p = *pairs.at(h).unwrap();
|
||||
if (p == nullptr || p->key == k) {
|
||||
return p;
|
||||
} else {
|
||||
@ -235,7 +236,7 @@ template<typename KK>
|
||||
constexpr typename HashMap<K, T>::Pair *&HashMap<K, T>::access(Vector<Pair*> &pairs, KK const&k) {
|
||||
auto h = static_cast<std::size_t>(ox::hash<KK>{}(k) % pairs.size());
|
||||
while (true) {
|
||||
auto &p = pairs[h];
|
||||
auto &p = *pairs.at(h).unwrap();
|
||||
if (p == nullptr || p->key == k) {
|
||||
return p;
|
||||
} else {
|
||||
|
197
deps/ox/src/ox/std/smallmap.hpp
vendored
197
deps/ox/src/ox/std/smallmap.hpp
vendored
@ -17,23 +17,23 @@
|
||||
|
||||
namespace ox {
|
||||
|
||||
template<typename K, typename T>
|
||||
template<typename K, typename T, size_t SmallSz = 0>
|
||||
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;
|
||||
|
||||
private:
|
||||
using PairVector = Vector<Pair, SmallSz>;
|
||||
PairVector m_pairs;
|
||||
|
||||
public:
|
||||
explicit constexpr SmallMap(std::size_t size = 127);
|
||||
constexpr SmallMap() = default;
|
||||
|
||||
constexpr SmallMap(SmallMap const&other);
|
||||
|
||||
@ -62,92 +62,84 @@ class SmallMap {
|
||||
constexpr std::size_t size() const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector<K> const&keys() const noexcept;
|
||||
constexpr Vector<K> 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();
|
||||
|
||||
private:
|
||||
constexpr void expand();
|
||||
template<typename KK>
|
||||
constexpr Pair const&access(PairVector const&pairs, KK const&key, bool &isNew) const;
|
||||
|
||||
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);
|
||||
constexpr Pair &access(PairVector &pairs, KK const&key, bool &isNew);
|
||||
|
||||
};
|
||||
|
||||
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) {
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr SmallMap<K, T, SmallSz>::SmallMap(SmallMap<K, T, SmallSz> 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);
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr SmallMap<K, T, SmallSz>::SmallMap(SmallMap<K, T, SmallSz> &&other) noexcept {
|
||||
m_pairs = std::move(other.m_pairs);
|
||||
}
|
||||
|
||||
template<typename K, typename T>
|
||||
constexpr SmallMap<K, T>::~SmallMap() {
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr SmallMap<K, T, SmallSz>::~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, size_t SmallSz>
|
||||
constexpr bool SmallMap<K, T, SmallSz>::operator==(SmallMap const&other) const {
|
||||
return m_pairs == other.m_pairs;
|
||||
}
|
||||
|
||||
template<typename K, typename T>
|
||||
constexpr SmallMap<K, T> &SmallMap<K, T>::operator=(SmallMap<K, T> const&other) {
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr SmallMap<K, T, SmallSz> &SmallMap<K, T, SmallSz>::operator=(SmallMap<K, T, SmallSz> 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 {
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr SmallMap<K, T, SmallSz> &SmallMap<K, T, SmallSz>::operator=(SmallMap<K, T, SmallSz> &&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;
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr T &SmallMap<K, T, SmallSz>::operator[](MaybeView_t<K> const&k) {
|
||||
bool isNew{};
|
||||
auto p = &access(m_pairs, k, isNew);
|
||||
if (isNew) {
|
||||
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 {
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr Result<T*> SmallMap<K, T, SmallSz>::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")};
|
||||
@ -155,8 +147,8 @@ constexpr Result<T*> SmallMap<K, T>::at(MaybeView_t<K> const&k) noexcept {
|
||||
return &p->value;
|
||||
}
|
||||
|
||||
template<typename K, typename T>
|
||||
constexpr Result<const T*> SmallMap<K, T>::at(MaybeView_t<K> const&k) const noexcept {
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr Result<const T*> SmallMap<K, T, SmallSz>::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")};
|
||||
@ -164,8 +156,8 @@ constexpr Result<const T*> SmallMap<K, T>::at(MaybeView_t<K> const&k) const noex
|
||||
return &p->value;
|
||||
}
|
||||
|
||||
template<typename K, typename T>
|
||||
constexpr void SmallMap<K, T>::erase(MaybeView_t<K> const&k) {
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr void SmallMap<K, T, SmallSz>::erase(MaybeView_t<K> const&k) {
|
||||
size_t i{};
|
||||
for (auto const&p : m_pairs) {
|
||||
if (k == p.key) {
|
||||
@ -174,65 +166,84 @@ constexpr void SmallMap<K, T>::erase(MaybeView_t<K> const&k) {
|
||||
++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 {
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr bool SmallMap<K, T, SmallSz>::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, size_t SmallSz>
|
||||
constexpr std::size_t SmallMap<K, T, SmallSz>::size() const noexcept {
|
||||
return m_pairs.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];
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr Vector<K> SmallMap<K, T, SmallSz>::keys() const noexcept {
|
||||
ox::Vector<K> keys;
|
||||
keys.reserve(m_pairs.size());
|
||||
for (auto const&p : m_pairs) {
|
||||
keys.emplace_back(p.key);
|
||||
}
|
||||
return keys;
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr K const&SmallMap<K, T, SmallSz>::key(size_t i) const noexcept {
|
||||
return m_pairs[i].key;
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr T const&SmallMap<K, T, SmallSz>::value(size_t i) const noexcept {
|
||||
return m_pairs[i].value;
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr T &SmallMap<K, T, SmallSz>::value(size_t i) noexcept {
|
||||
return m_pairs[i].value;
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr SmallMap<K, T, SmallSz>::Pair const&SmallMap<K, T, SmallSz>::get(size_t i) const noexcept {
|
||||
return m_pairs[i];
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr SmallMap<K, T, SmallSz>::Pair &SmallMap<K, T, SmallSz>::get(size_t i) noexcept {
|
||||
return m_pairs[i];
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr void SmallMap<K, T, SmallSz>::clear() {
|
||||
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 K, typename T, size_t SmallSz>
|
||||
template<typename KK>
|
||||
constexpr typename SmallMap<K, T>::Pair *const&SmallMap<K, T>::access(
|
||||
Vector<Pair> const&pairs, KK const&k) const {
|
||||
constexpr typename SmallMap<K, T, SmallSz>::Pair const&SmallMap<K, T, SmallSz>::access(
|
||||
PairVector const&pairs, KK const&k, bool &isNew) const {
|
||||
for (auto const&p : pairs) {
|
||||
if (p.key == k) {
|
||||
return &p;
|
||||
isNew = false;
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
isNew = true;
|
||||
return pairs.emplace_back();
|
||||
}
|
||||
|
||||
template<typename K, typename T>
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
template<typename KK>
|
||||
constexpr typename SmallMap<K, T>::Pair *&SmallMap<K, T>::access(
|
||||
Vector<Pair> &pairs, KK const&k) {
|
||||
constexpr typename SmallMap<K, T, SmallSz>::Pair &SmallMap<K, T, SmallSz>::access(
|
||||
PairVector &pairs, KK const&k, bool &isNew) {
|
||||
for (auto &p : pairs) {
|
||||
if (p.key == k) {
|
||||
return &p;
|
||||
isNew = false;
|
||||
return p;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
isNew = true;
|
||||
return pairs.emplace_back();
|
||||
}
|
||||
|
||||
}
|
||||
|
4
deps/ox/src/ox/std/test/CMakeLists.txt
vendored
4
deps/ox/src/ox/std/test/CMakeLists.txt
vendored
@ -12,8 +12,10 @@ add_test("[ox/std] ox_memcmp HIJKLMN != ABCDEFG" ${CMAKE_RUNTIME_OUTPUT_DIRECTOR
|
||||
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] itoa" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "itoa")
|
||||
add_test("[ox/std] BString" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "BString")
|
||||
add_test("[ox/std] IString" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "IString")
|
||||
add_test("[ox/std] String" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "String")
|
||||
add_test("[ox/std] SmallMap" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "SmallMap")
|
||||
add_test("[ox/std] SmallMap2" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "SmallMap2")
|
||||
add_test("[ox/std] Vector" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "Vector")
|
||||
add_test("[ox/std] HashMap" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "HashMap")
|
||||
add_test("[ox/std] HeapMgr" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest malloc)
|
||||
|
157
deps/ox/src/ox/std/test/tests.cpp
vendored
157
deps/ox/src/ox/std/test/tests.cpp
vendored
@ -6,13 +6,112 @@
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
#include "ox/std/def.hpp"
|
||||
#if __has_include(<chrono>)
|
||||
#include<chrono>
|
||||
#endif
|
||||
|
||||
#include <ox/std/def.hpp>
|
||||
#undef NDEBUG
|
||||
|
||||
#include <map>
|
||||
#include <ox/std/uuid.hpp>
|
||||
#include <ox/std/std.hpp>
|
||||
|
||||
[[nodiscard]]
|
||||
static uint64_t nowMs() {
|
||||
#if __has_include(<chrono>)
|
||||
using namespace std::chrono;
|
||||
return static_cast<uint64_t>(
|
||||
duration_cast<milliseconds>(
|
||||
system_clock::now().time_since_epoch()).count());
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
template<typename Map = ox::SmallMap<ox::String, ox::UUID>>
|
||||
uint64_t timeMapStrToUuid(int elemCnt, int lookups, uint64_t seed = 4321) noexcept {
|
||||
ox::UUID::seedGenerator({1234, seed});
|
||||
Map map;
|
||||
// setup test map
|
||||
for (int i = 0; i < elemCnt; ++i) {
|
||||
auto const uuid = ox::UUID::generate().unwrap();
|
||||
map[uuid.toString()] = uuid;
|
||||
}
|
||||
auto const keys = map.keys();
|
||||
ox::Random rand;
|
||||
// start
|
||||
auto const startTime = nowMs();
|
||||
for (int i = 0; i < lookups; ++i) {
|
||||
auto const&k = keys[rand.gen() % keys.size()];
|
||||
map[k];
|
||||
oxExpect(map[k], ox::UUID::fromString(k).unwrap());
|
||||
}
|
||||
return nowMs() - startTime;
|
||||
}
|
||||
|
||||
template<typename Map = ox::SmallMap<ox::UUID, ox::String>>
|
||||
uint64_t timeMapUuidToStr(int elemCnt, int lookups, uint64_t seed = 4321) noexcept {
|
||||
ox::UUID::seedGenerator({1234, seed});
|
||||
Map map;
|
||||
// setup test map
|
||||
for (int i = 0; i < elemCnt; ++i) {
|
||||
auto const uuid = ox::UUID::generate().unwrap();
|
||||
map[uuid] = uuid.toString();
|
||||
}
|
||||
auto const keys = map.keys();
|
||||
ox::Random rand;
|
||||
// start
|
||||
auto const startTime = nowMs();
|
||||
for (int i = 0; i < lookups; ++i) {
|
||||
auto const&k = keys[rand.gen() % keys.size()];
|
||||
oxExpect(map[k], k.toString());
|
||||
}
|
||||
return nowMs() - startTime;
|
||||
}
|
||||
|
||||
static ox::Error compareMaps(int lookupCnt = 1'000'000) {
|
||||
auto const seed = nowMs();
|
||||
uint64_t hashTime{};
|
||||
uint64_t smallTime{};
|
||||
int elemCnt = 1;
|
||||
oxOut("UUIDStr to UUID:\n\n");
|
||||
while (hashTime >= smallTime) {
|
||||
smallTime = timeMapStrToUuid<ox::SmallMap<ox::UUIDStr, ox::UUID>>(elemCnt, lookupCnt, seed);
|
||||
hashTime = timeMapStrToUuid<ox::HashMap<ox::UUIDStr, ox::UUID>>(elemCnt, lookupCnt, seed);
|
||||
oxOutf(
|
||||
"UUIDStr to UUID: elemCnt: {}, lookupCnt: {} - hash map time: {}ms, small map time: {}ms\n",
|
||||
elemCnt, lookupCnt, hashTime, smallTime);
|
||||
++elemCnt;
|
||||
}
|
||||
oxOut("\n\nString to UUID:\n\n");
|
||||
hashTime = 0;
|
||||
smallTime = 0;
|
||||
elemCnt = 1;
|
||||
while (hashTime >= smallTime) {
|
||||
smallTime = timeMapStrToUuid<ox::SmallMap<ox::String, ox::UUID>>(elemCnt, lookupCnt, seed);
|
||||
hashTime = timeMapStrToUuid<ox::HashMap<ox::String, ox::UUID>>(elemCnt, lookupCnt, seed);
|
||||
oxOutf(
|
||||
"String to UUID: elemCnt: {}, lookupCnt: {} - hash map time: {}ms, small map time: {}ms\n",
|
||||
elemCnt, lookupCnt, hashTime, smallTime);
|
||||
++elemCnt;
|
||||
}
|
||||
oxOut("\n\nUUID to UUIDStr:\n\n");
|
||||
hashTime = 0;
|
||||
smallTime = 0;
|
||||
elemCnt = 1;
|
||||
while (hashTime >= smallTime) {
|
||||
smallTime = timeMapUuidToStr<ox::SmallMap<ox::UUID, ox::UUIDStr>>(elemCnt, lookupCnt, seed);
|
||||
hashTime = timeMapUuidToStr<ox::HashMap<ox::UUID, ox::UUIDStr>>(elemCnt, lookupCnt, seed);
|
||||
oxOutf(
|
||||
"UUID to UUIDStr: elemCnt: {}, lookupCnt: {} - hash map time: {}ms, small map time: {}ms\n",
|
||||
elemCnt, lookupCnt, hashTime, smallTime);
|
||||
++elemCnt;
|
||||
}
|
||||
oxOut("\n");
|
||||
return {};
|
||||
}
|
||||
|
||||
static std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
{
|
||||
"malloc",
|
||||
@ -23,6 +122,8 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
auto a2 = static_cast<char*>(ox::heapmgr::malloc(5));
|
||||
oxAssert(a1 >= buff.front().unwrap() && a1 < buff.back().unwrap(), "malloc is broken");
|
||||
oxAssert(a2 >= buff.front().unwrap() && a2 < buff.back().unwrap() && a2 > a1 + 5, "malloc is broken");
|
||||
ox::heapmgr::free(a1);
|
||||
ox::heapmgr::free(a2);
|
||||
return OxError(0);
|
||||
}
|
||||
},
|
||||
@ -67,14 +168,14 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
}
|
||||
},
|
||||
{
|
||||
"BString",
|
||||
"IString",
|
||||
[]() {
|
||||
ox::IString<5> s;
|
||||
oxReturnError(s.append("A"));
|
||||
oxReturnError(s.append("B"));
|
||||
oxReturnError(s.append("9"));
|
||||
oxReturnError(s.append("C"));
|
||||
oxAssert(s == "AB9C", "BString append broken");
|
||||
oxAssert(s == "AB9C", "IString append broken");
|
||||
s = "asdf";
|
||||
oxAssert(s == "asdf", "String assign broken");
|
||||
oxAssert(s != "aoeu", "String assign broken");
|
||||
@ -134,10 +235,38 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
return OxError(0);
|
||||
}
|
||||
},
|
||||
{
|
||||
"SmallMap",
|
||||
[] {
|
||||
ox::SmallMap<ox::String, ox::String> map;
|
||||
map["asdf"] = "aoeu";
|
||||
oxExpect(map["asdf"], "aoeu");
|
||||
oxExpect(map.size(), 1u);
|
||||
oxExpect(map["aoeu"], "");
|
||||
oxExpect(map.size(), 2u);
|
||||
return OxError(0);
|
||||
}
|
||||
},
|
||||
{
|
||||
"SmallMap2",
|
||||
[] {
|
||||
ox::SmallMap<ox::String, int> si;
|
||||
si["asdf"] = 42;
|
||||
si["aoeu"] = 100;
|
||||
oxAssert(si["asdf"] == 42, "asdf != 42");
|
||||
oxAssert(si["aoeu"] == 100, "aoeu != 100");
|
||||
ox::SmallMap<int, int> ii;
|
||||
ii[4] = 42;
|
||||
ii[5] = 100;
|
||||
oxAssert(ii[4] == 42, "4 != 42");
|
||||
oxAssert(ii[5] == 100, "5 != 100");
|
||||
return OxError(0);
|
||||
}
|
||||
},
|
||||
{
|
||||
"HashMap",
|
||||
[] {
|
||||
ox::HashMap<const char*, int> si;
|
||||
ox::HashMap<ox::String, int> si;
|
||||
si["asdf"] = 42;
|
||||
si["aoeu"] = 100;
|
||||
oxAssert(si["asdf"] == 42, "asdf != 42");
|
||||
@ -150,6 +279,26 @@ static std::map<ox::StringView, ox::Error(*)()> tests = {
|
||||
return OxError(0);
|
||||
}
|
||||
},
|
||||
{
|
||||
"TimeSmallMapMillion",
|
||||
[] {
|
||||
timeMapStrToUuid<ox::SmallMap<ox::String, ox::UUID>>(1'000, 1'000'000);
|
||||
return ox::Error{};
|
||||
}
|
||||
},
|
||||
{
|
||||
"TimeHashMapMillion",
|
||||
[] {
|
||||
timeMapStrToUuid<ox::HashMap<ox::String, ox::UUID>>(1'000, 1'000'000);
|
||||
return ox::Error{};
|
||||
}
|
||||
},
|
||||
{
|
||||
"CompareMaps",
|
||||
[] {
|
||||
return compareMaps();
|
||||
}
|
||||
},
|
||||
{
|
||||
"Serialize-Int",
|
||||
[] {
|
||||
|
34
deps/ox/src/ox/std/uuid.hpp
vendored
34
deps/ox/src/ox/std/uuid.hpp
vendored
@ -8,9 +8,11 @@
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "bit.hpp"
|
||||
#include "ignore.hpp"
|
||||
#include "istring.hpp"
|
||||
#include "buffer.hpp"
|
||||
#include "hash.hpp"
|
||||
#include "random.hpp"
|
||||
#include "ranges.hpp"
|
||||
#include "stringview.hpp"
|
||||
@ -105,7 +107,7 @@ class UUID {
|
||||
static ox::Result<UUID> generate() noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr auto value() const noexcept {
|
||||
constexpr auto const&value() const noexcept {
|
||||
return m_value;
|
||||
}
|
||||
|
||||
@ -118,8 +120,8 @@ class UUID {
|
||||
return false;
|
||||
} else {
|
||||
constexpr uint64_t zero = 0;
|
||||
return ox::memcmp(&zero, m_value.data() + 0, 8) == 0
|
||||
&& ox::memcmp(&zero, m_value.data() + 8, 8) == 0;
|
||||
return memcmp(&zero, m_value.data() + 0, 8) == 0
|
||||
&& memcmp(&zero, m_value.data() + 8, 8) == 0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -146,6 +148,14 @@ class UUID {
|
||||
return out;
|
||||
}
|
||||
|
||||
constexpr bool operator==(UUID const&o) const noexcept {
|
||||
return m_value == o.m_value;
|
||||
}
|
||||
|
||||
constexpr bool operator!=(UUID const&o) const noexcept {
|
||||
return !operator==(o);
|
||||
}
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr ox::Error toString(Writer_c auto &writer) const noexcept {
|
||||
auto valueI = 0u;
|
||||
@ -184,6 +194,24 @@ class UUID {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
struct hash<ox::UUID> {
|
||||
[[nodiscard]]
|
||||
constexpr size_t operator()(ox::UUID v) const noexcept {
|
||||
size_t out{};
|
||||
if (std::is_constant_evaluated()) {
|
||||
for (auto i = 0u; i < sizeof(out); ++i) {
|
||||
out |= static_cast<size_t>(v.value()[i]) << (i * 8);
|
||||
}
|
||||
} else {
|
||||
memcpy(&out, &v, sizeof(out));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename T>
|
||||
constexpr Error model(T *io, ox::CommonPtrWith<UUID> auto *obj) noexcept {
|
||||
oxReturnError(io->template setTypeInfo<UUID>());
|
||||
|
@ -58,23 +58,25 @@ static class: public keel::Module {
|
||||
ox::Vector<keel::PackTransform> packTransforms() const noexcept final {
|
||||
return {
|
||||
// convert tilesheets to CompactTileSheets
|
||||
[](keel::Context &ctx, ox::Buffer &buff, ox::StringView typeId) -> ox::Error {
|
||||
[](keel::Context &ctx, ox::Buffer &buff, ox::StringView typeId) -> ox::Result<bool> {
|
||||
if (typeId == ox::ModelTypeId_v<TileSheetV1> ||
|
||||
typeId == ox::ModelTypeId_v<TileSheetV2> ||
|
||||
typeId == ox::ModelTypeId_v<TileSheetV3> ||
|
||||
typeId == ox::ModelTypeId_v<TileSheetV4>) {
|
||||
return keel::convertBuffToBuff<CompactTileSheet>(
|
||||
ctx, buff, ox::ClawFormat::Metal).moveTo(buff);
|
||||
oxReturnError(keel::convertBuffToBuff<CompactTileSheet>(
|
||||
ctx, buff, ox::ClawFormat::Metal).moveTo(buff));
|
||||
return true;
|
||||
}
|
||||
return {};
|
||||
return false;
|
||||
},
|
||||
[](keel::Context &ctx, ox::Buffer &buff, ox::StringView typeId) -> ox::Error {
|
||||
[](keel::Context &ctx, ox::Buffer &buff, ox::StringView typeId) -> ox::Result<bool> {
|
||||
if (typeId == ox::ModelTypeId_v<NostalgiaPalette> ||
|
||||
typeId == ox::ModelTypeId_v<PaletteV1>) {
|
||||
return keel::convertBuffToBuff<Palette>(
|
||||
ctx, buff, ox::ClawFormat::Metal).moveTo(buff);
|
||||
oxReturnError(keel::convertBuffToBuff<Palette>(
|
||||
ctx, buff, ox::ClawFormat::Metal).moveTo(buff));
|
||||
return true;
|
||||
}
|
||||
return {};
|
||||
return false;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
@ -12,7 +12,7 @@
|
||||
namespace keel {
|
||||
|
||||
class Context;
|
||||
using PackTransform = ox::Error(*)(Context&, ox::Buffer &clawData, ox::StringView);
|
||||
using PackTransform = ox::Result<bool>(*)(Context&, ox::Buffer &clawData, ox::StringView);
|
||||
|
||||
class Context {
|
||||
public:
|
||||
|
@ -162,11 +162,12 @@ ox::Result<ox::Buffer> convertBuffToBuff(
|
||||
}
|
||||
|
||||
template<typename From, typename To, ox::ClawFormat fmt = ox::ClawFormat::Metal>
|
||||
auto transformRule(keel::Context &ctx, ox::Buffer &buff, ox::StringView typeId) noexcept -> ox::Error {
|
||||
ox::Result<bool> transformRule(keel::Context &ctx, ox::Buffer &buff, ox::StringView typeId) noexcept {
|
||||
if (typeId == ox::ModelTypeId_v<From>) {
|
||||
oxReturnError(keel::convertBuffToBuff<To>(ctx, buff, fmt).moveTo(buff));
|
||||
return true;
|
||||
}
|
||||
return {};
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
|
@ -102,9 +102,13 @@ ox::Result<ox::String> uuidToPath(Context &ctx, ox::UUID const&uuid) noexcept {
|
||||
}
|
||||
|
||||
ox::Error performPackTransforms(Context &ctx, ox::Buffer &clawData) noexcept {
|
||||
oxRequire(typeId, readAssetTypeId(clawData).to<ox::String>());
|
||||
oxRequireM(typeId, readAssetTypeId(clawData));
|
||||
for (auto const tr : packTransforms(ctx)) {
|
||||
oxReturnError(tr(ctx, clawData, typeId));
|
||||
bool changed{};
|
||||
oxReturnError(tr(ctx, clawData, typeId).moveTo(changed));
|
||||
if (changed) {
|
||||
oxReturnError(readAssetTypeId(clawData).moveTo(typeId));
|
||||
}
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user