Cleanup jthread and fix win32 build
[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 "../porting.h" // For sleep_ms
28 #include <list>
29 #include <vector>
30 #include <map>
31
32 /*
33         Queue with unique values with fast checking of value existence
34 */
35
36 template<typename Value>
37 class UniqueQueue
38 {
39 public:
40         
41         /*
42                 Does nothing if value is already queued.
43                 Return value:
44                         true: value added
45                         false: value already exists
46         */
47         bool push_back(Value value)
48         {
49                 // Check if already exists
50                 if(m_map.find(value) != m_map.end())
51                         return false;
52
53                 // Add
54                 m_map[value] = 0;
55                 m_list.push_back(value);
56                 
57                 return true;
58         }
59
60         Value pop_front()
61         {
62                 typename std::list<Value>::iterator i = m_list.begin();
63                 Value value = *i;
64                 m_map.erase(value);
65                 m_list.erase(i);
66                 return value;
67         }
68
69         u32 size()
70         {
71                 return m_map.size();
72         }
73
74 private:
75         std::map<Value, u8> m_map;
76         std::list<Value> m_list;
77 };
78
79 #if 1
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::list<Value> getValues()
112         {
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);
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 #endif
132
133 /*
134         Generates ids for comparable values.
135         Id=0 is reserved for "no value".
136
137         Is fast at:
138         - Returning value by id (very fast)
139         - Returning id by value
140         - Generating a new id for a value
141
142         Is not able to:
143         - Remove an id/value pair (is possible to implement but slow)
144 */
145 template<typename T>
146 class MutexedIdGenerator
147 {
148 public:
149         MutexedIdGenerator()
150         {
151         }
152         
153         // Returns true if found
154         bool getValue(u32 id, T &value)
155         {
156                 if(id == 0)
157                         return false;
158                 JMutexAutoLock lock(m_mutex);
159                 if(m_id_to_value.size() < id)
160                         return false;
161                 value = m_id_to_value[id-1];
162                 return true;
163         }
164         
165         // If id exists for value, returns the id.
166         // Otherwise generates an id for the value.
167         u32 getId(const T &value)
168         {
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())
173                         return n->second;
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);
177                 return new_id;
178         }
179
180 private:
181         JMutex m_mutex;
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;
185 };
186
187 /*
188         FIFO queue (well, actually a FILO also)
189 */
190 template<typename T>
191 class Queue
192 {
193 public:
194         Queue():
195                 m_list_size(0)
196         {}
197
198         void push_back(T t)
199         {
200                 m_list.push_back(t);
201                 ++m_list_size;
202         }
203         
204         T pop_front()
205         {
206                 if(m_list.empty())
207                         throw ItemNotFoundException("Queue: queue is empty");
208
209                 typename std::list<T>::iterator begin = m_list.begin();
210                 T t = *begin;
211                 m_list.erase(begin);
212                 --m_list_size;
213                 return t;
214         }
215         T pop_back()
216         {
217                 if(m_list.empty())
218                         throw ItemNotFoundException("Queue: queue is empty");
219
220                 typename std::list<T>::iterator last = m_list.back();
221                 T t = *last;
222                 m_list.erase(last);
223                 --m_list_size;
224                 return t;
225         }
226
227         u32 size()
228         {
229                 return m_list_size;
230         }
231
232         bool empty()
233         {
234                 return m_list.empty();
235         }
236
237 protected:
238         std::list<T> m_list;
239         u32 m_list_size;
240 };
241
242 /*
243         Thread-safe FIFO queue (well, actually a FILO also)
244 */
245
246 template<typename T>
247 class MutexedQueue
248 {
249 public:
250         MutexedQueue()
251         {
252         }
253         bool empty()
254         {
255                 JMutexAutoLock lock(m_mutex);
256                 return m_list.empty();
257         }
258         void push_back(T t)
259         {
260                 JMutexAutoLock lock(m_mutex);
261                 m_list.push_back(t);
262         }
263         T pop_front(u32 wait_time_max_ms=0)
264         {
265                 u32 wait_time_ms = 0;
266
267                 for(;;)
268                 {
269                         {
270                                 JMutexAutoLock lock(m_mutex);
271
272                                 if(!m_list.empty())
273                                 {
274                                         typename std::list<T>::iterator begin = m_list.begin();
275                                         T t = *begin;
276                                         m_list.erase(begin);
277                                         return t;
278                                 }
279
280                                 if(wait_time_ms >= wait_time_max_ms)
281                                         throw ItemNotFoundException("MutexedQueue: queue is empty");
282                         }
283
284                         // Wait a while before trying again
285                         sleep_ms(10);
286                         wait_time_ms += 10;
287                 }
288         }
289         T pop_back(u32 wait_time_max_ms=0)
290         {
291                 u32 wait_time_ms = 0;
292
293                 for(;;)
294                 {
295                         {
296                                 JMutexAutoLock lock(m_mutex);
297
298                                 if(!m_list.empty())
299                                 {
300                                         typename std::list<T>::iterator last = m_list.back();
301                                         T t = *last;
302                                         m_list.erase(last);
303                                         return t;
304                                 }
305
306                                 if(wait_time_ms >= wait_time_max_ms)
307                                         throw ItemNotFoundException("MutexedQueue: queue is empty");
308                         }
309
310                         // Wait a while before trying again
311                         sleep_ms(10);
312                         wait_time_ms += 10;
313                 }
314         }
315
316         JMutex & getMutex()
317         {
318                 return m_mutex;
319         }
320
321         std::list<T> & getList()
322         {
323                 return m_list;
324         }
325
326 protected:
327         JMutex m_mutex;
328         std::list<T> m_list;
329 };
330
331 #endif
332