cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
deque.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_CORE_CONTAINER_DEQUE_H
4#define pRC_CORE_CONTAINER_DEQUE_H
5
9#include <prc/pragma.hpp>
10
11namespace pRC
12{
13 template<class T, Size N>
14 class Deque
15 {
16 public:
17 using Type = T;
18 static constexpr auto Capacity = N;
19
20 public:
21 ~Deque() = default;
22 constexpr Deque(Deque const &) = default;
23 constexpr Deque(Deque &&) = default;
24 constexpr Deque &operator=(Deque const &) & = default;
25 constexpr Deque &operator=(Deque &&) & = default;
26 constexpr Deque() = default;
27
28 constexpr auto size() const
29 {
30 return mSize;
31 }
32
33 constexpr decltype(auto) front(Index const position = 0) &&
34 {
35 return move(*this)[position];
36 }
37
38 constexpr decltype(auto) front(Index const position = 0) const &&
39 {
40 return move(*this)[position];
41 }
42
43 constexpr decltype(auto) front(Index const position = 0) &
44 {
45 return operator[](position);
46 }
47
48 constexpr decltype(auto) front(Index const position = 0) const &
49 {
50 return operator[](position);
51 }
52
53 constexpr decltype(auto) back(Index const position = 0) &&
54 {
55 return move(*this)[mSize - position - 1];
56 }
57
58 constexpr decltype(auto) back(Index const position = 0) const &&
59 {
60 return move(*this)[mSize - position - 1];
61 }
62
63 constexpr decltype(auto) back(Index const position = 0) &
64 {
65 return operator[](mSize - position - 1);
66 }
67
68 constexpr decltype(auto) back(Index const position = 0) const &
69 {
70 return operator[](mSize - position - 1);
71 }
72
73 constexpr auto &clear() &
74 {
75 mSize = 0;
76 mNext = 0;
77 return *this;
78 }
79
80 template<IsConvertible<T> R>
81 constexpr auto pushFront(R const &element) &&
82 {
83 updatePushFront();
84 front() = element;
85 return move(*this);
86 }
87
88 template<IsConvertible<T> R>
89 constexpr auto &pushFront(R const &element) &
90 {
91 updatePushFront();
92 front() = element;
93 return *this;
94 }
95
96 constexpr auto pushFront(T &&element) &&
97 {
98 updatePushFront();
99 front() = move(element);
100 return move(*this);
101 }
102
103 constexpr auto &pushFront(T &&element) &
104 {
105 updatePushFront();
106 front() = move(element);
107 return *this;
108 }
109
110 template<class... Args>
111 requires IsConstructible<T, Args...>
112 constexpr auto emplaceFront(Args &&...args) &&
113 {
114 updatePushFront();
115 front() = T(forward<Args>(args)...);
116 return move(*this);
117 }
118
119 template<class... Args>
120 requires IsConstructible<T, Args...>
121 constexpr auto &emplaceFront(Args &&...args) &
122 {
123 updatePushFront();
124 front() = T(forward<Args>(args)...);
125 return *this;
126 }
127
128 constexpr auto &popFront() &
129 {
130 updatePopFront();
131 return *this;
132 }
133
134 template<IsConvertible<T> R>
135 constexpr auto pushBack(R const &element) &&
136 {
137 updatePushBack();
138 back() = element;
139 return move(*this);
140 }
141
142 template<IsConvertible<T> R>
143 constexpr auto &pushBack(R const &element) &
144 {
145 updatePushBack();
146 back() = element;
147 return *this;
148 }
149
150 constexpr auto pushBack(T &&element) &&
151 {
152 updatePushBack();
153 back() = move(element);
154 return move(*this);
155 }
156
157 constexpr auto &pushBack(T &&element) &
158 {
159 updatePushBack();
160 back() = move(element);
161 return *this;
162 }
163
164 template<class... Args>
165 requires IsConstructible<T, Args...>
166 constexpr auto emplaceBack(Args &&...args) &&
167 {
168 updatePushBack();
169 back() = T(forward<Args>(args)...);
170 return move(*this);
171 }
172
173 template<class... Args>
174 requires IsConstructible<T, Args...>
175 constexpr auto &emplaceBack(Args &&...args) &
176 {
177 updatePushBack();
178 back() = T(forward<Args>(args)...);
179 return *this;
180 }
181
182 constexpr auto &popBack() &
183 {
184 updatePopBack();
185 return *this;
186 }
187
188 private:
189 constexpr decltype(auto) operator[](Index const position) &&
190 {
191 return move(mData)[getIndex(position)];
192 }
193
194 constexpr decltype(auto) operator[](Index const position) const &&
195 {
196 return move(mData)[getIndex(position)];
197 }
198
199 constexpr decltype(auto) operator[](Index const position) &
200 {
201 return mData[getIndex(position)];
202 }
203
204 constexpr decltype(auto) operator[](Index const position) const &
205 {
206 return mData[getIndex(position)];
207 }
208
209 constexpr auto getIndex(Index const position) const
210 {
211 if constexpr(cDebugLevel >= DebugLevel::Mid)
212 {
213 if(position >= mSize)
214 {
215 Logging::error("Deque out of range.");
216 }
217 }
218
219 if constexpr(N != 0)
220 {
221 return (mNext + N - 1 - position) % N;
222 }
223 else
224 {
225 return mNext;
226 }
227 }
228
229 constexpr auto updatePushFront()
230 {
231 if constexpr(N != 0)
232 {
233 mSize = min(mSize + 1, N);
234 mNext = (mNext + 1) % N;
235 }
236 }
237
238 constexpr auto updatePopFront()
239 {
240 if constexpr(cDebugLevel >= DebugLevel::Mid)
241 {
242 if(mSize == 0)
243 {
244 Logging::error("Deque is empty.");
245 }
246 }
247
248 if constexpr(N != 0)
249 {
250 mSize = mSize - 1;
251 mNext = (mNext - 1) % N;
252 }
253 }
254
255 constexpr auto updatePushBack()
256 {
257 if constexpr(N != 0)
258 {
259 if(mSize == N)
260 {
261 mNext = (mNext - 1) % N;
262 }
263 mSize = min(mSize + 1, N);
264 }
265 }
266
267 constexpr auto updatePopBack()
268 {
269 if constexpr(cDebugLevel >= DebugLevel::Mid)
270 {
271 if(mSize == 0)
272 {
273 Logging::error("Deque is empty.");
274 }
275 }
276
277 if constexpr(N != 0)
278 {
279 mSize = mSize - 1;
280 }
281 }
282
283 private:
284 BEGIN_IGNORE_DIAGNOSTIC_GCC("-Wpedantic")
285 StackArray<T, N> mData;
287
288 Size mSize = 0;
289 Index mNext = 0;
290 };
291
292 template<class T>
293 concept IsDeque = !IsReference<T> && requires {
294 {
295 []<class U, Size N>(Deque<U, N> const &&)
296 {
297 }(std::declval<T>())
298 };
299 };
300}
301#endif // pRC_CORE_CONTAINER_DEQUE_H
Definition deque.hpp:15
constexpr auto pushBack(T &&element) &&
Definition deque.hpp:150
constexpr auto & emplaceBack(Args &&...args) &
Definition deque.hpp:175
constexpr auto & pushFront(T &&element) &
Definition deque.hpp:103
constexpr auto emplaceBack(Args &&...args) &&
Definition deque.hpp:166
constexpr auto size() const
Definition deque.hpp:28
constexpr Deque & operator=(Deque const &) &=default
constexpr decltype(auto) front(Index const position=0) const &
Definition deque.hpp:48
constexpr Deque(Deque &&)=default
constexpr Deque()=default
constexpr auto & emplaceFront(Args &&...args) &
Definition deque.hpp:121
constexpr auto & pushBack(R const &element) &
Definition deque.hpp:143
constexpr auto & pushBack(T &&element) &
Definition deque.hpp:157
constexpr decltype(auto) front(Index const position=0) &&
Definition deque.hpp:33
constexpr auto emplaceFront(Args &&...args) &&
Definition deque.hpp:112
constexpr decltype(auto) front(Index const position=0) &
Definition deque.hpp:43
constexpr Deque(Deque const &)=default
constexpr decltype(auto) back(Index const position=0) const &&
Definition deque.hpp:58
constexpr auto & clear() &
Definition deque.hpp:73
static constexpr auto Capacity
Definition deque.hpp:18
constexpr decltype(auto) back(Index const position=0) &
Definition deque.hpp:63
constexpr auto & popFront() &
Definition deque.hpp:128
constexpr auto pushFront(T &&element) &&
Definition deque.hpp:96
constexpr Deque & operator=(Deque &&) &=default
constexpr auto & pushFront(R const &element) &
Definition deque.hpp:89
constexpr auto pushFront(R const &element) &&
Definition deque.hpp:81
constexpr decltype(auto) back(Index const position=0) const &
Definition deque.hpp:68
~Deque()=default
constexpr auto & popBack() &
Definition deque.hpp:182
constexpr decltype(auto) back(Index const position=0) &&
Definition deque.hpp:53
constexpr decltype(auto) front(Index const position=0) const &&
Definition deque.hpp:38
constexpr auto pushBack(R const &element) &&
Definition deque.hpp:135
Definition value.hpp:12
Definition concepts.hpp:37
Definition deque.hpp:293
Definition concepts.hpp:19
pRC::Float<> T
Definition externs_nonTT.hpp:1
static void error(Xs &&...args)
Definition log.hpp:14
Definition cholesky.hpp:10
Size Index
Definition basics.hpp:32
std::size_t Size
Definition basics.hpp:31
static constexpr decltype(auto) min(X &&a)
Definition min.hpp:13
CommonArray< Allocation::Stack, T, Ns... > StackArray
Definition declarations.hpp:15
constexpr auto cDebugLevel
Definition config.hpp:48
#define BEGIN_IGNORE_DIAGNOSTIC_GCC(warning)
Definition pragma.hpp:42
#define END_IGNORE_DIAGNOSTIC_GCC
Definition pragma.hpp:43