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"
24 #include "../exceptions.h"
25 #include "../jthread/jmutex.h"
26 #include "../jthread/jmutexautolock.h"
27 #include "../jthread/jsemaphore.h"
33 Queue with unique values with fast checking of value existence
36 template<typename Value>
42 Does nothing if value is already queued.
45 false: value already exists
47 bool push_back(Value value)
49 // Check if already exists
50 if(m_map.find(value) != m_map.end())
55 m_list.push_back(value);
62 typename std::list<Value>::iterator i = m_list.begin();
75 std::map<Value, u8> m_map;
76 std::list<Value> m_list;
80 template<typename Key, typename Value>
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);
128 std::map<Key, Value> m_values;
134 Generates ids for comparable values.
135 Id=0 is reserved for "no value".
138 - Returning value by id (very fast)
139 - Returning id by value
140 - Generating a new id for a value
143 - Remove an id/value pair (is possible to implement but slow)
146 class MutexedIdGenerator
153 // Returns true if found
154 bool getValue(u32 id, T &value)
158 JMutexAutoLock lock(m_mutex);
159 if(m_id_to_value.size() < id)
161 value = m_id_to_value[id-1];
165 // If id exists for value, returns the id.
166 // Otherwise generates an id for the value.
167 u32 getId(const T &value)
169 JMutexAutoLock lock(m_mutex);
170 typename std::map<T, u32>::iterator n;
171 n = m_value_to_id.find(value);
172 if(n != m_value_to_id.end())
174 m_id_to_value.push_back(value);
175 u32 new_id = m_id_to_value.size();
176 m_value_to_id.insert(value, new_id);
182 // Values are stored here at id-1 position (id 1 = [0])
183 std::vector<T> m_id_to_value;
184 std::map<T, u32> m_value_to_id;
188 FIFO queue (well, actually a FILO also)
206 m_list.push_front(t);
213 throw ItemNotFoundException("Queue: queue is empty");
215 typename std::list<T>::iterator begin = m_list.begin();
224 throw ItemNotFoundException("Queue: queue is empty");
226 typename std::list<T>::iterator last = m_list.back();
240 return m_list.empty();
249 Thread-safe FIFO queue (well, actually a FILO also)
256 template<typename Key, typename U, typename Caller, typename CallerData>
257 friend class RequestQueue;
264 JMutexAutoLock lock(m_mutex);
265 return (m_size.GetValue() == 0);
269 JMutexAutoLock lock(m_mutex);
274 /* this version of pop_front returns a empty element of T on timeout.
275 * Make sure default constructor of T creates a recognizable "empty" element
277 T pop_frontNoEx(u32 wait_time_max_ms)
279 if (m_size.Wait(wait_time_max_ms))
281 JMutexAutoLock lock(m_mutex);
283 typename std::list<T>::iterator begin = m_list.begin();
294 T pop_front(u32 wait_time_max_ms)
296 if (m_size.Wait(wait_time_max_ms))
298 JMutexAutoLock lock(m_mutex);
300 typename std::list<T>::iterator begin = m_list.begin();
307 throw ItemNotFoundException("MutexedQueue: queue is empty");
315 JMutexAutoLock lock(m_mutex);
317 typename std::list<T>::iterator begin = m_list.begin();
323 T pop_back(u32 wait_time_max_ms=0)
325 if (m_size.Wait(wait_time_max_ms))
327 JMutexAutoLock lock(m_mutex);
329 typename std::list<T>::iterator last = m_list.end();
337 throw ItemNotFoundException("MutexedQueue: queue is empty");
341 /* this version of pop_back returns a empty element of T on timeout.
342 * Make sure default constructor of T creates a recognizable "empty" element
344 T pop_backNoEx(u32 wait_time_max_ms=0)
346 if (m_size.Wait(wait_time_max_ms))
348 JMutexAutoLock lock(m_mutex);
350 typename std::list<T>::iterator last = m_list.end();
366 JMutexAutoLock lock(m_mutex);
368 typename std::list<T>::iterator last = m_list.end();
381 // NEVER EVER modify the >>list<< you got by using this function!
382 // You may only modify it's content
383 std::list<T> & getList()