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

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

#include <cstddef>
#include <functional>
#include <initializer_list>
#include <iostream>
#include <iterator>
#include <limits>
#include <memory>
#include <ranges>
#include <utility>

Go to the source code of this file.

Classes

class  dsa::ForwardList< T >
 Implements ForwardList using Node with pointer to next element as internal base. More...
class  dsa::ForwardList< T >::NodeBase
 Struct implements base pointer used by ForwardList. More...
class  dsa::ForwardList< T >::Node
 Implements Node class with user data. More...
class  dsa::ForwardList< T >::ForwardListIterator< IF_CONST >
 Implements ForwardListIterator. More...

Functions

template<typename T>
auto dsa::operator<< (std::ostream &out, const ForwardList< T > &list) -> std::ostream &
 Overloads operator to print all elements of ForwardList.
template<typename T>
auto dsa::operator== (const ForwardList< T > &lhs, const ForwardList< T > &rhs) noexcept(noexcept(*lhs.begin()== *rhs.begin())) -> bool
 The relational operator compares two ForwardList objects.
template<typename T>
auto dsa::operator<=> (const ForwardList< T > &lhs, const ForwardList< T > &rhs) noexcept(noexcept(*lhs.begin()== *rhs.begin())) -> std::compare_three_way_result_t< T >
 The relational operator compares two ForwardList objects.
template<typename T>
void dsa::swap (ForwardList< T > &lhs, ForwardList< T > &rhs) noexcept(noexcept(lhs.swap(rhs)))
 Exchanges content of two ForwardList containers.
template<typename T, typename U>
auto dsa::erase (ForwardList< T > &container, const U &value) -> ForwardList< T >::size_type
 Function erases from container all elements that are equal to value.
template<typename T, typename Pred>
auto dsa::erase_if (ForwardList< T > &container, Pred pred) -> ForwardList< T >::size_type
 Function erases from container all elements that satisfy the predicate pred.

Detailed Description

This file contains implementation of ForwardList class.

Author
Michal Zygmunt

Definition in file forward_list.h.

Function Documentation

◆ erase()

template<typename T, typename U>
auto dsa::erase ( ForwardList< T > & container,
const U & value ) -> ForwardList<T>::size_type

Function erases from container all elements that are equal to value.

Template Parameters
Tdata type stored in containers
Udata type of value
Parameters
[in,out]containerobject to remove erase elements from
[in]valuevalue to remove from container
Returns
size_type number of elements removed

Definition at line 1974 of file forward_list.h.

1975 {
1976 return erase_if(container, [&value](U node_val) { return node_val == value; });
1977 }
auto erase_if(ForwardList< T > &container, Pred pred) -> ForwardList< T >::size_type
Function erases from container all elements that satisfy the predicate pred.

◆ erase_if()

template<typename T, typename Pred>
auto dsa::erase_if ( ForwardList< T > & container,
Pred pred ) -> ForwardList<T>::size_type

Function erases from container all elements that satisfy the predicate pred.

Template Parameters
Tdata type stored in containers
Predpredicate to check if element should be erased
Parameters
[in,out]containercontainer object to remove erase elements from
[in]predpredicate which returns true if the element should be erased
Returns
size_type number of elements removed

Definition at line 1989 of file forward_list.h.

1990 {
1991 return container.remove_if(pred);
1992 }
auto remove_if(UnaryPred predicate) -> size_type
Function removes all elements for which predicate returns true.

◆ operator<<()

template<typename T>
auto dsa::operator<< ( std::ostream & out,
const ForwardList< T > & list ) -> std::ostream&

Overloads operator to print all elements of ForwardList.

Template Parameters
Ttype of initializer list elements
Parameters
[in,out]outreference to output stream
[in]listForwardList to print
Returns
std::ostream&

Definition at line 1863 of file forward_list.h.

1864 {
1865 if (list.empty())
1866 {
1867 return out;
1868 }
1869
1870 for (auto it = list.cbegin(); it != list.cend(); ++it)
1871 {
1872 T value = *it;
1873 out << value << ' ';
1874 }
1875
1876 return out;
1877 }
auto cend() const noexcept -> const_iterator
Function returns pointer to ForwardList last Node.
auto cbegin() const noexcept -> const_iterator
Function returns const pointer to ForwardList first Node.
auto empty() const -> bool
Function checks if container has no elements.

◆ operator<=>()

template<typename T>
auto dsa::operator<=> ( const ForwardList< T > & lhs,
const ForwardList< T > & rhs ) -> std::compare_three_way_result_t<T>
nodiscardnoexcept

The relational operator compares two ForwardList 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 1928 of file forward_list.h.

1930 {
1931 auto lhs_iter = lhs.cbegin();
1932 auto rhs_iter = rhs.cbegin();
1933
1934 while (lhs_iter != lhs.cend() && rhs_iter != rhs.cend())
1935 {
1936 auto cmp = *lhs_iter <=> *rhs_iter;
1937 if (cmp != 0)
1938 {
1939 return cmp;
1940 }
1941
1942 lhs_iter++;
1943 rhs_iter++;
1944 }
1945
1946 // first n elements are equal
1947 // check sizes
1948 return lhs.size() <=> rhs.size();
1949 }
auto size() const -> size_type
Function returns ForwardList size.

◆ operator==()

template<typename T>
auto dsa::operator== ( const ForwardList< T > & lhs,
const ForwardList< T > & rhs ) -> bool
nodiscardnoexcept

The relational operator compares two ForwardList objects.

Template Parameters
Ttype of data stored in ForwardList
Parameters
[in]lhsinput container
[in]rhsinput container
Return values
trueif containers are equal
falseif containers are not equal

Definition at line 1889 of file forward_list.h.

1891 {
1892 if (lhs.size() != rhs.size())
1893 {
1894 return false;
1895 }
1896
1897 auto lhs_iter = lhs.cbegin();
1898 auto rhs_iter = rhs.cbegin();
1899
1900 while (lhs_iter != lhs.cend())
1901 {
1902 if (*lhs_iter != *rhs_iter)
1903 {
1904 return false;
1905 }
1906
1907 lhs_iter++;
1908 rhs_iter++;
1909 }
1910
1911 return true;
1912 }

◆ swap()

template<typename T>
void dsa::swap ( ForwardList< T > & lhs,
ForwardList< T > & rhs )
noexcept

Exchanges content of two ForwardList containers.

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

Definition at line 1959 of file forward_list.h.

1960 {
1961 lhs.swap(rhs);
1962 }
void swap(ForwardList< T > &other) noexcept(std::is_nothrow_swappable_v< T >)
Function swaps content of two ForwardList objects.