2012-06-17 00:29:13 +02:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef UTIL_CONTAINER_HEADER
|
|
|
|
#define UTIL_CONTAINER_HEADER
|
|
|
|
|
|
|
|
#include "../irrlichttypes.h"
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "../exceptions.h"
|
2013-09-16 05:00:01 +02:00
|
|
|
#include "../jthread/jmutex.h"
|
|
|
|
#include "../jthread/jmutexautolock.h"
|
2012-06-17 00:29:13 +02:00
|
|
|
#include "../porting.h" // For sleep_ms
|
2012-12-20 18:19:49 +01:00
|
|
|
#include <list>
|
|
|
|
#include <vector>
|
2013-08-11 04:09:45 +02:00
|
|
|
#include <map>
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Queue with unique values with fast checking of value existence
|
|
|
|
*/
|
|
|
|
|
|
|
|
template<typename Value>
|
|
|
|
class UniqueQueue
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
/*
|
|
|
|
Does nothing if value is already queued.
|
|
|
|
Return value:
|
|
|
|
true: value added
|
|
|
|
false: value already exists
|
|
|
|
*/
|
|
|
|
bool push_back(Value value)
|
|
|
|
{
|
|
|
|
// Check if already exists
|
2012-12-20 18:19:49 +01:00
|
|
|
if(m_map.find(value) != m_map.end())
|
2012-06-17 00:29:13 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
// Add
|
2012-12-20 18:19:49 +01:00
|
|
|
m_map[value] = 0;
|
2012-06-17 00:29:13 +02:00
|
|
|
m_list.push_back(value);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
Value pop_front()
|
|
|
|
{
|
2012-12-20 18:19:49 +01:00
|
|
|
typename std::list<Value>::iterator i = m_list.begin();
|
2012-06-17 00:29:13 +02:00
|
|
|
Value value = *i;
|
2012-12-20 18:19:49 +01:00
|
|
|
m_map.erase(value);
|
2012-06-17 00:29:13 +02:00
|
|
|
m_list.erase(i);
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 size()
|
|
|
|
{
|
2012-12-20 18:19:49 +01:00
|
|
|
return m_map.size();
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-12-20 18:19:49 +01:00
|
|
|
std::map<Value, u8> m_map;
|
|
|
|
std::list<Value> m_list;
|
2012-06-17 00:29:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#if 1
|
|
|
|
template<typename Key, typename Value>
|
|
|
|
class MutexedMap
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MutexedMap()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(const Key &name, const Value &value)
|
|
|
|
{
|
|
|
|
JMutexAutoLock lock(m_mutex);
|
|
|
|
|
|
|
|
m_values[name] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool get(const Key &name, Value *result)
|
|
|
|
{
|
|
|
|
JMutexAutoLock lock(m_mutex);
|
|
|
|
|
2012-12-20 18:19:49 +01:00
|
|
|
typename std::map<Key, Value>::iterator n;
|
2012-06-17 00:29:13 +02:00
|
|
|
n = m_values.find(name);
|
|
|
|
|
2012-12-20 18:19:49 +01:00
|
|
|
if(n == m_values.end())
|
2012-06-17 00:29:13 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if(result != NULL)
|
2012-12-20 18:19:49 +01:00
|
|
|
*result = n->second;
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-12-20 18:19:49 +01:00
|
|
|
std::list<Value> getValues()
|
2012-11-30 17:12:32 +01:00
|
|
|
{
|
2012-12-20 18:19:49 +01:00
|
|
|
std::list<Value> result;
|
|
|
|
for(typename std::map<Key, Value>::iterator
|
|
|
|
i = m_values.begin();
|
|
|
|
i != m_values.end(); ++i){
|
|
|
|
result.push_back(i->second);
|
2012-11-30 17:12:32 +01:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2013-08-02 15:18:48 +02:00
|
|
|
|
|
|
|
void clear ()
|
|
|
|
{
|
|
|
|
m_values.clear();
|
|
|
|
}
|
2012-11-30 17:12:32 +01:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
private:
|
2012-12-20 18:19:49 +01:00
|
|
|
std::map<Key, Value> m_values;
|
2012-06-17 00:29:13 +02:00
|
|
|
JMutex m_mutex;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
Generates ids for comparable values.
|
|
|
|
Id=0 is reserved for "no value".
|
|
|
|
|
|
|
|
Is fast at:
|
|
|
|
- Returning value by id (very fast)
|
|
|
|
- Returning id by value
|
|
|
|
- Generating a new id for a value
|
|
|
|
|
|
|
|
Is not able to:
|
|
|
|
- Remove an id/value pair (is possible to implement but slow)
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
class MutexedIdGenerator
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MutexedIdGenerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if found
|
|
|
|
bool getValue(u32 id, T &value)
|
|
|
|
{
|
|
|
|
if(id == 0)
|
|
|
|
return false;
|
|
|
|
JMutexAutoLock lock(m_mutex);
|
|
|
|
if(m_id_to_value.size() < id)
|
|
|
|
return false;
|
|
|
|
value = m_id_to_value[id-1];
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If id exists for value, returns the id.
|
|
|
|
// Otherwise generates an id for the value.
|
|
|
|
u32 getId(const T &value)
|
|
|
|
{
|
|
|
|
JMutexAutoLock lock(m_mutex);
|
2012-12-20 18:19:49 +01:00
|
|
|
typename std::map<T, u32>::iterator n;
|
2012-06-17 00:29:13 +02:00
|
|
|
n = m_value_to_id.find(value);
|
2012-12-20 18:19:49 +01:00
|
|
|
if(n != m_value_to_id.end())
|
|
|
|
return n->second;
|
2012-06-17 00:29:13 +02:00
|
|
|
m_id_to_value.push_back(value);
|
|
|
|
u32 new_id = m_id_to_value.size();
|
|
|
|
m_value_to_id.insert(value, new_id);
|
|
|
|
return new_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
JMutex m_mutex;
|
|
|
|
// Values are stored here at id-1 position (id 1 = [0])
|
2012-12-20 18:19:49 +01:00
|
|
|
std::vector<T> m_id_to_value;
|
|
|
|
std::map<T, u32> m_value_to_id;
|
2012-06-17 00:29:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
FIFO queue (well, actually a FILO also)
|
|
|
|
*/
|
|
|
|
template<typename T>
|
|
|
|
class Queue
|
|
|
|
{
|
|
|
|
public:
|
2012-12-20 18:19:49 +01:00
|
|
|
Queue():
|
|
|
|
m_list_size(0)
|
|
|
|
{}
|
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
void push_back(T t)
|
|
|
|
{
|
|
|
|
m_list.push_back(t);
|
2012-12-20 18:19:49 +01:00
|
|
|
++m_list_size;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
T pop_front()
|
|
|
|
{
|
2012-12-20 18:19:49 +01:00
|
|
|
if(m_list.empty())
|
2012-06-17 00:29:13 +02:00
|
|
|
throw ItemNotFoundException("Queue: queue is empty");
|
|
|
|
|
2012-12-20 18:19:49 +01:00
|
|
|
typename std::list<T>::iterator begin = m_list.begin();
|
2012-06-17 00:29:13 +02:00
|
|
|
T t = *begin;
|
|
|
|
m_list.erase(begin);
|
2012-12-20 18:19:49 +01:00
|
|
|
--m_list_size;
|
2012-06-17 00:29:13 +02:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
T pop_back()
|
|
|
|
{
|
2012-12-20 18:19:49 +01:00
|
|
|
if(m_list.empty())
|
2012-06-17 00:29:13 +02:00
|
|
|
throw ItemNotFoundException("Queue: queue is empty");
|
|
|
|
|
2012-12-20 18:19:49 +01:00
|
|
|
typename std::list<T>::iterator last = m_list.back();
|
2012-06-17 00:29:13 +02:00
|
|
|
T t = *last;
|
|
|
|
m_list.erase(last);
|
2012-12-20 18:19:49 +01:00
|
|
|
--m_list_size;
|
2012-06-17 00:29:13 +02:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
u32 size()
|
|
|
|
{
|
2012-12-20 18:19:49 +01:00
|
|
|
return m_list_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool empty()
|
|
|
|
{
|
|
|
|
return m_list.empty();
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2012-12-20 18:19:49 +01:00
|
|
|
std::list<T> m_list;
|
|
|
|
u32 m_list_size;
|
2012-06-17 00:29:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
Thread-safe FIFO queue (well, actually a FILO also)
|
|
|
|
*/
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class MutexedQueue
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
MutexedQueue()
|
|
|
|
{
|
|
|
|
}
|
2012-12-20 18:19:49 +01:00
|
|
|
bool empty()
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
|
|
|
JMutexAutoLock lock(m_mutex);
|
2012-12-20 18:19:49 +01:00
|
|
|
return m_list.empty();
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
void push_back(T t)
|
|
|
|
{
|
|
|
|
JMutexAutoLock lock(m_mutex);
|
|
|
|
m_list.push_back(t);
|
|
|
|
}
|
|
|
|
T pop_front(u32 wait_time_max_ms=0)
|
|
|
|
{
|
|
|
|
u32 wait_time_ms = 0;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
JMutexAutoLock lock(m_mutex);
|
|
|
|
|
2012-12-20 18:19:49 +01:00
|
|
|
if(!m_list.empty())
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2012-12-20 18:19:49 +01:00
|
|
|
typename std::list<T>::iterator begin = m_list.begin();
|
2012-06-17 00:29:13 +02:00
|
|
|
T t = *begin;
|
|
|
|
m_list.erase(begin);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(wait_time_ms >= wait_time_max_ms)
|
|
|
|
throw ItemNotFoundException("MutexedQueue: queue is empty");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait a while before trying again
|
|
|
|
sleep_ms(10);
|
|
|
|
wait_time_ms += 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
T pop_back(u32 wait_time_max_ms=0)
|
|
|
|
{
|
|
|
|
u32 wait_time_ms = 0;
|
|
|
|
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
JMutexAutoLock lock(m_mutex);
|
|
|
|
|
2012-12-20 18:19:49 +01:00
|
|
|
if(!m_list.empty())
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
2012-12-20 18:19:49 +01:00
|
|
|
typename std::list<T>::iterator last = m_list.back();
|
2012-06-17 00:29:13 +02:00
|
|
|
T t = *last;
|
|
|
|
m_list.erase(last);
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(wait_time_ms >= wait_time_max_ms)
|
|
|
|
throw ItemNotFoundException("MutexedQueue: queue is empty");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait a while before trying again
|
|
|
|
sleep_ms(10);
|
|
|
|
wait_time_ms += 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
JMutex & getMutex()
|
|
|
|
{
|
|
|
|
return m_mutex;
|
|
|
|
}
|
|
|
|
|
2012-12-20 18:19:49 +01:00
|
|
|
std::list<T> & getList()
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
|
|
|
return m_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
JMutex m_mutex;
|
2012-12-20 18:19:49 +01:00
|
|
|
std::list<T> m_list;
|
2012-06-17 00:29:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|