3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #ifndef UTIL_CONTAINER_HEADER
21 #define UTIL_CONTAINER_HEADER
23 #include "../irrlichttypes.h"
25 #include <jmutexautolock.h>
26 #include "../porting.h" // For sleep_ms
31 Queue with unique values with fast checking of value existence
34 template<typename Value>
40 Does nothing if value is already queued.
43 false: value already exists
45 bool push_back(Value value)
47 // Check if already exists
48 if(m_map.find(value) != m_map.end())
53 m_list.push_back(value);
60 typename std::list<Value>::iterator i = m_list.begin();
73 std::map<Value, u8> m_map;
74 std::list<Value> m_list;
78 template<typename Key, typename Value>
85 assert(m_mutex.IsInitialized());
88 void set(const Key &name, const Value &value)
90 JMutexAutoLock lock(m_mutex);
92 m_values[name] = value;
95 bool get(const Key &name, Value *result)
97 JMutexAutoLock lock(m_mutex);
99 typename std::map<Key, Value>::iterator n;
100 n = m_values.find(name);
102 if(n == m_values.end())
111 std::list<Value> getValues()
113 std::list<Value> result;
114 for(typename std::map<Key, Value>::iterator
115 i = m_values.begin();
116 i != m_values.end(); ++i){
117 result.push_back(i->second);
123 std::map<Key, Value> m_values;
129 Generates ids for comparable values.
130 Id=0 is reserved for "no value".
133 - Returning value by id (very fast)
134 - Returning id by value
135 - Generating a new id for a value
138 - Remove an id/value pair (is possible to implement but slow)
141 class MutexedIdGenerator
147 assert(m_mutex.IsInitialized());
150 // Returns true if found
151 bool getValue(u32 id, T &value)
155 JMutexAutoLock lock(m_mutex);
156 if(m_id_to_value.size() < id)
158 value = m_id_to_value[id-1];
162 // If id exists for value, returns the id.
163 // Otherwise generates an id for the value.
164 u32 getId(const T &value)
166 JMutexAutoLock lock(m_mutex);
167 typename std::map<T, u32>::iterator n;
168 n = m_value_to_id.find(value);
169 if(n != m_value_to_id.end())
171 m_id_to_value.push_back(value);
172 u32 new_id = m_id_to_value.size();
173 m_value_to_id.insert(value, new_id);
179 // Values are stored here at id-1 position (id 1 = [0])
180 std::vector<T> m_id_to_value;
181 std::map<T, u32> m_value_to_id;
185 FIFO queue (well, actually a FILO also)
204 throw ItemNotFoundException("Queue: queue is empty");
206 typename std::list<T>::iterator begin = m_list.begin();
215 throw ItemNotFoundException("Queue: queue is empty");
217 typename std::list<T>::iterator last = m_list.back();
231 return m_list.empty();
240 Thread-safe FIFO queue (well, actually a FILO also)
253 JMutexAutoLock lock(m_mutex);
254 return m_list.empty();
258 JMutexAutoLock lock(m_mutex);
261 T pop_front(u32 wait_time_max_ms=0)
263 u32 wait_time_ms = 0;
268 JMutexAutoLock lock(m_mutex);
272 typename std::list<T>::iterator begin = m_list.begin();
278 if(wait_time_ms >= wait_time_max_ms)
279 throw ItemNotFoundException("MutexedQueue: queue is empty");
282 // Wait a while before trying again
287 T pop_back(u32 wait_time_max_ms=0)
289 u32 wait_time_ms = 0;
294 JMutexAutoLock lock(m_mutex);
298 typename std::list<T>::iterator last = m_list.back();
304 if(wait_time_ms >= wait_time_max_ms)
305 throw ItemNotFoundException("MutexedQueue: queue is empty");
308 // Wait a while before trying again
319 std::list<T> & getList()