MTCT
Design Automation and Optimization for ETCS Railway Networks
Loading...
Searching...
No Matches
FixedSizeVector.hpp
Go to the documentation of this file.
1#ifndef CDA_RAIL_FIXEDSIZEVECTOR_HPP
2#define CDA_RAIL_FIXEDSIZEVECTOR_HPP
3
4#include <algorithm>
5#include <cstddef>
6#include <initializer_list>
7#include <memory>
8#include <span>
9#include <stdexcept>
10#include <vector>
11
12namespace cda_rail {
13
14using std::size_t;
15
31// NOLINTBEGIN(cppcoreguidelines-avoid-c-arrays)
32template <typename T> class FixedSizeVector {
33private:
34 // NOLINTBEGIN(*-avoid-c-arrays)
35 std::unique_ptr<T[]> m_data{std::make_unique<T[]>(0)};
36 // NOLINTEND(*-avoid-c-arrays)
37 size_t m_len{0};
38
39public:
40 /*
41 * CONSTRUCTORS / RULE OF 5
42 */
43
47 FixedSizeVector() = default;
48
54 explicit FixedSizeVector(size_t const len)
55 // NOLINTNEXTLINE(*-avoid-c-arrays)
56 : m_data(std::make_unique<T[]>(len)), m_len(len) {}
57
63 FixedSizeVector(std::initializer_list<T> init)
64 // NOLINTNEXTLINE(*-avoid-c-arrays)
65 : m_data(std::make_unique<T[]>(init.size())), m_len(init.size()) {
66 std::copy(init.begin(), init.end(), begin());
67 }
68
75 // NOLINTNEXTLINE(*-avoid-c-arrays)
76 : m_data(std::make_unique<T[]>(other.m_len)), m_len(other.m_len) {
77 std::copy(other.begin(), other.end(), begin());
78 }
79
87 if (this == &other) {
88 return *this;
89 }
90 FixedSizeVector tmp(other);
91 swap(tmp);
92 return *this;
93 }
94
101 : m_data(std::move(other.m_data)), m_len(other.m_len) {
102 other.m_len = 0;
103 }
104
112 if (this != &other) {
113 m_data = std::move(other.m_data);
114 m_len = other.m_len;
115 other.m_len = 0;
116 }
117 return *this;
118 }
119
123 ~FixedSizeVector() = default;
124
125 /*
126 * MODIFIERS
127 */
128
134 void swap(FixedSizeVector& other) noexcept {
135 std::swap(m_data, other.m_data);
136 std::swap(m_len, other.m_len);
137 }
138
147 void delete_and_resize(size_t const len) {
148 // NOLINTNEXTLINE(*-avoid-c-arrays)
149 m_data = std::make_unique<T[]>(len);
150 m_len = len;
151 }
152
153 /*
154 * ELEMENT ACCESS
155 */
156
164 [[nodiscard]] T& operator[](size_t const i) { return m_data[i]; }
165
172 [[nodiscard]] const T& operator[](size_t const i) const { return m_data[i]; }
173
181 [[nodiscard]] T& at(size_t const i) {
182 if (i >= m_len) {
183 throw std::out_of_range("FixedSizeVector index out of range.");
184 }
185 return m_data[i];
186 }
187
195 [[nodiscard]] const T& at(size_t const i) const {
196 if (i >= m_len) {
197 throw std::out_of_range("FixedSizeVector index out of range.");
198 }
199 return m_data[i];
200 }
201
202 /*
203 * ITERATORS
204 */
205
211 [[nodiscard]] T* begin() { return m_data.get(); }
212
218 [[nodiscard]] T* end() {
219 return std::to_address(std::span<T>(m_data.get(), m_len).end());
220 }
221
228 [[nodiscard]] const T* begin() const { return m_data.get(); }
229
235 [[nodiscard]] const T* end() const {
236 return std::to_address(std::span<const T>(m_data.get(), m_len).end());
237 }
238
244 [[nodiscard]] const T* cbegin() const { return begin(); }
245
251 [[nodiscard]] const T* cend() const { return end(); }
252
253 /*
254 * CAPACITY
255 */
256
263 [[nodiscard]] size_t size() const { return m_len; }
264};
265
275template <typename T>
276bool operator==(const FixedSizeVector<T>& lhs, const std::vector<T>& rhs) {
277 if (lhs.size() != rhs.size()) {
278 return false;
279 }
280 for (size_t i = 0; i < lhs.size(); ++i) {
281 if (lhs[i] != rhs[i]) {
282 return false;
283 }
284 }
285 return true;
286}
287
297template <typename T>
298bool operator==(const std::vector<T>& lhs, const FixedSizeVector<T>& rhs) {
299 return rhs == lhs;
300}
301
309template <typename T>
310void swap(FixedSizeVector<T>& lhs, FixedSizeVector<T>& rhs) noexcept {
311 lhs.swap(rhs);
312}
313
314// NOLINTEND(cppcoreguidelines-avoid-c-arrays)
315} // namespace cda_rail
316
317#endif // CDA_RAIL_FIXEDSIZEVECTOR_HPP
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