bux API Reference 1.9.0
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.
Loading...
Searching...
No Matches
XQue.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef> // size_t
4#include <utility> // std::swap()
5
6namespace bux {
7
8//
9// Types
10//
11template<size_t N>
13{
14public:
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;
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
32protected:
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
40private:
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
57template<class T>
58class C_Queue: public C_RawQueue<sizeof(T)>
68{
69public:
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//
89template<size_t N>
90size_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
103template<size_t N>
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
126template<size_t N>
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
142template<size_t N>
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
167template<size_t N>
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
219template<size_t N>
223{
224 std::swap(m_pFront, other.m_pFront);
225 std::swap(m_pBack, other.m_pBack);
226}
227
228template<class T>
232{
233 while (!this->empty())
234 pop();
235}
236
237template<class T>
243{
244 reinterpret_cast<T*>(this->frontRaw())->~T();
245 this->popRaw();
246}
247
248} // namespace bux
T value_type
Definition XQue.h:72
void clear()
Definition XQue.h:229
~C_Queue()
Definition XQue.h:76
T & front() const
Definition XQue.h:79
T & back() const
Definition XQue.h:77
void pop()
Definition XQue.h:238
T & push(T_Args &&...args)
Definition XQue.h:83
T & push()
Definition XQue.h:81
C_Queue()=default
size_t compact()
Definition XQue.h:104
bool empty() const
Definition XQue.h:28
char * backRaw() const
Definition XQue.h:35
C_RawQueue(const C_RawQueue &another)=delete
C_RawQueue & operator=(const C_RawQueue &another)=delete
void popRaw()
Definition XQue.h:127
size_t size_type
Definition XQue.h:17
char * frontRaw() const
Definition XQue.h:36
C_RawQueue()=default
char * pushRaw()
Definition XQue.h:143
void splice(C_RawQueue &other)
Definition XQue.h:168
void swap(C_RawQueue &other)
Definition XQue.h:220
THE common namespace of bux library.
Definition AtomiX.cpp:3