Note
|
Precondition
namespace circular = trial::circular; |
The circular::array<T, N>
template class is a fixed-size double-ended circular queue.
The circular array has a fixed size, which means there can be at most N
elements
in the circular array, where N
is determined at compile-time. Like std::array<T, N>
,
all N
elements within the storage of the circular array are default constructed
when the circular array is instantiated.
Although all elements in the embedded array are constructed, the circular array
will appear as empty. Inserting elements will overwrite the default constructed
elements.
In the circular span tutorial we ended up with a class definition for a running average containing both an array and a span.
template <typename T, std::size_t N>
class average
{
public:
using value_type = T;
// Member functions...
private:
static_assert(N > 0, "N must be greater than zero");
value_type sum = {};
// Storage and window
value_type storage[N];
circular::span<value_type, N> window;
};
Combining a fixed-size storage with a circular span is exactly the purpose of
the circular array, so we can replace the storage and span with
circular::array<T, N>
.
template <typename T, std::size_t N>
class average
{
public:
using value_type = T;
// Member functions...
private:
static_assert(N > 0, "N must be greater than zero");
value_type sum = {};
// Window contains storage
circular::array<value_type, N> window;
};
This section describes the choices behind the design of the circular array.
The design rationale of circular::span<T, N>
also applies,
except the ability to use dynamic_extent
.
The circular array, being a fixed-size container, is modeled after std::array<T, N>
.
The storage is located inside the circular array, which gives good locality and this
cache performance, but move is linear time complexity.
There are some significant deviations.
-
The circular array keeps track of the number of inserted elements. Although the underlying storage is filled with default constructed elements, they do not count as inserted elements. Inserted elements are those that have been assigned to the container, or that have been pushed to the front or the back.
-
The circular array does not fulfill the ContiguousContainer requirements even though the storage is contiguous. This is because the circular array can wrap around the end of the storage, so it cannot fulfill the ContiguousIterator concept.
Defined in header <trial/circular/array.hpp>
.
Defined in namespace trial::circular
.
template <
typename T,
std::size_t N
> class array;
The circular array is a fixed-size circular queue.
The size is the current number of elements in the circular array.
The capacity is the maximum number of elements that can be inserted without
overwriting old elements.
The capacity is set to N
at compile-time and cannot be changed at run-time.
The underlying storage will default construct N
elements at construction-time.
Inserting elements into the circular array will overwrite elements in the
storage. Erasing elements from the circular array will leave the elements
in the storage either untouched or in a moved-from state. The only guarantee
given with regards to the elements in the storage is that they are in a
valid state and therefore can either can be overwritten when new elements
are inserted or destroyed when the circular array is destroyed.
|
Element type.
|
|
The maximum number of elements in the span.
|
Member type | Definition |
---|---|
|
|
|
|
|
|
|
|
|
|
|
RandomAccessIterator with |
|
RandomAccessIterator with |
|
|
|
|
|
ContiguousRange and SizedRange with |
|
ContiguousRange and SizedRange with |
Member function | Description |
---|---|
|
Creates an empty circular array.
|
|
Creates a circular array by copying.
|
|
Creates a circular array by moving.
|
|
Creates a circular array with elements from input.
|
|
Recreates circular array by copying.
|
|
Recreates circular array by moving.
|
|
Recreates circular array with elements from initializer list.
|
|
Checks if circular array is empty.
|
|
Checks if circular array is full.
|
|
Returns the maximum possible number of elements in the circular array. |
|
Returns the number of elements in the circular array. |
|
Returns the maximum possible number of elements in the circular array. |
|
Returns a reference to the first element in the circular array.
|
|
Returns a constant reference to the first element in the circular array.
|
|
Returns a reference to the last element in the circular array.
|
|
Returns a constant reference to the last element in the circular array.
|
|
Clears the circular array.
|
|
Replaces the circular array with elements from iterator range.
|
|
Replaces the circular array with elements from initializer list.
|
|
Inserts an element at the beginning of the circular array.
|
|
Inserts elements from iterator range at the beginning of the circular array.
|
|
Inserts an element at the end of the circular array.
|
|
Inserts elements from iterator range at the end of the circular array.
|
|
Removes and returns an element from the beginning of the circular array.
|
|
Removes and returns an element from the end of the circular array.
|
|
Inserts the given number of unspecified elements at the beginning of the circular array.
|
|
Inserts the given number of unspecified elements at the end of the circular array.
|
|
Removes the given number of elements from the beginning of the circular array.
|
|
Removes the given number of elements from the end of the circular array.
|
|
Returns an interator to the beginning of the circular array. |
|
Returns an interator to the end of the circular array. |
|
Returns a reverse interator to the beginning of the circular array. |
|
Returns a reverse interator to the end of the circular array. |
|
Returns the first contiguous segment of the circular array.
|
|
Returns the last contiguous segment of the circular array.
|
|
Returns a reference to the element at the given position in the circular array.
|
|
Returns a reference to the element at the given position in the circular array.
|
|
Returns the first contiguous unused segment of the circular array.
|
|
Returns the last contiguous unused segment of the circular array.
|