tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
distance.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_ITERATOR_DISTANCE_HPP
5#define TETL_ITERATOR_DISTANCE_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/// \brief Returns the number of hops from first to last.
14/// \details https://en.cppreference.com/w/cpp/iterator/distance
15/// \ingroup iterator
16template <typename It>
17constexpr auto distance(It first, It last) -> typename iterator_traits<It>::difference_type
18{
19 using category = typename iterator_traits<It>::iterator_category;
20 static_assert(is_base_of_v<input_iterator_tag, category>);
21
22 if constexpr (is_base_of_v<random_access_iterator_tag, category>) {
23 return last - first;
24 } else {
25 auto result = typename iterator_traits<It>::difference_type{};
26 while (first != last) {
27 ++first;
28 ++result;
29 }
30 return result;
31 }
32}
33
34} // namespace etl
35
36#endif // TETL_ITERATOR_DISTANCE_HPP
constexpr auto distance(It first, It last) -> typename iterator_traits< It >::difference_type
Returns the number of hops from first to last.
Definition distance.hpp:17
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: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