bux API Reference
1.12.6
Static library of whatever are seen required in general purpose but not directly supported from Modern C++. Or whatever reusable originated from my side projects.
Toggle main menu visibility
Loading...
Searching...
No Matches
include
bux
XQue.h
Go to the documentation of this file.
1
#pragma once
2
3
#include <cstddef>
// size_t
4
#include <utility>
// std::swap()
5
6
namespace
bux
{
7
8
//
9
// Types
10
//
11
template
<
size_t
N>
12
class
C_RawQueue
13
{
14
public
:
15
16
// Types
17
typedef
size_t
size_type
;
18
19
// Nonvirtuals
20
C_RawQueue
() =
default
;
21
C_RawQueue
(
const
C_RawQueue
&another) =
delete
;
22
C_RawQueue
&
operator=
(
const
C_RawQueue
&another) =
delete
;
23
~C_RawQueue
()
24
{
25
clearGarbage(m_pFront);
// Expected to clear first from its derived type
26
}
27
size_t
compact
();
28
bool
empty
()
const
{
return
!m_pBack; }
29
void
splice
(
C_RawQueue
&other);
30
void
swap
(
C_RawQueue
&other);
31
32
protected
:
33
34
// Nonvirtuals
35
char
*
backRaw
()
const
{
return
m_pBack->m_Datum; }
36
char
*
frontRaw
()
const
{
return
m_pFront->m_Datum; }
37
void
popRaw
();
38
char
*
pushRaw
();
39
40
private
:
41
42
// Types
43
struct
C_Link
44
{
45
C_Link *m_Next{};
46
char
m_Datum[N];
47
};
48
49
// Data
50
C_Link *m_pFront{};
51
C_Link *m_pBack{};
52
53
// Nonvirtuals
54
static
size_t
clearGarbage(C_Link *p);
55
};
56
57
template
<
class
T>
58
class
C_Queue
:
public
C_RawQueue
<sizeof(T)>
68
{
69
public
:
70
71
// Types
72
typedef
T
value_type
;
73
74
// Nonvirtuals
75
C_Queue
() =
default
;
76
~C_Queue
() {
clear
(); }
77
T &
back
()
const
{
return
*
reinterpret_cast<
T*
>
(this->
backRaw
()); }
78
void
clear
();
79
T &
front
()
const
{
return
*
reinterpret_cast<
T*
>
(this->
frontRaw
()); }
80
void
pop
();
81
T &
push
() {
return
*
new
(this->
pushRaw
()) T; }
82
template
<
class
...T_Args>
83
T &
push
(T_Args&&...args) {
return
*
new
(this->
pushRaw
()) T{std::forward<T_Args>(args)...}; }
84
};
85
86
//
87
// Implement Class Templates
88
//
89
template
<
size_t
N>
90
size_t
C_RawQueue<N>::clearGarbage(C_Link *p)
91
{
92
size_t
ret =0;
93
while
(p)
94
{
95
C_Link *
const
t =p;
96
p =p->m_Next;
97
delete
t;
98
++ret;
99
}
100
return
ret;
101
}
102
103
template
<
size_t
N>
104
size_t
C_RawQueue<N>::compact
()
109
{
110
size_t
ret;
111
if
(m_pBack)
112
// Not empty
113
{
114
ret =clearGarbage(m_pBack->m_Next);
115
m_pBack->m_Next =0;
116
}
117
else
118
// Empty queue
119
{
120
ret =clearGarbage(m_pFront);
121
m_pFront =0;
122
}
123
return
ret;
124
}
125
126
template
<
size_t
N>
127
void
C_RawQueue<N>::popRaw
()
128
{
129
if
(m_pFront == m_pBack)
130
// Become empty
131
m_pBack =0;
132
else
133
// Remain non-empty
134
{
135
C_Link *
const
t =m_pFront;
136
m_pFront =t->m_Next;
137
t->m_Next =m_pBack->m_Next;
138
m_pBack->m_Next =t;
139
}
140
}
141
142
template
<
size_t
N>
143
char
*
C_RawQueue<N>::pushRaw
()
144
{
145
if
(m_pBack)
146
// Not empty
147
{
148
if
(m_pBack->m_Next)
149
m_pBack =m_pBack->m_Next;
150
else
151
m_pBack =m_pBack->m_Next =
new
C_Link;
152
}
153
else
if
(m_pFront)
154
// Empty but garbage link is not empty
155
{
156
m_pBack =m_pFront;
157
}
158
else
159
// Both empty
160
{
161
m_pFront =
162
m_pBack =
new
C_Link;
163
}
164
return
m_pBack->m_Datum;
165
}
166
167
template
<
size_t
N>
168
void
C_RawQueue<N>::splice
(
C_RawQueue
&other)
177
{
178
if
(
this
== &other)
179
// Trivial splice
180
return
;
181
182
C_Link *recycled;
183
if
(m_pBack)
184
{
185
recycled =m_pBack->m_Next;
186
m_pBack->m_Next =other.m_pFront;
187
if
(other.m_pBack)
188
m_pBack =other.m_pBack;
189
}
190
else
191
{
192
recycled =m_pFront;
193
m_pFront =other.m_pFront;
194
m_pBack =other.m_pBack;
195
}
196
other.m_pFront =
197
other.m_pBack =0;
198
199
if
(recycled)
200
{
201
C_Link *last =m_pBack;
202
if
(!last)
203
last =m_pFront;
204
205
if
(last)
206
// Find the last node and append
207
{
208
while
(last->m_Next)
209
last =last->m_Next;
210
211
last->m_Next =recycled;
212
}
213
else
214
// null m_pFront
215
m_pFront =recycled;
216
}
217
}
218
219
template
<
size_t
N>
220
void
C_RawQueue<N>::swap
(
C_RawQueue
&other)
223
{
224
std::swap(m_pFront, other.m_pFront);
225
std::swap(m_pBack, other.m_pBack);
226
}
227
228
template
<
class
T>
229
void
C_Queue<T>::clear
()
232
{
233
while
(!this->
empty
())
234
pop
();
235
}
236
237
template
<
class
T>
238
void
C_Queue<T>::pop
()
243
{
244
reinterpret_cast<
T*
>
(this->
frontRaw
())->~T();
245
this->
popRaw
();
246
}
247
248
}
// namespace bux
bux::C_Queue::clear
void clear()
Definition
XQue.h:229
bux::C_Queue::~C_Queue
~C_Queue()
Definition
XQue.h:76
bux::C_Queue::value_type
T value_type
Definition
XQue.h:72
bux::C_Queue::front
T & front() const
Definition
XQue.h:79
bux::C_Queue::back
T & back() const
Definition
XQue.h:77
bux::C_Queue::pop
void pop()
Definition
XQue.h:238
bux::C_Queue::push
T & push(T_Args &&...args)
Definition
XQue.h:83
bux::C_Queue::push
T & push()
Definition
XQue.h:81
bux::C_Queue::C_Queue
C_Queue()=default
bux::C_RawQueue::compact
size_t compact()
Definition
XQue.h:104
bux::C_RawQueue::empty
bool empty() const
Definition
XQue.h:28
bux::C_RawQueue::backRaw
char * backRaw() const
Definition
XQue.h:35
bux::C_RawQueue::C_RawQueue
C_RawQueue(const C_RawQueue &another)=delete
bux::C_RawQueue::operator=
C_RawQueue & operator=(const C_RawQueue &another)=delete
bux::C_RawQueue::size_type
size_t size_type
Definition
XQue.h:17
bux::C_RawQueue::popRaw
void popRaw()
Definition
XQue.h:127
bux::C_RawQueue::frontRaw
char * frontRaw() const
Definition
XQue.h:36
bux::C_RawQueue::C_RawQueue
C_RawQueue()=default
bux::C_RawQueue::~C_RawQueue
~C_RawQueue()
Definition
XQue.h:23
bux::C_RawQueue::pushRaw
char * pushRaw()
Definition
XQue.h:143
bux::C_RawQueue::splice
void splice(C_RawQueue &other)
Definition
XQue.h:168
bux::C_RawQueue::swap
void swap(C_RawQueue &other)
Definition
XQue.h:220
bux
THE common namespace of bux library.
Definition
AtomiX.cpp:3
Generated by
1.18.0