[ox/std] Add small sz option to SmallMap

This commit is contained in:
Gary Talent 2024-05-11 00:53:31 -05:00
parent e8041121d0
commit 30797c710b

View File

@ -17,7 +17,7 @@
namespace ox { namespace ox {
template<typename K, typename T> template<typename K, typename T, size_t SmallSz = 0>
class SmallMap { class SmallMap {
public: public:
@ -29,7 +29,7 @@ class SmallMap {
K key = {}; K key = {};
T value{}; T value{};
}; };
using PairVector = Vector<Pair>; using PairVector = Vector<Pair, SmallSz>;
Vector<K> m_keys; Vector<K> m_keys;
PairVector m_pairs; PairVector m_pairs;
@ -76,24 +76,24 @@ class SmallMap {
}; };
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
constexpr SmallMap<K, T>::SmallMap(SmallMap<K, T> const&other) { constexpr SmallMap<K, T, SmallSz>::SmallMap(SmallMap<K, T, SmallSz> const&other) {
m_pairs = other.m_pairs; m_pairs = other.m_pairs;
} }
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
constexpr SmallMap<K, T>::SmallMap(SmallMap<K, T> &&other) noexcept { constexpr SmallMap<K, T, SmallSz>::SmallMap(SmallMap<K, T, SmallSz> &&other) noexcept {
m_keys = std::move(other.m_keys); m_keys = std::move(other.m_keys);
m_pairs = std::move(other.m_pairs); m_pairs = std::move(other.m_pairs);
} }
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
constexpr SmallMap<K, T>::~SmallMap() { constexpr SmallMap<K, T, SmallSz>::~SmallMap() {
clear(); clear();
} }
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
constexpr bool SmallMap<K, T>::operator==(SmallMap const&other) const { constexpr bool SmallMap<K, T, SmallSz>::operator==(SmallMap const&other) const {
if (m_keys != other.m_keys) { if (m_keys != other.m_keys) {
return false; return false;
} }
@ -106,8 +106,8 @@ constexpr bool SmallMap<K, T>::operator==(SmallMap const&other) const {
return true; return true;
} }
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
constexpr SmallMap<K, T> &SmallMap<K, T>::operator=(SmallMap<K, T> const&other) { constexpr SmallMap<K, T, SmallSz> &SmallMap<K, T, SmallSz>::operator=(SmallMap<K, T, SmallSz> const&other) {
if (this != &other) { if (this != &other) {
clear(); clear();
m_keys = other.m_keys; m_keys = other.m_keys;
@ -116,8 +116,8 @@ constexpr SmallMap<K, T> &SmallMap<K, T>::operator=(SmallMap<K, T> const&other)
return *this; return *this;
} }
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
constexpr SmallMap<K, T> &SmallMap<K, T>::operator=(SmallMap<K, T> &&other) noexcept { constexpr SmallMap<K, T, SmallSz> &SmallMap<K, T, SmallSz>::operator=(SmallMap<K, T, SmallSz> &&other) noexcept {
if (this != &other) { if (this != &other) {
clear(); clear();
m_keys = std::move(other.m_keys); 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; return *this;
} }
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
constexpr T &SmallMap<K, T>::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) {
@ -136,8 +136,8 @@ constexpr T &SmallMap<K, T>::operator[](MaybeView_t<K> const&k) {
return p->value; return p->value;
} }
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
constexpr Result<T*> SmallMap<K, T>::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); auto p = access(m_pairs, k);
if (!p) { if (!p) {
return {nullptr, OxError(1, "value not found for given key")}; 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; return &p->value;
} }
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
constexpr Result<const T*> SmallMap<K, T>::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); auto p = access(m_pairs, k);
if (!p) { if (!p) {
return {nullptr, OxError(1, "value not found for given key")}; 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; return &p->value;
} }
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
constexpr void SmallMap<K, T>::erase(MaybeView_t<K> const&k) { constexpr void SmallMap<K, T, SmallSz>::erase(MaybeView_t<K> const&k) {
size_t i{}; size_t i{};
for (auto const&p : m_pairs) { for (auto const&p : m_pairs) {
if (k == p.key) { 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); std::ignore = m_keys.erase(i);
} }
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
constexpr bool SmallMap<K, T>::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; return access(m_pairs, k) != nullptr;
} }
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
constexpr std::size_t SmallMap<K, T>::size() const noexcept { constexpr std::size_t SmallMap<K, T, SmallSz>::size() const noexcept {
return m_keys.size(); return m_keys.size();
} }
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
constexpr Vector<K> const&SmallMap<K, T>::keys() const noexcept { constexpr Vector<K> const&SmallMap<K, T, SmallSz>::keys() const noexcept {
return m_keys; return m_keys;
} }
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
constexpr void SmallMap<K, T>::clear() { constexpr void SmallMap<K, T, SmallSz>::clear() {
m_pairs.clear(); m_pairs.clear();
} }
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
template<typename KK> 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 { 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) {
@ -202,9 +202,9 @@ constexpr typename SmallMap<K, T>::Pair const&SmallMap<K, T>::access(
return pairs.emplace_back(); return pairs.emplace_back();
} }
template<typename K, typename T> template<typename K, typename T, size_t SmallSz>
template<typename KK> 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) { PairVector &pairs, KK const&k, bool &isNew) {
for (auto &p : pairs) { for (auto &p : pairs) {
if (p.key == k) { if (p.key == k) {