DSA - Data Structures and Algorithms
Loading...
Searching...
No Matches
dsa::Array< T, N > Struct Template Reference

Implements Array class template for fixed size container. More...

#include <array.h>

Public Types

using value_type = T
 Alias for data type used in class.
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 = value_type*
 Alias for pointer to data type used in class.
using const_pointer = const value_type*
 Alias for const pointer to data type used in class.
using reference = value_type&
 Alias for reference to data type used in class.
using const_reference = const value_type&
 Alias for const reference to data type used in class.
using iterator = value_type*
 Alias for iterator to data type used in class.
using const_iterator = const value_type*
 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 auto at (size_type pos) -> reference
 Returns a reference to Array 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 Array 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 Array 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 Array 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 array container.
constexpr auto data () const noexcept -> const_pointer
 Returns const_pointer to underlying array 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 array structure.
constexpr auto end () const noexcept -> const_iterator
 Returns const_iterator past last element of underlaying array structure.
constexpr auto cend () const noexcept -> const_iterator
 Returns const_iterator past last element of underlaying array structure.
constexpr auto rbegin () noexcept -> reverse_iterator
 Returns reverse_iterator to the first element of reversed underlaying array structure.
constexpr auto rbegin () const noexcept -> const_reverse_iterator
 Returns const_reverse_iterator to the first element of reversed underlaying array structure.
constexpr auto crbegin () const noexcept -> const_reverse_iterator
 Returns const_reverse_iterator to the first element of reversed underlaying array structure.
constexpr auto rend () noexcept -> reverse_iterator
 Returns reverse_iterator past the last element of reversed underlaying array structure.
constexpr auto rend () const noexcept -> const_reverse_iterator
 Returns const_reverse_iterator past the last element of reversed underlaying array structure.
constexpr auto crend () const noexcept -> const_reverse_iterator
 Returns const_reverse_iterator past the last element of reversed underlaying array structure.
constexpr auto empty () const noexcept -> 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.
void fill (const value_type &value)
 Assigns value to all elements of the container.
constexpr void swap (Array< T, N > &other) noexcept(std::is_nothrow_swappable_v< T >)
 Exchanges content of current container with other container.

Public Attributes

value_type m_data [N==0 ? 1 :N] {}
 Underlaying fixed size array containing all elements Conditional size is used to handle zero sized array without template specialization.

Detailed Description

template<typename T, std::size_t N>
struct dsa::Array< T, N >

Implements Array class template for fixed size container.

Template Parameters
Ttype of data stored in container
Nnumber of elements in container

Definition at line 31 of file array.h.

Member Typedef Documentation

◆ const_iterator

template<typename T, std::size_t N>
using dsa::Array< T, N >::const_iterator = const value_type*

Alias for const iterator to data type used in class.

Definition at line 90 of file array.h.

◆ const_pointer

template<typename T, std::size_t N>
using dsa::Array< T, N >::const_pointer = const value_type*

Alias for const pointer to data type used in class.

Template Parameters
T*pointer to data type

Definition at line 66 of file array.h.

◆ const_reference

template<typename T, std::size_t N>
using dsa::Array< T, N >::const_reference = const value_type&

Alias for const reference to data type used in class.

Template Parameters
T&const reference to data type

Definition at line 80 of file array.h.

◆ const_reverse_iterator

template<typename T, std::size_t N>
using dsa::Array< T, N >::const_reverse_iterator = std::reverse_iterator<const_iterator>

Alias for const reverse_iterator to data type used in class.

Definition at line 100 of file array.h.

◆ difference_type

template<typename T, std::size_t N>
using dsa::Array< T, N >::difference_type = std::ptrdiff_t

Alias for pointer difference type.

Used by STL to define distance between two pointers

Definition at line 52 of file array.h.

◆ iterator

template<typename T, std::size_t N>
using dsa::Array< T, N >::iterator = value_type*

Alias for iterator to data type used in class.

Definition at line 85 of file array.h.

◆ pointer

template<typename T, std::size_t N>
using dsa::Array< T, N >::pointer = value_type*

Alias for pointer to data type used in class.

Template Parameters
T*pointer to data type

Definition at line 59 of file array.h.

◆ reference

template<typename T, std::size_t N>
using dsa::Array< T, N >::reference = value_type&

Alias for reference to data type used in class.

Template Parameters
T&reference to data type

Definition at line 73 of file array.h.

◆ reverse_iterator

template<typename T, std::size_t N>
using dsa::Array< T, N >::reverse_iterator = std::reverse_iterator<iterator>

Alias for reverse_iterator to data type used in class.

Definition at line 95 of file array.h.

◆ size_type

template<typename T, std::size_t N>
using dsa::Array< T, N >::size_type = std::size_t

Alias for size type used in class.

Template Parameters
Tsize type

Definition at line 45 of file array.h.

◆ value_type

template<typename T, std::size_t N>
using dsa::Array< T, N >::value_type = T

Alias for data type used in class.

Template Parameters
Tdata type

Definition at line 38 of file array.h.

Member Function Documentation

◆ at() [1/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::at ( size_type pos) -> reference
inlinenodiscardconstexpr

Returns a reference to Array 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 Array element at pos index

Definition at line 109 of file array.h.

110 {
111 if (pos >= N)
112 {
113 throw std::out_of_range("Pos argument outside of container range");
114 }
115
116 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index)
117 return m_data[pos];
118 }
Implements Array class template for fixed size container.
Definition array.h:32
value_type m_data[N==0 ? 1 :N]
Underlaying fixed size array containing all elements Conditional size is used to handle zero sized ar...
Definition array.h:434

◆ at() [2/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::at ( size_type pos) const -> const_reference
inlinenodiscardconstexpr

Returns a const_reference to Array 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 Array element at pos index

Definition at line 127 of file array.h.

128 {
129 if (pos >= N)
130 {
131 throw std::out_of_range("Pos argument outside of container range");
132 }
133
134 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index)
135 return m_data[pos];
136 }

◆ back() [1/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::back ( ) -> reference
inlinenodiscardconstexpr

Returns reference to last Arary element.

Returns
reference to last element

Definition at line 191 of file array.h.

192 {
193 return m_data[N - 1];
194 }

◆ back() [2/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::back ( ) const -> const_reference
inlinenodiscardconstexpr

Returns const_reference to last Arary element.

Returns
const_reference to last element

Definition at line 201 of file array.h.

202 {
203 return m_data[N - 1];
204 }

◆ begin() [1/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::begin ( ) const -> const_iterator
inlinenodiscardconstexprnoexcept

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 245 of file array.h.

246 {
247 return &m_data[0];
248 }

◆ begin() [2/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::begin ( ) -> iterator
inlinenodiscardconstexprnoexcept

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 233 of file array.h.

234 {
235 return &m_data[0];
236 }

◆ cbegin()

template<typename T, std::size_t N>
auto dsa::Array< T, N >::cbegin ( ) const -> const_iterator
inlinenodiscardconstexprnoexcept

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 257 of file array.h.

258 {
259 return &m_data[0];
260 }

◆ cend()

template<typename T, std::size_t N>
auto dsa::Array< T, N >::cend ( ) const -> const_iterator
inlinenodiscardconstexprnoexcept

Returns const_iterator past last element of underlaying array structure.

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

Definition at line 293 of file array.h.

294 {
295 return &(m_data[N]);
296 }

◆ crbegin()

template<typename T, std::size_t N>
auto dsa::Array< T, N >::crbegin ( ) const -> const_reverse_iterator
inlinenodiscardconstexprnoexcept

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

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

Definition at line 329 of file array.h.

330 {
331 return const_reverse_iterator(end());
332 }
std::reverse_iterator< const_iterator > const_reverse_iterator
Alias for const reverse_iterator to data type used in class.
Definition array.h:100
constexpr auto end() noexcept -> iterator
Returns iterator past last element of underlaying array structure.
Definition array.h:269

◆ crend()

template<typename T, std::size_t N>
auto dsa::Array< T, N >::crend ( ) const -> const_reverse_iterator
inlinenodiscardconstexprnoexcept

Returns const_reverse_iterator past the last element of reversed underlaying array 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 365 of file array.h.

366 {
368 }
constexpr auto begin() noexcept -> iterator
Returns iterator to first element.
Definition array.h:233

◆ data() [1/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::data ( ) const -> const_pointer
inlinenodiscardconstexprnoexcept

Returns const_pointer to underlying array container.

Returns
const_pointer to underlaying array container

Definition at line 221 of file array.h.

222 {
223 return &m_data[0];
224 }

◆ data() [2/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::data ( ) -> pointer
inlinenodiscardconstexprnoexcept

Returns pointer to underlying array container.

Returns
pointer to underlaying array container

Definition at line 211 of file array.h.

212 {
213 return &m_data[0];
214 }

◆ empty()

template<typename T, std::size_t N>
auto dsa::Array< T, N >::empty ( ) const -> bool
inlinenodiscardconstexprnoexcept

Checks if container has elements.

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

Definition at line 376 of file array.h.

377 {
378 return N == 0;
379 }

◆ end() [1/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::end ( ) const -> const_iterator
inlinenodiscardconstexprnoexcept

Returns const_iterator past last element of underlaying array structure.

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

Definition at line 281 of file array.h.

282 {
283 return &(m_data[N]);
284 }

◆ end() [2/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::end ( ) -> iterator
inlinenodiscardconstexprnoexcept

Returns iterator past last element of underlaying array structure.

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

Definition at line 269 of file array.h.

270 {
271 return &m_data[N];
272 }

◆ fill()

template<typename T, std::size_t N>
void dsa::Array< T, N >::fill ( const value_type & value)
inline

Assigns value to all elements of the container.

Parameters
[in]valueto assign to all elements of container

Definition at line 406 of file array.h.

407 {
408 for (size_type i = 0; i < size(); i++)
409 {
410 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index)
411 m_data[i] = value;
412 }
413 }
constexpr auto size() const noexcept -> size_type
Returns number of elements in container.
Definition array.h:386
std::size_t size_type
Alias for size type used in class.
Definition array.h:45

◆ front() [1/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::front ( ) -> reference
inlinenodiscardconstexpr

Returns reference to first Arary element.

Returns
reference to first element

Definition at line 171 of file array.h.

172 {
173 return m_data[0];
174 }

◆ front() [2/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::front ( ) const -> const_reference
inlinenodiscardconstexpr

Returns const_reference to first Arary element.

Returns
const_reference to first element

Definition at line 181 of file array.h.

182 {
183 return m_data[0];
184 }

◆ max_size()

template<typename T, std::size_t N>
auto dsa::Array< T, N >::max_size ( ) const -> size_type
inlinenodiscardconstexprnoexcept

Returns maximum number of elements container can hold.

Returns
size_type maximum number of elements

Definition at line 396 of file array.h.

397 {
398 return N;
399 }

◆ operator[]() [1/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::operator[] ( size_type pos) -> reference
inlinenodiscardconstexpr

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

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

Definition at line 145 of file array.h.

146 {
147 // Same semantics as std::array::operator[], no bounds checking
148 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index)
149 return m_data[pos];
150 }

◆ operator[]() [2/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::operator[] ( size_type pos) const -> const_reference
inlinenodiscardconstexpr

Returns a const_reference to Array 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 Array element

Definition at line 159 of file array.h.

160 {
161 // Same semantics as std::array::operator[], no bounds checking
162 // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-constant-array-index)
163 return m_data[pos];
164 }

◆ rbegin() [1/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::rbegin ( ) const -> const_reverse_iterator
inlinenodiscardconstexprnoexcept

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

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

Definition at line 317 of file array.h.

318 {
319 return const_reverse_iterator(end());
320 }

◆ rbegin() [2/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::rbegin ( ) -> reverse_iterator
inlinenodiscardconstexprnoexcept

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

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

Definition at line 305 of file array.h.

306 {
307 return reverse_iterator(end());
308 }
std::reverse_iterator< iterator > reverse_iterator
Alias for reverse_iterator to data type used in class.
Definition array.h:95

◆ rend() [1/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::rend ( ) const -> const_reverse_iterator
inlinenodiscardconstexprnoexcept

Returns const_reverse_iterator past the last element of reversed underlaying array 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 353 of file array.h.

354 {
356 }

◆ rend() [2/2]

template<typename T, std::size_t N>
auto dsa::Array< T, N >::rend ( ) -> reverse_iterator
inlinenodiscardconstexprnoexcept

Returns reverse_iterator past the last element of reversed underlaying array 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 341 of file array.h.

342 {
343 return reverse_iterator(begin());
344 }

◆ size()

template<typename T, std::size_t N>
auto dsa::Array< T, N >::size ( ) const -> size_type
inlinenodiscardconstexprnoexcept

Returns number of elements in container.

Returns
size_type number of elements in container

Definition at line 386 of file array.h.

387 {
388 return N;
389 }

◆ swap()

template<typename T, std::size_t N>
void dsa::Array< T, N >::swap ( Array< T, N > & other)
inlineconstexprnoexcept

Exchanges content of current container with other container.

Parameters
[in]othercontainer to exchange content with

Definition at line 420 of file array.h.

421 {
422 if (*this != other)
423 {
425 }
426 }

Member Data Documentation

◆ m_data

template<typename T, std::size_t N>
value_type dsa::Array< T, N >::m_data[N==0 ? 1 :N] {}

Underlaying fixed size array containing all elements Conditional size is used to handle zero sized array without template specialization.

Definition at line 434 of file array.h.

434: N]{};

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