Use std::vector instead of std::list in StaticObjectList and MutexedMap::getValues()
[oweals/minetest.git] / src / util / container.h
1 /*
2 Minetest
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #ifndef UTIL_CONTAINER_HEADER
21 #define UTIL_CONTAINER_HEADER
22
23 #include "../irrlichttypes.h"
24 #include "../exceptions.h"
25 #include "../jthread/jmutex.h"
26 #include "../jthread/jmutexautolock.h"
27 #include "../jthread/jsemaphore.h"
28 #include <list>
29 #include <vector>
30 #include <map>
31 #include <set>
32 #include <queue>
33
34 /*
35 Queue with unique values with fast checking of value existence
36 */
37
38 template<typename Value>
39 class UniqueQueue
40 {
41 public:
42
43         /*
44         Does nothing if value is already queued.
45         Return value:
46         true: value added
47         false: value already exists
48         */
49         bool push_back(const Value& value)
50         {
51                 if (m_set.insert(value).second)
52                 {
53                         m_queue.push(value);
54                         return true;
55                 }
56                 return false;
57         }
58
59         void pop_front()
60         {
61                 m_set.erase(m_queue.front());
62                 m_queue.pop();
63         }
64
65         const Value& front() const
66         {
67                 return m_queue.front();
68         }
69
70         u32 size() const
71         {
72                 return m_queue.size();
73         }
74
75 private:
76         std::set<Value> m_set;
77         std::queue<Value> m_queue;
78 };
79
80 template<typename Key, typename Value>
81 class MutexedMap
82 {
83 public:
84         MutexedMap()
85         {
86         }
87
88         void set(const Key &name, const Value &value)
89         {
90                 JMutexAutoLock lock(m_mutex);
91
92                 m_values[name] = value;
93         }
94
95         bool get(const Key &name, Value *result)
96         {
97                 JMutexAutoLock lock(m_mutex);
98
99                 typename std::map<Key, Value>::iterator n;
100                 n = m_values.find(name);
101
102                 if(n == m_values.end())
103                         return false;
104
105                 if(result != NULL)
106                         *result = n->second;
107
108                 return true;
109         }
110
111         std::vector<Value> getValues()
112         {
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);
118                 }
119                 return result;
120         }
121
122         void clear ()
123         {
124                 m_values.clear();
125         }
126
127 private:
128         std::map<Key, Value> m_values;
129         JMutex m_mutex;
130 };
131
132 /*
133 Generates ids for comparable values.
134 Id=0 is reserved for "no value".
135
136 Is fast at:
137 - Returning value by id (very fast)
138 - Returning id by value
139 - Generating a new id for a value
140
141 Is not able to:
142 - Remove an id/value pair (is possible to implement but slow)
143 */
144 template<typename T>
145 class MutexedIdGenerator
146 {
147 public:
148         MutexedIdGenerator()
149         {
150         }
151
152         // Returns true if found
153         bool getValue(u32 id, T &value)
154         {
155                 if(id == 0)
156                         return false;
157                 JMutexAutoLock lock(m_mutex);
158                 if(m_id_to_value.size() < id)
159                         return false;
160                 value = m_id_to_value[id-1];
161                 return true;
162         }
163
164         // If id exists for value, returns the id.
165         // Otherwise generates an id for the value.
166         u32 getId(const T &value)
167         {
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())
172                         return n->second;
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);
176                 return new_id;
177         }
178
179 private:
180         JMutex m_mutex;
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;
184 };
185
186 /*
187 FIFO queue (well, actually a FILO also)
188 */
189 template<typename T>
190 class Queue
191 {
192 public:
193         Queue():
194                 m_list_size(0)
195         {}
196
197         void push_back(T t)
198         {
199                 m_list.push_back(t);
200                 ++m_list_size;
201         }
202
203         void push_front(T t)
204         {
205                 m_list.push_front(t);
206                 ++m_list_size;
207         }
208
209         T pop_front()
210         {
211                 if(m_list.empty())
212                         throw ItemNotFoundException("Queue: queue is empty");
213
214                 typename std::list<T>::iterator begin = m_list.begin();
215                 T t = *begin;
216                 m_list.erase(begin);
217                 --m_list_size;
218                 return t;
219         }
220         T pop_back()
221         {
222                 if(m_list.empty())
223                         throw ItemNotFoundException("Queue: queue is empty");
224
225                 typename std::list<T>::iterator last = m_list.back();
226                 T t = *last;
227                 m_list.erase(last);
228                 --m_list_size;
229                 return t;
230         }
231
232         u32 size()
233         {
234                 return m_list_size;
235         }
236
237         bool empty()
238         {
239                 return m_list.empty();
240         }
241
242 protected:
243         std::list<T> m_list;
244         u32 m_list_size;
245 };
246
247 /*
248 Thread-safe FIFO queue (well, actually a FILO also)
249 */
250
251 template<typename T>
252 class MutexedQueue
253 {
254 public:
255         template<typename Key, typename U, typename Caller, typename CallerData>
256         friend class RequestQueue;
257
258         MutexedQueue()
259         {
260         }
261         bool empty()
262         {
263                 JMutexAutoLock lock(m_mutex);
264                 return (m_size.GetValue() == 0);
265         }
266         void push_back(T t)
267         {
268                 JMutexAutoLock lock(m_mutex);
269                 m_list.push_back(t);
270                 m_size.Post();
271         }
272
273         /* this version of pop_front returns a empty element of T on timeout.
274         * Make sure default constructor of T creates a recognizable "empty" element
275         */
276         T pop_frontNoEx(u32 wait_time_max_ms)
277         {
278                 if (m_size.Wait(wait_time_max_ms))
279                 {
280                         JMutexAutoLock lock(m_mutex);
281
282                         typename std::list<T>::iterator begin = m_list.begin();
283                         T t = *begin;
284                         m_list.erase(begin);
285                         return t;
286                 }
287                 else
288                 {
289                         return T();
290                 }
291         }
292
293         T pop_front(u32 wait_time_max_ms)
294         {
295                 if (m_size.Wait(wait_time_max_ms))
296                 {
297                         JMutexAutoLock lock(m_mutex);
298
299                         typename std::list<T>::iterator begin = m_list.begin();
300                         T t = *begin;
301                         m_list.erase(begin);
302                         return t;
303                 }
304                 else
305                 {
306                         throw ItemNotFoundException("MutexedQueue: queue is empty");
307                 }
308         }
309
310         T pop_frontNoEx()
311         {
312                 m_size.Wait();
313
314                 JMutexAutoLock lock(m_mutex);
315
316                 typename std::list<T>::iterator begin = m_list.begin();
317                 T t = *begin;
318                 m_list.erase(begin);
319                 return t;
320         }
321
322         T pop_back(u32 wait_time_max_ms=0)
323         {
324                 if (m_size.Wait(wait_time_max_ms))
325                 {
326                         JMutexAutoLock lock(m_mutex);
327
328                         typename std::list<T>::iterator last = m_list.end();
329                         last--;
330                         T t = *last;
331                         m_list.erase(last);
332                         return t;
333                 }
334                 else
335                 {
336                         throw ItemNotFoundException("MutexedQueue: queue is empty");
337                 }
338         }
339
340         /* this version of pop_back returns a empty element of T on timeout.
341         * Make sure default constructor of T creates a recognizable "empty" element
342         */
343         T pop_backNoEx(u32 wait_time_max_ms=0)
344         {
345                 if (m_size.Wait(wait_time_max_ms))
346                 {
347                         JMutexAutoLock lock(m_mutex);
348
349                         typename std::list<T>::iterator last = m_list.end();
350                         last--;
351                         T t = *last;
352                         m_list.erase(last);
353                         return t;
354                 }
355                 else
356                 {
357                         return T();
358                 }
359         }
360
361         T pop_backNoEx()
362         {
363                 m_size.Wait();
364
365                 JMutexAutoLock lock(m_mutex);
366
367                 typename std::list<T>::iterator last = m_list.end();
368                 last--;
369                 T t = *last;
370                 m_list.erase(last);
371                 return t;
372         }
373
374 protected:
375         JMutex & getMutex()
376         {
377                 return m_mutex;
378         }
379
380         // NEVER EVER modify the >>list<< you got by using this function!
381         // You may only modify it's content
382         std::list<T> & getList()
383         {
384                 return m_list;
385         }
386
387         JMutex m_mutex;
388         std::list<T> m_list;
389         JSemaphore m_size;
390 };
391
392 #endif
393