Loading...
Searching...
No Matches
vector_queue.hpp
/*
SPDX-License-Identifier: MIT
Copyright © 2016-2023 Amebis
*/
#pragma once
#include "compat.hpp"
#include <stdexcept>
#include <utility>
namespace stdex
{
template <class T>
class vector_queue
{
public:
typedef size_t size_type;
typedef T value_type;
typedef T& reference;
typedef const T& const_reference;
typedef T* pointer;
typedef const T* const_pointer;
public:
vector_queue(_In_ size_type size_max) :
m_data(new value_type[size_max]),
m_head(0),
m_count(0),
m_size_max(size_max)
{
}
vector_queue(_In_ const vector_queue<value_type> &other) :
m_data(new value_type[other.m_size_max]),
m_head(other.m_head),
m_count(other.m_count),
m_size_max(other.m_size_max)
{
// Copy elements.
for (size_type i = 0; i < m_count; i++) {
size_type i_l = abs(i);
m_data[i_l] = other.m_data[i_l];
}
}
virtual ~vector_queue()
{
if (m_data) delete [] m_data;
}
vector_queue(_Inout_ vector_queue<value_type> &&other) :
m_data (std::move(other.m_data )),
m_head (std::move(other.m_head )),
m_count (std::move(other.m_count )),
m_size_max(std::move(other.m_size_max))
{
// Reset other to consistent state.
other.m_data = NULL;
other.m_head = 0;
other.m_count = 0;
other.m_size_max = 0;
}
vector_queue<value_type>& operator=(_In_ const vector_queue<value_type> &other)
{
if (this != std::addressof(other)) {
m_head = other.m_head;
m_count = other.m_count;
m_size_max = other.m_size_max;
// Copy elements.
if (m_data) delete [] m_data;
m_data = new value_type[other.m_size_max];
for (size_type i = 0; i < m_count; i++) {
size_type i_l = abs(i);
m_data[i_l] = other.m_data[i_l];
}
}
return *this;
}
vector_queue<value_type>& operator=(_Inout_ vector_queue<value_type> &&other)
{
if (this != std::addressof(other)) {
m_data = std::move(other.m_data );
m_head = std::move(other.m_head );
m_count = std::move(other.m_count );
m_size_max = std::move(other.m_size_max);
// Reset other to consistent state.
other.m_data = NULL;
other.m_head = 0;
other.m_count = 0;
other.m_size_max = 0;
}
return *this;
}
size_type size() const
{
return m_count;
}
size_type capacity() const
{
return m_size_max;
}
void clear()
{
m_count = 0;
}
bool empty() const
{
return m_count == 0;
}
reference at(_In_ size_type pos)
{
if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
return m_data[abs(pos)];
}
reference operator[](_In_ size_type pos)
{
if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
return m_data[abs(pos)];
}
const_reference at(_In_ size_type pos) const
{
if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
return m_data[abs(pos)];
}
const_reference operator[](_In_ size_type pos) const
{
if (pos >= m_count) throw std::invalid_argument("Invalid subscript");
return m_data[abs(pos)];
}
reference at_abs(_In_ size_type pos)
{
if (pos >= m_size_max) throw std::invalid_argument("Invalid subscript");
return m_data[pos];
}
const_reference at_abs(_In_ size_type pos) const
{
if (pos >= m_size_max) throw std::invalid_argument("Invalid subscript");
return m_data[pos];
}
size_type push_back(_In_ const value_type &v)
{
if (m_count < m_size_max) {
size_type pos = abs(m_count);
m_data[pos] = v;
m_count++;
return pos;
} else {
size_type pos = m_head;
m_data[pos] = v;
m_head = abs(1);
return pos;
}
}
size_type push_back(_Inout_ value_type&&v)
{
if (m_count < m_size_max) {
size_type pos = abs(m_count);
m_data[pos] = std::move(v);
m_count++;
return pos;
} else {
size_type pos = m_head;
m_data[pos] = std::move(v);
m_head = abs(1);
return pos;
}
}
void pop_back()
{
if (!m_count) throw std::invalid_argument("Empty storage");
m_count--;
}
size_type push_front(_In_ const value_type &v)
{
m_head = abs(-1);
if (m_count < m_size_max)
m_count++;
m_data[m_head] = v;
return m_head;
}
size_type push_front(_Inout_ value_type&&v)
{
m_head = abs(-1);
if (m_count < m_size_max)
m_count++;
m_data[m_head] = std::move(v);
return m_head;
}
void pop_front()
{
if (!m_count) throw std::invalid_argument("Empty storage");
m_head = abs(1);
m_count--;
}
reference front()
{
if (!m_count) throw std::invalid_argument("Empty storage");
return m_data[m_head];
}
const_reference front() const
{
if (!m_count) throw std::invalid_argument("Empty storage");
return m_data[m_head];
}
reference back()
{
return m_data[tail()];
}
const_reference back() const
{
return m_data[tail()];
}
size_type head() const
{
return m_head;
}
size_type tail() const
{
if (!m_count) throw std::invalid_argument("Empty storage");
return abs(m_count - 1);
}
size_type abs(_In_ size_type pos) const
{
return (m_head + pos) % m_size_max;
}
protected:
value_type *m_data;
size_type m_head;
size_type m_count;
size_type m_size_max;
};
}
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:114
stdex::vector_queue::const_pointer
const T * const_pointer
Constant pointer to element.
Definition vector_queue.hpp:49
stdex::vector_queue::empty
bool empty() const
Tests if the queue is empty.
Definition vector_queue.hpp:183
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:398
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:204
stdex::vector_queue::vector_queue
vector_queue(const vector_queue< value_type > &other)
Copies existing queue.
Definition vector_queue.hpp:70
stdex::vector_queue::m_data
value_type * m_data
Underlying data container.
Definition vector_queue.hpp:413
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::reference
T & reference
Reference to element type.
Definition vector_queue.hpp:34
stdex::vector_queue::back
const_reference back() const
Returns a constant reference to the last element in the queue.
Definition vector_queue.hpp:382
stdex::vector_queue::vector_queue
vector_queue(vector_queue< value_type > &&other)
Moves existing queue.
Definition vector_queue.hpp:96
stdex::vector_queue::back
reference back()
Returns a reference to the last element in the queue.
Definition vector_queue.hpp:374
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:287
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:390
stdex::vector_queue::m_count
size_type m_count
Number of elements.
Definition vector_queue.hpp:415
stdex::vector_queue::~vector_queue
virtual ~vector_queue()
Destroys the queue.
Definition vector_queue.hpp:86
stdex::vector_queue::front
reference front()
Returns a reference to the head element in the queue.
Definition vector_queue.hpp:356
stdex::vector_queue::m_size_max
size_type m_size_max
Maximum size.
Definition vector_queue.hpp:416
stdex::vector_queue::operator=
vector_queue< value_type > & operator=(vector_queue< value_type > &&other)
Moves existing queue.
Definition vector_queue.hpp:138
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:239
stdex::vector_queue::clear
void clear()
Erases the elements of the queue.
Definition vector_queue.hpp:175
stdex::vector_queue::front
const_reference front() const
Returns a constant reference to the head element in the queue.
Definition vector_queue.hpp:365
stdex::vector_queue::vector_queue
vector_queue(size_type size_max)
Construct queue of fixed size.
Definition vector_queue.hpp:57
stdex::vector_queue::value_type
T value_type
Element type.
Definition vector_queue.hpp:29
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:265
stdex::vector_queue::pop_back
void pop_back()
Removes (dequeues) the last element of the queue.
Definition vector_queue.hpp:305
stdex::vector_queue::m_head
size_type m_head
Index of the first element.
Definition vector_queue.hpp:414
stdex::vector_queue::pointer
T * pointer
Pointer to element.
Definition vector_queue.hpp:44
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:215
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:226
stdex::vector_queue::size
size_type size() const
Returns the number of elements in the vector.
Definition vector_queue.hpp:159
stdex::vector_queue::pop_front
void pop_front()
Removes (dequeues) the head element of the queue.
Definition vector_queue.hpp:346
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:167
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:318
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:193
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:407
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:334
stdex::vector_queue::const_reference
const T & const_reference
Constant reference to element type.
Definition vector_queue.hpp:39
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:252