-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathqueue.hpp
217 lines (188 loc) · 5.99 KB
/
queue.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// solid/utility/queue.hpp
//
// Copyright (c) 2007, 2008 Valentin Palade (vipalade @ gmail . com)
//
// This file is part of SolidFrame framework.
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.
//
#pragma once
#include <cstdlib>
#include <utility>
#include "solid/system/cassert.hpp"
#include "solid/system/convertors.hpp"
namespace solid {
//! A simple and fast queue with interface similar to std::queue
/*!
The advantages are:
- twice faster then the std one
- while pushing new objects, the allready pushed are not relocated
in memory (no reallocation is performed)
*/
template <class T, unsigned NBits = 5>
class Queue {
static constexpr const size_t node_mask = bits_to_mask(NBits);
static constexpr const size_t node_size = bits_to_count(NBits);
struct Node {
using Storage = typename std::aligned_storage<sizeof(T), alignof(T)>::type;
Node(Node* _pnext = nullptr)
: pnext_(_pnext)
{
}
Storage data_[node_size];
Node* pnext_;
};
size_t size_ = 0;
size_t current_node_pop_count_ = 0;
T* pback_ = nullptr;
T* pfront_ = nullptr;
Node* ptop_cached_nodes_ = nullptr;
public:
using reference = T&;
using const_reference = T const&;
public:
Queue() {}
Queue(Queue&& _rthat) noexcept
: size_(_rthat.size_)
, current_node_pop_count_(_rthat.current_node_pop_count_)
, pback_(_rthat.pback_)
, pfront_(_rthat.pfront_)
, ptop_cached_nodes_(_rthat.ptop_cached_nodes_)
{
_rthat.size_ = 0;
_rthat.current_node_pop_count_ = 0;
_rthat.pback_ = nullptr;
_rthat.pfront_ = nullptr;
_rthat.ptop_cached_nodes_ = nullptr;
}
Queue(const Queue&) = delete;
Queue& operator=(const Queue&) = delete;
~Queue()
{
clear();
}
Queue& operator=(Queue&& _rthat) noexcept
{
clear();
size_ = _rthat.size_;
current_node_pop_count_ = _rthat.current_node_pop_count_;
pback_ = _rthat.pback_;
pfront_ = _rthat.pfront_;
ptop_cached_nodes_ = _rthat.ptop_cached_nodes_;
_rthat.size_ = 0;
_rthat.current_node_pop_count_ = 0;
_rthat.pback_ = nullptr;
_rthat.pfront_ = nullptr;
_rthat.ptop_cached_nodes_ = nullptr;
return *this;
}
bool empty() const { return !size_; }
size_t size() const { return size_; }
void push(const T& _value)
{
if ((size_ + current_node_pop_count_) & node_mask) {
++pback_;
} else {
pback_ = pushNode(pback_);
}
++size_;
new (pback_) T{_value};
}
void push(T&& _value)
{
if ((size_ + current_node_pop_count_) & node_mask) {
++pback_;
} else {
pback_ = pushNode(pback_);
}
++size_;
new (pback_) T{std::move(_value)};
}
reference back()
{
return *pback_;
}
const_reference back() const
{
return *pback_;
}
reference front()
{
return *pfront_;
}
const_reference front() const
{
return *pfront_;
}
void pop()
{
std::destroy_at(std::launder(pfront_));
--size_;
if ((++current_node_pop_count_) & node_mask)
++pfront_;
else {
pfront_ = popNode(pfront_);
current_node_pop_count_ = 0;
}
}
void clear()
{
while (size_ != 0) {
pop();
}
Node* pcurrent_node = pfront_ ? node(pfront_, current_node_pop_count_) : nullptr;
while (ptop_cached_nodes_ != nullptr) {
Node* pnext_chached_node = ptop_cached_nodes_->pnext_;
solid_assert_log(ptop_cached_nodes_ != pnext_chached_node, generic_logger);
delete ptop_cached_nodes_;
ptop_cached_nodes_ = pnext_chached_node;
}
delete pcurrent_node;
}
private:
constexpr Node* node(T* _plast_in_node, const size_t _index = (node_size - 1)) const
{
return std::launder(reinterpret_cast<Node*>(_plast_in_node - _index));
}
T* pushNode(T* _pvalue)
{
Node* pcurrent_node = _pvalue ? node(_pvalue) : nullptr;
if (ptop_cached_nodes_) {
Node* pnode = ptop_cached_nodes_;
ptop_cached_nodes_ = ptop_cached_nodes_->pnext_;
pnode->pnext_ = nullptr;
if (pcurrent_node) {
pcurrent_node->pnext_ = pnode;
return std::launder(reinterpret_cast<T*>(&pnode->data_[0]));
} else {
return (pfront_ = std::launder(reinterpret_cast<T*>(&pnode->data_[0])));
}
} else {
if (pcurrent_node) {
pcurrent_node->pnext_ = new Node;
pcurrent_node = pcurrent_node->pnext_;
return std::launder(reinterpret_cast<T*>(&pcurrent_node->data_[0]));
} else {
pcurrent_node = new Node;
return (pfront_ = std::launder(reinterpret_cast<T*>(&pcurrent_node->data_[0])));
}
}
}
T* popNode(T* _pvalue)
{
solid_assert_log(_pvalue, generic_logger);
Node* pcurrent_node = node(_pvalue);
Node* pnext_node = pcurrent_node->pnext_;
pcurrent_node->pnext_ = ptop_cached_nodes_;
ptop_cached_nodes_ = pcurrent_node; // cache the node
if (pnext_node) {
return std::launder(reinterpret_cast<T*>(&pnext_node->data_[0]));
} else {
solid_assert_log(!size_, generic_logger);
pback_ = nullptr;
return nullptr;
}
}
};
} // namespace solid