1#ifndef CDA_RAIL_FIXEDSIZEVECTOR_HPP
2#define CDA_RAIL_FIXEDSIZEVECTOR_HPP
6#include <initializer_list>
35 std::unique_ptr<T[]> m_data{std::make_unique<T[]>(0)};
56 : m_data(std::make_unique<T[]>(len)), m_len(len) {}
65 : m_data(std::make_unique<T[]>(init.
size())), m_len(init.
size()) {
66 std::copy(init.begin(), init.end(),
begin());
76 : m_data(std::make_unique<T[]>(other.m_len)), m_len(other.m_len) {
101 : m_data(std::move(other.m_data)), m_len(other.m_len) {
112 if (
this != &other) {
113 m_data = std::move(other.m_data);
135 std::swap(m_data, other.m_data);
136 std::swap(m_len, other.m_len);
149 m_data = std::make_unique<T[]>(len);
164 [[nodiscard]] T&
operator[](
size_t const i) {
return m_data[i]; }
172 [[nodiscard]]
const T&
operator[](
size_t const i)
const {
return m_data[i]; }
181 [[nodiscard]] T&
at(
size_t const i) {
183 throw std::out_of_range(
"FixedSizeVector index out of range.");
195 [[nodiscard]]
const T&
at(
size_t const i)
const {
197 throw std::out_of_range(
"FixedSizeVector index out of range.");
211 [[nodiscard]] T*
begin() {
return m_data.get(); }
219 return std::to_address(std::span<T>(m_data.get(), m_len).end());
228 [[nodiscard]]
const T*
begin()
const {
return m_data.get(); }
235 [[nodiscard]]
const T*
end()
const {
236 return std::to_address(std::span<const T>(m_data.get(), m_len).end());
251 [[nodiscard]]
const T*
cend()
const {
return end(); }
263 [[nodiscard]]
size_t size()
const {
return m_len; }
277 if (lhs.
size() != rhs.size()) {
280 for (
size_t i = 0; i < lhs.
size(); ++i) {
281 if (lhs[i] != rhs[i]) {
A heap-allocated, fixed-size array whose capacity cannot change after construction.
Definition FixedSizeVector.hpp:32
FixedSizeVector()=default
Constructs an empty vector with zero elements.
void swap(FixedSizeVector &other) noexcept
Swaps the contents of this vector with other.
Definition FixedSizeVector.hpp:134
T * begin()
Accesses the first element of the underlying array.
Definition FixedSizeVector.hpp:211
void delete_and_resize(size_t const len)
Replaces the storage with a new allocation of len elements.
Definition FixedSizeVector.hpp:147
T & at(size_t const i)
Accesses an element at a given index with bounds checking.
Definition FixedSizeVector.hpp:181
const T * cbegin() const
Obtains a const pointer to the first element.
Definition FixedSizeVector.hpp:244
const T * end() const
Obtains a const pointer to one past the last element.
Definition FixedSizeVector.hpp:235
FixedSizeVector & operator=(FixedSizeVector &&other) noexcept
Move-assigns from another FixedSizeVector.
Definition FixedSizeVector.hpp:111
FixedSizeVector(const FixedSizeVector &other)
Creates a deep copy of another vector's elements.
Definition FixedSizeVector.hpp:74
size_t size() const
Queries the number of elements.
Definition FixedSizeVector.hpp:263
FixedSizeVector(std::initializer_list< T > init)
Constructs a fixed size vector with given values.
Definition FixedSizeVector.hpp:63
FixedSizeVector(FixedSizeVector &&other) noexcept
Transfers ownership of the managed array from another vector.
Definition FixedSizeVector.hpp:100
FixedSizeVector & operator=(const FixedSizeVector &other)
Assigns a copy of another vector to this vector.
Definition FixedSizeVector.hpp:86
~FixedSizeVector()=default
Destructor. Releases the managed array.
T * end()
Provides a pointer to the position past the last element.
Definition FixedSizeVector.hpp:218
const T & at(size_t const i) const
Bounds-checked element access (const).
Definition FixedSizeVector.hpp:195
T & operator[](size_t const i)
Unchecked element access (non-const).
Definition FixedSizeVector.hpp:164
FixedSizeVector(size_t const len)
Constructs a vector with len default-initialized elements.
Definition FixedSizeVector.hpp:54
const T * cend() const
Provides a const pointer to one past the last element.
Definition FixedSizeVector.hpp:251
const T & operator[](size_t const i) const
Accesses an element at a specific index without bounds checking.
Definition FixedSizeVector.hpp:172
const T * begin() const
Obtains a const pointer to the first element.
Definition FixedSizeVector.hpp:228
Definition CustomExceptions.hpp:15
bool operator==(const FixedSizeVector< T > &lhs, const std::vector< T > &rhs)
Equality comparison between a FixedSizeVector and a std::vector.
Definition FixedSizeVector.hpp:276
void swap(FixedSizeVector< T > &lhs, FixedSizeVector< T > &rhs) noexcept
ADL-reachable swap for FixedSizeVector.
Definition FixedSizeVector.hpp:310