Refureku v2.2.0
C++17 runtime reflection library.
Vector.h
1
8#pragma once
9
10#include <cassert>
11#include <memory> //std::allocator_traits
12
13#include "Refureku/Containers/Allocator.h"
14
15namespace rfk
16{
17 template <typename T, typename Allocator = rfk::Allocator<T>>
18 class Vector
19 {
20 //Friendship for move constructor from Vector holding another type of data
21 template <typename U, typename UAllocator>
22 friend class Vector;
23
24 private:
25 using AllocTraits = std::allocator_traits<Allocator>;
26
28 static constexpr float const _growthFactor = 2.0f;
29
31 T* _data;
32
34 std::size_t _size;
35
37 std::size_t _capacity;
38
40 Allocator _allocator;
41
48 void constructElements(T* from,
49 std::size_t count);
50
58 void copyElements(T const* from,
59 T* to,
60 std::size_t count);
61
70 void copyElementsReverse(T const* from,
71 T* to,
72 std::size_t count);
73
81 void moveElements(T* from,
82 T* to,
83 std::size_t count);
84
93 void moveElementsReverse(T* from,
94 T* to,
95 std::size_t count);
96
103 void destroyElements(T* from,
104 std::size_t count);
105
109 void checkedDelete();
110
117 void reallocateIfNecessary(std::size_t minCapacity);
118
127 void makeFreeSpaceForXElements(std::size_t index,
128 std::size_t count);
129
135 std::size_t computeNewCapacity(std::size_t minCapacity) const noexcept;
136
137 public:
138 using value_type = T;
140 using reference = value_type&;
141 using const_reference = value_type const&;
142
143 Vector(std::size_t initialCapacity = 0u) noexcept;
144
146 template <typename U, typename UAlloc>
148
149 Vector(Vector const&);
150 Vector(Vector&&) noexcept;
151 ~Vector();
152
159 T& front() noexcept;
160 T const& front() const noexcept;
161
168 T& back() noexcept;
169 T const& back() const noexcept;
170
176 T* data() noexcept;
177 T const* data() const noexcept;
178
185 void reserve(std::size_t capacity);
186
194 void resize(std::size_t size);
195
201 std::size_t size() const noexcept;
202
208 std::size_t capacity() const noexcept;
209
215 bool empty() const noexcept;
216
220 void clear();
221
227 void push_back(T const& value);
228
234 void push_back(T&& value);
235
241 void push_back(Vector const& other);
242 void push_back(Vector&& other);
243
250 void insert(std::size_t index,
251 T const& element);
252
259 void insert(std::size_t index,
260 T&& element);
261
269 template <typename... Args>
270 T& emplace_back(Args&&... args);
271
278 T* begin() noexcept;
279 T const* begin() const noexcept;
280 T const* cbegin() const noexcept;
281
287 T* end() noexcept;
288 T const* end() const noexcept;
289 T const* cend() const noexcept;
290
298 T& operator[](std::size_t index) noexcept;
299 T const& operator[](std::size_t index) const noexcept;
300
301 Vector& operator=(Vector const&);
302 Vector& operator=(Vector&&) noexcept;
303 };
304
305 #include "Refureku/Containers/Vector.inl"
306}
Definition: Allocator.h:19
Definition: Vector.h:19
T * end() noexcept
Get a pointer past the last element.
void reserve(std::size_t capacity)
Reallocate the underlying memory to have enough space to fit capacity elements. No reallocation happe...
void insert(std::size_t index, T const &element)
Insert an element at a specified index.
std::size_t size() const noexcept
Get the number of elements stored in the vector.
void resize(std::size_t size)
Resize the vector so that it has exactly the specified size. Reallocation occurs if the size is great...
T & front() noexcept
Get a reference to the first element of the vector. The behaviour is undefined if the vector is empty...
T & back() noexcept
Get a reference to the last element of the vector. The behaviour is undefined if the vector is empty.
void clear()
Remove all elements from the vector.
Vector(Vector< U, UAlloc > &&) noexcept
bool empty() const noexcept
Check if the container contains no elements.
std::size_t capacity() const noexcept
Get the maximum number of elements storable in the vector without reallocation.
T * begin() noexcept
Get a pointer to the first element. If the vector is empty, the pointed memory is undefined.
void push_back(T const &value)
Add an element to the vector.
T * data() noexcept
Get a pointer to the underlying allocated memory.
T & emplace_back(Args &&... args)
Construct in place an element at the end of the vector with the provided arguments.
Definition: Allocator.h:16