[ox/std] SmallMap fixes, add findIdx function
This commit is contained in:
parent
98ddb08abd
commit
d66da85753
14
deps/ox/src/ox/std/algorithm.hpp
vendored
14
deps/ox/src/ox/std/algorithm.hpp
vendored
@ -9,13 +9,25 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include "def.hpp"
|
#include "def.hpp"
|
||||||
|
#include "error.hpp"
|
||||||
|
|
||||||
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
|
OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage)
|
||||||
|
|
||||||
namespace ox {
|
namespace ox {
|
||||||
|
|
||||||
template<typename It, typename T>
|
template<typename It, typename T>
|
||||||
constexpr It find(It begin, It end, const T &value) {
|
constexpr ox::Result<size_t> findIdx(It begin, It end, T const&value) {
|
||||||
|
auto it = begin;
|
||||||
|
for (; it != end; ++it) {
|
||||||
|
if (*it == value) {
|
||||||
|
return it.offset();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return ox::Error{1, "item not found"};
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename It, typename T>
|
||||||
|
constexpr It find(It begin, It end, T const&value) {
|
||||||
for (; begin != end; ++begin) {
|
for (; begin != end; ++begin) {
|
||||||
if (*begin == value) {
|
if (*begin == value) {
|
||||||
return begin;
|
return begin;
|
||||||
|
27
deps/ox/src/ox/std/smallmap.hpp
vendored
27
deps/ox/src/ox/std/smallmap.hpp
vendored
@ -86,10 +86,10 @@ class SmallMap {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
template<typename KK>
|
template<typename KK>
|
||||||
constexpr Pair const&access(PairVector const&pairs, KK const&key, bool &isNew) const;
|
constexpr Pair const*access(PairVector const&pairs, KK const&key, bool &isNew) const;
|
||||||
|
|
||||||
template<typename KK>
|
template<typename KK>
|
||||||
constexpr Pair &access(PairVector &pairs, KK const&key, bool &isNew);
|
constexpr Pair *access(PairVector &pairs, KK const&key, bool &isNew);
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -129,7 +129,7 @@ constexpr SmallMap<K, T, SmallSz> &SmallMap<K, T, SmallSz>::operator=(SmallMap<K
|
|||||||
template<typename K, typename T, size_t SmallSz>
|
template<typename K, typename T, size_t SmallSz>
|
||||||
constexpr T &SmallMap<K, T, SmallSz>::operator[](MaybeView_t<K> const&k) {
|
constexpr T &SmallMap<K, T, SmallSz>::operator[](MaybeView_t<K> const&k) {
|
||||||
bool isNew{};
|
bool isNew{};
|
||||||
auto p = &access(m_pairs, k, isNew);
|
auto p = access(m_pairs, k, isNew);
|
||||||
if (isNew) {
|
if (isNew) {
|
||||||
p->key = k;
|
p->key = k;
|
||||||
}
|
}
|
||||||
@ -138,7 +138,8 @@ constexpr T &SmallMap<K, T, SmallSz>::operator[](MaybeView_t<K> const&k) {
|
|||||||
|
|
||||||
template<typename K, typename T, size_t SmallSz>
|
template<typename K, typename T, size_t SmallSz>
|
||||||
constexpr Result<T*> SmallMap<K, T, SmallSz>::at(MaybeView_t<K> const&k) noexcept {
|
constexpr Result<T*> SmallMap<K, T, SmallSz>::at(MaybeView_t<K> const&k) noexcept {
|
||||||
auto p = access(m_pairs, k);
|
bool isNew{};
|
||||||
|
auto p = access(m_pairs, k, isNew);
|
||||||
if (!p) {
|
if (!p) {
|
||||||
return {nullptr, ox::Error(1, "value not found for given key")};
|
return {nullptr, ox::Error(1, "value not found for given key")};
|
||||||
}
|
}
|
||||||
@ -147,7 +148,8 @@ constexpr Result<T*> SmallMap<K, T, SmallSz>::at(MaybeView_t<K> const&k) noexcep
|
|||||||
|
|
||||||
template<typename K, typename T, size_t SmallSz>
|
template<typename K, typename T, size_t SmallSz>
|
||||||
constexpr Result<const T*> SmallMap<K, T, SmallSz>::at(MaybeView_t<K> const&k) const noexcept {
|
constexpr Result<const T*> SmallMap<K, T, SmallSz>::at(MaybeView_t<K> const&k) const noexcept {
|
||||||
auto p = access(m_pairs, k);
|
bool isNew{};
|
||||||
|
auto p = access(m_pairs, k, isNew);
|
||||||
if (!p) {
|
if (!p) {
|
||||||
return {nullptr, ox::Error(1, "value not found for given key")};
|
return {nullptr, ox::Error(1, "value not found for given key")};
|
||||||
}
|
}
|
||||||
@ -168,7 +170,8 @@ constexpr void SmallMap<K, T, SmallSz>::erase(MaybeView_t<K> const&k) {
|
|||||||
|
|
||||||
template<typename K, typename T, size_t SmallSz>
|
template<typename K, typename T, size_t SmallSz>
|
||||||
constexpr bool SmallMap<K, T, SmallSz>::contains(MaybeView_t<K> const&k) const noexcept {
|
constexpr bool SmallMap<K, T, SmallSz>::contains(MaybeView_t<K> const&k) const noexcept {
|
||||||
return access(m_pairs, k) != nullptr;
|
bool isNew{};
|
||||||
|
return access(m_pairs, k, isNew) != nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename K, typename T, size_t SmallSz>
|
template<typename K, typename T, size_t SmallSz>
|
||||||
@ -218,30 +221,30 @@ constexpr void SmallMap<K, T, SmallSz>::clear() {
|
|||||||
|
|
||||||
template<typename K, typename T, size_t SmallSz>
|
template<typename K, typename T, size_t SmallSz>
|
||||||
template<typename KK>
|
template<typename KK>
|
||||||
constexpr typename SmallMap<K, T, SmallSz>::Pair const&SmallMap<K, T, SmallSz>::access(
|
constexpr typename SmallMap<K, T, SmallSz>::Pair const*SmallMap<K, T, SmallSz>::access(
|
||||||
PairVector const&pairs, KK const&k, bool &isNew) const {
|
PairVector const&pairs, KK const&k, bool &isNew) const {
|
||||||
for (auto const&p : pairs) {
|
for (auto const&p : pairs) {
|
||||||
if (p.key == k) {
|
if (p.key == k) {
|
||||||
isNew = false;
|
isNew = false;
|
||||||
return p;
|
return &p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
isNew = true;
|
isNew = true;
|
||||||
return pairs.emplace_back();
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename K, typename T, size_t SmallSz>
|
template<typename K, typename T, size_t SmallSz>
|
||||||
template<typename KK>
|
template<typename KK>
|
||||||
constexpr typename SmallMap<K, T, SmallSz>::Pair &SmallMap<K, T, SmallSz>::access(
|
constexpr typename SmallMap<K, T, SmallSz>::Pair *SmallMap<K, T, SmallSz>::access(
|
||||||
PairVector &pairs, KK const&k, bool &isNew) {
|
PairVector &pairs, KK const&k, bool &isNew) {
|
||||||
for (auto &p : pairs) {
|
for (auto &p : pairs) {
|
||||||
if (p.key == k) {
|
if (p.key == k) {
|
||||||
isNew = false;
|
isNew = false;
|
||||||
return p;
|
return &p;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
isNew = true;
|
isNew = true;
|
||||||
return pairs.emplace_back();
|
return &pairs.emplace_back();
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, typename K, typename V, size_t SmallSz>
|
template<typename T, typename K, typename V, size_t SmallSz>
|
||||||
|
1
deps/ox/src/ox/std/test/CMakeLists.txt
vendored
1
deps/ox/src/ox/std/test/CMakeLists.txt
vendored
@ -18,6 +18,7 @@ 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] SmallMap2" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "SmallMap2")
|
||||||
add_test("[ox/std] Vector" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "Vector")
|
add_test("[ox/std] Vector" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "Vector")
|
||||||
add_test("[ox/std] Vector::shrink_to_fit" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "Vector::shrink_to_fit")
|
add_test("[ox/std] Vector::shrink_to_fit" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "Vector::shrink_to_fit")
|
||||||
|
add_test("[ox/std] findIdx" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "findIdx")
|
||||||
add_test("[ox/std] HashMap" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "HashMap")
|
add_test("[ox/std] HashMap" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "HashMap")
|
||||||
add_test("[ox/std] HeapMgr" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest malloc)
|
add_test("[ox/std] HeapMgr" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest malloc)
|
||||||
add_test("[ox/std] Serialize-Int" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "Serialize-Int")
|
add_test("[ox/std] Serialize-Int" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "Serialize-Int")
|
||||||
|
27
deps/ox/src/ox/std/test/tests.cpp
vendored
27
deps/ox/src/ox/std/test/tests.cpp
vendored
@ -267,6 +267,20 @@ OX_CLANG_NOWARN_END
|
|||||||
return ox::Error{};
|
return ox::Error{};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"findIdx",
|
||||||
|
[] {
|
||||||
|
ox::Vector<ox::IString<8>> const v {"zero", "one", "two", "three", "four"};
|
||||||
|
oxExpect(ox::findIdx(v.begin(), v.end(), "zero").or_value(5), 0u);
|
||||||
|
oxExpect(ox::findIdx(v.begin(), v.end(), "one").or_value(5), 1u);
|
||||||
|
oxExpect(ox::findIdx(v.begin(), v.end(), "two").or_value(5), 2u);
|
||||||
|
oxExpect(ox::findIdx(v.begin(), v.end(), "three").or_value(5), 3u);
|
||||||
|
oxExpect(ox::findIdx(v.begin(), v.end(), "four").or_value(5), 4u);
|
||||||
|
oxExpect(ox::findIdx(v.begin(), v.end(), "five").or_value(5), 5u);
|
||||||
|
oxExpect(ox::findIdx(v.begin(), v.end(), "six").or_value(6), 6u);
|
||||||
|
return ox::Error{};
|
||||||
|
}
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"SmallMap",
|
"SmallMap",
|
||||||
[] {
|
[] {
|
||||||
@ -276,7 +290,18 @@ OX_CLANG_NOWARN_END
|
|||||||
oxExpect(map.size(), 1u);
|
oxExpect(map.size(), 1u);
|
||||||
oxExpect(map["aoeu"], "");
|
oxExpect(map["aoeu"], "");
|
||||||
oxExpect(map.size(), 2u);
|
oxExpect(map.size(), 2u);
|
||||||
return ox::Error(0);
|
ox::SmallMap<ox::String, ox::String> cmap;
|
||||||
|
cmap["asdf"] = "aoeu";
|
||||||
|
auto constexpr constTest = [](ox::SmallMap<ox::String, ox::String> const&map) {
|
||||||
|
OX_REQUIRE(asdf, map.at("asdf"));
|
||||||
|
oxExpect(*asdf, "aoeu");
|
||||||
|
oxExpect(map.size(), 1u);
|
||||||
|
auto const aoeu = map.at("aoeu");
|
||||||
|
oxExpect(aoeu.ok(), false);
|
||||||
|
oxExpect(map.size(), 1u);
|
||||||
|
return ox::Error{};
|
||||||
|
};
|
||||||
|
return constTest(cmap);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
Loading…
Reference in New Issue
Block a user