DSA - Data Structures and Algorithms
Loading...
Searching...
No Matches
array.h File Reference

This file contains implementation of Array class. More...

#include <cstddef>
#include <iostream>
#include <iterator>
#include <type_traits>
#include <utility>

Go to the source code of this file.

Classes

struct  dsa::Array< T, N >
 Implements Array class template for fixed size container. More...

Functions

template<std::size_t I, typename T, std::size_t N>
constexpr auto dsa::get (Array< T, N > &array) noexcept -> T &
 Provide access to Ith element of an array.
template<std::size_t I, typename T, std::size_t N>
constexpr auto dsa::get (const Array< T, N > &array) noexcept -> const T &
 Provide access to Ith element of an array.
template<std::size_t I, typename T, std::size_t N>
constexpr auto dsa::get (Array< T, N > &&array) noexcept -> T &&
 Provide access to Ith element of an array.
template<std::size_t I, typename T, std::size_t N>
constexpr auto dsa::get (const Array< T, N > &&array) noexcept -> const T &&
 Provide access to Ith element of an array.
template<typename T, std::size_t N>
void dsa::swap (Array< T, N > &lhs, Array< T, N > &rhs) noexcept(noexcept(lhs.swap(rhs)))
 Exchanges content of two Array containers.
template<typename T, std::size_t N>
requires (!std::is_array_v<T>)
constexpr auto dsa::to_array (T(&array)[N]) -> Array< std::remove_cv_t< T >, N >
 Creates Array from one dimensional C-style array using copy semantics.
template<typename T, std::size_t N>
requires (!std::is_array_v<T> && !std::is_const_v<T>)
constexpr auto dsa::to_array (T(&&array)[N]) -> Array< std::remove_cv_t< T >, N >
 Creates Array from one dimensional C-style array using move semantics.
template<typename T, std::size_t N>
auto dsa::operator<< (std::ostream &out, const Array< T, N > &array) -> std::ostream &
 Overloads operator to print all elements of Array.
template<typename T, std::size_t N>
constexpr auto dsa::operator== (const Array< T, N > &lhs, const Array< T, N > &rhs) noexcept(noexcept(*lhs.begin()== *rhs.begin())) -> bool
 The relational operator compares two Array objects.
template<typename T, std::size_t N>
constexpr auto dsa::operator<=> (const Array< T, N > &lhs, const Array< T, N > &rhs) noexcept(noexcept(*lhs.begin()== *rhs.begin())) -> std::compare_three_way_result_t< T >
 The relational operator compares two Array objects.

Detailed Description

This file contains implementation of Array class.

Author
Michal Zygmunt

Definition in file array.h.

Function Documentation

◆ get() [1/4]

template<std::size_t I, typename T, std::size_t N>
auto dsa::get ( Array< T, N > && array) -> T&&
nodiscardconstexprnoexcept

Provide access to Ith element of an array.

Intentionally not moving entire parameter array. This overload of get<I>(dsa::Array&&) returns an rvalue reference to an element, matching the behaviour of std::get<I>(std::array&&) in C++ standard library. Only the selected element is moved from std::move(array[I]), not the whole array.

Template Parameters
Iindex of element in container
Tdata type stored in container
Nnumber of elements in container
Parameters
[in]arraycontainer to extract element from
Returns
T&& reference to Ith element of array container

Definition at line 485 of file array.h.

486 {
487 static_assert(I < N, "Index out of range in dsa::Array::get");
488 return std::move(array[I]);
489 }

◆ get() [2/4]

template<std::size_t I, typename T, std::size_t N>
auto dsa::get ( Array< T, N > & array) -> T&
nodiscardconstexprnoexcept

Provide access to Ith element of an array.

Template Parameters
Iindex of element in container
Tdata type stored in container
Nnumber of elements in container
Parameters
[in]arraycontainer to extract element from
Returns
T& reference to Ith element of array container

Definition at line 447 of file array.h.

448 {
449 static_assert(I < N, "Index out of range in dsa::Array::get");
450 return array[I];
451 }

◆ get() [3/4]

template<std::size_t I, typename T, std::size_t N>
auto dsa::get ( const Array< T, N > && array) -> const T&&
nodiscardconstexprnoexcept

Provide access to Ith element of an array.

Template Parameters
Iindex of element in container
Tdata type stored in container
Nnumber of elements in container
Parameters
[in]arraycontainer to extract element from
Returns
const T&& reference to Ith element of array container

Definition at line 501 of file array.h.

502 {
503 static_assert(I < N, "Index out of range in dsa::Array::get");
504 return std::move(array[I]);
505 }

◆ get() [4/4]

template<std::size_t I, typename T, std::size_t N>
auto dsa::get ( const Array< T, N > & array) -> const T&
nodiscardconstexprnoexcept

Provide access to Ith element of an array.

Template Parameters
Iindex of element in container
Tdata type stored in container
Nnumber of elements in container
Parameters
[in]arraycontainer to extract element from
Returns
const T& reference to Ith element of array container

Definition at line 463 of file array.h.

464 {
465 static_assert(I < N, "Index out of range in dsa::Array::get");
466 return array[I];
467 }

◆ operator<<()

template<typename T, std::size_t N>
auto dsa::operator<< ( std::ostream & out,
const Array< T, N > & array ) -> std::ostream&

Overloads operator to print all elements of Array.

Template Parameters
Tdata type stored in container
Parameters
[in,out]outreference to output stream
[in]arrayArray to print
Returns
std::ostream&

Definition at line 572 of file array.h.

573 {
574 for (size_t i = 0; i < N; i++)
575 {
576 out << array[i] << ' ';
577 }
578
579 return out;
580 }

◆ operator<=>()

template<typename T, std::size_t N>
auto dsa::operator<=> ( const Array< T, N > & lhs,
const Array< T, N > & rhs ) -> std::compare_three_way_result_t<T>
nodiscardconstexprnoexcept

The relational operator compares two Array objects.

Depending on type T, function returns one of following objects: std::strong_ordering::less / equal / greater std::weak_ordering::less / equivalent / greater std::partial_ordering::less / equivalent / greater / unordered It is best to compare results with 0 to determine if lhs is <, >, or == to rhs

Parameters
[in]lhsinput container
[in]rhsinput container
Returns
three way comparison result type

Definition at line 625 of file array.h.

627 {
628 auto lhs_iter = lhs.cbegin();
629 auto rhs_iter = rhs.cbegin();
630
631 for (size_t i = 0; i < N; i++)
632 {
633 auto cmp = *lhs_iter <=> *rhs_iter;
634 if (cmp != 0)
635 {
636 return cmp;
637 }
638
639 lhs_iter++;
640 rhs_iter++;
641 }
642
643 return std::compare_three_way_result_t<T>::equivalent;
644 }
constexpr auto cbegin() const noexcept -> const_iterator
Returns const_iterator to first element.
Definition array.h:257

◆ operator==()

template<typename T, std::size_t N>
auto dsa::operator== ( const Array< T, N > & lhs,
const Array< T, N > & rhs ) -> bool
nodiscardconstexprnoexcept

The relational operator compares two Array objects.

Parameters
[in]lhsinput container
[in]rhsinput container
Return values
trueif input containers are equal
falseif input containers are not equal

Definition at line 591 of file array.h.

593 {
594 auto lhs_iter = lhs.cbegin();
595 auto rhs_iter = rhs.cbegin();
596
597 for (size_t i = 0; i < N; i++)
598 {
599 if (*lhs_iter != *rhs_iter)
600 {
601 return false;
602 }
603
604 lhs_iter++;
605 rhs_iter++;
606 }
607
608 return true;
609 }

◆ swap()

template<typename T, std::size_t N>
void dsa::swap ( Array< T, N > & lhs,
Array< T, N > & rhs )
noexcept

Exchanges content of two Array containers.

Template Parameters
Tdata type stored in containers
Nnumber of elements in containers
Parameters
[in]lhscontainer to swap content
[in]rhscontainer to swap content

Definition at line 516 of file array.h.

517 {
518 lhs.swap(rhs);
519 }
constexpr void swap(Array< T, N > &other) noexcept(std::is_nothrow_swappable_v< T >)
Exchanges content of current container with other container.
Definition array.h:420

◆ to_array() [1/2]

template<typename T, std::size_t N>
requires (!std::is_array_v<T> && !std::is_const_v<T>)
auto dsa::to_array ( T(&&) array[N]) -> Array<std::remove_cv_t<T>, N>
nodiscardconstexpr

Creates Array from one dimensional C-style array using move semantics.

Template Parameters
Tdata type stored in containers
Nnumber of elements in containers
Parameters
[in]arrayC-style array
Returns
Array container

Definition at line 554 of file array.h.

555 {
556 return[&]<std::size_t... I>(std::index_sequence<I...>)
557 {
558 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
559 return dsa::Array<std::remove_cv_t<T>, N>{std::move(array[I])...};
560 }(std::make_index_sequence<N>{});
561 }
Implements Array class template for fixed size container.
Definition array.h:32

◆ to_array() [2/2]

template<typename T, std::size_t N>
requires (!std::is_array_v<T>)
auto dsa::to_array ( T(&) array[N]) -> Array<std::remove_cv_t<T>, N>
nodiscardconstexpr

Creates Array from one dimensional C-style array using copy semantics.

Template Parameters
Tdata type stored in containers
Nnumber of elements in containers
Parameters
[in]arrayC-style array
Returns
Array container

Definition at line 533 of file array.h.

534 {
535 return[&]<std::size_t... I>(std::index_sequence<I...>)
536 {
537 // NOLINTNEXTLINE(cppcoreguidelines-avoid-c-arrays,modernize-avoid-c-arrays)
538 return dsa::Array<std::remove_cv_t<T>, N>{array[I]...};
539 }(std::make_index_sequence<N>{});
540 }