diff --git a/deps/ox/src/ox/std/algorithm.hpp b/deps/ox/src/ox/std/algorithm.hpp index c6c9bd62..72ff5532 100644 --- a/deps/ox/src/ox/std/algorithm.hpp +++ b/deps/ox/src/ox/std/algorithm.hpp @@ -9,13 +9,25 @@ #pragma once #include "def.hpp" +#include "error.hpp" OX_CLANG_NOWARN_BEGIN(-Wunsafe-buffer-usage) namespace ox { template -constexpr It find(It begin, It end, const T &value) { +constexpr ox::Result 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 +constexpr It find(It begin, It end, T const&value) { for (; begin != end; ++begin) { if (*begin == value) { return begin; diff --git a/deps/ox/src/ox/std/smallmap.hpp b/deps/ox/src/ox/std/smallmap.hpp index 0c3ab260..7355d32d 100644 --- a/deps/ox/src/ox/std/smallmap.hpp +++ b/deps/ox/src/ox/std/smallmap.hpp @@ -86,10 +86,10 @@ class SmallMap { private: template - 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 - 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 &SmallMap::operator=(SmallMap constexpr T &SmallMap::operator[](MaybeView_t const&k) { bool isNew{}; - auto p = &access(m_pairs, k, isNew); + auto p = access(m_pairs, k, isNew); if (isNew) { p->key = k; } @@ -138,7 +138,8 @@ constexpr T &SmallMap::operator[](MaybeView_t const&k) { template constexpr Result SmallMap::at(MaybeView_t const&k) noexcept { - auto p = access(m_pairs, k); + bool isNew{}; + auto p = access(m_pairs, k, isNew); if (!p) { return {nullptr, ox::Error(1, "value not found for given key")}; } @@ -147,7 +148,8 @@ constexpr Result SmallMap::at(MaybeView_t const&k) noexcep template constexpr Result SmallMap::at(MaybeView_t const&k) const noexcept { - auto p = access(m_pairs, k); + bool isNew{}; + auto p = access(m_pairs, k, isNew); if (!p) { return {nullptr, ox::Error(1, "value not found for given key")}; } @@ -168,7 +170,8 @@ constexpr void SmallMap::erase(MaybeView_t const&k) { template constexpr bool SmallMap::contains(MaybeView_t const&k) const noexcept { - return access(m_pairs, k) != nullptr; + bool isNew{}; + return access(m_pairs, k, isNew) != nullptr; } template @@ -218,30 +221,30 @@ constexpr void SmallMap::clear() { template template -constexpr typename SmallMap::Pair const&SmallMap::access( +constexpr typename SmallMap::Pair const*SmallMap::access( PairVector const&pairs, KK const&k, bool &isNew) const { for (auto const&p : pairs) { if (p.key == k) { isNew = false; - return p; + return &p; } } isNew = true; - return pairs.emplace_back(); + return nullptr; } template template -constexpr typename SmallMap::Pair &SmallMap::access( +constexpr typename SmallMap::Pair *SmallMap::access( PairVector &pairs, KK const&k, bool &isNew) { for (auto &p : pairs) { if (p.key == k) { isNew = false; - return p; + return &p; } } isNew = true; - return pairs.emplace_back(); + return &pairs.emplace_back(); } template diff --git a/deps/ox/src/ox/std/test/CMakeLists.txt b/deps/ox/src/ox/std/test/CMakeLists.txt index 1ecad4ed..2fa8f288 100644 --- a/deps/ox/src/ox/std/test/CMakeLists.txt +++ b/deps/ox/src/ox/std/test/CMakeLists.txt @@ -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] 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] findIdx" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "findIdx") 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] Serialize-Int" ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/StdTest "Serialize-Int") diff --git a/deps/ox/src/ox/std/test/tests.cpp b/deps/ox/src/ox/std/test/tests.cpp index 6325d151..ffef8b13 100644 --- a/deps/ox/src/ox/std/test/tests.cpp +++ b/deps/ox/src/ox/std/test/tests.cpp @@ -267,6 +267,20 @@ OX_CLANG_NOWARN_END return ox::Error{}; } }, + { + "findIdx", + [] { + ox::Vector> 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", [] { @@ -276,7 +290,18 @@ OX_CLANG_NOWARN_END oxExpect(map.size(), 1u); oxExpect(map["aoeu"], ""); oxExpect(map.size(), 2u); - return ox::Error(0); + ox::SmallMap cmap; + cmap["asdf"] = "aoeu"; + auto constexpr constTest = [](ox::SmallMap 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); } }, {