cMHN 1.1
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;
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<class R, If<IsConvertible<R, T>> = 0>
81 constexpr auto pushFront(R const &element) &&
82 {
83 updatePushFront();
84 front() = element;
85 return move(*this);
86 }
87
88 template<class R, If<IsConvertible<R, T>> = 0>
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, If<IsConstructible<T, Args...>> = 0>
111 constexpr auto emplaceFront(Args &&...args) &&
112 {
113 updatePushFront();
114 front() = T(forward<Args>(args)...);
115 return move(*this);
116 }
117
118 template<class... Args, If<IsConstructible<T, Args...>> = 0>
119 constexpr auto &emplaceFront(Args &&...args) &
120 {
121 updatePushFront();
122 front() = T(forward<Args>(args)...);
123 return *this;
124 }
125
126 constexpr auto &popFront() &
127 {
128 updatePopFront();
129 return *this;
130 }
131
132 template<class R, If<IsConvertible<R, T>> = 0>
133 constexpr auto pushBack(R const &element) &&
134 {
135 updatePushBack();
136 back() = element;
137 return move(*this);
138 }
139
140 template<class R, If<IsConvertible<R, T>> = 0>
141 constexpr auto &pushBack(R const &element) &
142 {
143 updatePushBack();
144 back() = element;
145 return *this;
146 }
147
148 constexpr auto pushBack(T &&element) &&
149 {
150 updatePushBack();
151 back() = move(element);
152 return move(*this);
153 }
154
155 constexpr auto &pushBack(T &&element) &
156 {
157 updatePushBack();
158 back() = move(element);
159 return *this;
160 }
161
162 template<class... Args, If<IsConstructible<T, Args...>> = 0>
163 constexpr auto emplaceBack(Args &&...args) &&
164 {
165 updatePushBack();
166 back() = T(forward<Args>(args)...);
167 return move(*this);
168 }
169
170 template<class... Args, If<IsConstructible<T, Args...>> = 0>
171 constexpr auto &emplaceBack(Args &&...args) &
172 {
173 updatePushBack();
174 back() = T(forward<Args>(args)...);
175 return *this;
176 }
177
178 constexpr auto &popBack() &
179 {
180 updatePopBack();
181 return *this;
182 }
183
184 private:
185 constexpr decltype(auto) operator[](Index const position) &&
186 {
187 return move(mData)[getIndex(position)];
188 }
189
190 constexpr decltype(auto) operator[](Index const position) const &&
191 {
192 return move(mData)[getIndex(position)];
193 }
194
195 constexpr decltype(auto) operator[](Index const position) &
196 {
197 return mData[getIndex(position)];
198 }
199
200 constexpr decltype(auto) operator[](Index const position) const &
201 {
202 return mData[getIndex(position)];
203 }
204
205 constexpr auto getIndex(Index const position) const
206 {
207 if constexpr(cDebugLevel >= DebugLevel::Mid)
208 {
209 if(position >= mSize)
210 {
211 Logging::error("Deque out of range.");
212 }
213 }
214
215 if constexpr(N != 0)
216 {
217 return (mNext + N - 1 - position) % N;
218 }
219 else
220 {
221 return mNext;
222 }
223 }
224
225 constexpr auto updatePushFront()
226 {
227 if constexpr(N != 0)
228 {
229 mSize = min(mSize + 1, N);
230 mNext = (mNext + 1) % N;
231 }
232 }
233
234 constexpr auto updatePopFront()
235 {
236 if constexpr(cDebugLevel >= DebugLevel::Mid)
237 {
238 if(mSize == 0)
239 {
240 Logging::error("Deque is empty.");
241 }
242 }
243
244 if constexpr(N != 0)
245 {
246 mSize = mSize - 1;
247 mNext = (mNext - 1) % N;
248 }
249 }
250
251 constexpr auto updatePushBack()
252 {
253 if constexpr(N != 0)
254 {
255 if(mSize == N)
256 {
257 mNext = (mNext - 1) % N;
258 }
259 mSize = min(mSize + 1, N);
260 }
261 }
262
263 constexpr auto updatePopBack()
264 {
265 if constexpr(cDebugLevel >= DebugLevel::Mid)
266 {
267 if(mSize == 0)
268 {
269 Logging::error("Deque is empty.");
270 }
271 }
272
273 if constexpr(N != 0)
274 {
275 mSize = mSize - 1;
276 }
277 }
278
279 private:
280 BEGIN_IGNORE_DIAGNOSTIC_GCC("-Wpedantic")
281 StackArray<T, N> mData;
283
284 Size mSize = 0;
285 Index mNext = 0;
286 };
287}
288#endif // pRC_CORE_CONTAINER_DEQUE_H
Definition deque.hpp:15
constexpr auto pushBack(T &&element) &&
Definition deque.hpp:148
constexpr auto & emplaceBack(Args &&...args) &
Definition deque.hpp:171
constexpr auto & pushFront(T &&element) &
Definition deque.hpp:103
constexpr auto pushFront(R const &element) &&
Definition deque.hpp:81
constexpr auto size() const
Definition deque.hpp:28
constexpr auto & pushBack(R const &element) &
Definition deque.hpp:141
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:119
Constant< Size, N > Space
Definition deque.hpp:18
constexpr auto & pushBack(T &&element) &
Definition deque.hpp:155
constexpr decltype(auto) front(Index const position=0) &&
Definition deque.hpp:33
constexpr auto pushBack(R const &element) &&
Definition deque.hpp:133
constexpr decltype(auto) front(Index const position=0) &
Definition deque.hpp:43
constexpr auto emplaceBack(Args &&...args) &&
Definition deque.hpp:163
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
constexpr auto emplaceFront(Args &&...args) &&
Definition deque.hpp:111
constexpr decltype(auto) back(Index const position=0) &
Definition deque.hpp:63
constexpr auto & popFront() &
Definition deque.hpp:126
constexpr auto pushFront(T &&element) &&
Definition deque.hpp:96
constexpr Deque & operator=(Deque &&) &=default
constexpr decltype(auto) back(Index const position=0) const &
Definition deque.hpp:68
~Deque()=default
constexpr auto & popBack() &
Definition deque.hpp:178
constexpr auto & pushFront(R const &element) &
Definition deque.hpp:89
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
pRC::Float<> T
Definition externs_nonTT.hpp:1
static void error(Xs &&...args)
Definition log.hpp:14
Definition cholesky.hpp:18
std::is_constructible< T, Args... > IsConstructible
Definition type_traits.hpp:143
static constexpr X min(X &&a)
Definition min.hpp:13
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
Size Index
Definition type_traits.hpp:21
std::size_t Size
Definition type_traits.hpp:20
std::enable_if_t< B{}, int > If
Definition type_traits.hpp:68
CommonArray< Allocation::Stack, T, Ns... > StackArray
Definition type_traits.hpp:52
constexpr auto cDebugLevel
Definition config.hpp:46
std::integral_constant< T, V > Constant
Definition type_traits.hpp:34
#define BEGIN_IGNORE_DIAGNOSTIC_GCC(warning)
Definition pragma.hpp:42
#define END_IGNORE_DIAGNOSTIC_GCC
Definition pragma.hpp:43