Flow
Documentation for the Flow C++ Library
Loading...
Searching...
No Matches
flow::Vector< T, Allocator, Strategy > Class Template Reference

#include <flow_vector.h>

Public Types

using value_type = T
using reference = T&
using const_reference = const T&
using pointer = T*
using const_pointer = const T*
using iterator = T*
using const_iterator = const T*
using size_type = std::size_t
using difference_type = std::ptrdiff_t
using allocator_type = Allocator

Public Member Functions

constexpr Vector () noexcept
 Constructs an empty vector with default allocator and growth strategy.
constexpr Vector (const allocator_type &allocator) noexcept(noexcept(allocator_type(allocator)))
 Constructs an empty vector with a specific allocator.
constexpr Vector (const Vector &rhs)
 Copy-constructs a vector using the same allocator as the source.
constexpr Vector (const Vector &rhs, const allocator_type &allocator)
 Copy-constructs a vector with a custom allocator.
constexpr Vector (Vector &&rhs) noexcept
 Move-constructs a vector with the same allocator as the source.
constexpr Vector (Vector &&rhs, const allocator_type &allocator)
 Move-constructs a vector with a custom allocator.
template<std::input_iterator It>
constexpr Vector (It first, It last, const allocator_type &allocator={})
 Constructs a vector from an iterator range.
constexpr Vector (std::initializer_list< T > list, const allocator_type &allocator={})
 Constructs a vector from an initializer list.
constexpr Vector (std::size_t count, const allocator_type &allocator={})
 Constructs a vector with count default-initialized elements.
constexpr Vector (std::size_t count, const T &value, const allocator_type &allocator={})
 Constructs a vector with count copies of a given value.
 ~Vector ()
Vectoroperator= (Vector rhs) noexcept(std::is_nothrow_swappable_v< Vector >)
T & operator[] (std::size_t i) noexcept
const T & operator[] (std::size_t i) const noexcept
allocator_type get_allocator () const noexcept
 Returns the current allocator.
iterator begin () noexcept
constexpr const_iterator begin () const noexcept
iterator end () noexcept
constexpr const_iterator end () const noexcept
constexpr std::size_t size () const noexcept
 Returns the number of elements in the vector.
constexpr std::size_t capacity () const noexcept
 Returns the total allocated capacity.
constexpr bool empty () const noexcept
 Checks whether the vector is empty.
T & front () noexcept
 Returns a reference to the first element.
constexpr const T & front () const noexcept
 Returns a reference to the first element.
T & back () noexcept
 Returns a reference to the last element.
constexpr const T & back () const noexcept
 Returns a reference to the last element.
void clear () noexcept
 Clears the contents of the vector.
void reserve (std::size_t capacity)
 Ensures the vector can hold at least the given capacity without reallocating.
void resize (std::size_t size)
 Resizes the vector to contain the given number of default constructed elements.
void resize (std::size_t size, const T &value)
 Resizes the vector and fills new elements with a given value.
void pushBack (const T &value)
 Adds an element to the end of the vector.
void pushBack (T &&value)
 Adds an element to the end of the vector.
template<typename ... Args>
void emplaceBack (Args &&... args)
 Constructs an element in-place at the end of the vector.
void popBack () noexcept
 Removes the last element of the vector.
iterator erase (iterator pos) noexcept
 Removes an element at the specified iterator position.
iterator erase (iterator first, iterator last) noexcept
 Removes elements in the range [first, last).
template<typename ... Args>
void emplace (iterator pos, Args &&... args)
 Constructs an element in-place at the specified iterator position.
void insert (iterator pos, const T &value)
 Inserts a copy of an element at the specified iterator position.
void insert (iterator pos, T &&value)
 Inserts a moved element at the specified iterator position.
void insert (iterator pos, std::size_t count, const T &value)
 Inserts multiple copies of a value at the specified position.
template<std::input_iterator It>
void insert (iterator pos, It first, It last)
 Inserts a range of elements at the specified position. Does not support self-insertion.
void insert (iterator pos, std::initializer_list< T > list)
 Inserts an initializer list of elements at the specified position.

Private Types

using allocator_trait = std::allocator_traits<Allocator>

Private Member Functions

void updateBuffer (T *buffer, std::size_t capacity)
void relocateBuffer (std::size_t capacity)
void relocateBufferWithHoles (std::size_t capacity, iterator pos, std::size_t holeSize)
template<typename ... U>
void resizeImp (std::size_t size, const U &... optionalValue)

Private Attributes

allocator_type allocator_
Strategy growthStrategy_
std::size_t size_
std::size_t capacity_
T * buffer_

Friends

void swap (Vector &lhs, Vector &rhs) noexcept

Detailed Description

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
class flow::Vector< T, Allocator, Strategy >

Definition at line 49 of file flow_vector.h.

Member Typedef Documentation

◆ allocator_trait

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
using flow::Vector< T, Allocator, Strategy >::allocator_trait = std::allocator_traits<Allocator>
private

Definition at line 65 of file flow_vector.h.

◆ allocator_type

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
using flow::Vector< T, Allocator, Strategy >::allocator_type = Allocator

Definition at line 61 of file flow_vector.h.

◆ const_iterator

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
using flow::Vector< T, Allocator, Strategy >::const_iterator = const T*

Definition at line 57 of file flow_vector.h.

◆ const_pointer

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
using flow::Vector< T, Allocator, Strategy >::const_pointer = const T*

Definition at line 55 of file flow_vector.h.

◆ const_reference

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
using flow::Vector< T, Allocator, Strategy >::const_reference = const T&

Definition at line 53 of file flow_vector.h.

◆ difference_type

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
using flow::Vector< T, Allocator, Strategy >::difference_type = std::ptrdiff_t

Definition at line 59 of file flow_vector.h.

◆ iterator

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
using flow::Vector< T, Allocator, Strategy >::iterator = T*

Definition at line 56 of file flow_vector.h.

◆ pointer

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
using flow::Vector< T, Allocator, Strategy >::pointer = T*

Definition at line 54 of file flow_vector.h.

◆ reference

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
using flow::Vector< T, Allocator, Strategy >::reference = T&

Definition at line 52 of file flow_vector.h.

◆ size_type

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
using flow::Vector< T, Allocator, Strategy >::size_type = std::size_t

Definition at line 58 of file flow_vector.h.

◆ value_type

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
using flow::Vector< T, Allocator, Strategy >::value_type = T

Definition at line 51 of file flow_vector.h.

Constructor & Destructor Documentation

◆ Vector() [1/10]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
flow::Vector< T, Allocator, Strategy >::Vector ( )
inlineconstexprnoexcept

Constructs an empty vector with default allocator and growth strategy.

Definition at line 75 of file flow_vector.h.

77 }
Allocator allocator_type
Definition flow_vector.h:61
constexpr Vector() noexcept
Constructs an empty vector with default allocator and growth strategy.
Definition flow_vector.h:75

◆ Vector() [2/10]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
flow::Vector< T, Allocator, Strategy >::Vector ( const allocator_type & allocator)
inlineexplicitconstexprnoexcept

Constructs an empty vector with a specific allocator.

Parameters
allocatorMemory allocator.

Definition at line 81 of file flow_vector.h.

85 size_(0),
86 capacity_(0),
87 buffer_(nullptr) {
88 }
Strategy growthStrategy_
Definition flow_vector.h:68
std::size_t size_
Definition flow_vector.h:69
std::size_t capacity_
Definition flow_vector.h:70
allocator_type allocator_
Definition flow_vector.h:67

◆ Vector() [3/10]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
flow::Vector< T, Allocator, Strategy >::Vector ( const Vector< T, Allocator, Strategy > & rhs)
inlineconstexpr

Copy-constructs a vector using the same allocator as the source.

Parameters
rhsSource vector.

Definition at line 92 of file flow_vector.h.

94 }
allocator_type get_allocator() const noexcept
Returns the current allocator.

◆ Vector() [4/10]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
flow::Vector< T, Allocator, Strategy >::Vector ( const Vector< T, Allocator, Strategy > & rhs,
const allocator_type & allocator )
inlineconstexpr

Copy-constructs a vector with a custom allocator.

Parameters
rhsSource vector.
allocatorMemory allocator.

Definition at line 99 of file flow_vector.h.

102 size_(rhs.size_),
106 }
iterator begin() noexcept
iterator end() noexcept
OutputIt uninitializedForward(AllocatorType &allocator, InputIt first, InputIt last, OutputIt dest)
Forward elements from a source range to uninitialized memory.

◆ Vector() [5/10]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
flow::Vector< T, Allocator, Strategy >::Vector ( Vector< T, Allocator, Strategy > && rhs)
inlineconstexprnoexcept

Move-constructs a vector with the same allocator as the source.

Parameters
rhsSource vector.

Definition at line 110 of file flow_vector.h.

112 }

◆ Vector() [6/10]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
flow::Vector< T, Allocator, Strategy >::Vector ( Vector< T, Allocator, Strategy > && rhs,
const allocator_type & allocator )
inlineconstexpr

Move-constructs a vector with a custom allocator.

Parameters
rhsSource vector.
allocatorMemory allocator.

Definition at line 117 of file flow_vector.h.

118 : Vector(allocator) {
119
120 // If allocator is different, then do not use the provided allocator
121 // to manage the memory of an existed vector.
122 if (rhs.get_allocator() == allocator) {
123 swap(*this, rhs);
124 } else {
125 // Create a copy with new allocator, then swap over.
127 swap(*this, rhsCopy);
128 }
129 }
friend void swap(Vector &lhs, Vector &rhs) noexcept

◆ Vector() [7/10]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
template<std::input_iterator It>
flow::Vector< T, Allocator, Strategy >::Vector ( It first,
It last,
const allocator_type & allocator = {} )
inlineexplicitconstexpr

Constructs a vector from an iterator range.

Template Parameters
ItInput iterator.
Parameters
firstStart of the range.
lastEnd of the range.
allocatorMemory allocator.

Definition at line 137 of file flow_vector.h.

137 {})
144 }
std::allocator_traits< Allocator > allocator_trait
Definition flow_vector.h:65

◆ Vector() [8/10]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
flow::Vector< T, Allocator, Strategy >::Vector ( std::initializer_list< T > list,
const allocator_type & allocator = {} )
inlineconstexpr

Constructs a vector from an initializer list.

Parameters
listElements to initialize the vector with.
allocatorMemory allocator.

Definition at line 149 of file flow_vector.h.

149 {})
150 : Vector(list.begin(), list.end(), allocator) {
151 }

◆ Vector() [9/10]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
flow::Vector< T, Allocator, Strategy >::Vector ( std::size_t count,
const allocator_type & allocator = {} )
inlineexplicitconstexpr

Constructs a vector with count default-initialized elements.

Parameters
countNumber of elements.
allocatorMemory allocator.

Definition at line 156 of file flow_vector.h.

156 {})
159 size_(count),
163 }
OutputIt uninitializedEmplaceN(AllocatorType &allocator, OutputIt first, std::size_t count, const Args &... args)
Constructs a specified number of objects in uninitialized memory by copying constructor arguments....

◆ Vector() [10/10]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
flow::Vector< T, Allocator, Strategy >::Vector ( std::size_t count,
const T & value,
const allocator_type & allocator = {} )
inlineexplicitconstexpr

Constructs a vector with count copies of a given value.

Parameters
countNumber of elements.
valueElement value.
allocatorMemory allocator.

Definition at line 169 of file flow_vector.h.

169 {})
172 size_(count),
176 }
OutputIt uninitializedFillN(AllocatorType &allocator, OutputIt first, std::size_t count, const T &value)
Fills count elements in uninitialized memory with a value.

◆ ~Vector()

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
flow::Vector< T, Allocator, Strategy >::~Vector ( )
inline

Definition at line 179 of file flow_vector.h.

179 {
181 }
void deleteBuffer(AllocatorType &allocator, T *buffer, std::size_t size, std::size_t capacity) noexcept
Destroys and deallocates the buffer.

Member Function Documentation

◆ back() [1/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
const T & flow::Vector< T, Allocator, Strategy >::back ( ) const
inlineconstexprnoexcept

Returns a reference to the last element.

Returns
Last element.

Definition at line 264 of file flow_vector.h.

264 {
265 assert(0 < size_ && "access empty Vector back");
266 return buffer_[size_ - 1];
267 }

◆ back() [2/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
T & flow::Vector< T, Allocator, Strategy >::back ( )
inlinenoexcept

Returns a reference to the last element.

Returns
Last element.

Definition at line 257 of file flow_vector.h.

257 {
258 assert(0 < size_ && "access empty Vector back");
259 return buffer_[size_ - 1];
260 }

Referenced by flow::Vector< T, Allocator >::emplace().

◆ begin() [1/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
const_iterator flow::Vector< T, Allocator, Strategy >::begin ( ) const
inlineconstexprnoexcept

Definition at line 211 of file flow_vector.h.

211 {
212 return buffer_;
213 }

◆ begin() [2/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
iterator flow::Vector< T, Allocator, Strategy >::begin ( )
inlinenoexcept

◆ capacity()

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
std::size_t flow::Vector< T, Allocator, Strategy >::capacity ( ) const
inlineconstexprnoexcept

Returns the total allocated capacity.

Returns
Capacity in elements.

Definition at line 231 of file flow_vector.h.

231 {
232 return capacity_;
233 }

◆ clear()

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
void flow::Vector< T, Allocator, Strategy >::clear ( )
inlinenoexcept

Clears the contents of the vector.

Definition at line 270 of file flow_vector.h.

270 {
272 size_ = 0;
273 }
void destroyElementsN(AllocatorType &allocator, InputIt first, std::size_t count) noexcept
Destroys count objects in a range.

◆ emplace()

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
template<typename ... Args>
void flow::Vector< T, Allocator, Strategy >::emplace ( iterator pos,
Args &&... args )
inline

Constructs an element in-place at the specified iterator position.

Parameters
posInsertion position.
...argsArguments to forward.

Definition at line 357 of file flow_vector.h.

357 {
358 // Special case: append at the end.
359 if (pos == end()) {
361 return;
362 }
363
364 if (size_ < capacity_) {
365 // Enough capacity, right shift the elements.
366 // Major optimization to use memcpy, copy_backward, or range move_backward instead of handroll loop. A 70% reduction in computation time.
368 std::move_backward(pos, end() - 1, end());
370
371 } else {
372 // Not enough capacity, relocate all to a new buffer.
376 }
377 ++size_;
378 }
void emplaceBack(Args &&... args)
Constructs an element in-place at the end of the vector.
void relocateBufferWithHoles(std::size_t capacity, iterator pos, std::size_t holeSize)
T & back() noexcept
Returns a reference to the last element.

Referenced by flow::Vector< T, Allocator >::insert(), and flow::Vector< T, Allocator >::insert().

◆ emplaceBack()

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
template<typename ... Args>
void flow::Vector< T, Allocator, Strategy >::emplaceBack ( Args &&... args)
inline

Constructs an element in-place at the end of the vector.

Parameters
...argsArguments to forward.

Definition at line 314 of file flow_vector.h.

314 {
315 if (size_ == capacity_) {
317 }
319 ++size_;
320 }
void relocateBuffer(std::size_t capacity)

Referenced by flow::Vector< T, Allocator >::emplace(), flow::Vector< T, Allocator >::pushBack(), and flow::Vector< T, Allocator >::pushBack().

◆ empty()

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
bool flow::Vector< T, Allocator, Strategy >::empty ( ) const
inlineconstexprnoexcept

Checks whether the vector is empty.

Returns
true if the vector is empty, false otherwise.

Definition at line 237 of file flow_vector.h.

237 {
238 return size_ == 0;
239 }

◆ end() [1/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
const_iterator flow::Vector< T, Allocator, Strategy >::end ( ) const
inlineconstexprnoexcept

Definition at line 219 of file flow_vector.h.

219 {
220 return buffer_ + size_;
221 }

◆ end() [2/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
iterator flow::Vector< T, Allocator, Strategy >::end ( )
inlinenoexcept

◆ erase() [1/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
iterator flow::Vector< T, Allocator, Strategy >::erase ( iterator first,
iterator last )
inlinenoexcept

Removes elements in the range [first, last).

Parameters
firstStart of range.
lastEnd of range.
Returns
Iterator to the next element.

Definition at line 343 of file flow_vector.h.

343 {
344 assert(begin() <= first && last <= end() && "can not erase before begin() or past the end()");
345 assert(first <= last && "first must be before last");
346
349 size_ -= last - first;
350 return first;
351 }
void destroyElements(AllocatorType &allocator, InputIt first, InputIt last) noexcept
Destroys a range of constructed objects in memory.

◆ erase() [2/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
iterator flow::Vector< T, Allocator, Strategy >::erase ( iterator pos)
inlinenoexcept

Removes an element at the specified iterator position.

Parameters
posIterator to the element to remove.
Returns
Iterator to the next element.

Definition at line 332 of file flow_vector.h.

332 {
333 assert(begin() <= pos && pos < end() && "can not erase before begin() or at the end()");
334 std::move(pos + 1, end(), pos);
335 popBack();
336 return pos;
337 }
void popBack() noexcept
Removes the last element of the vector.

◆ front() [1/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
const T & flow::Vector< T, Allocator, Strategy >::front ( ) const
inlineconstexprnoexcept

Returns a reference to the first element.

Returns
First element.

Definition at line 250 of file flow_vector.h.

250 {
251 assert(0 < size_ && "access empty Vector front");
252 return buffer_[0];
253 }

◆ front() [2/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
T & flow::Vector< T, Allocator, Strategy >::front ( )
inlinenoexcept

Returns a reference to the first element.

Returns
First element.

Definition at line 243 of file flow_vector.h.

243 {
244 assert(0 < size_ && "access empty Vector front");
245 return buffer_[0];
246 }

◆ get_allocator()

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
allocator_type flow::Vector< T, Allocator, Strategy >::get_allocator ( ) const
inlinenoexcept

Returns the current allocator.

Returns
The allocator.

Definition at line 202 of file flow_vector.h.

202 {
203 return allocator_;
204 }

◆ insert() [1/5]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
void flow::Vector< T, Allocator, Strategy >::insert ( iterator pos,
const T & value )
inline

Inserts a copy of an element at the specified iterator position.

Parameters
posInsertion position.
valueValue to insert.

Definition at line 383 of file flow_vector.h.

383 {
384 emplace(pos, value);
385 }
void emplace(iterator pos, Args &&... args)
Constructs an element in-place at the specified iterator position.

Referenced by flow::Vector< T, Allocator >::insert(), and flow::Vector< T, Allocator >::insert().

◆ insert() [2/5]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
template<std::input_iterator It>
void flow::Vector< T, Allocator, Strategy >::insert ( iterator pos,
It first,
It last )
inline

Inserts a range of elements at the specified position. Does not support self-insertion.

Template Parameters
ItInput iterator.
Parameters
posInsertion position.
firstStart of range.
lastEnd of range.

Definition at line 409 of file flow_vector.h.

409 {
412
413 if (capacity_ < requiredSize) {
414 // Not enough capacity, relocate to a new buffer.
419 } else {
420 // Enough capacity, shift elements.
421 // Inserted that are inbound = Shifted that are outbound.
426
427 // Shifted elements that are on uninitialized buffer range.
428 // This range can be empty if insert at end().
430
431 // Shifted elements that are on initialized buffer range.
432 // This range can be empty if all shifted elements must move to the uninitialized buffer from the previous step.
434
435 // Inserted elements that are on initialized buffer range starting at pos.
436 // This range can be empty if insert at end().
437 for (std::size_t i = 0; i < conflictedRangeSize; ++i, ++first, ++pos) {
438 *pos = *first;
439 }
440
441 // Inserted elements that are on uninitialized buffer range.
442 // This range can be empty if insertedElementSize <= shiftedElementSize -> conflictedRangeSize = insertedElementSize.
443 // Such that there are enough space in the initialized buffer range.
445 }
446
448 }
OutputIt uninitializedMove(AllocatorType &allocator, InputIt first, InputIt last, OutputIt dest) noexcept
Moves elements from a source range to uninitialized memory.
OutputIt uninitializedForwardN(AllocatorType &allocator, InputIt first, std::size_t count, OutputIt dest)
Forward count elements from a source range to uninitialized memory.

◆ insert() [3/5]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
void flow::Vector< T, Allocator, Strategy >::insert ( iterator pos,
std::initializer_list< T > list )
inline

Inserts an initializer list of elements at the specified position.

Parameters
posInsertion position.
listElements to insert.

Definition at line 453 of file flow_vector.h.

453 {
454 insert(pos, list.begin(), list.end());
455 }
void insert(iterator pos, const T &value)
Inserts a copy of an element at the specified iterator position.

◆ insert() [4/5]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
void flow::Vector< T, Allocator, Strategy >::insert ( iterator pos,
std::size_t count,
const T & value )
inline

Inserts multiple copies of a value at the specified position.

Parameters
posInsertion position.
countNumber of copies.
valueValue to insert.

Definition at line 398 of file flow_vector.h.

◆ insert() [5/5]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
void flow::Vector< T, Allocator, Strategy >::insert ( iterator pos,
T && value )
inline

Inserts a moved element at the specified iterator position.

Parameters
posInsertion position.
valueValue to insert.

Definition at line 390 of file flow_vector.h.

390 {
392 }

◆ operator=()

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
Vector & flow::Vector< T, Allocator, Strategy >::operator= ( Vector< T, Allocator, Strategy > rhs)
inlinenoexcept

Definition at line 184 of file flow_vector.h.

185 {
186 swap(*this, rhs);
187 return *this;
188 }

◆ operator[]() [1/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
const T & flow::Vector< T, Allocator, Strategy >::operator[] ( std::size_t i) const
inlinenoexcept

Definition at line 195 of file flow_vector.h.

195 {
196 assert(i < size_ && "index out of bound");
197 return buffer_[i];
198 }

◆ operator[]() [2/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
T & flow::Vector< T, Allocator, Strategy >::operator[] ( std::size_t i)
inlinenoexcept

Definition at line 190 of file flow_vector.h.

190 {
191 assert(i < size_ && "index out of bound");
192 return buffer_[i];
193 }

◆ popBack()

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
void flow::Vector< T, Allocator, Strategy >::popBack ( )
inlinenoexcept

Removes the last element of the vector.

Definition at line 323 of file flow_vector.h.

323 {
324 assert(0 < size_ && "can not pop back from an empty vector");
325 --size_;
327 }

Referenced by flow::Vector< T, Allocator >::erase().

◆ pushBack() [1/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
void flow::Vector< T, Allocator, Strategy >::pushBack ( const T & value)
inline

Adds an element to the end of the vector.

Parameters
valueElement to add.

Definition at line 298 of file flow_vector.h.

298 {
299 // https://stackoverflow.com/questions/10890653/why-would-i-ever-use-push-back-instead-of-emplace-back
300 // TLDR: Consider emplace back an address when constructing unique_ptr.
301 // pushBack disallows implicit constructor call.
303 }

◆ pushBack() [2/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
void flow::Vector< T, Allocator, Strategy >::pushBack ( T && value)
inline

Adds an element to the end of the vector.

Parameters
valueElement to add.

Definition at line 307 of file flow_vector.h.

307 {
309 }

◆ relocateBuffer()

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
void flow::Vector< T, Allocator, Strategy >::relocateBuffer ( std::size_t capacity)
inlineprivate

Definition at line 467 of file flow_vector.h.

467 {
468 assert(capacity_ < capacity && "new capacity is no larger than the old capacity");
469
473 }
constexpr std::size_t capacity() const noexcept
Returns the total allocated capacity.
void updateBuffer(T *buffer, std::size_t capacity)

Referenced by flow::Vector< T, Allocator >::emplaceBack(), flow::Vector< T, Allocator >::reserve(), and flow::Vector< T, Allocator >::resizeImp().

◆ relocateBufferWithHoles()

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
void flow::Vector< T, Allocator, Strategy >::relocateBufferWithHoles ( std::size_t capacity,
iterator pos,
std::size_t holeSize )
inlineprivate

Definition at line 476 of file flow_vector.h.

476 {
477 assert(capacity_ < capacity && "new capacity is no larger than the old capacity");
478
480 iterator leftHalf = uninitializedMove(allocator_, begin(), pos, buffer); // Move left half.
481 uninitializedMove(allocator_, pos, end(), leftHalf + holeSize); // Move right half.
483 }

Referenced by flow::Vector< T, Allocator >::emplace(), and flow::Vector< T, Allocator >::insert().

◆ reserve()

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
void flow::Vector< T, Allocator, Strategy >::reserve ( std::size_t capacity)
inline

Ensures the vector can hold at least the given capacity without reallocating.

Parameters
capacityDesired minimum capacity.

Definition at line 277 of file flow_vector.h.

277 {
278 if (capacity_ < capacity) {
280 }
281 }

◆ resize() [1/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
void flow::Vector< T, Allocator, Strategy >::resize ( std::size_t size)
inline

Resizes the vector to contain the given number of default constructed elements.

Parameters
sizeNew size.

Definition at line 285 of file flow_vector.h.

285 {
287 }
constexpr std::size_t size() const noexcept
Returns the number of elements in the vector.
void resizeImp(std::size_t size, const U &... optionalValue)

◆ resize() [2/2]

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
void flow::Vector< T, Allocator, Strategy >::resize ( std::size_t size,
const T & value )
inline

Resizes the vector and fills new elements with a given value.

Parameters
sizeNew size.
valueValue to fill.

Definition at line 292 of file flow_vector.h.

292 {
294 }

◆ resizeImp()

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
template<typename ... U>
void flow::Vector< T, Allocator, Strategy >::resizeImp ( std::size_t size,
const U &... optionalValue )
inlineprivate

Definition at line 486 of file flow_vector.h.

486 {
487 static_assert(sizeof...(optionalValue) <= 1, "no fill value or exactly one copy");
488 if (size_ < size) {
489 // Relocate if not enough capacity.
490 if (capacity_ < size) {
492 }
494
495 } else if (size < size_) {
496 // Shrink.
498 }
499 size_ = size;
500 }
OutputIt uninitializedEmplace(AllocatorType &allocator, OutputIt first, OutputIt last, const Args &... args)
Constructs objects in uninitialized memory by copying arguments to their constructor....

Referenced by flow::Vector< T, Allocator >::resize(), and flow::Vector< T, Allocator >::resize().

◆ size()

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
std::size_t flow::Vector< T, Allocator, Strategy >::size ( ) const
inlineconstexprnoexcept

Returns the number of elements in the vector.

Returns
Number of elements.

Definition at line 225 of file flow_vector.h.

225 {
226 return size_;
227 }

◆ updateBuffer()

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
void flow::Vector< T, Allocator, Strategy >::updateBuffer ( T * buffer,
std::size_t capacity )
inlineprivate

◆ swap

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
void swap ( Vector< T, Allocator, Strategy > & lhs,
Vector< T, Allocator, Strategy > & rhs )
friend

Member Data Documentation

◆ allocator_

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
allocator_type flow::Vector< T, Allocator, Strategy >::allocator_
private

Definition at line 67 of file flow_vector.h.

◆ buffer_

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
T* flow::Vector< T, Allocator, Strategy >::buffer_
private

Definition at line 71 of file flow_vector.h.

◆ capacity_

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
std::size_t flow::Vector< T, Allocator, Strategy >::capacity_
private

Definition at line 70 of file flow_vector.h.

◆ growthStrategy_

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
Strategy flow::Vector< T, Allocator, Strategy >::growthStrategy_
private

◆ size_

template<typename T, typename Allocator = PolymorphicAllocator<T>, GrowthStrategy Strategy = VectorGrowthStrategy::GoldenExpand>
std::size_t flow::Vector< T, Allocator, Strategy >::size_
private

Definition at line 69 of file flow_vector.h.


The documentation for this class was generated from the following file: