mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-09 02:07:04 +08:00
feat(tools): add RecentQueue to replace std::deque in server_world
This commit is contained in:
@@ -6,6 +6,7 @@
|
||||
#include "Cubed/gameplay/river_worm.hpp"
|
||||
#include "Cubed/gameplay/server_chunk.hpp"
|
||||
#include "Cubed/gameplay/server_player.hpp"
|
||||
#include "Cubed/tools/recent_queue.hpp"
|
||||
#include "Cubed/tools/thread_pool.hpp"
|
||||
#include "world/block_change.pb.h"
|
||||
|
||||
@@ -128,7 +129,7 @@ private:
|
||||
std::mutex m_need_gen_queue_mutex;
|
||||
std::condition_variable_any m_gen_cv;
|
||||
|
||||
std::deque<std::string> m_need_gen_queue;
|
||||
RecentQueue<std::string> m_need_gen_queue;
|
||||
|
||||
std::atomic<std::shared_ptr<ThreadPool>> m_gen_thread_pool;
|
||||
|
||||
|
||||
55
include/Cubed/tools/recent_queue.hpp
Normal file
55
include/Cubed/tools/recent_queue.hpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#pragma once
|
||||
#include <list>
|
||||
#include <unordered_map>
|
||||
namespace Cubed {
|
||||
template <typename T> class RecentQueue {
|
||||
private:
|
||||
std::list<T> m_list;
|
||||
std::unordered_map<T, typename std::list<T>::iterator> m_map;
|
||||
|
||||
public:
|
||||
void enqueue(T key) {
|
||||
auto it = m_map.find(key);
|
||||
if (it != m_map.end()) {
|
||||
m_list.splice(m_list.end(), m_list, it->second);
|
||||
return;
|
||||
}
|
||||
m_list.emplace_back(std::move(key));
|
||||
auto iter = std::prev(m_list.end());
|
||||
m_map.emplace(*iter, iter);
|
||||
}
|
||||
|
||||
void pop() {
|
||||
if (m_list.empty()) {
|
||||
return;
|
||||
}
|
||||
m_map.erase(m_list.front());
|
||||
m_list.pop_front();
|
||||
}
|
||||
void clear() {
|
||||
m_list.clear();
|
||||
m_map.clear();
|
||||
}
|
||||
|
||||
const T& front() const {
|
||||
assert(!empty());
|
||||
return m_list.front();
|
||||
}
|
||||
const T& back() const {
|
||||
assert(!empty());
|
||||
return m_list.back();
|
||||
}
|
||||
[[nodiscard]]
|
||||
bool empty() const {
|
||||
return m_list.empty();
|
||||
}
|
||||
[[nodiscard]]
|
||||
size_t size() const {
|
||||
return m_list.size();
|
||||
}
|
||||
[[nodiscard]]
|
||||
bool contains(const T& key) const {
|
||||
return m_map.find(key) != m_map.end();
|
||||
}
|
||||
};
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user