DSA - Data Structures and Algorithms
Loading...
Searching...
No Matches
dsa::Vector< T > Class Template Reference

Implements Vector class template for dynamic size container. More...

#include <vector.h>

Public Types

using value_type = T
 Alias for data type used in class.
using allocator_type = std::allocator<value_type>
 Alias for memory allocator.
using size_type = std::size_t
 Alias for size type used in class.
using difference_type = std::ptrdiff_t
 Alias for pointer difference type.
using pointer = T*
 Alias for pointer to data type used in class.
using const_pointer = const T*
 Alias for const pointer to data type used in class.
using reference = T&
 Alias for reference to data type used in class.
using const_reference = const T&
 Alias for const reference to data type used in class.
using iterator = T*
 Alias for iterator to data type used in class.
using const_iterator = const T*
 Alias for const iterator to data type used in class.
using reverse_iterator = std::reverse_iterator<iterator>
 Alias for reverse_iterator to data type used in class.
using const_reverse_iterator = std::reverse_iterator<const_iterator>
 Alias for const reverse_iterator to data type used in class.

Public Member Functions

constexpr Vector ()
 Construct a new Vector object.
constexpr Vector (size_type count)
 Construct a new Vector object of size count, using default value of type T.
constexpr Vector (size_type count, const T &value)
 Construct a new Vector object of size count, using provided value of type T.
template<typename InputIt>
requires std::input_iterator<InputIt>
constexpr Vector (InputIt first, InputIt last)
 Construct a new Vector object using elements from range [ first , last )
constexpr Vector (const Vector< T > &other)
 Construct a new Vector object using copy constructor.
constexpr Vector (Vector< T > &&other) noexcept
 Construct a new Vector object using move constructor.
constexpr Vector (std::initializer_list< T > init_list)
 Construct a new Vector object using initializer list.
 ~Vector ()
 Destroy the Vector object.
constexpr auto operator= (const Vector< T > &other) -> Vector< T > &
 Assign Vector object using copy assignment.
constexpr auto operator= (Vector< T > &&other) noexcept -> Vector< T > &
 Assign Vector object using move assignment.
constexpr auto operator= (std::initializer_list< T > init_list) -> Vector< T > &
 Assign Vector object from init_list elements.
constexpr void assign (size_type count, const T &value)
 Function assign count , elements of value to Vector object.
template<typename InputIt>
requires std::input_iterator<InputIt>
constexpr void assign (InputIt first, InputIt last)
 Function assign elements from range [ first , last ) to Vector object.
constexpr void assign (std::initializer_list< T > init_list)
 Function assign values of init_list to Vector object.
constexpr auto get_allocator () const -> allocator_type
 Get the allocator object.
constexpr auto at (size_type pos) -> reference
 Returns a reference to Vector element at pos index. If pos is outside of container range, an exception std::out_of_range is thrown.
constexpr auto at (size_type pos) const -> const_reference
 Returns a const_reference to Vector element at pos index. If pos is outside of container range, an exception std::out_of_range is thrown.
constexpr auto operator[] (size_type pos) -> reference
 Returns a reference to Vector element at pos index. If pos is outside of container range, undefined behaviour occurs.
constexpr auto operator[] (size_type pos) const -> const_reference
 Returns a const_reference to Vector element at pos index. If pos is outside of container range, undefined behaviour occurs.
constexpr auto front () -> reference
 Returns reference to first Arary element.
constexpr auto front () const -> const_reference
 Returns const_reference to first Arary element.
constexpr auto back () -> reference
 Returns reference to last Arary element.
constexpr auto back () const -> const_reference
 Returns const_reference to last Arary element.
constexpr auto data () noexcept -> pointer
 Returns pointer to underlying data container.
constexpr auto data () const noexcept -> const_pointer
 Returns const_pointer to underlying data container.
constexpr auto begin () noexcept -> iterator
 Returns iterator to first element.
constexpr auto begin () const noexcept -> const_iterator
 Returns const_iterator to first element.
constexpr auto cbegin () const noexcept -> const_iterator
 Returns const_iterator to first element.
constexpr auto end () noexcept -> iterator
 Returns iterator past last element of underlaying data structure.
constexpr auto end () const noexcept -> const_iterator
 Returns const_iterator past last element of underlaying data structure.
constexpr auto cend () const noexcept -> const_iterator
 Returns const_iterator past last element of underlaying data structure.
constexpr auto rbegin () -> reverse_iterator
 Returns reverse_iterator to the first element of reversed underlaying data structure.
constexpr auto rbegin () const -> const_reverse_iterator
 Returns const_reverse_iterator to the first element of reversed underlaying data structure.
constexpr auto crbegin () const noexcept -> const_reverse_iterator
 Returns const_reverse_iterator to the first element of reversed underlaying data structure.
constexpr auto rend () -> reverse_iterator
 Returns reverse_iterator past the last element of reversed underlaying data structure.
constexpr auto rend () const -> const_reverse_iterator
 Returns const_reverse_iterator past the last element of reversed underlaying data structure.
constexpr auto crend () const noexcept -> const_reverse_iterator
 Returns const_reverse_iterator past the last element of reversed underlaying data structure.
constexpr auto empty () const -> bool
 Checks if container has elements.
constexpr auto size () const noexcept -> size_type
 Returns number of elements in container.
constexpr auto max_size () const noexcept -> size_type
 Returns maximum number of elements container can hold.
constexpr void reserve (size_type new_cap)
 Increase the capacity of the container.
constexpr auto capacity () -> size_type
 Returns the number of elements allocated for container.
constexpr void shrink_to_fit ()
 Request to remove of unused capacity.
constexpr void clear ()
 Erases all elements of the container Does not affect container capacity.
constexpr auto insert (const_iterator pos, const T &value) -> iterator
 Insert a copy of value before pos.
constexpr auto insert (const_iterator pos, T &&value) -> iterator
 Insert value before pos possibly using move semantics.
constexpr auto insert (const_iterator pos, size_type count, const T &value) -> iterator
 Insert count copies of value before pos.
template<typename InputIt>
requires std::input_iterator<InputIt>
constexpr auto insert (const_iterator pos, InputIt first, InputIt last) -> iterator
 Insert elements from range [ first , last ) befor pos.
constexpr auto insert (const_iterator pos, std::initializer_list< T > init_list) -> iterator
 Insert constent of init_list before pos.
template<typename... Args>
constexpr auto emplace (const_iterator pos, Args &&... args) -> iterator
 Insert new element into the container before pos.
template<typename... Args>
constexpr auto emplace_back (Args &&... args) -> reference
 Appends a copy of value at the end of the container.
constexpr auto erase (iterator pos) -> iterator
 Erases specified element from the container.
constexpr auto erase (const_iterator pos) -> iterator
 Erases specified element from the container.
constexpr auto erase (iterator first, iterator last) -> iterator
 Erases elements in range [ first , last ) from the container.
constexpr auto erase (const_iterator first, const_iterator last) -> iterator
 Erases elements in range [ first , last ) from the container.
constexpr void push_back (const T &value)
 Appends a copy of value at the end of the container.
constexpr void push_back (T &&value)
 Appends a copy of value at the end of the container.
constexpr void pop_back ()
 Removes the last element of the container.
constexpr void resize (size_type count)
 Resizez the container to contain count elements if count is equal to current size, does nothing if current size is greater than count , container size is reduced if current size is less than count , additional copies of T() are appended.
constexpr void resize (size_type count, const value_type &value)
 Resizes the container to contain count elements if count is equal to current size, does nothing if current size is greater than count , container size is reduced if current size is less than count , additional copies of value are appended.
constexpr void swap (Vector< T > &other) noexcept
 Exchanges content of current container with other container.

Detailed Description

template<typename T>
class dsa::Vector< T >

Implements Vector class template for dynamic size container.

Template Parameters
Ttype of data stored in container
Note
This template intentionally does not implement a bit-packed specialization for bool, as found in std::vector<bool>. The standard container's specialization breaks reference semantics and replaces bool& with proxy objects. dsa::Vector<bool> behaves like a regular container, without bit-packing. This design choice prioritizes correctness and predictable semantics over memory optimization.
Todo

add non-member specialized erase function

add non-member specialized erase_if function

Definition at line 41 of file vector.h.

Member Typedef Documentation

◆ allocator_type

template<typename T>
using dsa::Vector< T >::allocator_type = std::allocator<value_type>

Alias for memory allocator.

Definition at line 55 of file vector.h.

◆ const_iterator

template<typename T>
using dsa::Vector< T >::const_iterator = const T*

Alias for const iterator to data type used in class.

Definition at line 107 of file vector.h.

◆ const_pointer

template<typename T>
using dsa::Vector< T >::const_pointer = const T*

Alias for const pointer to data type used in class.

Template Parameters
T*pointer to data type

Definition at line 83 of file vector.h.

◆ const_reference

template<typename T>
using dsa::Vector< T >::const_reference = const T&

Alias for const reference to data type used in class.

Template Parameters
T&const reference to data type

Definition at line 97 of file vector.h.

◆ const_reverse_iterator

template<typename T>
using dsa::Vector< T >::const_reverse_iterator = std::reverse_iterator<const_iterator>

Alias for const reverse_iterator to data type used in class.

Definition at line 117 of file vector.h.

◆ difference_type

template<typename T>
using dsa::Vector< T >::difference_type = std::ptrdiff_t

Alias for pointer difference type.

Used by STL to define distance between two pointers

Definition at line 69 of file vector.h.

◆ iterator

template<typename T>
using dsa::Vector< T >::iterator = T*

Alias for iterator to data type used in class.

Definition at line 102 of file vector.h.

◆ pointer

template<typename T>
using dsa::Vector< T >::pointer = T*

Alias for pointer to data type used in class.

Template Parameters
T*pointer to data type

Definition at line 76 of file vector.h.

◆ reference

template<typename T>
using dsa::Vector< T >::reference = T&

Alias for reference to data type used in class.

Template Parameters
T&reference to data type

Definition at line 90 of file vector.h.

◆ reverse_iterator

template<typename T>
using dsa::Vector< T >::reverse_iterator = std::reverse_iterator<iterator>

Alias for reverse_iterator to data type used in class.

Definition at line 112 of file vector.h.

◆ size_type

template<typename T>
using dsa::Vector< T >::size_type = std::size_t

Alias for size type used in class.

Template Parameters
Tsize type

Definition at line 62 of file vector.h.

◆ value_type

template<typename T>
using dsa::Vector< T >::value_type = T

Alias for data type used in class.

Template Parameters
Tdata type

Definition at line 50 of file vector.h.

Constructor & Destructor Documentation

◆ Vector() [1/6]

template<typename T>
dsa::Vector< T >::Vector ( size_type count)
constexpr

Construct a new Vector object of size count, using default value of type T.

Parameters
[in]countelement count

Definition at line 719 of file vector.h.

720 : Vector(count, T())
721 {
722 }
Implements Vector class template for dynamic size container.
Definition vector.h:42
constexpr Vector()
Construct a new Vector object.

◆ Vector() [2/6]

template<typename T>
dsa::Vector< T >::Vector ( size_type count,
const T & value )
constexpr

Construct a new Vector object of size count, using provided value of type T.

Parameters
[in]countelement count
[in]valuevalue for all elements

Definition at line 725 of file vector.h.

726 {
727 for (size_t i = 0; i < count; i++)
728 {
730 }
731 }
constexpr void push_back(const T &value)
Appends a copy of value at the end of the container.
Definition vector.h:1225

◆ Vector() [3/6]

template<typename T>
requires std::input_iterator<InputIt>
template<typename InputIt>
requires std::input_iterator<InputIt>
dsa::Vector< T >::Vector ( InputIt first,
InputIt last )
constexpr

Construct a new Vector object using elements from range [ first , last )

Template Parameters
InputIt
Parameters
[in]firstelement defining range of elements to insert
[in]lastelement definig range of elements to insert

Definition at line 736 of file vector.h.

737 {
738 // do not use const reference to keep arguments the same as in std
739 // NOLINTNEXTLINE(performance-unnecessary-value-param)
740 assign(first, last);
741 }
constexpr void assign(size_type count, const T &value)
Function assign count , elements of value to Vector object.
Definition vector.h:826

◆ Vector() [4/6]

template<typename T>
dsa::Vector< T >::Vector ( const Vector< T > & other)
constexpr

Construct a new Vector object using copy constructor.

Parameters
[in]otherVector object of type T

Definition at line 744 of file vector.h.

745 {
746 reserve(other.size());
747 for (const auto& item : other)
748 {
750 }
751 }
constexpr auto size() const noexcept -> size_type
Returns number of elements in container.
Definition vector.h:1042
constexpr void reserve(size_type new_cap)
Increase the capacity of the container.
Definition vector.h:1054

◆ Vector() [5/6]

template<typename T>
dsa::Vector< T >::Vector ( Vector< T > && other)
constexprnoexcept

Construct a new Vector object using move constructor.

Content of other object will be taken by constructed object

Parameters
[in,out]otherVector object of type T

Definition at line 754 of file vector.h.

755 {
757 }
constexpr auto operator=(const Vector< T > &other) -> Vector< T > &
Assign Vector object using copy assignment.
Definition vector.h:772

◆ Vector() [6/6]

template<typename T>
dsa::Vector< T >::Vector ( std::initializer_list< T > init_list)
constexpr

Construct a new Vector object using initializer list.

Parameters
[in]init_listinitializer list of values of type T

Definition at line 760 of file vector.h.

761 {
763 }

◆ ~Vector()

template<typename T>
dsa::Vector< T >::~Vector ( )

Destroy the Vector object.

Definition at line 766 of file vector.h.

767 {
768 clear_allocation();
769 }

Member Function Documentation

◆ assign() [1/3]

template<typename T>
requires std::input_iterator<InputIt>
template<typename InputIt>
requires std::input_iterator<InputIt>
void dsa::Vector< T >::assign ( InputIt first,
InputIt last )
constexpr

Function assign elements from range [ first , last ) to Vector object.

Template Parameters
InputIt
Parameters
[in]firstelement defining range of elements to insert
[in]lastelement definig range of elements to insert

Definition at line 844 of file vector.h.

845 {
846 const auto count = static_cast<size_type>(std::distance(first, last));
847
848 clear_allocation();
849 m_capacity = 0;
850 m_size = 0;
851
852 m_data = allocator_type().allocate(count);
853 while (first != last)
854 {
856 first++;
857 }
858 }
std::size_t size_type
Alias for size type used in class.
Definition vector.h:62
std::allocator< value_type > allocator_type
Alias for memory allocator.
Definition vector.h:55

◆ assign() [2/3]

template<typename T>
void dsa::Vector< T >::assign ( size_type count,
const T & value )
constexpr

Function assign count , elements of value to Vector object.

Parameters
[in]countnew size of the container
[in]valuevalue to initialize elements of the container with

Definition at line 826 of file vector.h.

827 {
828 clear_allocation();
829 m_capacity = 0;
830 m_size = 0;
831
832 m_data = allocator_type().allocate(count);
833 for (size_t i = 0; i < count; i++)
834 {
836 }
837 }

◆ assign() [3/3]

template<typename T>
void dsa::Vector< T >::assign ( std::initializer_list< T > init_list)
constexpr

Function assign values of init_list to Vector object.

Parameters
[in]init_listinitializer list of values of type T

Definition at line 861 of file vector.h.

862 {
863 clear_allocation();
864 m_capacity = 0;
865 m_size = 0;
866
867 m_data = allocator_type().allocate(init_list.size());
868 for (auto const& item : init_list)
869 {
871 }
872 }

◆ at() [1/2]

template<typename T>
auto dsa::Vector< T >::at ( size_type pos) -> reference
nodiscardconstexpr

Returns a reference to Vector element at pos index. If pos is outside of container range, an exception std::out_of_range is thrown.

Parameters
[in]posindex of element to return
Returns
reference to Vector element at pos index

Definition at line 881 of file vector.h.

882 {
883 if (pos >= m_size)
884 {
885 throw std::out_of_range("Pos argument outside of container range");
886 }
887
888 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
889 return m_data[pos];
890 }

◆ at() [2/2]

template<typename T>
auto dsa::Vector< T >::at ( size_type pos) const -> const_reference
nodiscardconstexpr

Returns a const_reference to Vector element at pos index. If pos is outside of container range, an exception std::out_of_range is thrown.

Parameters
[in]posindex of element to return
Returns
const_reference to Vector element at pos index

Definition at line 893 of file vector.h.

894 {
895 if (pos >= m_size)
896 {
897 throw std::out_of_range("Pos argument outside of container range");
898 }
899
900 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
901 return m_data[pos];
902 }

◆ back() [1/2]

template<typename T>
auto dsa::Vector< T >::back ( ) -> reference
nodiscardconstexpr

Returns reference to last Arary element.

Returns
reference to last element

Definition at line 933 of file vector.h.

934 {
935 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
936 return m_data[m_size - 1];
937 }

◆ back() [2/2]

template<typename T>
auto dsa::Vector< T >::back ( ) const -> const_reference
nodiscardconstexpr

Returns const_reference to last Arary element.

Returns
const_reference to last element

Definition at line 940 of file vector.h.

941 {
942 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
943 return m_data[m_size - 1];
944 }

◆ begin() [1/2]

template<typename T>
auto dsa::Vector< T >::begin ( ) const -> const_iterator
nodiscardconstexprnoexcept

Returns const_iterator to first element.

Note
If container is empty, returned iterator will be equal to end()
Returns
const_iterator to first element

Definition at line 967 of file vector.h.

968 {
969 return m_data;
970 }

◆ begin() [2/2]

template<typename T>
auto dsa::Vector< T >::begin ( ) -> iterator
nodiscardconstexprnoexcept

Returns iterator to first element.

Note
If container is empty, returned iterator will be equal to end()
Returns
iterator to first element

Definition at line 961 of file vector.h.

962 {
963 return m_data;
964 }

◆ capacity()

template<typename T>
auto dsa::Vector< T >::capacity ( ) -> size_type
constexpr

Returns the number of elements allocated for container.

Returns
size_type number of allocated elements

Definition at line 1064 of file vector.h.

1065 {
1066 return m_capacity;
1067 }

◆ cbegin()

template<typename T>
auto dsa::Vector< T >::cbegin ( ) const -> const_iterator
nodiscardconstexprnoexcept

Returns const_iterator to first element.

Note
If container is empty, returned iterator will be equal to end()
Returns
const_iterator to first element

Definition at line 973 of file vector.h.

974 {
975 return m_data;
976 }

◆ cend()

template<typename T>
auto dsa::Vector< T >::cend ( ) const -> const_iterator
nodiscardconstexprnoexcept

Returns const_iterator past last element of underlaying data structure.

Note
Iterator act as a sentinel, it is not guaranteed to be dereferencable
Returns
const_iterator past last element

Definition at line 993 of file vector.h.

994 {
995 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
996 return m_data + m_size;
997 }

◆ clear()

template<typename T>
void dsa::Vector< T >::clear ( )
constexpr

Erases all elements of the container Does not affect container capacity.

Note
Operation invalidates all pointers and references

Definition at line 1079 of file vector.h.

1080 {
1081 destroy_elements();
1082 m_size = 0;
1083 }

◆ crbegin()

template<typename T>
auto dsa::Vector< T >::crbegin ( ) const -> const_reverse_iterator
nodiscardconstexprnoexcept

Returns const_reverse_iterator to the first element of reversed underlaying data structure.

Note
If container is empty, returned iterator will be equal to end()
Returns
const_reverse_iterator to the first element

Definition at line 1012 of file vector.h.

1013 {
1014 return const_reverse_iterator(end());
1015 }
std::reverse_iterator< const_iterator > const_reverse_iterator
Alias for const reverse_iterator to data type used in class.
Definition vector.h:117
constexpr auto end() noexcept -> iterator
Returns iterator past last element of underlaying data structure.
Definition vector.h:979

◆ crend()

template<typename T>
auto dsa::Vector< T >::crend ( ) const -> const_reverse_iterator
nodiscardconstexprnoexcept

Returns const_reverse_iterator past the last element of reversed underlaying data structure.

Note
Iterator act as a sentinel, it is not guaranteed to be dereferencable
Returns
const_reverse_iterator to the element after the last element

Definition at line 1030 of file vector.h.

1031 {
1032 return const_reverse_iterator(begin());
1033 }
constexpr auto begin() noexcept -> iterator
Returns iterator to first element.
Definition vector.h:961

◆ data() [1/2]

template<typename T>
auto dsa::Vector< T >::data ( ) const -> const_pointer
nodiscardconstexprnoexcept

Returns const_pointer to underlying data container.

Returns
const_pointer to underlaying data container

Definition at line 954 of file vector.h.

955 {
956 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
957 return &m_data[0];
958 }

◆ data() [2/2]

template<typename T>
auto dsa::Vector< T >::data ( ) -> pointer
nodiscardconstexprnoexcept

Returns pointer to underlying data container.

Returns
pointer to underlaying data container

Definition at line 947 of file vector.h.

948 {
949 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
950 return &m_data[0];
951 }

◆ emplace()

template<typename T>
template<typename... Args>
auto dsa::Vector< T >::emplace ( const_iterator pos,
Args &&... args ) -> iterator
constexpr

Insert new element into the container before pos.

Template Parameters
Args
Parameters
[in]positerator before which new element will be inserted
[in]argsarguments to forward to the constructor of the element
Returns
iterator pointing to the emplaced element
Note
if no reallocation occurs, all iterators and references before pos remains valid if reallocation occurs, all iterators and references are invalidated

Definition at line 1152 of file vector.h.

1153 {
1154 iterator new_pos{ insert_make_space_for_new_elems(pos, 1) };
1155
1156 // emplace new element
1158
1159 return new_pos;
1160 }
T * iterator
Alias for iterator to data type used in class.
Definition vector.h:102

◆ emplace_back()

template<typename T>
template<typename... Args>
auto dsa::Vector< T >::emplace_back ( Args &&... args) -> reference
constexpr

Appends a copy of value at the end of the container.

Template Parameters
Args
Parameters
[in]argsarguments to forward to the constructor of the element
Returns
reference to emplaced element

Definition at line 1164 of file vector.h.

1165 {
1166 /*
1167 * If an exception is thrown for any reason, this function has no effect
1168 * strong exception guarantee by:
1169 * - reallocate
1170 * - if construction of new element fails, state of object does not change
1171 */
1172
1173 if (m_size >= m_capacity)
1174 {
1175 reallocate(calc_new_capacity());
1176 }
1178 ++m_size;
1179
1180 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
1181 return m_data[m_size - 1];
1182 }

◆ empty()

template<typename T>
auto dsa::Vector< T >::empty ( ) const -> bool
nodiscardconstexpr

Checks if container has elements.

Returns
true if container is empty
false if container is not empty

Definition at line 1036 of file vector.h.

1037 {
1038 return m_size == 0;
1039 }

◆ end() [1/2]

template<typename T>
auto dsa::Vector< T >::end ( ) const -> const_iterator
nodiscardconstexprnoexcept

Returns const_iterator past last element of underlaying data structure.

Note
Iterator act as a sentinel, it is not guaranteed to be dereferencable
Returns
const_iterator past last element

Definition at line 986 of file vector.h.

987 {
988 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
989 return m_data + m_size;
990 }

◆ end() [2/2]

template<typename T>
auto dsa::Vector< T >::end ( ) -> iterator
nodiscardconstexprnoexcept

Returns iterator past last element of underlaying data structure.

Note
Iterator act as a sentinel, it is not guaranteed to be dereferencable
Returns
iterator past last element

Definition at line 979 of file vector.h.

980 {
981 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
982 return m_data + m_size;
983 }

◆ erase() [1/4]

template<typename T>
auto dsa::Vector< T >::erase ( const_iterator first,
const_iterator last ) -> iterator
constexpr

Erases elements in range [ first , last ) from the container.

Parameters
[in]firstelement defining range of elements to insert
[in]lastelement definig range of elements to insert
Returns
iterator iterator to element after last erased element
Note
iterator first does not need to be dereferencable iterators and references at and after first are invalidated

Definition at line 1203 of file vector.h.

1204 {
1205 const auto offset_last = static_cast<size_type>(last - cbegin());
1206 const auto offset_first = static_cast<size_type>(first - cbegin());
1208
1209 // destroy objects in range [first, last)
1210 while (first != last)
1211 {
1213 ++first;
1214 }
1215
1216 // move remaining objects into empty space
1217 // this moves memory in range [last, end()] to addres pointed by `first`, that was already released
1219
1220 m_size -= count;
1221 return begin() + offset_first;
1222 }
constexpr auto cbegin() const noexcept -> const_iterator
Returns const_iterator to first element.
Definition vector.h:973

◆ erase() [2/4]

template<typename T>
auto dsa::Vector< T >::erase ( const_iterator pos) -> iterator
constexpr

Erases specified element from the container.

Parameters
[in]positerator to the element to remove
Returns
iterator iterator to element after last erased element
Note
iterator pos must be valid and dereferencable iterators and references at and after pos are invalidated

Definition at line 1191 of file vector.h.

1192 {
1193 return erase(pos, pos + 1);
1194 }
constexpr auto erase(iterator pos) -> iterator
Erases specified element from the container.
Definition vector.h:1185

◆ erase() [3/4]

template<typename T>
auto dsa::Vector< T >::erase ( iterator first,
iterator last ) -> iterator
constexpr

Erases elements in range [ first , last ) from the container.

Parameters
[in]firstelement defining range of elements to insert
[in]lastelement definig range of elements to insert
Returns
iterator iterator to element after last erased element
Note
iterator first does not need to be dereferencable iterators and references at and after first are invalidated

Definition at line 1197 of file vector.h.

1198 {
1199 return erase(static_cast<const_iterator>(first), static_cast<const_iterator>(last));
1200 }
const T * const_iterator
Alias for const iterator to data type used in class.
Definition vector.h:107

◆ erase() [4/4]

template<typename T>
auto dsa::Vector< T >::erase ( iterator pos) -> iterator
constexpr

Erases specified element from the container.

Parameters
[in]positerator to the element to remove
Returns
iterator iterator to element after last erased element
Note
iterator pos must be valid and dereferencable iterators and references at and after pos are invalidated

Definition at line 1185 of file vector.h.

1186 {
1187 return erase(pos, pos + 1);
1188 }

◆ front() [1/2]

template<typename T>
auto dsa::Vector< T >::front ( ) -> reference
nodiscardconstexpr

Returns reference to first Arary element.

Returns
reference to first element

Definition at line 919 of file vector.h.

920 {
921 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
922 return m_data[0];
923 }

◆ front() [2/2]

template<typename T>
auto dsa::Vector< T >::front ( ) const -> const_reference
nodiscardconstexpr

Returns const_reference to first Arary element.

Returns
const_reference to first element

Definition at line 926 of file vector.h.

927 {
928 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
929 return m_data[0];
930 }

◆ get_allocator()

template<typename T>
auto dsa::Vector< T >::get_allocator ( ) const -> allocator_type
nodiscardconstexpr

Get the allocator object.

Returns
allocator_type type of memory allocator

Definition at line 875 of file vector.h.

876 {
877 return m_allocator;
878 }

◆ insert() [1/5]

template<typename T>
auto dsa::Vector< T >::insert ( const_iterator pos,
const T & value ) -> iterator
constexpr

Insert a copy of value before pos.

Parameters
[in]positerator before which new element will be inserted
[in]valueelement to insert into container
Returns
iterator pointing to inserted element

Definition at line 1086 of file vector.h.

1087 {
1088 return insert(pos, 1, value);
1089 }
constexpr auto insert(const_iterator pos, const T &value) -> iterator
Insert a copy of value before pos.
Definition vector.h:1086

◆ insert() [2/5]

template<typename T>
requires std::input_iterator<InputIt>
template<typename InputIt>
requires std::input_iterator<InputIt>
auto dsa::Vector< T >::insert ( const_iterator pos,
InputIt first,
InputIt last ) -> iterator
constexpr

Insert elements from range [ first , last ) befor pos.

Template Parameters
InputIt
Parameters
[in]positerator before which new element will be inserted
[in]firstelement defining range of elements to insert
[in]lastelement definig range of elements to insert
Returns
iterator pointing to the first inserted element, or pos if no element was inserted
Note
if no reallocation occurs, all iterators and references before pos remains valid if reallocation occurs, all iterators and references are invalidated

Definition at line 1116 of file vector.h.

1117 {
1118 const auto count = static_cast<size_type>(std::distance(first, last));
1119 iterator new_pos{ insert_make_space_for_new_elems(pos, count) };
1120
1121 // create objects in range [pos, pos + count]
1123 while (first != last)
1124 {
1126 ++new_iter;
1127 ++first;
1128 }
1129
1130 return new_pos;
1131 }

◆ insert() [3/5]

template<typename T>
auto dsa::Vector< T >::insert ( const_iterator pos,
size_type count,
const T & value ) -> iterator
constexpr

Insert count copies of value before pos.

Parameters
[in]positerator before which new element will be inserted
[in]countnumber of copies to insert into container
[in]valueelement to insert into container
Returns
iterator pointing to the first inserted element, or pos if no element was inserted
Note
if no reallocation occurs, all iterators and references before pos remains valid if reallocation occurs, all iterators and references are invalidated

Definition at line 1098 of file vector.h.

1099 {
1100 iterator new_pos{ insert_make_space_for_new_elems(pos, count) };
1101
1102 // create objects in range [pos, pos + count]
1104 for (size_t i = 0; i < count; i++)
1105 {
1107 ++new_iter;
1108 }
1109
1110 return new_pos;
1111 }

◆ insert() [4/5]

template<typename T>
auto dsa::Vector< T >::insert ( const_iterator pos,
std::initializer_list< T > init_list ) -> iterator
constexpr

Insert constent of init_list before pos.

Parameters
[in]positerator before which new element will be inserted
[in]init_listcontainer to insert befor pos
Returns
iterator pointing to the first inserted element, or pos if no element was inserted
Note
if no reallocation occurs, all iterators and references before pos remains valid if reallocation occurs, all iterators and references are invalidated

Definition at line 1134 of file vector.h.

1135 {
1136 const size_type count = init_list.size();
1137 iterator new_pos{ insert_make_space_for_new_elems(pos, count) };
1138
1139 // create objects in range [pos, pos + count]
1141 for (const auto& value : init_list)
1142 {
1144 ++new_iter;
1145 }
1146
1147 return new_pos;
1148 }

◆ insert() [5/5]

template<typename T>
auto dsa::Vector< T >::insert ( const_iterator pos,
T && value ) -> iterator
constexpr

Insert value before pos possibly using move semantics.

Parameters
[in]positerator before which new element will be inserted
[in]valueelement to insert into container
Returns
iterator pointing to inserted element

Definition at line 1092 of file vector.h.

1093 {
1094 return insert(pos, 1, std::move(value));
1095 }

◆ max_size()

template<typename T>
auto dsa::Vector< T >::max_size ( ) const -> size_type
nodiscardconstexprnoexcept

Returns maximum number of elements container can hold.

Returns
size_type maximum number of elements

Definition at line 1048 of file vector.h.

1049 {
1051 }
constexpr auto get_allocator() const -> allocator_type
Get the allocator object.
Definition vector.h:875

◆ operator=() [1/3]

template<typename T>
auto dsa::Vector< T >::operator= ( const Vector< T > & other) -> Vector<T>&
constexpr

Assign Vector object using copy assignment.

Content of other object will be taken by constructed object

Parameters
[in,out]otherVector object of type T
Returns
Vector& reference to constructed Vector of type T

Definition at line 772 of file vector.h.

773 {
774 if (&other != this)
775 {
776 clear_allocation();
777 m_capacity = 0;
778 m_size = 0;
779
780 m_data = allocator_type().allocate(other.size());
781 for (const auto& item : other)
782 {
784 }
785 }
786
787 return *this;
788 }

◆ operator=() [2/3]

template<typename T>
auto dsa::Vector< T >::operator= ( std::initializer_list< T > init_list) -> Vector<T>&
constexpr

Assign Vector object from init_list elements.

Parameters
[in]init_listvalues to replace Vector with
Returns
Vector& reference to constructed Vector of type T

Definition at line 810 of file vector.h.

811 {
812 clear_allocation();
813 m_capacity = 0;
814 m_size = 0;
815
816 m_data = allocator_type().allocate(init_list.size());
817 for (auto const& item : init_list)
818 {
820 }
821
822 return *this;
823 }

◆ operator=() [3/3]

template<typename T>
auto dsa::Vector< T >::operator= ( Vector< T > && other) -> Vector<T>&
constexprnoexcept

Assign Vector object using move assignment.

Content of other object will be taken by constructed object

Parameters
[in,out]otherVector object of type T
Returns
Vector& reference to constructed Vector of type T

Definition at line 791 of file vector.h.

792 {
793 if (&other != this)
794 {
795 clear_allocation();
796
797 m_data = other.m_data;
798 m_capacity = other.m_capacity;
799 m_size = other.m_size;
800
801 other.m_data = nullptr;
802 other.m_capacity = 0;
803 other.m_size = 0;
804 }
805
806 return *this;
807 }

◆ operator[]() [1/2]

template<typename T>
auto dsa::Vector< T >::operator[] ( size_type pos) -> reference
nodiscardconstexpr

Returns a reference to Vector element at pos index. If pos is outside of container range, undefined behaviour occurs.

Parameters
[in]posindex of element to return
Returns
reference to Vector element

Definition at line 905 of file vector.h.

906 {
907 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
908 return m_data[pos];
909 }

◆ operator[]() [2/2]

template<typename T>
auto dsa::Vector< T >::operator[] ( size_type pos) const -> const_reference
nodiscardconstexpr

Returns a const_reference to Vector element at pos index. If pos is outside of container range, undefined behaviour occurs.

Parameters
[in]posindex of element to return
Returns
const_reference to Vector element

Definition at line 912 of file vector.h.

913 {
914 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
915 return m_data[pos];
916 }

◆ pop_back()

template<typename T>
void dsa::Vector< T >::pop_back ( )
constexpr

Removes the last element of the container.

Note
iterators and references to the last element are invalidated

Definition at line 1250 of file vector.h.

1251 {
1252 if (m_size > 0)
1253 {
1255 --m_size;
1256 }
1257 }

◆ push_back() [1/2]

template<typename T>
void dsa::Vector< T >::push_back ( const T & value)
constexpr

Appends a copy of value at the end of the container.

Parameters
[in]valuethe value of the element to append

Definition at line 1225 of file vector.h.

1226 {
1227 if (m_size >= m_capacity)
1228 {
1229 reallocate(calc_new_capacity());
1230 }
1231
1233 ++m_size;
1234 }

◆ push_back() [2/2]

template<typename T>
void dsa::Vector< T >::push_back ( T && value)
constexpr

Appends a copy of value at the end of the container.

Parameters
[in]valuethe value of the element to append

Definition at line 1237 of file vector.h.

1238 {
1239 // Some implementations throw std::lengt_error when push_back causes a reallocation that exceeds mas_size()
1240 if (m_size >= m_capacity)
1241 {
1242 reallocate(calc_new_capacity());
1243 }
1244
1246 ++m_size;
1247 }

◆ rbegin() [1/2]

template<typename T>
auto dsa::Vector< T >::rbegin ( ) -> reverse_iterator
nodiscardconstexpr

Returns reverse_iterator to the first element of reversed underlaying data structure.

Note
If container is empty, returned iterator will be equal to end()
Returns
reverse_iterator to the first element

Definition at line 1000 of file vector.h.

1001 {
1002 return reverse_iterator(end());
1003 }
std::reverse_iterator< iterator > reverse_iterator
Alias for reverse_iterator to data type used in class.
Definition vector.h:112

◆ rbegin() [2/2]

template<typename T>
auto dsa::Vector< T >::rbegin ( ) const -> const_reverse_iterator
nodiscardconstexpr

Returns const_reverse_iterator to the first element of reversed underlaying data structure.

Note
If container is empty, returned iterator will be equal to end()
Returns
const_reverse_iterator to the first element

Definition at line 1006 of file vector.h.

1007 {
1008 return const_reverse_iterator(end());
1009 }

◆ rend() [1/2]

template<typename T>
auto dsa::Vector< T >::rend ( ) -> reverse_iterator
nodiscardconstexpr

Returns reverse_iterator past the last element of reversed underlaying data structure.

Note
Iterator act as a sentinel, it is not guaranteed to be dereferencable
Returns
reverse_iterator to the element after the last element

Definition at line 1018 of file vector.h.

1019 {
1020 return reverse_iterator(begin());
1021 }

◆ rend() [2/2]

template<typename T>
auto dsa::Vector< T >::rend ( ) const -> const_reverse_iterator
nodiscardconstexpr

Returns const_reverse_iterator past the last element of reversed underlaying data structure.

Note
Iterator act as a sentinel, it is not guaranteed to be dereferencable
Returns
const_reverse_iterator to the element after the last element

Definition at line 1024 of file vector.h.

1025 {
1026 return const_reverse_iterator(begin());
1027 }

◆ reserve()

template<typename T>
void dsa::Vector< T >::reserve ( size_type new_cap)
constexpr

Increase the capacity of the container.

Parameters
[in]new_capnew capacity of container, new number of elements the container can store

Definition at line 1054 of file vector.h.

1055 {
1056 if (new_cap > capacity())
1057 {
1058 // strong exception guarantee by reallocate
1059 reallocate(new_cap);
1060 }
1061 }
constexpr auto capacity() -> size_type
Returns the number of elements allocated for container.
Definition vector.h:1064

◆ resize() [1/2]

template<typename T>
void dsa::Vector< T >::resize ( size_type count)
constexpr

Resizez the container to contain count elements if count is equal to current size, does nothing if current size is greater than count , container size is reduced if current size is less than count , additional copies of T() are appended.

Parameters
[in]countnew size of the container

Definition at line 1260 of file vector.h.

1261 {
1262 resize(count, T{});
1263 }
constexpr void resize(size_type count)
Resizez the container to contain count elements if count is equal to current size,...
Definition vector.h:1260

◆ resize() [2/2]

template<typename T>
void dsa::Vector< T >::resize ( size_type count,
const value_type & value )
constexpr

Resizes the container to contain count elements if count is equal to current size, does nothing if current size is greater than count , container size is reduced if current size is less than count , additional copies of value are appended.

Parameters
[in]countnew size of the container
[in]valuethe value to initialize the new element with

Definition at line 1266 of file vector.h.

1267 {
1268 if (count >= max_size())
1269 {
1270 throw std::length_error("Capacity required by new vector would exceed maximum allowed size");
1271 }
1272
1273 if (count == m_size)
1274 {
1275 return;
1276 }
1277
1278 if (m_size > count)
1279 {
1280 // container is reduced to its count elements
1281 while (m_size > count)
1282 {
1283 pop_back();
1284 }
1285 }
1286
1287 if (m_size < count)
1288 {
1289 // additional copies of value are appended
1290 while (m_size < count)
1291 {
1293 }
1294 }
1295 }
constexpr auto max_size() const noexcept -> size_type
Returns maximum number of elements container can hold.
Definition vector.h:1048
constexpr void pop_back()
Removes the last element of the container.
Definition vector.h:1250

◆ shrink_to_fit()

template<typename T>
void dsa::Vector< T >::shrink_to_fit ( )
constexpr

Request to remove of unused capacity.

Note
If request is fulfilled and reallocation occurs, all iterators and all references to the container elements are invalidated

Definition at line 1070 of file vector.h.

1071 {
1072 if (m_capacity > m_size)
1073 {
1074 reallocate(m_size);
1075 }
1076 }

◆ size()

template<typename T>
auto dsa::Vector< T >::size ( ) const -> size_type
nodiscardconstexprnoexcept

Returns number of elements in container.

Returns
size_type number of elements in container

Definition at line 1042 of file vector.h.

1043 {
1044 return m_size;
1045 }

◆ swap()

template<typename T>
void dsa::Vector< T >::swap ( Vector< T > & other)
constexprnoexcept

Exchanges content of current container with other container.

Parameters
[in]othercontainer to exchange content with

Definition at line 1298 of file vector.h.

1299 {
1300 std::swap(*this, other);
1301 }

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