Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
distance.hpp
Go to the documentation of this file.
1// Copyright 2024, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ITERATOR_DISTANCE_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ITERATOR_DISTANCE_HPP_
7
8// IWYU pragma: private, include "arene/base/iterator.hpp"
9// IWYU pragma: friend "(arene/base(?!/tests)|stdlib/include/stdlib_detail)/.*"
10
11// parasoft-begin-suppress AUTOSAR-A16_2_2-a-2 "Arene Base aggregate headers permitted by A16-2-2 Permit #1"
12#include "arene/base/stdlib_choice/iterator_tags.hpp"
13#include "arene/base/stdlib_choice/iterator_traits.hpp"
14
15namespace arene {
16namespace base {
17
18namespace distance_impl {
19
20// parasoft-begin-suppress CERT_C-EXP37-a "False positive: The rule does not mention naming all parameters"
21// parasoft-begin-suppress AUTOSAR-A5_0_3-a "False positive: There is no pointer indirection in return types"
22
23// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
24/// @brief Implementation helper for @c arene::base::iterator::distance for random-access iterators.
25/// @tparam Iterator the type of the iterator
26/// @param first The first iterator in the range
27/// @param last The last iterator in the range
28/// @return The number of times @c first must be incremented to reach @c last
29template <typename Iterator>
30constexpr auto do_distance(
31 Iterator first,
32 Iterator last,
33 std::random_access_iterator_tag
34) noexcept(noexcept(last - first)) //
35 -> typename std::iterator_traits<Iterator>::difference_type {
36 // parasoft-begin-suppress AUTOSAR-M5_0_15-a-2 "False positive: first is an iterator, bounded by last"
37 return last - first;
38 // parasoft-end-suppress AUTOSAR-M5_0_15-a-2
39}
40// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
41
42// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: inline function used in multiple translation units"
43/// @brief Implementation helper for @c arene::base::iterator::distance for input iterators.
44/// @tparam Iterator the type of the iterator
45/// @param first The first iterator in the range
46/// @param last The last iterator in the range
47/// @return The number of times @c first must be incremented to reach @c last
48template <typename Iterator>
49constexpr auto do_distance(
50 Iterator first,
51 Iterator last,
52 std::input_iterator_tag
53) noexcept(noexcept(first != last) && noexcept(++first)) //
54 -> typename std::iterator_traits<Iterator>::difference_type {
55 typename std::iterator_traits<Iterator>::difference_type result{0};
56 while (first != last) {
57 ++first;
58 ++result;
59 }
60 return result;
61}
62// parasoft-end-suppress AUTOSAR-M3_3_2-a
63
64// parasoft-end-suppress CERT_C-EXP37-a
65
66} // namespace distance_impl
67
68// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
69/// @brief Find the number of elements between @c first and @c last.
70/// @tparam Iterator The type of iterator to compute the distance between
71/// @param first The iterator to start the distance computation from
72/// @param last The iterator end the distance computation at
73/// @return The number of elements needed to go from @c first to @c last.
74/// For iterators satisfying @c random_access_iterator, this value
75/// can be negative if @c first is reachable from @c last.
76/// @pre @c last must be reachable from @c first, or else the behavior is
77/// undefined.
78template <typename Iterator>
79constexpr auto distance(Iterator first, Iterator last) noexcept(
80 noexcept(distance_impl::do_distance(first, last, typename std::iterator_traits<Iterator>::iterator_category{}))
81) //
83 return distance_impl::do_distance(first, last, typename std::iterator_traits<Iterator>::iterator_category{});
84}
85// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
86// parasoft-end-suppress AUTOSAR-A5_0_3-a "False positive: There is no pointer indirection in return types"
87
88} // namespace base
89} // namespace arene
90
91#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ITERATOR_DISTANCE_HPP_
provides a uniform interface to the properties of an iterator
Definition iterator_traits.hpp:90
Definition accumulate.hpp:37
constexpr auto distance(Iterator first, Iterator last) noexcept(noexcept(distance_impl::do_distance(first, last, typename std::iterator_traits< Iterator >::iterator_category{}))) -> typename std::iterator_traits< Iterator >::difference_type
Find the number of elements between first and last.
Definition distance.hpp:79
Definition accumulate.hpp:36