|
|
@@ -20,37 +20,34 @@ |
|
|
|
namespace lol |
|
|
|
{ |
|
|
|
|
|
|
|
/* A stupidly linear map for now */ |
|
|
|
/* A stupidly linear map for now. */ |
|
|
|
template<typename K, typename V> class Map : protected Hash<K> |
|
|
|
{ |
|
|
|
public: |
|
|
|
/* I choose to make this inline because passing the key by reference |
|
|
|
* is usually suboptimal. */ |
|
|
|
inline V const& operator[] (K const &key) const |
|
|
|
{ |
|
|
|
/* Look for the hash in our table and return the value. */ |
|
|
|
uint32_t hash = ((Hash<K> const &)*this)(key); |
|
|
|
for (int i = 0; i < m_array.Count(); ++i) |
|
|
|
if (m_array[i].m1 == hash) |
|
|
|
if (m_array[i].m2 == key) |
|
|
|
return m_array[i].m3; |
|
|
|
/* XXX: this in an error! */ |
|
|
|
return V(); |
|
|
|
} |
|
|
|
|
|
|
|
inline V & operator[] (K const &key) |
|
|
|
{ |
|
|
|
return const_cast<V &>((const_cast<Map<K,V> const &>(*this))[key]); |
|
|
|
} |
|
|
|
|
|
|
|
inline V & Set(K const &key, V const &val) |
|
|
|
{ |
|
|
|
/* Look for the hash in our table and return the value if found. */ |
|
|
|
uint32_t hash = ((Hash<K> const &)*this)(key); |
|
|
|
for (int i = 0; i < m_array.Count(); ++i) |
|
|
|
if (m_array[i].m1 == hash) |
|
|
|
if (m_array[i].m2 == key) |
|
|
|
{ |
|
|
|
m_array[i].m3.~V(); |
|
|
|
return m_array[i].m3 = val; |
|
|
|
} |
|
|
|
|
|
|
|
m_array.Push(hash, key, val); |
|
|
|
return m_array[i].m3; |
|
|
|
/* If not found, insert a new value. */ |
|
|
|
m_array.Push(hash, key, V()); |
|
|
|
return m_array.Last().m3; |
|
|
|
} |
|
|
|
|
|
|
@@ -66,6 +63,16 @@ public: |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
inline bool HasKey(K const &key) |
|
|
|
{ |
|
|
|
uint32_t hash = ((Hash<K> const &)*this)(key); |
|
|
|
for (int i = 0; i < m_array.Count(); ++i) |
|
|
|
if (m_array[i].m1 == hash) |
|
|
|
if (m_array[i].m2 == key) |
|
|
|
return true; |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
private: |
|
|
|
Array<uint32_t, K, V> m_array; |
|
|
|
}; |
|
|
|