NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
bounded_queue.hpp
1#ifndef NEFORCE_CORE_MEMORY_BOUNDED_QUEUE_HPP__
2#define NEFORCE_CORE_MEMORY_BOUNDED_QUEUE_HPP__
4NEFORCE_BEGIN_NAMESPACE__
5
14template <typename T>
16private:
17 vector<T> buffer_;
18 size_t head_{0};
19 size_t tail_{0};
20 size_t count_{0};
21 size_t capacity_;
22
23public:
28 explicit bounded_queue(const size_t cap) :
29 buffer_(cap),
30 capacity_(cap) {}
31
32 bounded_queue(const bounded_queue&) = default;
33 bounded_queue& operator=(const bounded_queue&) = default;
34 bounded_queue(bounded_queue&&) noexcept = default;
35 bounded_queue& operator=(bounded_queue&&) noexcept = default;
36
38 NEFORCE_NODISCARD bool full() const noexcept { return count_ == capacity_; }
39
41 NEFORCE_NODISCARD bool empty() const noexcept { return count_ == 0; }
42
44 NEFORCE_NODISCARD size_t size() const noexcept { return count_; }
45
47 NEFORCE_NODISCARD size_t capacity() const noexcept { return capacity_; }
48
53 void push(T&& item) noexcept {
54 buffer_[tail_] = _NEFORCE move(item);
55 tail_ = (tail_ + 1) % capacity_;
56 ++count_;
57 }
58
63 NEFORCE_NODISCARD T pop() noexcept {
64 T item = _NEFORCE move(buffer_[head_]);
65 head_ = (head_ + 1) % capacity_;
66 --count_;
67 return item;
68 }
69
74 NEFORCE_NODISCARD T& front() noexcept { return buffer_[head_]; }
75};
76
77NEFORCE_END_NAMESPACE__
78#endif // NEFORCE_CORE_MEMORY_BOUNDED_QUEUE_HPP__
线程安全的有界环形队列(非阻塞)
size_t size() const noexcept
bool full() const noexcept
bool empty() const noexcept
T & front() noexcept
访问队首元素
T pop() noexcept
从队首弹出元素(调用者需确保队列非空)
size_t capacity() const noexcept
bounded_queue(const size_t cap)
构造指定容量的队列
void push(T &&item) noexcept
向队尾压入元素(调用者需确保队列未满)
动态大小数组容器
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
动态大小数组容器