diff --git a/src/base/hash.cpp b/src/base/hash.cpp index 2560a8ff..b38861d8 100644 --- a/src/base/hash.cpp +++ b/src/base/hash.cpp @@ -167,10 +167,20 @@ uint32_t Hash::operator ()(char const *s) const return HashCharString(s); } +uint32_t Hash::operator ()(String const &s) const +{ + return HashCharString(&s[0]); +} + uint32_t Hash::operator ()(String const &s) const { return HashCharString(&s[0]); } +uint32_t Hash::operator ()(char const *s) const +{ + return HashCharString(s); +} + } /* namespace lol */ diff --git a/src/lol/base/hash.h b/src/lol/base/hash.h index 6df32a9b..95e0ec42 100644 --- a/src/lol/base/hash.h +++ b/src/lol/base/hash.h @@ -40,12 +40,14 @@ template<> class Hash { public: uint32_t operator()(char const *x) const; + uint32_t operator()(String const &s) const; }; template<> class Hash { public: uint32_t operator()(String const &s) const; + uint32_t operator()(char const *x) const; }; } /* namespace lol */ diff --git a/src/lol/base/map.h b/src/lol/base/map.h index b6170598..55649126 100644 --- a/src/lol/base/map.h +++ b/src/lol/base/map.h @@ -24,9 +24,14 @@ namespace lol template class Map : protected Hash { public: + /* If E is different from K, Hash 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 + inline V const& operator[] (E const &key) const { /* Look for the hash in our table and return the value. */ uint32_t hash = ((Hash const &)*this)(key); @@ -39,7 +44,8 @@ public: return V(); } - inline V & operator[] (K const &key) + template + inline V & operator[] (E const &key) { /* Look for the hash in our table and return the value if found. */ uint32_t hash = ((Hash const &)*this)(key); @@ -52,7 +58,8 @@ public: return m_array.Last().m3; } - inline void Remove(K const &key) + template + inline void Remove(E const &key) { uint32_t hash = ((Hash const &)*this)(key); for (int i = 0; i < m_array.Count(); ++i) @@ -64,16 +71,34 @@ public: } } - inline bool HasKey(K const &key) + template + inline bool HasKey(E const &key) + { + uint32_t hash = ((Hash 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 + inline bool TryGetValue(E const &key, V& value) { uint32_t hash = ((Hash 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 m_array; };