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

Implements ForwardList using Node with pointer to next element as internal base. More...

#include <forward_list.h>

Classes

class  ForwardListIterator
 Implements ForwardListIterator. More...
class  Node
 Implements Node class with user data. More...
class  NodeBase
 Struct implements base pointer used by ForwardList. More...

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 used in class.
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 const_iterator = ForwardListIterator<true>
 Alias for const iterator to data type used in class.
using iterator = ForwardListIterator<false>
 Alias for iterator to data type used in class.

Public Member Functions

 ForwardList ()
 Construct a new ForwardList object.
 ForwardList (size_type count)
 Construct a new ForwardList object of size count, using default value of type T.
 ForwardList (size_type count, const T &value)
 Construct a new ForwardList object of size count, using provided value of type T.
template<typename InputIt>
requires std::input_iterator<InputIt>
 ForwardList (InputIt first, InputIt last)
 Construct a new ForwardList object using elements from range [ first , last )
 ForwardList (const std::initializer_list< T > &init_list)
 Construct a new ForwardList object using initializer list.
 ForwardList (const ForwardList< T > &other)
 Construct a new ForwardList object using copy constructor.
auto operator= (const ForwardList< T > &other) -> ForwardList &
 Constructs ForwardList using copy assignment.
 ForwardList (ForwardList< T > &&other) noexcept
 Construct a new ForwardList object using move constructor.
auto operator= (ForwardList< T > &&other) noexcept -> ForwardList &
 Assign ForwardList object using move assignment.
 ~ForwardList ()
 Destroy the ForwardList object.
void assign (size_type count, const_reference value)
 Function assign values to the ForwardList.
template<typename InputIt>
requires std::input_iterator<InputIt>
void assign (InputIt first, InputIt last)
 Function assign elements from range [ first , last ) to ForwardList object.
void assign (const std::initializer_list< T > &init_list)
 Function assign values to the ForwardList.
constexpr auto get_allocator () const -> allocator_type
 Get the allocator object.
auto front () -> reference
 Function returns reference to value stored in ForwardList first Node.
auto front () const -> const_reference
 Function returns const reference value stored in ForwardList first Node.
auto before_begin () noexcept -> iterator
 Function returns iterator just before ForwardList first Node.
auto before_begin () const noexcept -> const_iterator
 Function returns const_iterator just before ForwardList first Node.
auto cbefore_begin () const noexcept -> const_iterator
 Function returns const_iterator just before ForwardList first Node.
auto begin () noexcept -> iterator
 Function returns pointer to ForwardList first Node.
auto begin () const noexcept -> const_iterator
 Function returns const pointer to ForwardList first Node.
auto cbegin () const noexcept -> const_iterator
 Function returns const pointer to ForwardList first Node.
auto end () noexcept -> iterator
 Function returns pointer to ForwardList last Node.
auto end () const noexcept -> const_iterator
 Function returns pointer to ForwardList last Node.
auto cend () const noexcept -> const_iterator
 Function returns pointer to ForwardList last Node.
auto empty () const -> bool
 Function checks if container has no elements.
auto max_size () const noexcept -> size_type
 Function returns maximum number of elements container can hold.
void clear ()
 Function removes all elements of ForwardList.
auto insert_after (const const_iterator &pos, const_reference value) -> iterator
 Function inserts new Node after specified ForwardList iterator.
auto insert_after (const const_iterator &pos, T &&value) -> iterator
 Function inserts new Node after specified ForwardList iterator.
auto insert_after (const const_iterator &pos, size_type count, const_reference value) -> iterator
 Function inserts new Node after specified ForwardList const_iterator.
template<typename InputIt>
requires std::input_iterator<InputIt>
auto insert_after (const const_iterator &pos, InputIt first, InputIt last) -> iterator
 Function inserts new Node after specified ForwardList const_iterator.
auto insert_after (const const_iterator &pos, std::initializer_list< T > init_list) -> iterator
 Function inserts new Node after specified ForwardList const_iterator.
template<typename... Args>
auto emplace_after (const_iterator pos, Args &&... args) -> iterator
 Insert new element into the container after pos.
auto erase_after (const const_iterator &pos) -> iterator
 Function erases Node after specified ForwardList const_iterator.
auto erase_after (const const_iterator &first, const const_iterator &last) -> iterator
 Function erases Node between specified ForwardList const_iterators.
void push_front (const_reference value)
 Function adds new Node at the beginning of ForwardList.
void push_front (T &&value)
 Function adds new Node at the beginning of ForwardList.
template<typename... Args>
auto emplace_front (Args &&... args) -> reference
 Inserts a new element to the beginning of the container.
void pop_front ()
 Function removes first Node of ForwardList.
void resize (size_type count)
 Function resize ForwardList to specified number of elements.
void resize (size_type count, const_reference value)
 Function resize ForwardList to specified number of elements.
void swap (ForwardList< T > &other) noexcept(std::is_nothrow_swappable_v< T >)
 Function swaps content of two ForwardList objects.
void merge (ForwardList< T > &other)
 Function combines two sorted ForwardLists into one sorted ForwardList.
void merge (ForwardList< T > &&other)
 Function combines two sorted ForwardLists into one sorted ForwardList.
template<typename Compare>
void merge (ForwardList< T > &other, Compare comp)
 Function combines two sorted ForwardLists into one sorted ForwardList.
template<typename Compare>
void merge (ForwardList< T > &&other, Compare comp)
 Function combines two sorted ForwardLists into one sorted ForwardList.
void splice_after (const const_iterator &pos, ForwardList< T > &other)
 Function moves elements from other ForwardList object.
void splice_after (const_iterator pos, ForwardList< T > &&other)
 Function moves elements from other ForwardList object.
void splice_after (const const_iterator &pos, ForwardList< T > &other, const const_iterator &iter)
 Function moves elements from other ForwardList object.
void splice_after (const_iterator pos, ForwardList< T > &&other, const_iterator iter)
 Function moves elements from other ForwardList object.
void splice_after (const const_iterator &pos, ForwardList< T > &other, const const_iterator &first, const const_iterator &last)
 Function moves elements from other ForwardList object.
void splice_after (const_iterator pos, ForwardList< T > &&other, const_iterator first, const_iterator last)
 Function moves elements from other ForwardList object.
auto remove (const_reference value) -> size_type
 Function removes all elements equal to value.
template<typename UnaryPred>
auto remove_if (UnaryPred predicate) -> size_type
 Function removes all elements for which predicate returns true.
void reverse ()
 Function reverts in place Nodes of ForwardList.
auto unique () -> size_type
 Function removes consecutive duplicated elements.
template<typename BinaryPred>
auto unique (BinaryPred predicate) -> size_type
 Function removes consecutive duplicated elements.
void sort ()
 Function sorts the elements and preserves the order of equivalent elements.
template<typename Compare>
void sort (Compare comp)
 Function sorts the elements and preserves the order of equivalent elements.
auto size () const -> size_type
 Function returns ForwardList size.

Detailed Description

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

Implements ForwardList using Node with pointer to next element as internal base.

Template Parameters
Ttype of data stored in ForwardList Node

Definition at line 38 of file forward_list.h.

Member Typedef Documentation

◆ allocator_type

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

Alias for memory allocator.

Definition at line 355 of file forward_list.h.

◆ const_iterator

template<typename T>
using dsa::ForwardList< T >::const_iterator = ForwardListIterator<true>

Alias for const iterator to data type used in class.

Definition at line 402 of file forward_list.h.

◆ const_pointer

template<typename T>
using dsa::ForwardList< T >::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 383 of file forward_list.h.

◆ const_reference

template<typename T>
using dsa::ForwardList< T >::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 397 of file forward_list.h.

◆ difference_type

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

Alias for pointer difference type used in class.

Template Parameters
Tpointer size type

Definition at line 369 of file forward_list.h.

◆ iterator

template<typename T>
using dsa::ForwardList< T >::iterator = ForwardListIterator<false>

Alias for iterator to data type used in class.

Definition at line 407 of file forward_list.h.

◆ pointer

template<typename T>
using dsa::ForwardList< T >::pointer = value_type*

Alias for pointer to data type used in class.

Template Parameters
T*pointer to data type

Definition at line 376 of file forward_list.h.

◆ reference

template<typename T>
using dsa::ForwardList< T >::reference = value_type&

Alias for reference to data type used in class.

Template Parameters
T&reference to data type

Definition at line 390 of file forward_list.h.

◆ size_type

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

Alias for size type used in class.

Template Parameters
Tsize type

Definition at line 362 of file forward_list.h.

◆ value_type

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

Alias for data type used in class.

Template Parameters
Tdata type

Definition at line 350 of file forward_list.h.

Constructor & Destructor Documentation

◆ ForwardList() [1/7]

template<typename T>
dsa::ForwardList< T >::ForwardList ( )

Construct a new ForwardList object.

Definition at line 1129 of file forward_list.h.

1130 {
1131 init_node();
1132 }

◆ ForwardList() [2/7]

template<typename T>
dsa::ForwardList< T >::ForwardList ( size_type count)

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

Parameters
[in]countelement count

Definition at line 1135 of file forward_list.h.

1136 : ForwardList(count, T{})
1137 {}
Implements ForwardList using Node with pointer to next element as internal base.
ForwardList()
Construct a new ForwardList object.

◆ ForwardList() [3/7]

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

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

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

Definition at line 1140 of file forward_list.h.

1141 {
1142 init_node();
1143
1144 for (size_type i = 0; i < count; i++)
1145 {
1147 }
1148 }
std::size_t size_type
Alias for size type used in class.
void push_front(const_reference value)
Function adds new Node at the beginning of ForwardList.

◆ ForwardList() [4/7]

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

Construct a new ForwardList 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 1153 of file forward_list.h.

1154 {
1155 init_node();
1156
1157 assign(first, last);
1158 }
void assign(size_type count, const_reference value)
Function assign values to the ForwardList.

◆ ForwardList() [5/7]

template<typename T>
dsa::ForwardList< T >::ForwardList ( const std::initializer_list< T > & init_list)

Construct a new ForwardList object using initializer list.

Parameters
[in]init_listinitializer list of values of type T

Definition at line 1161 of file forward_list.h.

1162 {
1163 init_node();
1164
1165 auto iter = before_begin();
1166 for (const auto& item : init_list)
1167 {
1169 }
1170 }
auto before_begin() noexcept -> iterator
Function returns iterator just before ForwardList first Node.
auto insert_after(const const_iterator &pos, const_reference value) -> iterator
Function inserts new Node after specified ForwardList iterator.

◆ ForwardList() [6/7]

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

Construct a new ForwardList object using copy constructor.

Parameters
[in]otherForwardList object of type T

Definition at line 1173 of file forward_list.h.

1174 {
1175 init_node();
1176
1177 auto iter = before_begin();
1178 for (const auto& item : other)
1179 {
1181 }
1182 }

◆ ForwardList() [7/7]

template<typename T>
dsa::ForwardList< T >::ForwardList ( ForwardList< T > && other)
noexcept

Construct a new ForwardList object using move constructor.

Content of other object will be taken by constructed object

Parameters
[in,out]otherForwardList object of type T

Definition at line 1207 of file forward_list.h.

1208 {
1210 }
auto operator=(const ForwardList< T > &other) -> ForwardList &
Constructs ForwardList using copy assignment.

◆ ~ForwardList()

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

Destroy the ForwardList object.

Definition at line 1231 of file forward_list.h.

1232 {
1233 clear();
1234 delete m_head;
1235 }
void clear()
Function removes all elements of ForwardList.

Member Function Documentation

◆ assign() [1/3]

template<typename T>
void dsa::ForwardList< T >::assign ( const std::initializer_list< T > & init_list)

Function assign values to the ForwardList.

Parameters
[in]init_listvalues to replace ForwardList with

Definition at line 1268 of file forward_list.h.

1269 {
1270 while (m_head->m_next)
1271 {
1272 pop_front();
1273 }
1274
1275 auto iter = before_begin();
1276 for (const auto& item : init_list)
1277 {
1279 }
1280 }
void pop_front()
Function removes first Node of ForwardList.

◆ assign() [2/3]

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

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

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

Definition at line 1255 of file forward_list.h.

1256 {
1257 clear();
1258
1259 auto iter = before_begin();
1260 while (first != last)
1261 {
1263 first++;
1264 }
1265 }

◆ assign() [3/3]

template<typename T>
void dsa::ForwardList< T >::assign ( size_type count,
const_reference value )

Function assign values to the ForwardList.

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

Definition at line 1238 of file forward_list.h.

1239 {
1240 while (m_head->m_next)
1241 {
1242 pop_front();
1243 }
1244
1245 auto iter = before_begin();
1246 for (size_type i = 0; i < count; i++)
1247 {
1249 }
1250 }

◆ before_begin() [1/2]

template<typename T>
auto dsa::ForwardList< T >::before_begin ( ) const -> const_iterator
nodiscardnoexcept

Function returns const_iterator just before ForwardList first Node.

Returns
const_iterator iterator just before ForwardList first Node

Definition at line 1307 of file forward_list.h.

1308 {
1309 return const_iterator(m_head);
1310 }
ForwardListIterator< true > const_iterator
Alias for const iterator to data type used in class.

◆ before_begin() [2/2]

template<typename T>
auto dsa::ForwardList< T >::before_begin ( ) -> iterator
nodiscardnoexcept

Function returns iterator just before ForwardList first Node.

Returns
iterator iterator just before ForwardList first Node

Definition at line 1301 of file forward_list.h.

1302 {
1303 return iterator(m_head);
1304 }
ForwardListIterator< false > iterator
Alias for iterator to data type used in class.

◆ begin() [1/2]

template<typename T>
auto dsa::ForwardList< T >::begin ( ) const -> const_iterator
nodiscardnoexcept

Function returns const pointer to ForwardList first Node.

Returns
const_iterator const iterator to ForwardList first Node

Definition at line 1325 of file forward_list.h.

1326 {
1327 return const_iterator(m_head->m_next);
1328 }

◆ begin() [2/2]

template<typename T>
auto dsa::ForwardList< T >::begin ( ) -> iterator
nodiscardnoexcept

Function returns pointer to ForwardList first Node.

Returns
iterator iterator to ForwardList first Node

Definition at line 1319 of file forward_list.h.

1320 {
1321 return iterator(m_head->m_next);
1322 }

◆ cbefore_begin()

template<typename T>
auto dsa::ForwardList< T >::cbefore_begin ( ) const -> const_iterator
nodiscardnoexcept

Function returns const_iterator just before ForwardList first Node.

Returns
const_iterator iterator just before ForwardList first Node

Definition at line 1313 of file forward_list.h.

1314 {
1315 return before_begin();
1316 }

◆ cbegin()

template<typename T>
auto dsa::ForwardList< T >::cbegin ( ) const -> const_iterator
nodiscardnoexcept

Function returns const pointer to ForwardList first Node.

Returns
const_iterator const iterator to ForwardList first Node

Definition at line 1331 of file forward_list.h.

1332 {
1333 return begin();
1334 }
auto begin() noexcept -> iterator
Function returns pointer to ForwardList first Node.

◆ cend()

template<typename T>
auto dsa::ForwardList< T >::cend ( ) const -> const_iterator
nodiscardnoexcept

Function returns pointer to ForwardList last Node.

Returns
const_iterator const iterator to ForwardList last Node

Definition at line 1349 of file forward_list.h.

1350 {
1351 return end();
1352 }
auto end() noexcept -> iterator
Function returns pointer to ForwardList last Node.

◆ clear()

template<typename T>
void dsa::ForwardList< T >::clear ( )

Function removes all elements of ForwardList.

Definition at line 1368 of file forward_list.h.

1369 {
1370 if (m_head)
1371 {
1372 NodeBase* temp{ m_head->m_next };
1373 while (temp)
1374 {
1375 m_head->m_next = temp->m_next;
1376 destroy_node(temp);
1377 temp = m_head->m_next;
1378 }
1379
1380 m_size = 0;
1381 m_head->m_next = nullptr;
1382 }
1383 }
Struct implements base pointer used by ForwardList.

◆ emplace_after()

template<typename T>
template<typename... Args>
auto dsa::ForwardList< T >::emplace_after ( const_iterator pos,
Args &&... args ) -> iterator

Insert new element into the container after pos.

Template Parameters
...Args
Parameters
[in]positerator before which new element will be inserted
[in]...argsargs arguments to forward to the constructor of the element
Returns
iterator pointing to the emplaced element
Note
no iterators or references are invalidated, if construction of new element fails or an exception is thrown for any reason state of the object does not change and this function has no effect

Definition at line 1466 of file forward_list.h.

1467 {
1468 const iterator current{ pos.m_current_node };
1469
1470 Node* newNode{ construct_node(current.m_current_node->m_next, std::forward<Args>(args)...) };
1471 current.m_current_node->m_next = newNode;
1472 ++m_size;
1473
1474 return iterator(newNode);
1475 }
Implements Node class with user data.

◆ emplace_front()

template<typename T>
template<typename... Args>
auto dsa::ForwardList< T >::emplace_front ( Args &&... args) -> reference

Inserts a new element to the beginning of the container.

Template Parameters
...Args
Parameters
[in]...argsarguments to forward to the constructor of the element
Returns
reference to emplaced element
Note
no iterators or references are invalidated, if construction of new element fails or an exception is thrown for any reason state of the object does not change and this function has no effect

Definition at line 1524 of file forward_list.h.

1525 {
1527 return front();
1528 }
auto emplace_after(const_iterator pos, Args &&... args) -> iterator
Insert new element into the container after pos.
auto front() -> reference
Function returns reference to value stored in ForwardList first Node.

◆ empty()

template<typename T>
auto dsa::ForwardList< T >::empty ( ) const -> bool
nodiscard

Function checks if container has no elements.

Return values
trueif container is empty
falseif container is not empty

Definition at line 1356 of file forward_list.h.

1357 {
1358 return m_size == 0;
1359 }

◆ end() [1/2]

template<typename T>
auto dsa::ForwardList< T >::end ( ) const -> const_iterator
nodiscardnoexcept

Function returns pointer to ForwardList last Node.

Returns
const_iterator const iterator to ForwardList last Node

Definition at line 1343 of file forward_list.h.

1344 {
1345 return const_iterator(nullptr);
1346 }

◆ end() [2/2]

template<typename T>
auto dsa::ForwardList< T >::end ( ) -> iterator
nodiscardnoexcept

Function returns pointer to ForwardList last Node.

Returns
iterator iterator to ForwardList last Node

Definition at line 1337 of file forward_list.h.

1338 {
1339 return iterator(nullptr);
1340 }

◆ erase_after() [1/2]

template<typename T>
auto dsa::ForwardList< T >::erase_after ( const const_iterator & first,
const const_iterator & last ) -> iterator

Function erases Node between specified ForwardList const_iterators.

Parameters
[in]firstelement after which element will be erased
[in]lastelement after last erased element
Returns
pointer to element after last deleted element
Return values
iteratorto element after last deleted element
nullptrif invalid iterator

Definition at line 1492 of file forward_list.h.

1494 {
1495 if (!if_valid_iterator(first) || !if_valid_iterator(last))
1496 {
1497 return nullptr;
1498 }
1499
1500 const iterator iter(first.m_current_node);
1501 const size_type dist = distance(first, last);
1502 for (size_type i = 0; i < dist - 1; i++)
1503 {
1504 erase_element_after(iter);
1505 }
1506
1507 return first.m_current_node->m_next;
1508 }

◆ erase_after() [2/2]

template<typename T>
auto dsa::ForwardList< T >::erase_after ( const const_iterator & pos) -> iterator

Function erases Node after specified ForwardList const_iterator.

Parameters
[in]posconst_iterator after which element will be erased
Returns
pointer to element after deleted iterator
Return values
iteratorelement after deleted element
nullptrif invalid iterator

Definition at line 1478 of file forward_list.h.

1479 {
1480 if (!if_valid_iterator(pos))
1481 {
1482 return nullptr;
1483 }
1484
1485 iterator iter{ pos.m_current_node };
1486 iter = erase_element_after(iter);
1487
1488 return iter;
1489 }

◆ front() [1/2]

template<typename T>
auto dsa::ForwardList< T >::front ( ) -> reference
nodiscard

Function returns reference to value stored in ForwardList first Node.

Returns
reference to data stored in ForwardList first Node

Definition at line 1289 of file forward_list.h.

1290 {
1291 return *begin();
1292 }

◆ front() [2/2]

template<typename T>
auto dsa::ForwardList< T >::front ( ) const -> const_reference
nodiscard

Function returns const reference value stored in ForwardList first Node.

Returns
const reference to data stored in ForwardList first Node

Definition at line 1295 of file forward_list.h.

1296 {
1297 return *cbegin();
1298 }
auto cbegin() const noexcept -> const_iterator
Function returns const pointer to ForwardList first Node.

◆ get_allocator()

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

Get the allocator object.

Returns
allocator_type type of memory allocator

Definition at line 1283 of file forward_list.h.

1284 {
1285 return m_allocator;
1286 }

◆ insert_after() [1/5]

template<typename T>
auto dsa::ForwardList< T >::insert_after ( const const_iterator & pos,
const_reference value ) -> iterator

Function inserts new Node after specified ForwardList iterator.

Parameters
[in]posconst_iterator to insert element after
[in]valueelement of type T to be inserted after pos
Returns
pointer to ForwardList element
Return values
iteratorto inserted value
posif no element was inserted

Definition at line 1386 of file forward_list.h.

1388 {
1389 return insert_after(pos, 1, value);
1390 }

◆ insert_after() [2/5]

template<typename T>
requires std::input_iterator<InputIt>
template<typename InputIt>
requires std::input_iterator<InputIt>
auto dsa::ForwardList< T >::insert_after ( const const_iterator & pos,
InputIt first,
InputIt last ) -> iterator

Function inserts new Node after specified ForwardList const_iterator.

Parameters
[in]posconst_iterator to insert element after
[in]firstelement defining range of elements to insert
[in]lastelement definig range of elements to insert
Returns
pointer to ForwardList element
Return values
iteratorpointer to last inserted element
posif no element was inserted

Definition at line 1428 of file forward_list.h.

1430 {
1431 if (!if_valid_iterator(pos))
1432 {
1433 return nullptr;
1434 }
1435
1436 iterator iter{ pos.m_current_node };
1437 while (first != last)
1438 {
1440 first++;
1441 }
1442
1443 return iter;
1444 }

◆ insert_after() [3/5]

template<typename T>
auto dsa::ForwardList< T >::insert_after ( const const_iterator & pos,
size_type count,
const_reference value ) -> iterator

Function inserts new Node after specified ForwardList const_iterator.

Parameters
[in]posconst_iterator to insert element after
[in]countnumber of elements to insert after pos
[in]valueelement of type T to be inserted
Returns
pointer to ForwardList element
Return values
iteratorpointer to last inserted element
posif no element was inserted

Definition at line 1408 of file forward_list.h.

1410 {
1411 if (!if_valid_iterator(pos))
1412 {
1413 return nullptr;
1414 }
1415
1416 iterator iter{ pos.m_current_node };
1417 for (size_type i = 0; i < count; i++)
1418 {
1420 }
1421
1422 return iter;
1423 }

◆ insert_after() [4/5]

template<typename T>
auto dsa::ForwardList< T >::insert_after ( const const_iterator & pos,
std::initializer_list< T > init_list ) -> iterator

Function inserts new Node after specified ForwardList const_iterator.

Parameters
[in]posconst_iterator to insert element after
[in]init_listinitializer_list with elements to insert after pos
Returns
pointer to the last inserted element
Return values
iteratorto last inserted element
posif no element was inserted

Definition at line 1447 of file forward_list.h.

1449 {
1450 if (!if_valid_iterator(pos))
1451 {
1452 return nullptr;
1453 }
1454
1455 iterator iter{ pos.m_current_node };
1456 for (const auto val : init_list)
1457 {
1459 }
1460
1461 return iter;
1462 }

◆ insert_after() [5/5]

template<typename T>
auto dsa::ForwardList< T >::insert_after ( const const_iterator & pos,
T && value ) -> iterator

Function inserts new Node after specified ForwardList iterator.

Parameters
[in]posconst_iterator to insert element after
[in]valueelement of type T to be inserted after pos
Returns
pointer to ForwardList element
Return values
iteratorto inserted value
posif no element was inserted

Definition at line 1393 of file forward_list.h.

1395 {
1396 if (!if_valid_iterator(pos))
1397 {
1398 return nullptr;
1399 }
1400
1401 iterator iter{ pos.m_current_node };
1403
1404 return iter;
1405 }

◆ max_size()

template<typename T>
auto dsa::ForwardList< T >::max_size ( ) const -> size_type
nodiscardnoexcept

Function returns maximum number of elements container can hold.

Returns
size_type maximum number of elements

Definition at line 1362 of file forward_list.h.

1363 {
1365 }

◆ merge() [1/4]

template<typename T>
void dsa::ForwardList< T >::merge ( ForwardList< T > && other)

Function combines two sorted ForwardLists into one sorted ForwardList.

Parameters
[in,out]othercontainer to take elements from

Content of other object will be taken by constructed object

Note
no iterators or references become invalidated, iterators or references of objects moved from other will refer to the same elements of this

Definition at line 1611 of file forward_list.h.

1612 {
1614 }
void merge(ForwardList< T > &other)
Function combines two sorted ForwardLists into one sorted ForwardList.

◆ merge() [2/4]

template<typename T>
template<typename Compare>
void dsa::ForwardList< T >::merge ( ForwardList< T > && other,
Compare comp )

Function combines two sorted ForwardLists into one sorted ForwardList.

Parameters
[in,out]othercontainer to take elements from
[in]compcomparison function object

Content of other object will be taken by constructed object

Note
no iterators or references become invalidated, iterators or references of objects moved from other will refer to the same elements of this

Definition at line 1627 of file forward_list.h.

1628 {
1629 if (&other == this)
1630 {
1631 return;
1632 }
1633
1634 if (m_size != 0)
1635 {
1636 Node* temp_head{ construct_node(nullptr, T{}) };
1638
1639 NodeBase* to_move{};
1641
1642 while (m_head->m_next && other.m_head->m_next)
1643 {
1644 Node* node_this = dynamic_cast<Node*>(m_head->m_next);
1645 Node* node_other = dynamic_cast<Node*>(other.m_head->m_next);
1646
1647 if (node_this && node_other)
1648 {
1649 if (comp(node_this->value(), node_other->value()))
1650 {
1651 to_move = m_head->m_next;
1652 to_return = to_move->m_next;
1653 temp_tail->m_next = to_move;
1654 m_head->m_next = to_return;
1655 }
1656 else
1657 {
1658 to_move = other.m_head->m_next;
1659 to_return = to_move->m_next;
1660 temp_tail->m_next = to_move;
1661 other.m_head->m_next = to_return;
1662 }
1663
1664 temp_tail = temp_tail->m_next;
1665 }
1666 }
1667
1668 if (m_head->m_next == nullptr)
1669 {
1670 temp_tail->m_next = other.m_head->m_next;
1671 other.m_head->m_next = nullptr;
1672 }
1673 else
1674 {
1675 temp_tail->m_next = m_head->m_next;
1676 m_head->m_next = nullptr;
1677 }
1678
1679 m_head->m_next = temp_head->m_next;
1680 destroy_node(temp_head);
1681
1682 m_size += other.m_size;
1683 other.m_size = 0;
1684 }
1685 else
1686 {
1687 swap(other);
1688 }
1689 }
void swap(ForwardList< T > &other) noexcept(std::is_nothrow_swappable_v< T >)
Function swaps content of two ForwardList objects.

◆ merge() [3/4]

template<typename T>
void dsa::ForwardList< T >::merge ( ForwardList< T > & other)

Function combines two sorted ForwardLists into one sorted ForwardList.

Parameters
[in,out]othercontainer to take elements from

Content of other object will be taken by constructed object

Note
no iterators or references become invalidated, iterators or references of objects moved from other will refer to the same elements of this

Definition at line 1605 of file forward_list.h.

1606 {
1608 }

◆ merge() [4/4]

template<typename T>
template<typename Compare>
void dsa::ForwardList< T >::merge ( ForwardList< T > & other,
Compare comp )

Function combines two sorted ForwardLists into one sorted ForwardList.

Parameters
[in,out]othercontainer to take elements from
[in]compcomparison function object

Content of other object will be taken by constructed object

Note
no iterators or references become invalidated, iterators or references of objects moved from other will refer to the same elements of this

Definition at line 1618 of file forward_list.h.

1619 {
1621 }

◆ operator=() [1/2]

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

Constructs ForwardList using copy assignment.

Parameters
[in]otherForwardList object of type T
Returns
ForwardList& reference to ForwardList object

Definition at line 1185 of file forward_list.h.

1186 {
1187 init_node();
1188
1189 if (&other != this)
1190 {
1191 while (m_head->m_next)
1192 {
1193 pop_front();
1194 }
1195
1196 auto iter = before_begin();
1197 for (const auto& item : other)
1198 {
1200 }
1201 }
1202
1203 return *this;
1204 }

◆ operator=() [2/2]

template<typename T>
auto dsa::ForwardList< T >::operator= ( ForwardList< T > && other) -> ForwardList&
noexcept

Assign ForwardList object using move assignment.

Content of other object will be taken by constructed object

Parameters
[in,out]otherForwardList object of type T
Returns
ForwardList& reference to ForwardList object

Definition at line 1213 of file forward_list.h.

1214 {
1215 if (&other != this)
1216 {
1217 clear();
1218 init_node();
1219
1220 m_head->m_next = other.m_head->m_next;
1221 m_size = other.m_size;
1222
1223 other.m_head->m_next = nullptr;
1224 other.m_size = 0;
1225 }
1226
1227 return *this;
1228 }

◆ pop_front()

template<typename T>
void dsa::ForwardList< T >::pop_front ( )

Function removes first Node of ForwardList.

Definition at line 1531 of file forward_list.h.

1532 {
1533 if (!m_head->m_next)
1534 {
1535 return;
1536 }
1537
1538 NodeBase* temp{ m_head->m_next };
1539 if (m_size == 1)
1540 {
1541 m_head->m_next = nullptr;
1542 }
1543 else
1544 {
1545 m_head->m_next = temp->m_next;
1546 }
1547 destroy_node(temp);
1548
1549 m_size--;
1550 }

◆ push_front() [1/2]

template<typename T>
void dsa::ForwardList< T >::push_front ( const_reference value)

Function adds new Node at the beginning of ForwardList.

Parameters
[in]valueelement of type T
Note
no iterators or references are invalidated, if construction of new element fails or an exception is thrown for any reason state of the object does not change and this function has no effect

Definition at line 1511 of file forward_list.h.

1512 {
1514 }

◆ push_front() [2/2]

template<typename T>
void dsa::ForwardList< T >::push_front ( T && value)

Function adds new Node at the beginning of ForwardList.

Parameters
[in]valueelement of type T
Note
no iterators or references are invalidated, if construction of new element fails or an exception is thrown for any reason state of the object does not change and this function has no effect

Definition at line 1517 of file forward_list.h.

1518 {
1520 }

◆ remove()

template<typename T>
auto dsa::ForwardList< T >::remove ( const_reference value) -> size_type

Function removes all elements equal to value.

Parameters
[in]valuevalue of elements to remove
Returns
size_type number of elements removed
Note
invalidates only the iterators and references to the removed elements

Definition at line 1729 of file forward_list.h.

1730 {
1731 return remove_if([value](T node_val) { return node_val == value; });
1732 }
auto remove_if(UnaryPred predicate) -> size_type
Function removes all elements for which predicate returns true.

◆ remove_if()

template<typename T>
template<typename UnaryPred>
auto dsa::ForwardList< T >::remove_if ( UnaryPred predicate) -> size_type

Function removes all elements for which predicate returns true.

Template Parameters
UnaryPred
Parameters
[in]predicateto remove elements
Returns
size_type number of elements removed
Note
invalidates only the iterators and references to the removed elements

Definition at line 1736 of file forward_list.h.

1737 {
1738 NodeBase* temp{ m_head };
1739 NodeBase* next{};
1740
1742
1743 while (temp)
1744 {
1745 next = temp->m_next;
1746
1747 if (Node* node = dynamic_cast<Node*>(next))
1748 {
1749 if (predicate(node->value()))
1750 {
1751 NodeBase* to_remove = temp->m_next;
1752 temp->m_next = to_remove->m_next;
1753 destroy_node(to_remove);
1754
1755 removed_count++;
1756 m_size--;
1757 continue;
1758 }
1759 }
1760
1761 temp = temp->m_next;
1762 }
1763
1764 return removed_count;
1765 }

◆ resize() [1/2]

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

Function resize ForwardList to specified number of elements.

Parameters
[in]countnew size of container

Definition at line 1553 of file forward_list.h.

1554 {
1555 resize(count, T{});
1556 }
void resize(size_type count)
Function resize ForwardList to specified number of elements.

◆ resize() [2/2]

template<typename T>
void dsa::ForwardList< T >::resize ( size_type count,
const_reference value )

Function resize ForwardList to specified number of elements.

Parameters
[in]countcount new size of container
[in]valuevalue to initialize new elements

Definition at line 1559 of file forward_list.h.

1560 {
1561 init_node();
1562
1563 if (count == m_size)
1564 {
1565 return;
1566 }
1567
1568 if (m_size > count)
1569 {
1570 auto iter = begin();
1571 for (size_type i = 0; i < count - 1; i++)
1572 {
1573 ++iter;
1574 }
1575
1576 while (m_size > count)
1577 {
1579 }
1580 }
1581
1582 if (m_size < count)
1583 {
1584 auto iter = find_iter_before_last();
1585 if (iter != before_begin())
1586 {
1587 ++iter;
1588 }
1589
1590 while (m_size < count)
1591 {
1593 }
1594 }
1595 }
auto erase_after(const const_iterator &pos) -> iterator
Function erases Node after specified ForwardList const_iterator.

◆ reverse()

template<typename T>
void dsa::ForwardList< T >::reverse ( )

Function reverts in place Nodes of ForwardList.

Definition at line 1768 of file forward_list.h.

1769 {
1770 NodeBase* temp{ m_head->m_next };
1771
1772 auto iter = find_iter_before_last();
1773 NodeBase* last{ iter.m_current_node->m_next };
1774
1775 m_head->m_next = last;
1776 last = temp;
1777
1778 NodeBase* prev{};
1779 NodeBase* next{};
1780
1781 for (size_type i = 0; i < m_size; i++)
1782 {
1783 next = temp->m_next;
1784 temp->m_next = prev;
1785
1786 prev = temp;
1787 temp = next;
1788 }
1789
1790 m_head->m_next = std::move(prev);
1791 }

◆ size()

template<typename T>
auto dsa::ForwardList< T >::size ( ) const -> size_type
inlinenodiscard

Function returns ForwardList size.

Returns
size_type number of elements in container

Definition at line 988 of file forward_list.h.

989 {
990 return m_size;
991 }

◆ sort() [1/2]

template<typename T>
void dsa::ForwardList< T >::sort ( )

Function sorts the elements and preserves the order of equivalent elements.

elements are compared using operator<

Note
no iterators or references are invalidated, if an exception is thrown, the order of elements is unspecified

Definition at line 1842 of file forward_list.h.

1843 {
1844 m_head->m_next = merge_sort(m_head->m_next, std::less<>());
1845 }

◆ sort() [2/2]

template<typename T>
template<typename Compare>
void dsa::ForwardList< T >::sort ( Compare comp)

Function sorts the elements and preserves the order of equivalent elements.

elements are compared using comp

Template Parameters
Compare
Parameters
[in]compcomparison function object, returns true if the first argument is less than second
Note
no iterators or references are invalidated, if an exception is thrown, the order of elements is unspecified

Definition at line 1849 of file forward_list.h.

1850 {
1851 m_head->m_next = merge_sort(m_head->m_next, comp);
1852 }

◆ splice_after() [1/6]

template<typename T>
void dsa::ForwardList< T >::splice_after ( const const_iterator & pos,
ForwardList< T > & other )

Function moves elements from other ForwardList object.

Content of other object will be taken by constructed object

Parameters
[in]posconst_iterator after which content of other container will be inserted
[in,out]othercontainer to take elements from
Note
no iterators or references become invalidated, iterators or references of objects moved from other will refer to the same elements of this

Definition at line 1692 of file forward_list.h.

1693 {
1694 transfer(pos, std::move(other), other.before_begin(), other.end());
1695 }

◆ splice_after() [2/6]

template<typename T>
void dsa::ForwardList< T >::splice_after ( const const_iterator & pos,
ForwardList< T > & other,
const const_iterator & first,
const const_iterator & last )

Function moves elements from other ForwardList object.

Content of other object will be taken by constructed object

Parameters
[in]posconst_iterator after which content of other container will be inserted
[in,out]othercontainer to take elements from
[in]firstconst_iterator after which elements of other will be taken
[in]lastconst_iterator until which elements of other will be taken
Note
no iterators or references become invalidated, iterators or references of objects moved from other will refer to the same elements of this

Definition at line 1716 of file forward_list.h.

1718 {
1719 transfer(pos, std::move(other), first, last);
1720 }

◆ splice_after() [3/6]

template<typename T>
void dsa::ForwardList< T >::splice_after ( const const_iterator & pos,
ForwardList< T > & other,
const const_iterator & iter )

Function moves elements from other ForwardList object.

Content of other object will be taken by constructed object

Parameters
[in]posconst_iterator after which content of other container will be inserted
[in,out]othercontainer to take elements from
[in]iterconst_iterator after which elements of other will be taken
Note
no iterators or references become invalidated, iterators or references of objects moved from other will refer to the same elements of this

Definition at line 1704 of file forward_list.h.

1705 {
1706 transfer(pos, std::move(other), iter);
1707 }

◆ splice_after() [4/6]

template<typename T>
void dsa::ForwardList< T >::splice_after ( const_iterator pos,
ForwardList< T > && other )

Function moves elements from other ForwardList object.

Content of other object will be taken by constructed object

Parameters
[in]posconst_iterator after which content of other container will be inserted
[in,out]othercontainer to take elements from
Note
no iterators or references become invalidated, iterators or references of objects moved from other will refer to the same elements of this

Definition at line 1698 of file forward_list.h.

1699 {
1700 transfer(pos, std::move(other), other.before_begin(), other.end());
1701 }

◆ splice_after() [5/6]

template<typename T>
void dsa::ForwardList< T >::splice_after ( const_iterator pos,
ForwardList< T > && other,
const_iterator first,
const_iterator last )

Function moves elements from other ForwardList object.

Content of other object will be taken by constructed object

Parameters
[in]posconst_iterator after which content of other container will be inserted
[in,out]othercontainer to take elements from
[in]firstconst_iterator after which elements of other will be taken
[in]lastconst_iterator until which elements of other will be taken
Note
no iterators or references become invalidated, iterators or references of objects moved from other will refer to the same elements of this

Definition at line 1723 of file forward_list.h.

1724 {
1725 transfer(pos, std::move(other), first, last);
1726 }

◆ splice_after() [6/6]

template<typename T>
void dsa::ForwardList< T >::splice_after ( const_iterator pos,
ForwardList< T > && other,
const_iterator iter )

Function moves elements from other ForwardList object.

Content of other object will be taken by constructed object

Parameters
[in]posconst_iterator after which content of other container will be inserted
[in,out]othercontainer to take elements from
[in]iterconst_iterator after which elements of other will be taken
Note
no iterators or references become invalidated, iterators or references of objects moved from other will refer to the same elements of this

Definition at line 1710 of file forward_list.h.

1711 {
1712 transfer(pos, std::move(other), iter);
1713 }

◆ swap()

template<typename T>
void dsa::ForwardList< T >::swap ( ForwardList< T > & other)
noexcept

Function swaps content of two ForwardList objects.

Parameters
[in,out]otherobject to swap content with

Definition at line 1598 of file forward_list.h.

1599 {
1600 std::swap(m_head->m_next, other.m_head->m_next);
1601 std::swap(m_size, other.m_size);
1602 }

◆ unique() [1/2]

template<typename T>
auto dsa::ForwardList< T >::unique ( ) -> size_type

Function removes consecutive duplicated elements.

Only the first occurrence of given element in each group is preserved

Returns
size_type number of elements removed

Definition at line 1794 of file forward_list.h.

1795 {
1796 return unique(std::equal_to<>());
1797 }
auto unique() -> size_type
Function removes consecutive duplicated elements.

◆ unique() [2/2]

template<typename T>
template<typename BinaryPred>
auto dsa::ForwardList< T >::unique ( BinaryPred predicate) -> size_type

Function removes consecutive duplicated elements.

Only the first occurrence of given element in each group is preserved

Template Parameters
BinaryPred
Parameters
[in]predicatebinary predicate which returns true if the element should be treated as equal
Returns
size_type number of elements removed

Definition at line 1801 of file forward_list.h.

1802 {
1803 NodeBase* temp{ m_head };
1804 NodeBase* prev{};
1805 NodeBase* next{};
1806
1808
1809 while (temp)
1810 {
1811 prev = temp;
1812
1813 while (prev)
1814 {
1815 next = prev->m_next;
1816
1817 Node* node_next = dynamic_cast<Node*>(next);
1818 Node* node_temp = dynamic_cast<Node*>(temp);
1819 if (node_next && node_temp)
1820 {
1821 if (predicate(node_next->value(), node_temp->value()))
1822 {
1824 prev->m_next = to_remove->m_next;
1825 destroy_node(to_remove);
1826
1827 removed_count++;
1828 m_size--;
1829 continue;
1830 }
1831 }
1832
1833 prev = prev->m_next;
1834 temp = temp->m_next;
1835 }
1836 }
1837
1838 return removed_count;
1839 }

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