libbmb
Modern implementation of STL
Loading...
Searching...
No Matches
bmb::Vector< T, Allocator > Class Template Reference

AllocatorAware dynamic array structure. More...

#include <vector.h>

Public Types

using value_type = T
 
using reference = value_type &
 
using const_reference = const value_type &
 
using pointer = value_type *
 
using const_pointer = const value_type *
 
using size_type = size_t
 
using allocator_type = Allocator
 
using iterator = detail::BaseVectorIter< T, false >
 
using const_iterator = detail::BaseVectorIter< T, true >
 

Public Member Functions

 Vector () noexcept(noexcept(Allocator()))
 
 Vector (const Allocator &alloc)
 
 Vector (size_t count, const value_type &value=value_type(), const Allocator &alloc=Allocator())
 Constructs count elements with given value.
 
template<InputIterator Iter>
 Vector (Iter first, Iter last, const Allocator &alloc=Allocator())
 Initialize Vector with the given range.
 
 Vector (std::initializer_list< value_type > init_list, const Allocator &alloc=Allocator())
 Initialize Vector with the given initializer_list.
 
 Vector (const Vector &other)
 Copy constructor.
 
 Vector (Vector &&other) noexcept(noexcept(Allocator(move(other.alloc_))))
 
Vectoroperator= (Vector other)
 Copy and Move assignment operator.
 
 ~Vector ()
 
bool isEmpty () const noexcept
 Checks whether Vector has elements.
 
void reserve (size_t new_capacity)
 Preallocates 'new_capacity' memory.
 
void pushBack (value_type value)
 Wrapper for emplaceBack. See docs for emplaceBack.
 
template<typename... Args>
reference emplaceBack (Args &&... args)
 Constructs new element from given args.
 
void popBack () noexcept
 Destroyes the last object in the array.
 
void boundPopBack ()
 Destroyes the last object in the array.
 
void clear () noexcept
 Empties the Vector.
 
size_t size () const noexcept
 
size_t capacity () const noexcept
 
pointer getRawData () noexcept
 
const_pointer getRawData () const noexcept
 
reference operator[] (size_t n) noexcept
 
const_reference operator[] (size_t n) const noexcept
 
reference at (size_t n)
 
const_reference at (size_t n) const
 
reference front () noexcept
 
const_reference front () const noexcept
 
reference back () noexcept
 
const_reference back () const noexcept
 
iterator begin () noexcept
 
iterator end () noexcept
 
const_iterator begin () const noexcept
 
const_iterator end () const noexcept
 
const_iterator cbegin () const noexcept
 
const_iterator cend () const noexcept
 
bool operator== (const Vector &other) const
 
auto operator<=> (const Vector &other) const
 

Friends

void swap (Vector &a, Vector &b)
 Efficiently swaps data of the Vectors.
 

Detailed Description

template<typename T, typename Allocator = PrimitiveAllocator>
requires is_same_v<T, remove_const_t<T>>
class bmb::Vector< T, Allocator >

AllocatorAware dynamic array structure.

All methods provides strong/conditional exception safety.

Notes: Memory incrementation coefficient is hardcoded and equals 2. Probably, clients will be able to change it dynamically in future.

Current state notes: no support of insert in arbitrary place of the array. User provided type must not delete move constructor.

Member Typedef Documentation

◆ allocator_type

template<typename T , typename Allocator = PrimitiveAllocator>
using bmb::Vector< T, Allocator >::allocator_type = Allocator

◆ const_iterator

template<typename T , typename Allocator = PrimitiveAllocator>
using bmb::Vector< T, Allocator >::const_iterator = detail::BaseVectorIter<T, true>

◆ const_pointer

template<typename T , typename Allocator = PrimitiveAllocator>
using bmb::Vector< T, Allocator >::const_pointer = const value_type*

◆ const_reference

template<typename T , typename Allocator = PrimitiveAllocator>
using bmb::Vector< T, Allocator >::const_reference = const value_type&

◆ iterator

template<typename T , typename Allocator = PrimitiveAllocator>
using bmb::Vector< T, Allocator >::iterator = detail::BaseVectorIter<T, false>

◆ pointer

template<typename T , typename Allocator = PrimitiveAllocator>
using bmb::Vector< T, Allocator >::pointer = value_type*

◆ reference

template<typename T , typename Allocator = PrimitiveAllocator>
using bmb::Vector< T, Allocator >::reference = value_type&

◆ size_type

template<typename T , typename Allocator = PrimitiveAllocator>
using bmb::Vector< T, Allocator >::size_type = size_t

◆ value_type

template<typename T , typename Allocator = PrimitiveAllocator>
using bmb::Vector< T, Allocator >::value_type = T

Constructor & Destructor Documentation

◆ Vector() [1/7]

template<typename T , typename Allocator = PrimitiveAllocator>
bmb::Vector< T, Allocator >::Vector ( )
inlinenoexcept

◆ Vector() [2/7]

template<typename T , typename Allocator = PrimitiveAllocator>
bmb::Vector< T, Allocator >::Vector ( const Allocator alloc)
inlineexplicit

◆ Vector() [3/7]

template<typename T , typename Allocator = PrimitiveAllocator>
bmb::Vector< T, Allocator >::Vector ( size_t  count,
const value_type value = value_type(),
const Allocator alloc = Allocator() 
)
inlineexplicit

Constructs count elements with given value.

Allocates memory for exactly count elements. Then copies given value into count elements.

Parameters
countNumber of elements to construct
valueValue to initialize the array. Has default value
allocContainer's allocator. Has default value
Exceptions
Providesstrong exception guarantee. If exception is thrown during coping the value - all constructed elements are destroyed, allocated memory is deallocated and the exception is rethrown.

◆ Vector() [4/7]

template<typename T , typename Allocator = PrimitiveAllocator>
template<InputIterator Iter>
bmb::Vector< T, Allocator >::Vector ( Iter  first,
Iter  last,
const Allocator alloc = Allocator() 
)
inline

Initialize Vector with the given range.

Constructs Vector with elements equal to the ones in [first, last).

Parameters
firstStart of the range
lastEnd of the range
allocContainer's allocator. Has default value
Exceptions
Providesstrong exception guarantee. If exception is thrown during coping the range's value - all constructed elements are destroyed, allocated memory is deallocated and the exception is rethrown.

◆ Vector() [5/7]

template<typename T , typename Allocator = PrimitiveAllocator>
bmb::Vector< T, Allocator >::Vector ( std::initializer_list< value_type init_list,
const Allocator alloc = Allocator() 
)
inline

Initialize Vector with the given initializer_list.

Constructs Vector with elements equal to the ones in the initializer_list.

Parameters
init_listInitializer list to copy
allocContainer's allocator. Has default value
Exceptions
Providesstrong exception guarantee. If exception is thrown during coping the initializer_list's value - all constructed elements are destroyed, allocated memory is deallocated and the exception is rethrown.

◆ Vector() [6/7]

template<typename T , typename Allocator = PrimitiveAllocator>
bmb::Vector< T, Allocator >::Vector ( const Vector< T, Allocator > &  other)
inline

Copy constructor.

Exceptions
Providesstrong exception guarantee. If exception is thrown during coping - all constructed elements are destroyed, allocated memory is deallocated and the exception is rethrown.

◆ Vector() [7/7]

template<typename T , typename Allocator = PrimitiveAllocator>
bmb::Vector< T, Allocator >::Vector ( Vector< T, Allocator > &&  other)
inlinenoexcept

◆ ~Vector()

template<typename T , typename Allocator = PrimitiveAllocator>
bmb::Vector< T, Allocator >::~Vector ( )
inline

Member Function Documentation

◆ at() [1/2]

template<typename T , typename Allocator = PrimitiveAllocator>
reference bmb::Vector< T, Allocator >::at ( size_t  n)
inline

Returns element at n-th index in the array. If index is out of bounds, throws std::out_of_range

◆ at() [2/2]

template<typename T , typename Allocator = PrimitiveAllocator>
const_reference bmb::Vector< T, Allocator >::at ( size_t  n) const
inline

Returns element at n-th index in the array. If index is out of bounds, throws std::out_of_range

◆ back() [1/2]

template<typename T , typename Allocator = PrimitiveAllocator>
const_reference bmb::Vector< T, Allocator >::back ( ) const
inlinenoexcept

Returns last element in the array. If index is out of bounds - undefined behaviour.

◆ back() [2/2]

template<typename T , typename Allocator = PrimitiveAllocator>
reference bmb::Vector< T, Allocator >::back ( )
inlinenoexcept

Returns last element in the array. If index is out of bounds - undefined behaviour.

◆ begin() [1/2]

template<typename T , typename Allocator = PrimitiveAllocator>
const_iterator bmb::Vector< T, Allocator >::begin ( ) const
inlinenoexcept

◆ begin() [2/2]

template<typename T , typename Allocator = PrimitiveAllocator>
iterator bmb::Vector< T, Allocator >::begin ( )
inlinenoexcept

◆ boundPopBack()

template<typename T , typename Allocator = PrimitiveAllocator>
void bmb::Vector< T, Allocator >::boundPopBack ( )
inline

Destroyes the last object in the array.

The end() iterator is invalidated.

Exceptions
Throwsstd::logic_error if array is empty

◆ capacity()

template<typename T , typename Allocator = PrimitiveAllocator>
size_t bmb::Vector< T, Allocator >::capacity ( ) const
inlinenoexcept

Returns current Vector's capacity.

◆ cbegin()

template<typename T , typename Allocator = PrimitiveAllocator>
const_iterator bmb::Vector< T, Allocator >::cbegin ( ) const
inlinenoexcept

◆ cend()

template<typename T , typename Allocator = PrimitiveAllocator>
const_iterator bmb::Vector< T, Allocator >::cend ( ) const
inlinenoexcept

◆ clear()

template<typename T , typename Allocator = PrimitiveAllocator>
void bmb::Vector< T, Allocator >::clear ( )
inlinenoexcept

Empties the Vector.

Destroyes all elements in the array and deallocate storage. All fields are nullified.

Exceptions
noexcept

◆ emplaceBack()

template<typename T , typename Allocator = PrimitiveAllocator>
template<typename... Args>
reference bmb::Vector< T, Allocator >::emplaceBack ( Args &&...  args)
inline

Constructs new element from given args.

If array has memory for one more element - constructs an element at the end of the storage. Only end() iterator is invalidated.

If array is full - reallocates storage with the larger capacity and constructs an element there. All references and iterators are invalidated.

After this operation Vector's size is incremented by 1.

Time complexity: amortized O(1).

Parameters
args...Arguments to perfect forward in the element c-tor
Returns
Reference to the inserted element
Exceptions
Conditionalsafety. Strong safety, but the basic one in case: if template type has move c-tor and it throws during moving. All elements in new storage will be destroyed, thus, initial array will start with 'empty' elements and finish with initial ones.

◆ end() [1/2]

template<typename T , typename Allocator = PrimitiveAllocator>
const_iterator bmb::Vector< T, Allocator >::end ( ) const
inlinenoexcept

◆ end() [2/2]

template<typename T , typename Allocator = PrimitiveAllocator>
iterator bmb::Vector< T, Allocator >::end ( )
inlinenoexcept

◆ front() [1/2]

template<typename T , typename Allocator = PrimitiveAllocator>
const_reference bmb::Vector< T, Allocator >::front ( ) const
inlinenoexcept

Returns first element in the array. If index is out of bounds - undefined behaviour.

◆ front() [2/2]

template<typename T , typename Allocator = PrimitiveAllocator>
reference bmb::Vector< T, Allocator >::front ( )
inlinenoexcept

Returns first element in the array. If index is out of bounds - undefined behaviour.

◆ getRawData() [1/2]

template<typename T , typename Allocator = PrimitiveAllocator>
const_pointer bmb::Vector< T, Allocator >::getRawData ( ) const
inlinenoexcept

Returns pointer to the raw underlying array.

◆ getRawData() [2/2]

template<typename T , typename Allocator = PrimitiveAllocator>
pointer bmb::Vector< T, Allocator >::getRawData ( )
inlinenoexcept

Returns pointer to the raw underlying array.

◆ isEmpty()

template<typename T , typename Allocator = PrimitiveAllocator>
bool bmb::Vector< T, Allocator >::isEmpty ( ) const
inlinenoexcept

Checks whether Vector has elements.

Returns
true - if Vector's size == 0. false otherwise.

◆ operator<=>()

template<typename T , typename Allocator = PrimitiveAllocator>
auto bmb::Vector< T, Allocator >::operator<=> ( const Vector< T, Allocator > &  other) const
inline

◆ operator=()

template<typename T , typename Allocator = PrimitiveAllocator>
Vector & bmb::Vector< T, Allocator >::operator= ( Vector< T, Allocator other)
inline

Copy and Move assignment operator.

Implemented through copy/move-and-swap idiom.

Exceptions
Strongsafety when allocators swap throws. Otherwise noexcept.

◆ operator==()

template<typename T , typename Allocator = PrimitiveAllocator>
bool bmb::Vector< T, Allocator >::operator== ( const Vector< T, Allocator > &  other) const
inline

◆ operator[]() [1/2]

template<typename T , typename Allocator = PrimitiveAllocator>
const_reference bmb::Vector< T, Allocator >::operator[] ( size_t  n) const
inlinenoexcept

Returns element at n-th index in the array. If index is out of bounds - undefined behaviour.

◆ operator[]() [2/2]

template<typename T , typename Allocator = PrimitiveAllocator>
reference bmb::Vector< T, Allocator >::operator[] ( size_t  n)
inlinenoexcept

Returns element at n-th index in the array. If index is out of bounds - undefined behaviour.

◆ popBack()

template<typename T , typename Allocator = PrimitiveAllocator>
void bmb::Vector< T, Allocator >::popBack ( )
inlinenoexcept

Destroyes the last object in the array.

The end() iterator is invalidated. If array is empty - undefined behaviour.

Exceptions
noexcept

◆ pushBack()

template<typename T , typename Allocator = PrimitiveAllocator>
void bmb::Vector< T, Allocator >::pushBack ( value_type  value)
inline

Wrapper for emplaceBack. See docs for emplaceBack.

◆ reserve()

template<typename T , typename Allocator = PrimitiveAllocator>
void bmb::Vector< T, Allocator >::reserve ( size_t  new_capacity)
inline

Preallocates 'new_capacity' memory.

If new_capacity > the actual one - allocates new memory, moves elements into it, deallocates previous storage. All references and iterators are invalidated.

If new capacity <= the actual one, the function has no effect.

Parameters
new_capacityNumber of elements to preallocate memory for
Exceptions
Conditionalsafety. Strong safety, but the basic one in case: if template type has a move c-tor and it throws during moving. All elements in new storage will be destroyed, thus, initial array will start with 'empty' elements and finish with initial ones.

◆ size()

template<typename T , typename Allocator = PrimitiveAllocator>
size_t bmb::Vector< T, Allocator >::size ( ) const
inlinenoexcept

Returns current number of elements in the Vector.

Friends And Related Symbol Documentation

◆ swap

template<typename T , typename Allocator = PrimitiveAllocator>
void swap ( Vector< T, Allocator > &  a,
Vector< T, Allocator > &  b 
)
friend

Efficiently swaps data of the Vectors.

All iterators and references remain valid.

Parameters
aVector to swap
bVector to swap
Exceptions
Strongsafety when allocators swap throws. Otherwise noexcept.

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