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
22// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
23/// @brief Implementation helper for @c arene::base::iterator::distance for random-access iterators.
24/// @tparam Iterator the type of the iterator
25/// @param first The first iterator in the range
26/// @param last The last iterator in the range
27/// @return The number of times @c first must be incremented to reach @c last
28template <typename Iterator>
29constexpr auto do_distance(
30 Iterator first,
31 Iterator last,
32 std::random_access_iterator_tag
33) noexcept(noexcept(last - first)) //
34 -> typename std::iterator_traits<Iterator>::difference_type {
35 // parasoft-begin-suppress AUTOSAR-M5_0_15-a-2 "False positive: first is an iterator, bounded by last"
36 return last - first;
37 // parasoft-end-suppress AUTOSAR-M5_0_15-a-2
38}
39// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
40
41/// @brief Implementation helper for @c arene::base::iterator::distance for input iterators.
42/// @tparam Iterator the type of the iterator
43/// @param first The first iterator in the range
44/// @param last The last iterator in the range
45/// @return The number of times @c first must be incremented to reach @c last
46template <typename Iterator>
47constexpr auto do_distance(
48 Iterator first,
49 Iterator last,
50 std::input_iterator_tag
51) noexcept(noexcept(first != last) && noexcept(++first)) //
52 -> typename std::iterator_traits<Iterator>::difference_type {
53 typename std::iterator_traits<Iterator>::difference_type result{0};
54 while (first != last) {
55 ++first;
56 ++result;
57 }
58 return result;
59}
60
61// parasoft-end-suppress CERT_C-EXP37-a
62
63} // namespace distance_impl
64
65// parasoft-begin-suppress AUTOSAR-M3_3_2-a-2 "False positive: inline function used in multiple translation units"
66/// @brief Find the number of elements between @c first and @c last.
67/// @tparam Iterator The type of iterator to compute the distance between
68/// @param first The iterator to start the distance computation from
69/// @param last The iterator end the distance computation at
70/// @return The number of elements needed to go from @c first to @c last.
71/// For iterators satisfying @c random_access_iterator, this value
72/// can be negative if @c first is reachable from @c last.
73/// @pre @c last must be reachable from @c first, or else the behavior is
74/// undefined.
75template <typename Iterator>
82// parasoft-end-suppress AUTOSAR-M3_3_2-a-2
83
84} // namespace base
85} // namespace arene
86
87#endif // INCLUDE_GUARD_ARENE_BASE_ARENE_BASE_ITERATOR_DISTANCE_HPP_
Definition array_exceptions_disabled.cpp:11
Copyright 2026, Toyota Motor Corporation.
Definition array_exceptions_disabled.cpp:10