[ox/std] Add SmallMap::values()
This commit is contained in:
parent
d66da85753
commit
75819a1797
31
deps/ox/src/ox/std/smallmap.hpp
vendored
31
deps/ox/src/ox/std/smallmap.hpp
vendored
@ -62,6 +62,9 @@ class SmallMap {
|
||||
[[nodiscard]]
|
||||
constexpr Vector<K> keys() const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr Vector<T> values() const noexcept;
|
||||
|
||||
[[nodiscard]]
|
||||
constexpr K const&key(size_t i) const noexcept;
|
||||
|
||||
@ -91,6 +94,9 @@ class SmallMap {
|
||||
template<typename KK>
|
||||
constexpr Pair *access(PairVector &pairs, KK const&key, bool &isNew);
|
||||
|
||||
template<typename KK>
|
||||
constexpr Pair *accessNoCreate(PairVector &pairs, KK const&key);
|
||||
|
||||
};
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
@ -138,8 +144,7 @@ constexpr T &SmallMap<K, T, SmallSz>::operator[](MaybeView_t<K> const&k) {
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr Result<T*> SmallMap<K, T, SmallSz>::at(MaybeView_t<K> const&k) noexcept {
|
||||
bool isNew{};
|
||||
auto p = access(m_pairs, k, isNew);
|
||||
auto const p = accessNoCreate(m_pairs, k);
|
||||
if (!p) {
|
||||
return {nullptr, ox::Error(1, "value not found for given key")};
|
||||
}
|
||||
@ -189,6 +194,16 @@ constexpr Vector<K> SmallMap<K, T, SmallSz>::keys() const noexcept {
|
||||
return keys;
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr Vector<T> SmallMap<K, T, SmallSz>::values() const noexcept {
|
||||
ox::Vector<T> 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;
|
||||
@ -247,6 +262,18 @@ constexpr typename SmallMap<K, T, SmallSz>::Pair *SmallMap<K, T, SmallSz>::acces
|
||||
return &pairs.emplace_back();
|
||||
}
|
||||
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
template<typename KK>
|
||||
constexpr typename SmallMap<K, T, SmallSz>::Pair *SmallMap<K, T, SmallSz>::accessNoCreate(
|
||||
PairVector &pairs, KK const&k) {
|
||||
for (auto &p : pairs) {
|
||||
if (p.key == k) {
|
||||
return &p;
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template<typename T, typename K, typename V, size_t SmallSz>
|
||||
constexpr Error model(T *io, ox::CommonPtrWith<SmallMap<K, V, SmallSz>> auto *obj) noexcept {
|
||||
using Map = SmallMap<K, V, SmallSz>;
|
||||
|
Loading…
Reference in New Issue
Block a user