Skip to content

Commit ee1d388

Browse files
Rename detail::iterator, simplify implementation
1 parent 76932a0 commit ee1d388

File tree

4 files changed

+48
-64
lines changed

4 files changed

+48
-64
lines changed

include/beman/any_view/any_view.hpp

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#include <beman/any_view/concepts.hpp>
77
#include <beman/any_view/config.hpp>
88
#include <beman/any_view/detail/intrusive_small_ptr.hpp>
9-
#include <beman/any_view/detail/iterator.hpp>
9+
#include <beman/any_view/detail/any_iterator.hpp>
1010
#include <beman/any_view/detail/view_adaptor.hpp>
1111
#include <beman/any_view/detail/view_interface.hpp>
1212

@@ -46,7 +46,7 @@ template <class ElementT,
4646
class DiffT = std::ptrdiff_t>
4747
class any_view : public std::ranges::view_interface<any_view<ElementT, OptionsV, RefT, RValueRefT, DiffT>> {
4848
using iterator_concept = decltype(detail::get_iterator_concept<OptionsV>());
49-
using iterator = detail::iterator<iterator_concept, ElementT, RefT, RValueRefT, DiffT>;
49+
using iterator = detail::any_iterator<iterator_concept, ElementT, RefT, RValueRefT, DiffT>;
5050
using size_type = std::make_unsigned_t<DiffT>;
5151

5252
static constexpr bool sized = (OptionsV & any_view_options::sized) == any_view_options::sized;
@@ -61,13 +61,10 @@ class any_view : public std::ranges::view_interface<any_view<ElementT, OptionsV,
6161
// inplace storage sufficient for a vtable pointer and a std::vector<T>
6262
detail::intrusive_small_ptr<interface_type, 4 * sizeof(void*)> view_ptr;
6363

64-
template <class RangeT>
65-
using adaptor_type =
66-
detail::view_adaptor<iterator_concept, ElementT, RefT, RValueRefT, DiffT, std::views::all_t<RangeT>>;
67-
6864
template <class RangeT>
6965
static consteval auto get_in_place_adaptor_type() {
70-
return std::in_place_type<adaptor_type<RangeT>>;
66+
return std::in_place_type<
67+
detail::view_adaptor<iterator_concept, ElementT, RefT, RValueRefT, DiffT, std::views::all_t<RangeT>>>;
7168
}
7269

7370
public:
@@ -118,7 +115,7 @@ class any_view : public std::ranges::view_interface<any_view<ElementT, RangeTrai
118115
detail::rvalue_reference_type_or_t<detail::as_rvalue_t<reference_type>, RangeTraitsT>;
119116
using difference_type = detail::difference_type_or_t<std::ptrdiff_t, RangeTraitsT>;
120117
using iterator =
121-
detail::iterator<iterator_concept, ElementT, reference_type, rvalue_reference_type, difference_type>;
118+
detail::any_iterator<iterator_concept, ElementT, reference_type, rvalue_reference_type, difference_type>;
122119
using size_type = std::make_unsigned_t<difference_type>;
123120

124121
static constexpr bool sized = detail::sized_or_v<false, RangeTraitsT>;
@@ -134,17 +131,14 @@ class any_view : public std::ranges::view_interface<any_view<ElementT, RangeTrai
134131
// inplace storage sufficient for a vtable pointer and a std::vector<T>
135132
detail::intrusive_small_ptr<interface_type, 4 * sizeof(void*)> view_ptr;
136133

137-
template <class RangeT>
138-
using adaptor_type = detail::view_adaptor<iterator_concept,
139-
ElementT,
140-
reference_type,
141-
rvalue_reference_type,
142-
difference_type,
143-
std::views::all_t<RangeT>>;
144-
145134
template <class RangeT>
146135
static consteval auto get_in_place_adaptor_type() {
147-
return std::in_place_type<adaptor_type<RangeT>>;
136+
return std::in_place_type<detail::view_adaptor<iterator_concept,
137+
ElementT,
138+
reference_type,
139+
rvalue_reference_type,
140+
difference_type,
141+
std::views::all_t<RangeT>>>;
148142
}
149143

150144
public:
@@ -194,7 +188,7 @@ class any_view : public std::ranges::view_interface<any_view<ElementT, OptionsV>
194188
using rvalue_reference_type = decltype(OptionsV.rvalue_reference_type)::type;
195189
using difference_type = decltype(OptionsV.difference_type)::type;
196190
using iterator =
197-
detail::iterator<iterator_concept, ElementT, reference_type, rvalue_reference_type, difference_type>;
191+
detail::any_iterator<iterator_concept, ElementT, reference_type, rvalue_reference_type, difference_type>;
198192
using size_type = std::make_unsigned_t<difference_type>;
199193

200194
static constexpr bool sized = OptionsV.sized;
@@ -210,17 +204,14 @@ class any_view : public std::ranges::view_interface<any_view<ElementT, OptionsV>
210204
// inplace storage sufficient for a vtable pointer and a std::vector<T>
211205
detail::intrusive_small_ptr<interface_type, 4 * sizeof(void*)> view_ptr;
212206

213-
template <class RangeT>
214-
using adaptor_type = detail::view_adaptor<iterator_concept,
215-
ElementT,
216-
reference_type,
217-
rvalue_reference_type,
218-
difference_type,
219-
std::views::all_t<RangeT>>;
220-
221207
template <class RangeT>
222208
static consteval auto get_in_place_adaptor_type() {
223-
return std::in_place_type<adaptor_type<RangeT>>;
209+
return std::in_place_type<detail::view_adaptor<iterator_concept,
210+
ElementT,
211+
reference_type,
212+
rvalue_reference_type,
213+
difference_type,
214+
std::views::all_t<RangeT>>>;
224215
}
225216

226217
public:

include/beman/any_view/detail/iterator.hpp renamed to include/beman/any_view/detail/any_iterator.hpp

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
22

3-
#ifndef BEMAN_ANY_VIEW_DETAIL_ITERATOR_HPP
4-
#define BEMAN_ANY_VIEW_DETAIL_ITERATOR_HPP
3+
#ifndef BEMAN_ANY_VIEW_DETAIL_ANY_ITERATOR_HPP
4+
#define BEMAN_ANY_VIEW_DETAIL_ANY_ITERATOR_HPP
55

66
#include <beman/any_view/concepts.hpp>
77
#include <beman/any_view/detail/intrusive_small_ptr.hpp>
@@ -16,7 +16,7 @@
1616
namespace beman::any_view::detail {
1717

1818
template <class IterConceptT, class ElementT, class RefT, class RValueRefT, class DiffT>
19-
class iterator {
19+
class any_iterator {
2020
using reference = RefT;
2121
using rvalue_reference = RValueRefT;
2222
using pointer = std::add_pointer_t<RefT>;
@@ -38,16 +38,9 @@ class iterator {
3838
intrusive_small_ptr<interface_type, 3 * sizeof(void*)> iterator_ptr;
3939

4040
template <class IteratorT, class SentinelT>
41-
using adaptor_type = detail::iterator_adaptor<ElementT, reference, rvalue_reference, DiffT, IteratorT, SentinelT>;
42-
43-
template <class IteratorT, class SentinelT>
44-
static constexpr auto get_noexcept() {
45-
return std::is_nothrow_constructible_v<adaptor_type<IteratorT, SentinelT>, IteratorT, SentinelT>;
46-
}
47-
48-
template <class IteratorT, class SentinelT>
49-
static constexpr auto get_in_place_adaptor_type() {
50-
return std::in_place_type<adaptor_type<IteratorT, SentinelT>>;
41+
static consteval auto get_in_place_adaptor_type() {
42+
return std::in_place_type<
43+
detail::iterator_adaptor<ElementT, reference, rvalue_reference, DiffT, IteratorT, SentinelT>>;
5144
}
5245

5346
public:
@@ -56,28 +49,28 @@ class iterator {
5649
using difference_type = DiffT;
5750

5851
template <detail::iterator_compatible_with<range_traits> IteratorT, std::sentinel_for<IteratorT> SentinelT>
59-
constexpr iterator(IteratorT iterator, SentinelT sentinel) noexcept(get_noexcept<IteratorT, SentinelT>())
52+
constexpr any_iterator(IteratorT iterator, SentinelT sentinel)
6053
: iterator_ptr(get_in_place_adaptor_type<IteratorT, SentinelT>(), std::move(iterator), std::move(sentinel)) {}
6154

62-
constexpr iterator() noexcept
55+
constexpr any_iterator() noexcept
6356
requires forward
64-
: iterator(pointer(nullptr), pointer(nullptr)) {}
57+
: any_iterator(pointer(nullptr), pointer(nullptr)) {}
6558

66-
constexpr iterator(const iterator&)
59+
constexpr any_iterator(const any_iterator&)
6760
requires forward
6861
= default;
6962

70-
constexpr iterator(iterator&&) noexcept = default;
63+
constexpr any_iterator(any_iterator&&) noexcept = default;
7164

72-
constexpr auto operator=(const iterator&) -> iterator&
65+
constexpr auto operator=(const any_iterator&) -> any_iterator&
7366
requires forward
7467
= default;
7568

76-
constexpr auto operator=(iterator&&) noexcept -> iterator& = default;
69+
constexpr auto operator=(any_iterator&&) noexcept -> any_iterator& = default;
7770

7871
[[nodiscard]] constexpr auto operator*() const -> reference { return **iterator_ptr; }
7972

80-
friend constexpr auto iter_move(const iterator& other) -> rvalue_reference {
73+
[[nodiscard]] friend constexpr auto iter_move(const any_iterator& other) -> rvalue_reference {
8174
return other.iterator_ptr->iter_move();
8275
}
8376

@@ -87,83 +80,83 @@ class iterator {
8780
return std::to_address(*iterator_ptr);
8881
}
8982

90-
constexpr auto operator++() -> iterator& {
83+
constexpr auto operator++() -> any_iterator& {
9184
++*iterator_ptr;
9285
return *this;
9386
}
9487

9588
constexpr auto operator++(int) -> void { ++*this; }
9689

97-
[[nodiscard]] constexpr auto operator++(int) -> iterator
90+
[[nodiscard]] constexpr auto operator++(int) -> any_iterator
9891
requires forward
9992
{
10093
const auto other = *this;
10194
++*this;
10295
return other;
10396
}
10497

105-
[[nodiscard]] constexpr auto operator==(const iterator& other) const -> bool
98+
[[nodiscard]] constexpr auto operator==(const any_iterator& other) const -> bool
10699
requires forward
107100
{
108101
return *iterator_ptr == *other.iterator_ptr;
109102
}
110103

111-
constexpr auto operator--() -> iterator&
104+
constexpr auto operator--() -> any_iterator&
112105
requires bidirectional
113106
{
114107
--*iterator_ptr;
115108
return *this;
116109
}
117110

118-
[[nodiscard]] constexpr auto operator--(int) -> iterator
111+
[[nodiscard]] constexpr auto operator--(int) -> any_iterator
119112
requires bidirectional
120113
{
121114
const auto other = *this;
122115
--*this;
123116
return other;
124117
}
125118

126-
[[nodiscard]] constexpr auto operator<=>(const iterator& other) const -> std::partial_ordering
119+
[[nodiscard]] constexpr auto operator<=>(const any_iterator& other) const -> std::partial_ordering
127120
requires random_access
128121
{
129122
return *iterator_ptr <=> *other.iterator_ptr;
130123
}
131124

132-
[[nodiscard]] constexpr auto operator-(const iterator& other) const -> difference_type
125+
[[nodiscard]] constexpr auto operator-(const any_iterator& other) const -> difference_type
133126
requires random_access
134127
{
135128
return *iterator_ptr - *other.iterator_ptr;
136129
}
137130

138-
constexpr auto operator+=(difference_type offset) -> iterator&
131+
constexpr auto operator+=(difference_type offset) -> any_iterator&
139132
requires random_access
140133
{
141134
*iterator_ptr += offset;
142135
return *this;
143136
}
144137

145-
[[nodiscard]] constexpr auto operator+(difference_type offset) const -> iterator
138+
[[nodiscard]] constexpr auto operator+(difference_type offset) const -> any_iterator
146139
requires random_access
147140
{
148141
auto other = *this;
149142
other += offset;
150143
return other;
151144
}
152145

153-
[[nodiscard]] friend constexpr auto operator+(difference_type offset, const iterator& other) -> iterator
146+
[[nodiscard]] friend constexpr auto operator+(difference_type offset, const any_iterator& other) -> any_iterator
154147
requires random_access
155148
{
156149
return other + offset;
157150
}
158151

159-
constexpr auto operator-=(difference_type offset) -> iterator&
152+
constexpr auto operator-=(difference_type offset) -> any_iterator&
160153
requires random_access
161154
{
162155
*iterator_ptr -= offset;
163156
return *this;
164157
}
165158

166-
[[nodiscard]] constexpr auto operator-(difference_type offset) const -> iterator
159+
[[nodiscard]] constexpr auto operator-(difference_type offset) const -> any_iterator
167160
requires random_access
168161
{
169162
auto other = *this;
@@ -184,4 +177,4 @@ class iterator {
184177

185178
} // namespace beman::any_view::detail
186179

187-
#endif // BEMAN_ANY_VIEW_DETAIL_ITERATOR_HPP
180+
#endif // BEMAN_ANY_VIEW_DETAIL_ANY_ITERATOR_HPP

include/beman/any_view/detail/view_adaptor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace beman::any_view::detail {
1313

1414
template <class IterConceptT, class ElementT, class RefT, class RValueRefT, class DiffT, std::ranges::view ViewT>
1515
class view_adaptor : public view_interface<IterConceptT, ElementT, RefT, RValueRefT, DiffT> {
16-
using iterator = detail::iterator<IterConceptT, ElementT, RefT, RValueRefT, DiffT>;
16+
using iterator = detail::any_iterator<IterConceptT, ElementT, RefT, RValueRefT, DiffT>;
1717
using size_type = std::make_unsigned_t<DiffT>;
1818

1919
[[no_unique_address]] ViewT view;

include/beman/any_view/detail/view_interface.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
#ifndef BEMAN_ANY_VIEW_DETAIL_VIEW_INTERFACE_HPP
44
#define BEMAN_ANY_VIEW_DETAIL_VIEW_INTERFACE_HPP
55

6-
#include <beman/any_view/detail/iterator.hpp>
6+
#include <beman/any_view/detail/any_iterator.hpp>
77

88
namespace beman::any_view::detail {
99

1010
template <class IterConceptT, class ElementT, class RefT, class RValueRefT, class DiffT>
1111
class view_interface {
12-
using iterator = detail::iterator<IterConceptT, ElementT, RefT, RValueRefT, DiffT>;
12+
using iterator = detail::any_iterator<IterConceptT, ElementT, RefT, RValueRefT, DiffT>;
1313
using size_type = std::make_unsigned_t<DiffT>;
1414

1515
public:

0 commit comments

Comments
 (0)