From 6a1670dbc31cc0e44178bbd9ad34ff0d5981a060 Mon Sep 17 00:00:00 2001 From: Ilya Zhuravlev Date: Thu, 20 Dec 2012 21:19:49 +0400 Subject: Migrate to STL containers/algorithms. --- src/util/container.h | 88 ++++++++++++++++++++++++++++++---------------------- src/util/numeric.cpp | 2 +- src/util/numeric.h | 3 +- src/util/thread.h | 16 +++++----- 4 files changed, 62 insertions(+), 47 deletions(-) (limited to 'src/util') diff --git a/src/util/container.h b/src/util/container.h index 775372649..9bb388f0e 100644 --- a/src/util/container.h +++ b/src/util/container.h @@ -24,6 +24,8 @@ with this program; if not, write to the Free Software Foundation, Inc., #include #include #include "../porting.h" // For sleep_ms +#include +#include /* Queue with unique values with fast checking of value existence @@ -43,11 +45,11 @@ public: bool push_back(Value value) { // Check if already exists - if(m_map.find(value) != NULL) + if(m_map.find(value) != m_map.end()) return false; // Add - m_map.insert(value, 0); + m_map[value] = 0; m_list.push_back(value); return true; @@ -55,22 +57,21 @@ public: Value pop_front() { - typename core::list::Iterator i = m_list.begin(); + typename std::list::iterator i = m_list.begin(); Value value = *i; - m_map.remove(value); + m_map.erase(value); m_list.erase(i); return value; } u32 size() { - assert(m_list.size() == m_map.size()); - return m_list.size(); + return m_map.size(); } private: - core::map m_map; - core::list m_list; + std::map m_map; + std::list m_list; }; #if 1 @@ -95,31 +96,31 @@ public: { JMutexAutoLock lock(m_mutex); - typename core::map::Node *n; + typename std::map::iterator n; n = m_values.find(name); - if(n == NULL) + if(n == m_values.end()) return false; if(result != NULL) - *result = n->getValue(); + *result = n->second; return true; } - core::list getValues() + std::list getValues() { - core::list result; - for(typename core::map::Iterator - i = m_values.getIterator(); - i.atEnd() == false; i++){ - result.push_back(i.getNode()->getValue()); + std::list result; + for(typename std::map::iterator + i = m_values.begin(); + i != m_values.end(); ++i){ + result.push_back(i->second); } return result; } private: - core::map m_values; + std::map m_values; JMutex m_mutex; }; #endif @@ -163,10 +164,10 @@ public: u32 getId(const T &value) { JMutexAutoLock lock(m_mutex); - typename core::map::Node *n; + typename std::map::iterator n; n = m_value_to_id.find(value); - if(n != NULL) - return n->getValue(); + if(n != m_value_to_id.end()) + return n->second; m_id_to_value.push_back(value); u32 new_id = m_id_to_value.size(); m_value_to_id.insert(value, new_id); @@ -176,8 +177,8 @@ public: private: JMutex m_mutex; // Values are stored here at id-1 position (id 1 = [0]) - core::array m_id_to_value; - core::map m_value_to_id; + std::vector m_id_to_value; + std::map m_value_to_id; }; /* @@ -187,39 +188,52 @@ template class Queue { public: + Queue(): + m_list_size(0) + {} + void push_back(T t) { m_list.push_back(t); + ++m_list_size; } T pop_front() { - if(m_list.size() == 0) + if(m_list.empty()) throw ItemNotFoundException("Queue: queue is empty"); - typename core::list::Iterator begin = m_list.begin(); + typename std::list::iterator begin = m_list.begin(); T t = *begin; m_list.erase(begin); + --m_list_size; return t; } T pop_back() { - if(m_list.size() == 0) + if(m_list.empty()) throw ItemNotFoundException("Queue: queue is empty"); - typename core::list::Iterator last = m_list.getLast(); + typename std::list::iterator last = m_list.back(); T t = *last; m_list.erase(last); + --m_list_size; return t; } u32 size() { - return m_list.size(); + return m_list_size; + } + + bool empty() + { + return m_list.empty(); } protected: - core::list m_list; + std::list m_list; + u32 m_list_size; }; /* @@ -234,10 +248,10 @@ public: { m_mutex.Init(); } - u32 size() + bool empty() { JMutexAutoLock lock(m_mutex); - return m_list.size(); + return m_list.empty(); } void push_back(T t) { @@ -253,9 +267,9 @@ public: { JMutexAutoLock lock(m_mutex); - if(m_list.size() > 0) + if(!m_list.empty()) { - typename core::list::Iterator begin = m_list.begin(); + typename std::list::iterator begin = m_list.begin(); T t = *begin; m_list.erase(begin); return t; @@ -279,9 +293,9 @@ public: { JMutexAutoLock lock(m_mutex); - if(m_list.size() > 0) + if(!m_list.empty()) { - typename core::list::Iterator last = m_list.getLast(); + typename std::list::iterator last = m_list.back(); T t = *last; m_list.erase(last); return t; @@ -302,14 +316,14 @@ public: return m_mutex; } - core::list & getList() + std::list & getList() { return m_list; } protected: JMutex m_mutex; - core::list m_list; + std::list m_list; }; #endif diff --git a/src/util/numeric.cpp b/src/util/numeric.cpp index a79454628..ed83df7d7 100644 --- a/src/util/numeric.cpp +++ b/src/util/numeric.cpp @@ -24,7 +24,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include // Calculate the borders of a "d-radius" cube -void getFacePositions(core::list &list, u16 d) +void getFacePositions(std::list &list, u16 d) { if(d == 0) { diff --git a/src/util/numeric.h b/src/util/numeric.h index 450a98e40..e66af2376 100644 --- a/src/util/numeric.h +++ b/src/util/numeric.h @@ -25,9 +25,10 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "../irr_v3d.h" #include "../irr_aabb3d.h" #include +#include // Calculate the borders of a "d-radius" cube -void getFacePositions(core::list &list, u16 d); +void getFacePositions(std::list &list, u16 d); class IndentationRaiser { diff --git a/src/util/thread.h b/src/util/thread.h index 949bb4204..6b2cf5b6c 100644 --- a/src/util/thread.h +++ b/src/util/thread.h @@ -120,7 +120,7 @@ class GetResult public: Key key; T item; - core::list > callers; + std::list > callers; }; template @@ -152,16 +152,16 @@ public: Key key; ResultQueue *dest; - core::list > callers; + std::list > callers; }; template class RequestQueue { public: - u32 size() + bool empty() { - return m_queue.size(); + return m_queue.empty(); } void add(Key key, Caller caller, CallerData callerdata, @@ -172,17 +172,17 @@ public: /* If the caller is already on the list, only update CallerData */ - for(typename core::list< GetRequest >::Iterator + for(typename std::list< GetRequest >::iterator i = m_queue.getList().begin(); - i != m_queue.getList().end(); i++) + i != m_queue.getList().end(); ++i) { GetRequest &request = *i; if(request.key == key) { - for(typename core::list< CallerInfo >::Iterator + for(typename std::list< CallerInfo >::iterator i = request.callers.begin(); - i != request.callers.end(); i++) + i != request.callers.end(); ++i) { CallerInfo &ca = *i; if(ca.caller == caller) -- cgit v1.2.3