[ox/std] Add small sz option to SmallMap
This commit is contained in:
parent
e8041121d0
commit
30797c710b
68
deps/ox/src/ox/std/smallmap.hpp
vendored
68
deps/ox/src/ox/std/smallmap.hpp
vendored
@ -17,7 +17,7 @@
|
||||
|
||||
namespace ox {
|
||||
|
||||
template<typename K, typename T>
|
||||
template<typename K, typename T, size_t SmallSz = 0>
|
||||
class SmallMap {
|
||||
|
||||
public:
|
||||
@ -29,7 +29,7 @@ class SmallMap {
|
||||
K key = {};
|
||||
T value{};
|
||||
};
|
||||
using PairVector = Vector<Pair>;
|
||||
using PairVector = Vector<Pair, SmallSz>;
|
||||
Vector<K> m_keys;
|
||||
PairVector m_pairs;
|
||||
|
||||
@ -76,24 +76,24 @@ class SmallMap {
|
||||
|
||||
};
|
||||
|
||||
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 {
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr SmallMap<K, T, SmallSz>::SmallMap(SmallMap<K, T, SmallSz> &&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() {
|
||||
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 {
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr bool SmallMap<K, T, SmallSz>::operator==(SmallMap const&other) const {
|
||||
if (m_keys != other.m_keys) {
|
||||
return false;
|
||||
}
|
||||
@ -106,8 +106,8 @@ constexpr bool SmallMap<K, T>::operator==(SmallMap const&other) const {
|
||||
return true;
|
||||
}
|
||||
|
||||
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;
|
||||
@ -116,8 +116,8 @@ constexpr SmallMap<K, T> &SmallMap<K, T>::operator=(SmallMap<K, T> const&other)
|
||||
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);
|
||||
@ -126,8 +126,8 @@ constexpr SmallMap<K, T> &SmallMap<K, T>::operator=(SmallMap<K, T> &&other) noex
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename K, typename T>
|
||||
constexpr T &SmallMap<K, T>::operator[](MaybeView_t<K> const&k) {
|
||||
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) {
|
||||
@ -136,8 +136,8 @@ constexpr T &SmallMap<K, T>::operator[](MaybeView_t<K> const&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")};
|
||||
@ -145,8 +145,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")};
|
||||
@ -154,8 +154,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) {
|
||||
@ -167,29 +167,29 @@ constexpr void SmallMap<K, T>::erase(MaybeView_t<K> const&k) {
|
||||
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 {
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr std::size_t SmallMap<K, T, SmallSz>::size() const noexcept {
|
||||
return m_keys.size();
|
||||
}
|
||||
|
||||
template<typename K, typename T>
|
||||
constexpr Vector<K> const&SmallMap<K, T>::keys() const noexcept {
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr Vector<K> const&SmallMap<K, T, SmallSz>::keys() const noexcept {
|
||||
return m_keys;
|
||||
}
|
||||
|
||||
template<typename K, typename T>
|
||||
constexpr void SmallMap<K, T>::clear() {
|
||||
template<typename K, typename T, size_t SmallSz>
|
||||
constexpr void SmallMap<K, T, SmallSz>::clear() {
|
||||
m_pairs.clear();
|
||||
}
|
||||
|
||||
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(
|
||||
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) {
|
||||
@ -202,9 +202,9 @@ constexpr typename SmallMap<K, T>::Pair const&SmallMap<K, T>::access(
|
||||
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(
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user