Changeset 2807
 Timestamp:
 Jul 20, 2013, 4:31:13 PM (4 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

trunk/src/lol/base/map.h
r2806 r2807 26 26 public: 27 27 /* If E is different from K, Hash<K> must implement operator()(E const&) 28 * and an equality operator between K and E must exist in order to use this method.29 */28 * and an equality operator between K and E must exist in order to use 29 * this method. */ 30 30 31 31 /* I choose to make this inline because passing the key by reference … … 35 35 { 36 36 /* Look for the hash in our table and return the value. */ 37 uint32_t hash = ((Hash<K> const &)*this)(key); 38 for (int i = 0; i < m_array.Count(); ++i) 39 if (m_array[i].m1 == hash) 40 if (m_array[i].m2 == key) 41 return m_array[i].m3; 42 /* XXX: this in an error! */ 43 ASSERT(0, "trying to read a nonexistent key in map"); 44 return V(); 37 int i = FindIndex(key); 38 ASSERT(i >= 0, "trying to read a nonexistent key in map"); 39 return m_array[i].m3; 45 40 } 46 41 … … 49 44 { 50 45 /* Look for the hash in our table and return the value if found. */ 51 uint32_t hash = ((Hash<K> const &)*this)(key); 52 for (int i = 0; i < m_array.Count(); ++i) 53 if (m_array[i].m1 == hash) 54 if (m_array[i].m2 == key) 55 return m_array[i].m3; 46 int i = FindIndex(key); 47 if (i >= 0) 48 return m_array[i].m3; 49 56 50 /* If not found, insert a new value. */ 57 51 m_array.Push(hash, key, V()); … … 62 56 inline void Remove(E const &key) 63 57 { 64 uint32_t hash = ((Hash<K> const &)*this)(key); 65 for (int i = 0; i < m_array.Count(); ++i) 66 if (m_array[i].m1 == hash) 67 if (m_array[i].m2 == key) 68 { 69 m_array.Remove(i); 70 return; 71 } 58 int i = FindIndex(key); 59 if (i >= 0) 60 m_array.Remove(i); 72 61 } 73 62 … … 75 64 inline bool HasKey(E const &key) 76 65 { 77 uint32_t hash = ((Hash<K> const &)*this)(key); 78 for (int i = 0; i < m_array.Count(); ++i) 79 if (m_array[i].m1 == hash) 80 if (m_array[i].m2 == key) 81 return true; 82 return false; 66 return FindIndex(key) >= 0; 83 67 } 84 68 … … 86 70 inline bool TryGetValue(E const &key, V& value) 87 71 { 72 int i = FindIndex(key); 73 if (i >= 0) 74 { 75 value = m_array[i].m3; 76 return true; 77 } 78 79 return false; 80 } 81 82 inline int Count() const 83 { 84 return m_array.Count(); 85 } 86 87 private: 88 template <typename E> 89 int FindIndex(E const &key) 90 { 88 91 uint32_t hash = ((Hash<K> const &)*this)(key); 89 92 for (int i = 0; i < m_array.Count(); ++i) 90 93 if (m_array[i].m1 == hash) 91 94 if (m_array[i].m2 == key) 92 { 93 value = m_array[i].m3; 94 return true; 95 } 96 97 return false; 95 return i; 96 return 1; 98 97 } 99 98 100 inline int Count() const { return m_array.Count(); }101 102 private:103 99 Array<uint32_t, K, V> m_array; 104 100 };
Note: See TracChangeset
for help on using the changeset viewer.