From f140c4752cf6da92f0a11f15380df6231cf7116f Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Sat, 20 Jul 2013 14:31:13 +0000 Subject: [PATCH] base: simplify the Map code. --- src/lol/base/map.h | 72 ++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 38 deletions(-) diff --git a/src/lol/base/map.h b/src/lol/base/map.h index 14e2745a..d26efcfc 100644 --- a/src/lol/base/map.h +++ b/src/lol/base/map.h @@ -25,8 +25,8 @@ 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. - */ + * 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. */ @@ -34,25 +34,19 @@ public: 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); - 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! */ - ASSERT(0, "trying to read a nonexistent key in map"); - return V(); + int i = FindIndex(key); + ASSERT(i >= 0, "trying to read a nonexistent key in map"); + return m_array[i].m3; } 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); - 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; + int i = FindIndex(key); + if (i >= 0) + return m_array[i].m3; + /* If not found, insert a new value. */ m_array.Push(hash, key, V()); return m_array.Last().m3; @@ -61,45 +55,47 @@ public: template inline void Remove(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) - { - m_array.Remove(i); - return; - } + int i = FindIndex(key); + if (i >= 0) + m_array.Remove(i); } 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; + return FindIndex(key) >= 0; } 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; - } + int i = FindIndex(key); + if (i >= 0) + { + value = m_array[i].m3; + return true; + } return false; } - inline int Count() const { return m_array.Count(); } + inline int Count() const + { + return m_array.Count(); + } private: + template + int FindIndex(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 i; + return -1; + } + Array m_array; };