stdex
Additional custom or not Standard C++ covered algorithms
vector_queue.h
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2016-2022 Amebis
4*/
5
6#pragma once
7
8namespace stdex
9{
13 template <class T>
15 {
16 public:
20 typedef size_t size_type;
21
25 typedef T value_type;
26
30 typedef T& reference;
31
35 typedef const T& const_reference;
36
40 typedef T* pointer;
41
45 typedef const T* const_pointer;
46
47 public:
53 vector_queue(_In_ size_type size_max) :
54 m_data(new value_type[size_max]),
55 m_head(0),
56 m_count(0),
57 m_size_max(size_max)
58 {
59 }
60
67 m_data(new value_type[other.m_size_max]),
68 m_head(other.m_head),
69 m_count(other.m_count),
71 {
72 // Copy elements.
73 for (size_type i = 0; i < m_count; i++) {
74 size_type i_l = abs(i);
75 m_data[i_l] = other.m_data[i_l];
76 }
77 }
78
82 virtual ~vector_queue()
83 {
84 if (m_data) delete [] m_data;
85 }
86
93 m_data (std::move(other.m_data )),
94 m_head (std::move(other.m_head )),
95 m_count (std::move(other.m_count )),
96 m_size_max(std::move(other.m_size_max))
97 {
98 // Reset other to consistent state.
99 other.m_data = NULL;
100 other.m_head = 0;
101 other.m_count = 0;
102 other.m_size_max = 0;
103 }
104
111 {
112 if (this != std::addressof(other)) {
113 m_head = other.m_head;
114 m_count = other.m_count;
115 m_size_max = other.m_size_max;
116
117 // Copy elements.
118 if (m_data) delete [] m_data;
119 m_data = new value_type[other.m_size_max];
120 for (size_type i = 0; i < m_count; i++) {
121 size_type i_l = abs(i);
122 m_data[i_l] = other.m_data[i_l];
123 }
124 }
125
126 return *this;
127 }
128
135 {
136 if (this != std::addressof(other)) {
137 m_data = std::move(other.m_data );
138 m_head = std::move(other.m_head );
139 m_count = std::move(other.m_count );
140 m_size_max = std::move(other.m_size_max);
141
142 // Reset other to consistent state.
143 other.m_data = NULL;
144 other.m_head = 0;
145 other.m_count = 0;
146 other.m_size_max = 0;
147 }
148
149 return *this;
150 }
151
156 {
157 return m_count;
158 }
159
164 {
165 return m_size_max;
166 }
167
171 void clear()
172 {
173 m_count = 0;
174 }
175
179 bool empty() const
180 {
181 return m_count == 0;
182 }
183
190 {
191 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
192 return m_data[abs(pos)];
193 }
194
201 {
202 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
203 return m_data[abs(pos)];
204 }
205
212 {
213 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
214 return m_data[abs(pos)];
215 }
216
223 {
224 if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
225 return m_data[abs(pos)];
226 }
227
236 {
237 if (pos >= m_size_max) throw std::invalid_argument("Invalid subscript");
238 return m_data[pos];
239 }
240
249 {
250 if (pos >= m_size_max) throw std::invalid_argument("Invalid subscript");
251 return m_data[pos];
252 }
253
262 {
263 if (m_count < m_size_max) {
264 size_type pos = abs(m_count);
265 m_data[pos] = v;
266 m_count++;
267 return pos;
268 } else {
269 size_type pos = m_head;
270 m_data[pos] = v;
271 m_head = abs(1);
272 return pos;
273 }
274 }
275
284 {
285 if (m_count < m_size_max) {
286 size_type pos = abs(m_count);
287 m_data[pos] = std::move(v);
288 m_count++;
289 return pos;
290 } else {
291 size_type pos = m_head;
292 m_data[pos] = std::move(v);
293 m_head = abs(1);
294 return pos;
295 }
296 }
297
301 void pop_back()
302 {
303 if (!m_count) throw std::invalid_argument("Empty storage");
304 m_count--;
305 }
306
315 {
316 m_head = abs(-1);
317 if (m_count < m_size_max)
318 m_count++;
319 m_data[m_head] = v;
320 return m_head;
321 }
322
331 {
332 m_head = abs(-1);
333 if (m_count < m_size_max)
334 m_count++;
335 m_data[m_head] = std::move(v);
336 return m_head;
337 }
338
343 {
344 if (!m_count) throw std::invalid_argument("Empty storage");
345 m_head = abs(1);
346 m_count--;
347 }
348
353 {
354 if (!m_count) throw std::invalid_argument("Empty storage");
355 return m_data[m_head];
356 }
357
362 {
363 if (!m_count) throw std::invalid_argument("Empty storage");
364 return m_data[m_head];
365 }
366
371 {
372 return m_data[tail()];
373 }
374
379 {
380 return m_data[tail()];
381 }
382
387 {
388 return m_head;
389 }
390
395 {
396 if (!m_count) throw std::invalid_argument("Empty storage");
397 return abs(m_count - 1);
398 }
399
402 size_type abs(_In_ size_type pos) const
403 {
404 return (m_head + pos) % m_size_max;
405 }
406
407 protected:
412 };
413}
Helper class to allow limited size FIFO queues implemented as vector of elements.
Definition: vector_queue.h:15
vector_queue< value_type > & operator=(const vector_queue< value_type > &other)
Copies existing queue.
Definition: vector_queue.h:110
const T * const_pointer
Constant pointer to element.
Definition: vector_queue.h:45
bool empty() const
Tests if the queue is empty.
Definition: vector_queue.h:179
size_type tail() const
Returns absolute subscript or position number of the last element in the queue. The element must exis...
Definition: vector_queue.h:394
reference operator[](size_type pos)
Returns a reference to the element at a specified location in the queue.
Definition: vector_queue.h:200
vector_queue(const vector_queue< value_type > &other)
Copies existing queue.
Definition: vector_queue.h:66
value_type * m_data
Underlying data container.
Definition: vector_queue.h:408
size_t size_type
Type to measure element count and indices in.
Definition: vector_queue.h:20
T & reference
Reference to element type.
Definition: vector_queue.h:30
const_reference back() const
Returns a constant reference to the last element in the queue.
Definition: vector_queue.h:378
vector_queue(vector_queue< value_type > &&other)
Moves existing queue.
Definition: vector_queue.h:92
reference back()
Returns a reference to the last element in the queue.
Definition: vector_queue.h:370
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.h:283
size_type head() const
Returns absolute subscript or position number of the head element in the queue. The element does not ...
Definition: vector_queue.h:386
size_type m_count
Number of elements.
Definition: vector_queue.h:410
virtual ~vector_queue()
Destroys the queue.
Definition: vector_queue.h:82
reference front()
Returns a reference to the head element in the queue.
Definition: vector_queue.h:352
size_type m_size_max
Maximum size.
Definition: vector_queue.h:411
vector_queue< value_type > & operator=(vector_queue< value_type > &&other)
Moves existing queue.
Definition: vector_queue.h:134
reference at_abs(size_type pos)
Returns a reference to the element at the absolute location in the queue.
Definition: vector_queue.h:235
void clear()
Erases the elements of the queue.
Definition: vector_queue.h:171
const_reference front() const
Returns a constant reference to the head element in the queue.
Definition: vector_queue.h:361
vector_queue(size_type size_max)
Construct queue of fixed size.
Definition: vector_queue.h:53
T value_type
Element type.
Definition: vector_queue.h:25
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.h:261
void pop_back()
Removes (dequeues) the last element of the queue.
Definition: vector_queue.h:301
size_type m_head
Index of the first element.
Definition: vector_queue.h:409
T * pointer
Pointer to element.
Definition: vector_queue.h:40
const_reference at(size_type pos) const
Returns a constant reference to the element at a specified location in the queue.
Definition: vector_queue.h:211
const_reference operator[](size_type pos) const
Returns a constant reference to the element at a specified location in the queue.
Definition: vector_queue.h:222
size_type size() const
Returns the number of elements in the vector.
Definition: vector_queue.h:155
void pop_front()
Removes (dequeues) the head element of the queue.
Definition: vector_queue.h:342
size_type capacity() const
Returns the number of elements that the queue can contain before overwriting head ones.
Definition: vector_queue.h:163
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.h:314
reference at(size_type pos)
Returns a reference to the element at a specified location in the queue.
Definition: vector_queue.h:189
size_type abs(size_type pos) const
Returns absolute subscript or position number of the given element in the queue.
Definition: vector_queue.h:402
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.h:330
const T & const_reference
Constant reference to element type.
Definition: vector_queue.h:35
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.h:248