tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
advance.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2020 Tobias Hienzsch
3
4#ifndef TETL_ITERATOR_ADVANCE_HPP
5#define TETL_ITERATOR_ADVANCE_HPP
6
7#include <etl/_iterator/iterator_traits.hpp>
8#include <etl/_iterator/tags.hpp>
9#include <etl/_type_traits/is_base_of.hpp>
10
11namespace etl {
12
13/// Increments given iterator it by n elements.
14///
15/// If n is negative, the iterator is decremented. In this case,
16/// InputIt must meet the requirements of LegacyBidirectionalIterator,
17/// otherwise the behavior is undefined.
18///
19/// https://en.cppreference.com/w/cpp/iterator/advance
20///
21/// \ingroup iterator
22template <typename It, typename Distance>
23constexpr auto advance(It& it, Distance n) -> void
24{
25 using category = typename iterator_traits<It>::iterator_category;
26 static_assert(is_base_of_v<input_iterator_tag, category>);
27
28 auto dist = typename iterator_traits<It>::difference_type(n);
29 if constexpr (is_base_of_v<random_access_iterator_tag, category>) {
30 it += dist;
31 } else {
32 while (dist > 0) {
33 --dist;
34 ++it;
35 }
36 if constexpr (is_base_of_v<bidirectional_iterator_tag, category>) {
37 while (dist < 0) {
38 ++dist;
39 --it;
40 }
41 }
42 }
43}
44
45} // namespace etl
46
47#endif // TETL_ITERATOR_ADVANCE_HPP
constexpr auto advance(It &it, Distance n) -> void
Increments given iterator it by n elements.
Definition advance.hpp:23
Definition adjacent_find.hpp:9
Defines the category of an iterator. Each tag is an empty type and corresponds to one of the five (un...
Definition tags.hpp:31
Defines the category of an iterator. Each tag is an empty type and corresponds to one of the five (un...
Definition tags.hpp:13
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