Browse Source

core: implement a simple linear search map; the API is here, we just

need to make it O(logn) or even O(1) now.
legacy
Sam Hocevar sam 12 years ago
parent
commit
38d4d0302e
1 changed files with 49 additions and 1 deletions
  1. +49
    -1
      src/lol/core/map.h

+ 49
- 1
src/lol/core/map.h View File

@@ -20,7 +20,55 @@
namespace lol
{

template<typename K, typename V> class Map;
/* A stupidly linear map for now */
template<typename K, typename V> class Map : protected Hash<K>
{
public:
inline V const& operator[] (K const &key) const
{
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;
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)
{
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.Last().m3;
}

inline void Remove(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)
{
m_array.Remove(i);
return;
}
}

private:
Array<uint32_t, K, V> m_array;
};

} /* namespace lol */



Loading…
Cancel
Save