Преглед изворни кода

base: map methods can now take any type of object that is equatable to the key type and can be hashed by the key type specialization of the Hash class (and added string/char const* compatibility)

legacy
Benlitz benlitz пре 12 година
родитељ
комит
5f72c16f60
3 измењених фајлова са 41 додато и 4 уклоњено
  1. +10
    -0
      src/base/hash.cpp
  2. +2
    -0
      src/lol/base/hash.h
  3. +29
    -4
      src/lol/base/map.h

+ 10
- 0
src/base/hash.cpp Прегледај датотеку

@@ -167,10 +167,20 @@ uint32_t Hash<char const *>::operator ()(char const *s) const
return HashCharString(s);
}

uint32_t Hash<char const *>::operator ()(String const &s) const
{
return HashCharString(&s[0]);
}

uint32_t Hash<String>::operator ()(String const &s) const
{
return HashCharString(&s[0]);
}

uint32_t Hash<String>::operator ()(char const *s) const
{
return HashCharString(s);
}

} /* namespace lol */


+ 2
- 0
src/lol/base/hash.h Прегледај датотеку

@@ -40,12 +40,14 @@ template<> class Hash<char const *>
{
public:
uint32_t operator()(char const *x) const;
uint32_t operator()(String const &s) const;
};

template<> class Hash<String>
{
public:
uint32_t operator()(String const &s) const;
uint32_t operator()(char const *x) const;
};

} /* namespace lol */


+ 29
- 4
src/lol/base/map.h Прегледај датотеку

@@ -24,9 +24,14 @@ namespace lol
template<typename K, typename V> class Map : protected Hash<K>
{
public:
/* If E is different from K, Hash<K> must implement operator()(E const&)
* and an equality operator between K and E must exist in order to use this method.
*/

/* I choose to make this inline because passing the key by reference
* is usually suboptimal. */
inline V const& operator[] (K const &key) const
template <typename E>
inline V const& operator[] (E const &key) const
{
/* Look for the hash in our table and return the value. */
uint32_t hash = ((Hash<K> const &)*this)(key);
@@ -39,7 +44,8 @@ public:
return V();
}

inline V & operator[] (K const &key)
template <typename E>
inline V & operator[] (E const &key)
{
/* Look for the hash in our table and return the value if found. */
uint32_t hash = ((Hash<K> const &)*this)(key);
@@ -52,7 +58,8 @@ public:
return m_array.Last().m3;
}

inline void Remove(K const &key)
template <typename E>
inline void Remove(E const &key)
{
uint32_t hash = ((Hash<K> const &)*this)(key);
for (int i = 0; i < m_array.Count(); ++i)
@@ -64,16 +71,34 @@ public:
}
}

inline bool HasKey(K const &key)
template <typename E>
inline bool HasKey(E 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;
}

template <typename E>
inline bool TryGetValue(E const &key, V& 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)
{
value = m_array[i].m3;
return true;
}

return false;
}

inline int Count() const { return m_array.Count(); }

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


Loading…
Откажи
Сачувај