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