diff options
Diffstat (limited to 'src/util/container.h')
-rw-r--r-- | src/util/container.h | 88 |
1 files changed, 51 insertions, 37 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 |