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"
35 Queue with unique values with fast checking of value existence
38 template<typename Value>
44 Does nothing if value is already queued.
47 false: value already exists
49 bool push_back(const Value& value)
51 if (m_set.insert(value).second)
61 m_set.erase(m_queue.front());
65 const Value& front() const
67 return m_queue.front();
72 return m_queue.size();
76 std::set<Value> m_set;
77 std::queue<Value> m_queue;
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::vector<Value> getValues()
113 std::vector<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;
133 Generates ids for comparable values.
134 Id=0 is reserved for "no value".
137 - Returning value by id (very fast)
138 - Returning id by value
139 - Generating a new id for a value
142 - Remove an id/value pair (is possible to implement but slow)
145 class MutexedIdGenerator
152 // Returns true if found
153 bool getValue(u32 id, T &value)
157 JMutexAutoLock lock(m_mutex);
158 if(m_id_to_value.size() < id)
160 value = m_id_to_value[id-1];
164 // If id exists for value, returns the id.
165 // Otherwise generates an id for the value.
166 u32 getId(const T &value)
168 JMutexAutoLock lock(m_mutex);
169 typename std::map<T, u32>::iterator n;
170 n = m_value_to_id.find(value);
171 if(n != m_value_to_id.end())
173 m_id_to_value.push_back(value);
174 u32 new_id = m_id_to_value.size();
175 m_value_to_id.insert(value, new_id);
181 // Values are stored here at id-1 position (id 1 = [0])
182 std::vector<T> m_id_to_value;
183 std::map<T, u32> m_value_to_id;
187 Thread-safe FIFO queue (well, actually a FILO also)
194 template<typename Key, typename U, typename Caller, typename CallerData>
195 friend class RequestQueue;
202 JMutexAutoLock lock(m_mutex);
203 return (m_queue.size() == 0);
207 JMutexAutoLock lock(m_mutex);
208 m_queue.push_back(t);
212 /* this version of pop_front returns a empty element of T on timeout.
213 * Make sure default constructor of T creates a recognizable "empty" element
215 T pop_frontNoEx(u32 wait_time_max_ms)
217 if (m_size.Wait(wait_time_max_ms)) {
218 JMutexAutoLock lock(m_mutex);
220 T t = m_queue.front();
229 T pop_front(u32 wait_time_max_ms)
231 if (m_size.Wait(wait_time_max_ms)) {
232 JMutexAutoLock lock(m_mutex);
234 T t = m_queue.front();
239 throw ItemNotFoundException("MutexedQueue: queue is empty");
247 JMutexAutoLock lock(m_mutex);
249 T t = m_queue.front();
254 T pop_back(u32 wait_time_max_ms=0)
256 if (m_size.Wait(wait_time_max_ms)) {
257 JMutexAutoLock lock(m_mutex);
259 T t = m_queue.back();
264 throw ItemNotFoundException("MutexedQueue: queue is empty");
268 /* this version of pop_back returns a empty element of T on timeout.
269 * Make sure default constructor of T creates a recognizable "empty" element
271 T pop_backNoEx(u32 wait_time_max_ms=0)
273 if (m_size.Wait(wait_time_max_ms)) {
274 JMutexAutoLock lock(m_mutex);
276 T t = m_queue.back();
289 JMutexAutoLock lock(m_mutex);
291 T t = m_queue.back();
302 std::deque<T> & getQueue()
307 std::deque<T> m_queue;
312 template<typename K, typename V>
316 LRUCache(size_t limit, void (*cache_miss)(void *data, const K &key, V *dest),
320 m_cache_miss = cache_miss;
321 m_cache_miss_data = data;
324 void setLimit(size_t limit)
336 const V *lookupCache(K key)
338 typename cache_type::iterator it = m_map.find(key);
340 if (it != m_map.end()) {
343 cache_entry_t &entry = it->second;
347 // update the usage information
348 m_queue.erase(entry.first);
349 m_queue.push_front(key);
350 entry.first = m_queue.begin();
352 // cache miss -- enter into cache
353 cache_entry_t &entry =
356 m_cache_miss(m_cache_miss_data, key, &entry.second);
358 // delete old entries
359 if (m_queue.size() == m_limit) {
360 const K &id = m_queue.back();
365 m_queue.push_front(key);
366 entry.first = m_queue.begin();
371 void (*m_cache_miss)(void *data, const K &key, V *dest);
372 void *m_cache_miss_data;
374 typedef typename std::template pair<typename std::template list<K>::iterator, V> cache_entry_t;
375 typedef std::template map<K, cache_entry_t> cache_type;
377 // we can't use std::deque here, because its iterators get invalidated
378 std::list<K> m_queue;