From 5829785014b08e2ea0406335861430530804306c Mon Sep 17 00:00:00 2001 From: Guillaume Bittoun Date: Sun, 19 Oct 2014 09:03:30 +0000 Subject: [PATCH] avl_tree: using avl_tree in map object. --- src/lol/base/avl_tree.h | 2 ++ src/lol/base/map.h | 69 +++++++++++++++++++---------------------- src/lol/base/string.h | 12 ++----- 3 files changed, 37 insertions(+), 46 deletions(-) diff --git a/src/lol/base/avl_tree.h b/src/lol/base/avl_tree.h index a49058d7..34b5f01e 100644 --- a/src/lol/base/avl_tree.h +++ b/src/lol/base/avl_tree.h @@ -43,6 +43,8 @@ public: for (auto iterator : other) this->insert(iterator.key, iterator.value); + + return *this; } ~avl_tree() diff --git a/src/lol/base/map.h b/src/lol/base/map.h index 2e1b684e..e5fdab93 100644 --- a/src/lol/base/map.h +++ b/src/lol/base/map.h @@ -16,6 +16,7 @@ // A very simple map class. // +#include #include namespace lol @@ -35,46 +36,54 @@ public: inline V const& operator[] (E const &key) const { /* Look for the hash in our table and return the value. */ - ptrdiff_t i = FindIndex(key); - ASSERT(i >= 0, "trying to read a nonexistent key in map"); - return m_array[i].m3; + V * value_ptr = nullptr; + ASSERT(m_tree.try_get_value(key, value_ptr), "trying to read a nonexistent key in map"); + + return *value_ptr; } template inline V & operator[] (E const &key) { /* Look for the hash in our table and return the value if found. */ - uint32_t hashed = ((hash const &)*this)(key); - ptrdiff_t i = FindIndex(key, hashed); - if (i >= 0) - return m_array[i].m3; + + K typed_key(key); + V * value_ptr = nullptr; + + if (!m_tree.try_get_value(typed_key, value_ptr)) + { + V default_value = V(); + m_tree.insert(typed_key, default_value); + } + + ASSERT(m_tree.try_get_value(typed_key, value_ptr), "inner tree is messed up in map"); /* If not found, insert a new value. */ - m_array.Push(hashed, key, V()); - return m_array.Last().m3; + return *value_ptr; } template inline void Remove(E const &key) { - ptrdiff_t i = FindIndex(key); - if (i >= 0) - m_array.Remove(i); + K typed_key(key); + m_tree.erase(typed_key); } template inline bool HasKey(E const &key) { - return FindIndex(key) >= 0; + K typed_key(key); + return m_tree.exists(typed_key); } template inline bool TryGetValue(E const &key, V& value) { - ptrdiff_t i = FindIndex(key); - if (i >= 0) + K typed_key(key); + V * value_ptr; + if (m_tree.try_get_value(typed_key, value_ptr)) { - value = m_array[i].m3; + value = *value_ptr; return true; } @@ -84,40 +93,26 @@ public: array Keys() const { array ret; - for (auto it : m_array) - ret.Push(it.m2); + + for (auto iterator : m_tree) + ret.Push(iterator.key); + return ret; } inline ptrdiff_t Count() const { - return m_array.Count(); + return m_tree.get_count(); } inline void Empty() { - m_array.Empty(); + m_tree.clear(); } private: - template - inline ptrdiff_t FindIndex(E const &key, uint32_t hashed) - { - for (ptrdiff_t i = 0; i < m_array.Count(); ++i) - if (m_array[i].m1 == hashed) - if (m_array[i].m2 == key) - return i; - return -1; - } - - template - inline ptrdiff_t FindIndex(E const &key) - { - uint32_t hashed = ((hash const &)*this)(key); - return FindIndex(key, hashed); - } - array m_array; + avl_tree m_tree; }; } /* namespace lol */ diff --git a/src/lol/base/string.h b/src/lol/base/string.h index 421ae570..86c549bb 100644 --- a/src/lol/base/string.h +++ b/src/lol/base/string.h @@ -276,16 +276,10 @@ public: return !(*this == sz); } - inline bool operator <(String const & that) + inline bool operator <(String const & s) const { - auto it_this = begin(*this); - auto it_that = begin(that); - - for ( ; it_this != end(*this) && it_that != end(that) ; ++it_this, ++it_that) - if (*it_this < *it_that) - return true; - - return (!(it_this != end(*this)) && (it_that != end(that))); + using namespace std; + return memcmp(C(), s.C(), Count()) < 0; } #ifdef __GNUC__