diff options
author | Ilya Zhuravlev <zhuravlevilya@ya.ru> | 2012-12-20 21:19:49 +0400 |
---|---|---|
committer | kwolekr <kwolekr@minetest.net> | 2013-03-11 19:08:39 -0400 |
commit | 6a1670dbc31cc0e44178bbd9ad34ff0d5981a060 (patch) | |
tree | ce32cd4be20e9be30367f2ad25d9dae6a0482898 /src/util | |
parent | e204bedf1d781e43b8caa334a99319efc5b7ce46 (diff) | |
download | minetest-6a1670dbc31cc0e44178bbd9ad34ff0d5981a060.tar.gz minetest-6a1670dbc31cc0e44178bbd9ad34ff0d5981a060.tar.bz2 minetest-6a1670dbc31cc0e44178bbd9ad34ff0d5981a060.zip |
Migrate to STL containers/algorithms.
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/container.h | 88 | ||||
-rw-r--r-- | src/util/numeric.cpp | 2 | ||||
-rw-r--r-- | src/util/numeric.h | 3 | ||||
-rw-r--r-- | src/util/thread.h | 16 |
4 files changed, 62 insertions, 47 deletions
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 <jmutex.h> #include <jmutexautolock.h> #include "../porting.h" // For sleep_ms +#include <list> +#include <vector> /* 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<Value>::Iterator i = m_list.begin(); + typename std::list<Value>::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<Value, u8> m_map; - core::list<Value> m_list; + std::map<Value, u8> m_map; + std::list<Value> m_list; }; #if 1 @@ -95,31 +96,31 @@ public: { JMutexAutoLock lock(m_mutex); - typename core::map<Key, Value>::Node *n; + typename std::map<Key, Value>::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<Value> getValues() + std::list<Value> getValues() { - core::list<Value> result; - for(typename core::map<Key, Value>::Iterator - i = m_values.getIterator(); - i.atEnd() == false; i++){ - result.push_back(i.getNode()->getValue()); + std::list<Value> result; + for(typename std::map<Key, Value>::iterator + i = m_values.begin(); + i != m_values.end(); ++i){ + result.push_back(i->second); } return result; } private: - core::map<Key, Value> m_values; + std::map<Key, Value> m_values; JMutex m_mutex; }; #endif @@ -163,10 +164,10 @@ public: u32 getId(const T &value) { JMutexAutoLock lock(m_mutex); - typename core::map<T, u32>::Node *n; + typename std::map<T, u32>::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<T> m_id_to_value; - core::map<T, u32> m_value_to_id; + std::vector<T> m_id_to_value; + std::map<T, u32> m_value_to_id; }; /* @@ -187,39 +188,52 @@ template<typename T> 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<T>::Iterator begin = m_list.begin(); + typename std::list<T>::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<T>::Iterator last = m_list.getLast(); + typename std::list<T>::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<T> m_list; + std::list<T> 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<T>::Iterator begin = m_list.begin(); + typename std::list<T>::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<T>::Iterator last = m_list.getLast(); + typename std::list<T>::iterator last = m_list.back(); T t = *last; m_list.erase(last); return t; @@ -302,14 +316,14 @@ public: return m_mutex; } - core::list<T> & getList() + std::list<T> & getList() { return m_list; } protected: JMutex m_mutex; - core::list<T> m_list; + std::list<T> 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 <iostream> // Calculate the borders of a "d-radius" cube -void getFacePositions(core::list<v3s16> &list, u16 d) +void getFacePositions(std::list<v3s16> &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 <irrList.h> +#include <list> // Calculate the borders of a "d-radius" cube -void getFacePositions(core::list<v3s16> &list, u16 d); +void getFacePositions(std::list<v3s16> &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<CallerInfo<Caller, CallerData> > callers; + std::list<CallerInfo<Caller, CallerData> > callers; }; template<typename Key, typename T, typename Caller, typename CallerData> @@ -152,16 +152,16 @@ public: Key key; ResultQueue<Key, T, Caller, CallerData> *dest; - core::list<CallerInfo<Caller, CallerData> > callers; + std::list<CallerInfo<Caller, CallerData> > callers; }; template<typename Key, typename T, typename Caller, typename CallerData> 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<Key, T, Caller, CallerData> >::Iterator + for(typename std::list< GetRequest<Key, T, Caller, CallerData> >::iterator i = m_queue.getList().begin(); - i != m_queue.getList().end(); i++) + i != m_queue.getList().end(); ++i) { GetRequest<Key, T, Caller, CallerData> &request = *i; if(request.key == key) { - for(typename core::list< CallerInfo<Caller, CallerData> >::Iterator + for(typename std::list< CallerInfo<Caller, CallerData> >::iterator i = request.callers.begin(); - i != request.callers.end(); i++) + i != request.callers.end(); ++i) { CallerInfo<Caller, CallerData> &ca = *i; if(ca.caller == caller) |