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-2024 Amebis
4*/
5
6#pragma once
7
8#include "compat.hpp"
9#include <stdexcept>
10#include <utility>
11
12 namespace stdex
13{
17 template <class T>
18 class vector_queue
19 {
20 public:
24 typedef size_t size_type;
25
29 typedef T value_type;
30
34 typedef T& reference;
35
39 typedef const T& const_reference;
40
44 typedef T* pointer;
45
49 typedef const T* const_pointer;
50
51 public:
57 vector_queue(_In_ size_type size_max) :
58 m_data(new value_type[size_max]),
59 m_head(0),
60 m_count(0),
61 m_size_max(size_max)
62 {}
63
69 vector_queue(_In_ const vector_queue<value_type> &other) :
70 m_data(new value_type[other.m_size_max]),
71 m_head(other.m_head),
72 m_count(other.m_count),
73 m_size_max(other.m_size_max)
74 {
75 // Copy elements.
76 for (size_type i = 0; i < m_count; i++) {
77 size_type i_l = abs(i);
78 m_data[i_l] = other.m_data[i_l];
79 }
80 }
81
85 virtual ~vector_queue()
86 {
87 if (m_data) delete [] m_data;
88 }
89
95 vector_queue(_Inout_ vector_queue<value_type> &&other) :
96 m_data (std::move(other.m_data )),
97 m_head (std::move(other.m_head )),
98 m_count (std::move(other.m_count )),
99 m_size_max(std::move(other.m_size_max))
100 {
101 // Reset other to consistent state.
102 other.m_data = NULL;
103 other.m_head = 0;
104 other.m_count = 0;
105 other.m_size_max = 0;
106 }
107
113 vector_queue<value_type>& operator=(_In_ const vector_queue<value_type> &other)
114 {
115 if (this != std::addressof(other)) {
116 m_head = other.m_head;
117 m_count = other.m_count;
118 m_size_max = other.m_size_max;
119
120 // Copy elements.
121 if (m_data) delete [] m_data;
122 m_data = new value_type[other.m_size_max];
123 for (size_type i = 0; i < m_count; i++) {
124 size_type i_l = abs(i);
125 m_data[i_l] = other.m_data[i_l];
126 }
127 }
128
129 return *this;
130 }
131
137 vector_queue<value_type>& operator=(_Inout_ vector_queue<value_type> &&other)
138 {
139 if (this != std::addressof(other)) {
140 m_data = std::move(other.m_data );
141 m_head = std::move(other.m_head );
142 m_count = std::move(other.m_count );
143 m_size_max = std::move(other.m_size_max);
144
145 // Reset other to consistent state.
146 other.m_data = NULL;
147 other.m_head = 0;
148 other.m_count = 0;
149 other.m_size_max = 0;
150 }
151
152 return *this;
153 }
154
158 size_type size() const
159 {
160 return m_count;
161 }
162
166 size_type capacity() const
167 {
168 return m_size_max;
169 }
170
174 void clear()
175 {
176 m_count = 0;
177 }
178
182 bool empty() const
183 {
184 return m_count == 0;
185 }
186
192 reference at(_In_ size_type pos)
193 {
194 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
195 return m_data[abs(pos)];
196 }
197
203 reference operator[](_In_ size_type pos)
204 {
205 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
206 return m_data[abs(pos)];
207 }
208
214 const_reference at(_In_ size_type pos) const
215 {
216 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
217 return m_data[abs(pos)];
218 }
219
225 const_reference operator[](_In_ size_type pos) const
226 {
227 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
228 return m_data[abs(pos)];
229 }
230
238 reference at_abs(_In_ size_type pos)
239 {
240 if (pos >= m_size_max) throw std::invalid_argument("Invalid subscript");
241 return m_data[pos];
242 }
243
251 const_reference at_abs(_In_ size_type pos) const
252 {
253 if (pos >= m_size_max) throw std::invalid_argument("Invalid subscript");
254 return m_data[pos];
255 }
256
264 size_type push_back(_In_ const value_type &v)
265 {
266 if (m_count < m_size_max) {
267 size_type pos = abs(m_count);
268 m_data[pos] = v;
269 m_count++;
270 return pos;
271 } else {
272 size_type pos = m_head;
273 m_data[pos] = v;
274 m_head = abs(1);
275 return pos;
276 }
277 }
278
286 size_type push_back(_Inout_ value_type&&v)
287 {
288 if (m_count < m_size_max) {
289 size_type pos = abs(m_count);
290 m_data[pos] = std::move(v);
291 m_count++;
292 return pos;
293 } else {
294 size_type pos = m_head;
295 m_data[pos] = std::move(v);
296 m_head = abs(1);
297 return pos;
298 }
299 }
300
304 void pop_back()
305 {
306 if (!m_count) throw std::invalid_argument("Empty storage");
307 m_count--;
308 }
309
317 size_type push_front(_In_ const value_type &v)
318 {
319 m_head = abs(-1);
320 if (m_count < m_size_max)
321 m_count++;
322 m_data[m_head] = v;
323 return m_head;
324 }
325
333 size_type push_front(_Inout_ value_type&&v)
334 {
335 m_head = abs(-1);
336 if (m_count < m_size_max)
337 m_count++;
338 m_data[m_head] = std::move(v);
339 return m_head;
340 }
341
345 void pop_front()
346 {
347 if (!m_count) throw std::invalid_argument("Empty storage");
348 m_head = abs(1);
349 m_count--;
350 }
351
355 reference front()
356 {
357 if (!m_count) throw std::invalid_argument("Empty storage");
358 return m_data[m_head];
359 }
360
364 const_reference front() const
365 {
366 if (!m_count) throw std::invalid_argument("Empty storage");
367 return m_data[m_head];
368 }
369
373 reference back()
374 {
375 return m_data[tail()];
376 }
377
381 const_reference back() const
382 {
383 return m_data[tail()];
384 }
385
389 size_type head() const
390 {
391 return m_head;
392 }
393
397 size_type tail() const
398 {
399 if (!m_count) throw std::invalid_argument("Empty storage");
400 return abs(m_count - 1);
401 }
402
406 size_type abs(_In_ size_type pos) const
407 {
408 return (m_head + pos) % m_size_max;
409 }
410
411 protected:
412 value_type *m_data;
413 size_type m_head;
414 size_type m_count;
415 size_type m_size_max;
416 };
417}
stdex::vector_queue
Helper class to allow limited size FIFO queues implemented as vector of elements.
Definition vector_queue.hpp:19
stdex::vector_queue::operator=
vector_queue< value_type > & operator=(const vector_queue< value_type > &other)
Copies existing queue.
Definition vector_queue.hpp:113
stdex::vector_queue::value_type
T value_type
Element type.
Definition vector_queue.hpp:29
stdex::vector_queue::empty
bool empty() const
Tests if the queue is empty.
Definition vector_queue.hpp:182
stdex::vector_queue::tail
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:397
stdex::vector_queue::operator[]
reference operator[](size_type pos)
Returns a reference to the element at a specified location in the queue.
Definition vector_queue.hpp:203
stdex::vector_queue::vector_queue
vector_queue(const vector_queue< value_type > &other)
Copies existing queue.
Definition vector_queue.hpp:69
stdex::vector_queue::m_data
value_type * m_data
Underlying data container.
Definition vector_queue.hpp:412
stdex::vector_queue::const_reference
const T & const_reference
Constant reference to element type.
Definition vector_queue.hpp:39
stdex::vector_queue::back
const_reference back() const
Returns a constant reference to the last element in the queue.
Definition vector_queue.hpp:381
stdex::vector_queue::vector_queue
vector_queue(vector_queue< value_type > &&other)
Moves existing queue.
Definition vector_queue.hpp:95
stdex::vector_queue::back
reference back()
Returns a reference to the last element in the queue.
Definition vector_queue.hpp:373
stdex::vector_queue::push_back
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:286
stdex::vector_queue::head
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:389
stdex::vector_queue::m_count
size_type m_count
Number of elements.
Definition vector_queue.hpp:414
stdex::vector_queue::~vector_queue
virtual ~vector_queue()
Destroys the queue.
Definition vector_queue.hpp:85
stdex::vector_queue::front
reference front()
Returns a reference to the head element in the queue.
Definition vector_queue.hpp:355
stdex::vector_queue::m_size_max
size_type m_size_max
Maximum size.
Definition vector_queue.hpp:415
stdex::vector_queue::size_type
size_t size_type
Type to measure element count and indices in.
Definition vector_queue.hpp:24
stdex::vector_queue::operator=
vector_queue< value_type > & operator=(vector_queue< value_type > &&other)
Moves existing queue.
Definition vector_queue.hpp:137
stdex::vector_queue::at_abs
reference at_abs(size_type pos)
Returns a reference to the element at the absolute location in the queue.
Definition vector_queue.hpp:238
stdex::vector_queue::reference
T & reference
Reference to element type.
Definition vector_queue.hpp:34
stdex::vector_queue::clear
void clear()
Erases the elements of the queue.
Definition vector_queue.hpp:174
stdex::vector_queue::front
const_reference front() const
Returns a constant reference to the head element in the queue.
Definition vector_queue.hpp:364
stdex::vector_queue::vector_queue
vector_queue(size_type size_max)
Construct queue of fixed size.
Definition vector_queue.hpp:57
stdex::vector_queue::push_back
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:264
stdex::vector_queue::pop_back
void pop_back()
Removes (dequeues) the last element of the queue.
Definition vector_queue.hpp:304
stdex::vector_queue::m_head
size_type m_head
Index of the first element.
Definition vector_queue.hpp:413
stdex::vector_queue::at
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:214
stdex::vector_queue::pointer
T * pointer
Pointer to element.
Definition vector_queue.hpp:44
stdex::vector_queue::operator[]
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:225
stdex::vector_queue::size
size_type size() const
Returns the number of elements in the vector.
Definition vector_queue.hpp:158
stdex::vector_queue::pop_front
void pop_front()
Removes (dequeues) the head element of the queue.
Definition vector_queue.hpp:345
stdex::vector_queue::capacity
size_type capacity() const
Returns the number of elements that the queue can contain before overwriting head ones.
Definition vector_queue.hpp:166
stdex::vector_queue::const_pointer
const T * const_pointer
Constant pointer to element.
Definition vector_queue.hpp:49
stdex::vector_queue::push_front
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:317
stdex::vector_queue::at
reference at(size_type pos)
Returns a reference to the element at a specified location in the queue.
Definition vector_queue.hpp:192
stdex::vector_queue::abs
size_type abs(size_type pos) const
Returns absolute subscript or position number of the given element in the queue.
Definition vector_queue.hpp:406
stdex::vector_queue::push_front
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:333
stdex::vector_queue::at_abs
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:251