[ox/std] Cleanup SmallMap, make it easier to make potential changes

This commit is contained in:
Gary Talent 2024-05-10 23:58:07 -05:00
parent b66f61c217
commit aeb1ef3b12

View File

@ -29,11 +29,12 @@ class SmallMap {
K key = {}; K key = {};
T value{}; T value{};
}; };
using PairVector = Vector<Pair>;
Vector<K> m_keys; Vector<K> m_keys;
Vector<Pair> m_pairs; PairVector m_pairs;
public: public:
explicit constexpr SmallMap(std::size_t size = 127); constexpr SmallMap() = default;
constexpr SmallMap(SmallMap const&other); constexpr SmallMap(SmallMap const&other);
@ -68,17 +69,13 @@ class SmallMap {
private: private:
template<typename KK> template<typename KK>
constexpr Pair const&access(Vector<Pair> 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(Vector<Pair> &pairs, KK const&key, bool &isNew); constexpr Pair &access(PairVector &pairs, KK const&key, bool &isNew);
}; };
template<typename K, typename T>
constexpr SmallMap<K, T>::SmallMap(std::size_t size): m_pairs(size) {
}
template<typename K, typename T> template<typename K, typename T>
constexpr SmallMap<K, T>::SmallMap(SmallMap<K, T> const&other) { constexpr SmallMap<K, T>::SmallMap(SmallMap<K, T> const&other) {
m_pairs = other.m_pairs; m_pairs = other.m_pairs;
@ -188,13 +185,12 @@ constexpr Vector<K> const&SmallMap<K, T>::keys() const noexcept {
template<typename K, typename T> template<typename K, typename T>
constexpr void SmallMap<K, T>::clear() { constexpr void SmallMap<K, T>::clear() {
m_pairs.clear(); m_pairs.clear();
m_pairs.resize(127);
} }
template<typename K, typename T> template<typename K, typename T>
template<typename KK> template<typename KK>
constexpr typename SmallMap<K, T>::Pair const&SmallMap<K, T>::access( constexpr typename SmallMap<K, T>::Pair const&SmallMap<K, T>::access(
Vector<Pair> 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;
@ -209,7 +205,7 @@ constexpr typename SmallMap<K, T>::Pair const&SmallMap<K, T>::access(
template<typename K, typename T> template<typename K, typename T>
template<typename KK> template<typename KK>
constexpr typename SmallMap<K, T>::Pair &SmallMap<K, T>::access( constexpr typename SmallMap<K, T>::Pair &SmallMap<K, T>::access(
Vector<Pair> &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;