• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

STEllAR-GROUP / hpx / #882

31 Aug 2023 07:44PM UTC coverage: 41.798% (-44.7%) from 86.546%
#882

push

19442 of 46514 relevant lines covered (41.8%)

126375.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

82.35
/libs/core/iterator_support/include/hpx/iterator_support/counting_iterator.hpp
1
//  Copyright (c) 2020-2025 Hartmut Kaiser
2
//
3
//  SPDX-License-Identifier: BSL-1.0
4
//  Distributed under the Boost Software License, Version 1.0. (See accompanying
5
//  file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6
//
7
//  This code is based on boost::iterators::counting_iterator
8
//  Copyright David Abrahams 2003.
9

10
#pragma once
11

12
#include <hpx/config.hpp>
13
#include <hpx/iterator_support/iterator_adaptor.hpp>
14
#include <hpx/iterator_support/iterator_range.hpp>
15
#include <hpx/iterator_support/range.hpp>
16
#include <hpx/iterator_support/traits/is_range.hpp>
17
#include <hpx/modules/type_support.hpp>
18

19
#include <cstddef>
20
#include <cstdint>
21
#include <iterator>
22
#include <type_traits>
23

24
namespace hpx::util {
25

26
    HPX_CXX_EXPORT template <typename Incrementable,
27
        typename CategoryOrTraversal = void, typename Difference = void,
28
        typename Enable = void>
29
    class counting_iterator;
30

31
    namespace detail {
32

33
        template <typename Incrementable, typename CategoryOrTraversal,
34
            typename Difference>
35
        struct counting_iterator_base
36
        {
37
            // calculate category of the resulting iterator
38
            template <typename Iterator>
39
            struct iterator_category
40
            {
41
                using type =
42
                    typename std::iterator_traits<Iterator>::iterator_category;
43
            };
44

45
            using base_traversal =
46
                util::lazy_conditional<std::is_integral_v<Incrementable>,
47
                    hpx::type_identity<std::random_access_iterator_tag>,
48
                    iterator_category<Incrementable>>;
49

50
            using traversal =
51
                util::lazy_conditional_t<std::is_void_v<CategoryOrTraversal>,
52
                    base_traversal, hpx::type_identity<CategoryOrTraversal>>;
53

54
            // calculate difference_type of the resulting iterator
55
            template <typename Integer>
56
            struct integer_difference_type
57
            {
58
                using type = std::conditional_t<(sizeof(Integer) >=
59
                                                    sizeof(std::intmax_t)),
60

61
                    std::conditional_t<std::is_signed_v<Integer>, Integer,
62
                        std::intmax_t>,
63

64
                    std::conditional_t<(sizeof(Integer) <
65
                                           sizeof(std::ptrdiff_t)),
66
                        std::ptrdiff_t, std::intmax_t>>;
67
            };
68

69
            template <typename Iterator>
70
            struct iterator_difference_type
71
            {
72
                using type =
73
                    typename std::iterator_traits<Iterator>::difference_type;
74
            };
75

76
            using base_difference =
77
                util::lazy_conditional<std::is_integral_v<Incrementable>,
78
                    integer_difference_type<Incrementable>,
79
                    iterator_difference_type<Incrementable>>;
80

81
            using difference =
82
                util::lazy_conditional_t<std::is_void_v<Difference>,
83
                    base_difference, hpx::type_identity<Difference>>;
84

85
            using type = iterator_adaptor<counting_iterator<Incrementable,
86
                                              CategoryOrTraversal, Difference>,
87
                Incrementable, Incrementable, traversal, Incrementable const&,
88
                difference>;
89
        };
90
    }    // namespace detail
91

92
    ////////////////////////////////////////////////////////////////////////////
93
    // specialization for Iterators (non-integral types)
94
    HPX_CXX_EXPORT template <typename Incrementable,
95
        typename CategoryOrTraversal, typename Difference, typename Enable>
96
    class counting_iterator
97
      : public detail::counting_iterator_base<Incrementable,
98
            CategoryOrTraversal, Difference>::type
99
    {
100
    private:
101
        using base_type = typename detail::counting_iterator_base<Incrementable,
102
            CategoryOrTraversal, Difference>::type;
103

104
        friend class iterator_core_access;
105

106
    public:
107
        counting_iterator() = default;
108
        ~counting_iterator() = default;
109
        counting_iterator(counting_iterator&& rhs) = default;
110
        counting_iterator& operator=(counting_iterator&& rhs) = default;
111
        counting_iterator(counting_iterator const& rhs) = default;
112
        counting_iterator& operator=(counting_iterator const& rhs) = default;
113

114
        HPX_HOST_DEVICE explicit constexpr counting_iterator(Incrementable x)
115
          : base_type(x)
116
        {
117
        }
118

119
    private:
120
        HPX_HOST_DEVICE constexpr typename base_type::reference dereference()
121
            const
122
        {
123
            return this->base_reference();
124
        }
125
    };
126

127
    HPX_CXX_EXPORT template <typename Incrementable,
128
        typename CategoryOrTraversal, typename Difference>
129
    class counting_iterator<Incrementable, CategoryOrTraversal, Difference,
130
        std::enable_if_t<std::is_integral_v<Incrementable>>>
131
      : public detail::counting_iterator_base<Incrementable,
132
            CategoryOrTraversal, Difference>::type
133
    {
134
    private:
135
        using base_type = typename detail::counting_iterator_base<Incrementable,
136
            CategoryOrTraversal, Difference>::type;
137

138
        friend class iterator_core_access;
139

140
    public:
141
        counting_iterator() = default;
142
        ~counting_iterator() = default;
143
        counting_iterator(counting_iterator&& rhs) = default;
144
        counting_iterator& operator=(counting_iterator&& rhs) = default;
145
        counting_iterator(counting_iterator const& rhs) = default;
146
        counting_iterator& operator=(counting_iterator const& rhs) = default;
147

148
        HPX_HOST_DEVICE explicit constexpr counting_iterator(
149
            Incrementable x) noexcept
150
          : base_type(x)
151
        {
152
        }
153

154
    private:
155
        template <typename Iterator>
156
        HPX_HOST_DEVICE constexpr bool equal(Iterator const& rhs) const noexcept
1✔
157
        {
158
            return this->base() == rhs.base();
1✔
159
        }
160

161
        HPX_HOST_DEVICE void increment() noexcept
10✔
162
        {
163
            ++this->base_reference();
10✔
164
        }
10✔
165

166
        HPX_HOST_DEVICE void decrement() noexcept
×
167
        {
168
            --this->base_reference();
×
169
        }
×
170

171
        template <typename Distance>
172
        HPX_HOST_DEVICE void advance(Distance n) noexcept
10,010✔
173
        {
174
            this->base_reference() +=
10,010✔
175
                static_cast<typename base_type::base_type>(n);
10,010✔
176
        }
10,010✔
177

178
        HPX_HOST_DEVICE constexpr typename base_type::reference dereference()
10,007✔
179
            const noexcept
180
        {
181
            return this->base_reference();
10,007✔
182
        }
183

184
        template <typename OtherIncrementable>
185
        HPX_HOST_DEVICE typename base_type::difference_type distance_to(
1✔
186
            counting_iterator<OtherIncrementable, CategoryOrTraversal,
187
                Difference> const& y) const noexcept
188
        {
189
            using difference_type = typename base_type::difference_type;
190
            return static_cast<difference_type>(y.base()) -
1✔
191
                static_cast<difference_type>(this->base());
1✔
192
        }
193
    };
194

195
    // Manufacture a counting iterator for an arbitrary incrementable type
196
    HPX_CXX_EXPORT template <typename Incrementable>
197
    counting_iterator(Incrementable x) -> counting_iterator<Incrementable>;
198

199
    template <typename Incrementable>
200
    HPX_DEPRECATED_V(1, 9,
201
        "hpx::util::make_counting_iterator is deprecated, use "
202
        "hpx::util::counting_iterator instead")
203
    HPX_HOST_DEVICE
204
        constexpr counting_iterator<Incrementable> make_counting_iterator(
205
            Incrementable x) noexcept
206
    {
207
        return counting_iterator<Incrementable>(HPX_MOVE(x));
208
    }
209
}    // namespace hpx::util
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc