tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
reverse.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2019 Tobias Hienzsch
3
4#ifndef TETL_ALGORITHM_REVERSE_HPP
5#define TETL_ALGORITHM_REVERSE_HPP
6
7#include <etl/_algorithm/iter_swap.hpp>
8#include <etl/_iterator/iterator_traits.hpp>
9#include <etl/_iterator/tags.hpp>
10#include <etl/_type_traits/is_base_of.hpp>
11
12namespace etl {
13
14/// \brief Reverses the order of the elements in the range `[first, last)`.
15/// \ingroup algorithm
16template <typename BidirIt>
17constexpr auto reverse(BidirIt first, BidirIt last) -> void
18{
19 using category = typename etl::iterator_traits<BidirIt>::iterator_category;
20 if constexpr (etl::is_base_of_v<etl::random_access_iterator_tag, category>) {
21 if (first == last) {
22 return;
23 }
24
25 for (--last; first < last; (void)++first, --last) {
26 etl::iter_swap(first, last);
27 }
28 } else {
29 while (first != last and first != --last) {
30 etl::iter_swap(first++, last);
31 }
32 }
33}
34
35} // namespace etl
36
37#endif // TETL_ALGORITHM_REVERSE_HPP
constexpr auto reverse(BidirIt first, BidirIt last) -> void
Reverses the order of the elements in the range [first, last).
Definition reverse.hpp:17
Definition adjacent_find.hpp:9
iterator_traits is the type trait class that provides uniform interface to the properties of LegacyIt...
Definition iterator_traits.hpp:48
Defines the category of an iterator. Each tag is an empty type and corresponds to one of the five (un...
Definition tags.hpp:37