Change UniqueQueue to use a queue and a set.

This commit is contained in:
unknown 2015-01-13 23:28:21 +11:00 committed by Craig Robbins
parent 227e4807b4
commit bd0d786590
2 changed files with 59 additions and 54 deletions

View File

@ -1663,7 +1663,8 @@ void Map::transformLiquids(std::map<v3s16, MapBlock*> & modified_blocks)
/* /*
Get a queued transforming liquid node Get a queued transforming liquid node
*/ */
v3s16 p0 = m_transforming_liquid.pop_front(); v3s16 p0 = m_transforming_liquid.front();
m_transforming_liquid.pop_front();
MapNode n0 = getNodeNoEx(p0); MapNode n0 = getNodeNoEx(p0);
@ -1909,7 +1910,10 @@ void Map::transformLiquids(std::map<v3s16, MapBlock*> & modified_blocks)
} }
//infostream<<"Map::transformLiquids(): loopcount="<<loopcount<<std::endl; //infostream<<"Map::transformLiquids(): loopcount="<<loopcount<<std::endl;
while (must_reflow.size() > 0) while (must_reflow.size() > 0)
m_transforming_liquid.push_back(must_reflow.pop_front()); {
m_transforming_liquid.push_back(must_reflow.front());
must_reflow.pop_front();
}
updateLighting(lighting_modified_blocks, modified_blocks); updateLighting(lighting_modified_blocks, modified_blocks);
@ -2380,8 +2384,8 @@ void ServerMap::finishBlockMake(BlockMakeData *data,
*/ */
while(data->transforming_liquid.size() > 0) while(data->transforming_liquid.size() > 0)
{ {
v3s16 p = data->transforming_liquid.pop_front(); m_transforming_liquid.push_back(data->transforming_liquid.front());
m_transforming_liquid.push_back(p); data->transforming_liquid.pop_front();
} }
/* /*

View File

@ -28,52 +28,53 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <list> #include <list>
#include <vector> #include <vector>
#include <map> #include <map>
#include <set>
#include <queue>
/* /*
Queue with unique values with fast checking of value existence Queue with unique values with fast checking of value existence
*/ */
template<typename Value> template<typename Value>
class UniqueQueue class UniqueQueue
{ {
public: public:
/* /*
Does nothing if value is already queued. Does nothing if value is already queued.
Return value: Return value:
true: value added true: value added
false: value already exists false: value already exists
*/ */
bool push_back(Value value) bool push_back(const Value& value)
{ {
// Check if already exists if (m_set.insert(value).second)
if(m_map.find(value) != m_map.end()) {
return false; m_queue.push(value);
return true;
// Add }
m_map[value] = 0; return false;
m_list.push_back(value);
return true;
} }
Value pop_front() void pop_front()
{ {
typename std::list<Value>::iterator i = m_list.begin(); m_set.erase(m_queue.front());
Value value = *i; m_queue.pop();
m_map.erase(value);
m_list.erase(i);
return value;
} }
u32 size() const Value& front() const
{ {
return m_map.size(); return m_queue.front();
}
u32 size() const
{
return m_queue.size();
} }
private: private:
std::map<Value, u8> m_map; std::set<Value> m_set;
std::list<Value> m_list; std::queue<Value> m_queue;
}; };
#if 1 #if 1
@ -84,14 +85,14 @@ public:
MutexedMap() MutexedMap()
{ {
} }
void set(const Key &name, const Value &value) void set(const Key &name, const Value &value)
{ {
JMutexAutoLock lock(m_mutex); JMutexAutoLock lock(m_mutex);
m_values[name] = value; m_values[name] = value;
} }
bool get(const Key &name, Value *result) bool get(const Key &name, Value *result)
{ {
JMutexAutoLock lock(m_mutex); JMutexAutoLock lock(m_mutex);
@ -101,10 +102,10 @@ public:
if(n == m_values.end()) if(n == m_values.end())
return false; return false;
if(result != NULL) if(result != NULL)
*result = n->second; *result = n->second;
return true; return true;
} }
@ -112,13 +113,13 @@ public:
{ {
std::list<Value> result; std::list<Value> result;
for(typename std::map<Key, Value>::iterator for(typename std::map<Key, Value>::iterator
i = m_values.begin(); i = m_values.begin();
i != m_values.end(); ++i){ i != m_values.end(); ++i){
result.push_back(i->second); result.push_back(i->second);
} }
return result; return result;
} }
void clear () void clear ()
{ {
m_values.clear(); m_values.clear();
@ -131,16 +132,16 @@ private:
#endif #endif
/* /*
Generates ids for comparable values. Generates ids for comparable values.
Id=0 is reserved for "no value". Id=0 is reserved for "no value".
Is fast at: Is fast at:
- Returning value by id (very fast) - Returning value by id (very fast)
- Returning id by value - Returning id by value
- Generating a new id for a value - Generating a new id for a value
Is not able to: Is not able to:
- Remove an id/value pair (is possible to implement but slow) - Remove an id/value pair (is possible to implement but slow)
*/ */
template<typename T> template<typename T>
class MutexedIdGenerator class MutexedIdGenerator
@ -149,7 +150,7 @@ public:
MutexedIdGenerator() MutexedIdGenerator()
{ {
} }
// Returns true if found // Returns true if found
bool getValue(u32 id, T &value) bool getValue(u32 id, T &value)
{ {
@ -161,7 +162,7 @@ public:
value = m_id_to_value[id-1]; value = m_id_to_value[id-1];
return true; return true;
} }
// If id exists for value, returns the id. // If id exists for value, returns the id.
// Otherwise generates an id for the value. // Otherwise generates an id for the value.
u32 getId(const T &value) u32 getId(const T &value)
@ -185,7 +186,7 @@ private:
}; };
/* /*
FIFO queue (well, actually a FILO also) FIFO queue (well, actually a FILO also)
*/ */
template<typename T> template<typename T>
class Queue class Queue
@ -200,7 +201,7 @@ public:
m_list.push_back(t); m_list.push_back(t);
++m_list_size; ++m_list_size;
} }
void push_front(T t) void push_front(T t)
{ {
m_list.push_front(t); m_list.push_front(t);
@ -246,7 +247,7 @@ protected:
}; };
/* /*
Thread-safe FIFO queue (well, actually a FILO also) Thread-safe FIFO queue (well, actually a FILO also)
*/ */
template<typename T> template<typename T>
@ -272,8 +273,8 @@ public:
} }
/* this version of pop_front returns a empty element of T on timeout. /* this version of pop_front returns a empty element of T on timeout.
* Make sure default constructor of T creates a recognizable "empty" element * Make sure default constructor of T creates a recognizable "empty" element
*/ */
T pop_frontNoEx(u32 wait_time_max_ms) T pop_frontNoEx(u32 wait_time_max_ms)
{ {
if (m_size.Wait(wait_time_max_ms)) if (m_size.Wait(wait_time_max_ms))
@ -339,8 +340,8 @@ public:
} }
/* this version of pop_back returns a empty element of T on timeout. /* this version of pop_back returns a empty element of T on timeout.
* Make sure default constructor of T creates a recognizable "empty" element * Make sure default constructor of T creates a recognizable "empty" element
*/ */
T pop_backNoEx(u32 wait_time_max_ms=0) T pop_backNoEx(u32 wait_time_max_ms=0)
{ {
if (m_size.Wait(wait_time_max_ms)) if (m_size.Wait(wait_time_max_ms))