stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
vector_queue.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2016-2023 Amebis
4*/
5
6#pragma once
7
8#include "sal.hpp"
9
10namespace stdex
11{
15 template <class T>
17 {
18 public:
22 typedef size_t size_type;
23
27 typedef T value_type;
28
32 typedef T& reference;
33
37 typedef const T& const_reference;
38
42 typedef T* pointer;
43
47 typedef const T* const_pointer;
48
49 public:
55 vector_queue(_In_ size_type size_max) :
56 m_data(new value_type[size_max]),
57 m_head(0),
58 m_count(0),
59 m_size_max(size_max)
60 {
61 }
62
69 m_data(new value_type[other.m_size_max]),
70 m_head(other.m_head),
71 m_count(other.m_count),
73 {
74 // Copy elements.
75 for (size_type i = 0; i < m_count; i++) {
76 size_type i_l = abs(i);
77 m_data[i_l] = other.m_data[i_l];
78 }
79 }
80
84 virtual ~vector_queue()
85 {
86 if (m_data) delete [] m_data;
87 }
88
95 m_data (std::move(other.m_data )),
96 m_head (std::move(other.m_head )),
97 m_count (std::move(other.m_count )),
98 m_size_max(std::move(other.m_size_max))
99 {
100 // Reset other to consistent state.
101 other.m_data = NULL;
102 other.m_head = 0;
103 other.m_count = 0;
104 other.m_size_max = 0;
105 }
106
113 {
114 if (this != std::addressof(other)) {
115 m_head = other.m_head;
116 m_count = other.m_count;
117 m_size_max = other.m_size_max;
118
119 // Copy elements.
120 if (m_data) delete [] m_data;
121 m_data = new value_type[other.m_size_max];
122 for (size_type i = 0; i < m_count; i++) {
123 size_type i_l = abs(i);
124 m_data[i_l] = other.m_data[i_l];
125 }
126 }
127
128 return *this;
129 }
130
137 {
138 if (this != std::addressof(other)) {
139 m_data = std::move(other.m_data );
140 m_head = std::move(other.m_head );
141 m_count = std::move(other.m_count );
142 m_size_max = std::move(other.m_size_max);
143
144 // Reset other to consistent state.
145 other.m_data = NULL;
146 other.m_head = 0;
147 other.m_count = 0;
148 other.m_size_max = 0;
149 }
150
151 return *this;
152 }
153
158 {
159 return m_count;
160 }
161
166 {
167 return m_size_max;
168 }
169
173 void clear()
174 {
175 m_count = 0;
176 }
177
181 bool empty() const
182 {
183 return m_count == 0;
184 }
185
192 {
193 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
194 return m_data[abs(pos)];
195 }
196
203 {
204 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
205 return m_data[abs(pos)];
206 }
207
214 {
215 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
216 return m_data[abs(pos)];
217 }
218
225 {
226 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
227 return m_data[abs(pos)];
228 }
229
238 {
239 if (pos >= m_size_max) throw std::invalid_argument("Invalid subscript");
240 return m_data[pos];
241 }
242
251 {
252 if (pos >= m_size_max) throw std::invalid_argument("Invalid subscript");
253 return m_data[pos];
254 }
255
264 {
265 if (m_count < m_size_max) {
266 size_type pos = abs(m_count);
267 m_data[pos] = v;
268 m_count++;
269 return pos;
270 } else {
271 size_type pos = m_head;
272 m_data[pos] = v;
273 m_head = abs(1);
274 return pos;
275 }
276 }
277
286 {
287 if (m_count < m_size_max) {
288 size_type pos = abs(m_count);
289 m_data[pos] = std::move(v);
290 m_count++;
291 return pos;
292 } else {
293 size_type pos = m_head;
294 m_data[pos] = std::move(v);
295 m_head = abs(1);
296 return pos;
297 }
298 }
299
303 void pop_back()
304 {
305 if (!m_count) throw std::invalid_argument("Empty storage");
306 m_count--;
307 }
308
317 {
318 m_head = abs(-1);
319 if (m_count < m_size_max)
320 m_count++;
321 m_data[m_head] = v;
322 return m_head;
323 }
324
333 {
334 m_head = abs(-1);
335 if (m_count < m_size_max)
336 m_count++;
337 m_data[m_head] = std::move(v);
338 return m_head;
339 }
340
345 {
346 if (!m_count) throw std::invalid_argument("Empty storage");
347 m_head = abs(1);
348 m_count--;
349 }
350
355 {
356 if (!m_count) throw std::invalid_argument("Empty storage");
357 return m_data[m_head];
358 }
359
364 {
365 if (!m_count) throw std::invalid_argument("Empty storage");
366 return m_data[m_head];
367 }
368
373 {
374 return m_data[tail()];
375 }
376
381 {
382 return m_data[tail()];
383 }
384
389 {
390 return m_head;
391 }
392
397 {
398 if (!m_count) throw std::invalid_argument("Empty storage");
399 return abs(m_count - 1);
400 }
401
405 size_type abs(_In_ size_type pos) const
406 {
407 return (m_head + pos) % m_size_max;
408 }
409
410 protected:
415 };
416}
Helper class to allow limited size FIFO queues implemented as vector of elements.
Definition vector_queue.hpp:17
vector_queue< value_type > & operator=(const vector_queue< value_type > &other)
Copies existing queue.
Definition vector_queue.hpp:112
const T * const_pointer
Constant pointer to element.
Definition vector_queue.hpp:47
bool empty() const
Tests if the queue is empty.
Definition vector_queue.hpp:181
size_type tail() const
Returns absolute subscript or position number of the last element in the queue. The element must exis...
Definition vector_queue.hpp:396
reference operator[](size_type pos)
Returns a reference to the element at a specified location in the queue.
Definition vector_queue.hpp:202
vector_queue(const vector_queue< value_type > &other)
Copies existing queue.
Definition vector_queue.hpp:68
value_type * m_data
Underlying data container.
Definition vector_queue.hpp:411
size_t size_type
Type to measure element count and indices in.
Definition vector_queue.hpp:22
T & reference
Reference to element type.
Definition vector_queue.hpp:32
const_reference back() const
Returns a constant reference to the last element in the queue.
Definition vector_queue.hpp:380
vector_queue(vector_queue< value_type > &&other)
Moves existing queue.
Definition vector_queue.hpp:94
reference back()
Returns a reference to the last element in the queue.
Definition vector_queue.hpp:372
size_type push_back(value_type &&v)
Moves the element to the end of the queue, overriding the first one when queue is out of space.
Definition vector_queue.hpp:285
size_type head() const
Returns absolute subscript or position number of the head element in the queue. The element does not ...
Definition vector_queue.hpp:388
size_type m_count
Number of elements.
Definition vector_queue.hpp:413
virtual ~vector_queue()
Destroys the queue.
Definition vector_queue.hpp:84
reference front()
Returns a reference to the head element in the queue.
Definition vector_queue.hpp:354
size_type m_size_max
Maximum size.
Definition vector_queue.hpp:414
vector_queue< value_type > & operator=(vector_queue< value_type > &&other)
Moves existing queue.
Definition vector_queue.hpp:136
reference at_abs(size_type pos)
Returns a reference to the element at the absolute location in the queue.
Definition vector_queue.hpp:237
void clear()
Erases the elements of the queue.
Definition vector_queue.hpp:173
const_reference front() const
Returns a constant reference to the head element in the queue.
Definition vector_queue.hpp:363
vector_queue(size_type size_max)
Construct queue of fixed size.
Definition vector_queue.hpp:55
T value_type
Element type.
Definition vector_queue.hpp:27
size_type push_back(const value_type &v)
Copies an existing element to the end of the queue, overriding the first one when queue is out of spa...
Definition vector_queue.hpp:263
void pop_back()
Removes (dequeues) the last element of the queue.
Definition vector_queue.hpp:303
size_type m_head
Index of the first element.
Definition vector_queue.hpp:412
T * pointer
Pointer to element.
Definition vector_queue.hpp:42
const_reference at(size_type pos) const
Returns a constant reference to the element at a specified location in the queue.
Definition vector_queue.hpp:213
const_reference operator[](size_type pos) const
Returns a constant reference to the element at a specified location in the queue.
Definition vector_queue.hpp:224
size_type size() const
Returns the number of elements in the vector.
Definition vector_queue.hpp:157
void pop_front()
Removes (dequeues) the head element of the queue.
Definition vector_queue.hpp:344
size_type capacity() const
Returns the number of elements that the queue can contain before overwriting head ones.
Definition vector_queue.hpp:165
size_type push_front(const value_type &v)
Copies an existing element to the head of the queue, overriding the last one when queue is out of spa...
Definition vector_queue.hpp:316
reference at(size_type pos)
Returns a reference to the element at a specified location in the queue.
Definition vector_queue.hpp:191
size_type abs(size_type pos) const
Returns absolute subscript or position number of the given element in the queue.
Definition vector_queue.hpp:405
size_type push_front(value_type &&v)
Moves the element to the head of the queue, overriding the last one when queue is out of space and mo...
Definition vector_queue.hpp:332
const T & const_reference
Constant reference to element type.
Definition vector_queue.hpp:37
const_reference at_abs(size_type pos) const
Returns a constant reference to the element at the absolute location in the queue: measured from the ...
Definition vector_queue.hpp:250