Skip to content

Commit f4dcff3

Browse files
committed
review
review benchmark format fix comma fix comma lwg4482 rebase error [LLVM][CMake] Build examples for llvm-test-depends Build examples and example plug-ins by default when running tests. If examples are unwanted, they can still be disabled completely using LLVM_INCLUDE_EXAMPLES=OFF. Plugin tests depend on examples and it is beneficial to test them by default. By default, Examples will still not be included in the default target or be installed, this remains controlled by LLVM_BUILD_EXAMPLES (which defaults to OFF). The additional cost for building examples for tests is 17 compilation units (12 C++, 5 C), which should be tolerable. I don't know how broken the examples currently are in the various build configurations, but if we find breakage, it would be good to fix it. Pull Request: #171998 Update libcxx/include/__ranges/adjacent_view.h Co-authored-by: Louis Dionne <ldionne.2@gmail.com> ?
1 parent 2739a8d commit f4dcff3

22 files changed

+310
-181
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,6 @@ set(files
744744
__ranges/view_interface.h
745745
__ranges/views.h
746746
__ranges/zip_transform_view.h
747-
__ranges/zip_utils.h
748747
__ranges/zip_view.h
749748
__split_buffer
750749
__std_mbstate_t.h
@@ -790,6 +789,7 @@ set(files
790789
__tuple/tuple_like.h
791790
__tuple/tuple_like_no_subrange.h
792791
__tuple/tuple_size.h
792+
__tuple/tuple_transform.h
793793
__type_traits/add_cv_quals.h
794794
__type_traits/add_pointer.h
795795
__type_traits/add_reference.h

libcxx/include/__ranges/adjacent_view.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
#include <__ranges/range_adaptor.h>
3636
#include <__ranges/size.h>
3737
#include <__ranges/view_interface.h>
38-
#include <__ranges/zip_utils.h>
38+
#include <__tuple/tuple_transform.h>
3939
#include <__type_traits/common_type.h>
4040
#include <__type_traits/is_nothrow_constructible.h>
4141
#include <__type_traits/make_unsigned.h>
@@ -95,7 +95,7 @@ class adjacent_view : public view_interface<adjacent_view<_View, _Np>> {
9595
}
9696

9797
_LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
98-
requires range<const _View> // todo: this seems under-constrained. lwg issue?
98+
requires range<const _View> // LWG4482 This is under-constrained.
9999
{
100100
return __iterator<true>(ranges::begin(__base_), ranges::end(__base_));
101101
}
@@ -111,7 +111,7 @@ class adjacent_view : public view_interface<adjacent_view<_View, _Np>> {
111111
}
112112

113113
_LIBCPP_HIDE_FROM_ABI constexpr auto end() const
114-
requires range<const _View>
114+
requires range<const _View> // LWG4482 This is under-constrained.
115115
{
116116
if constexpr (common_range<const _View>) {
117117
return __iterator<true>(__as_sentinel{}, ranges::begin(__base_), ranges::end(__base_));
@@ -151,7 +151,7 @@ class adjacent_view<_View, _Np>::__iterator {
151151

152152
_LIBCPP_HIDE_FROM_ABI constexpr __iterator(iterator_t<_Base> __first, sentinel_t<_Base> __last) {
153153
__current_[0] = __first;
154-
for (int __i = 1; __i < static_cast<int>(_Np); ++__i) {
154+
for (size_t __i = 1; __i < _Np; ++__i) {
155155
__current_[__i] = ranges::next(__current_[__i - 1], 1, __last);
156156
}
157157
}
@@ -180,8 +180,11 @@ class adjacent_view<_View, _Np>::__iterator {
180180
return forward_iterator_tag{};
181181
}
182182

183+
template <class _Tp, size_t _Index>
184+
using __always = _Tp;
185+
183186
template <class _Tp, size_t... _Is>
184-
static auto __repeat_tuple_helper(index_sequence<_Is...>) -> tuple<decltype((_Is, std::declval<_Tp (*)()>()()))...>;
187+
static auto __repeat_tuple_helper(index_sequence<_Is...>) -> tuple<__always<_Tp, _Is>...>;
185188

186189
public:
187190
using iterator_category = input_iterator_tag;
@@ -191,11 +194,11 @@ class adjacent_view<_View, _Np>::__iterator {
191194

192195
_LIBCPP_HIDE_FROM_ABI __iterator() = default;
193196
_LIBCPP_HIDE_FROM_ABI constexpr __iterator(__iterator<!_Const> __i)
194-
requires _Const && convertible_to<iterator_t<_View>, iterator_t<_Base>>
197+
requires _Const && convertible_to<iterator_t<_View>, iterator_t<const _View>>
195198
: __iterator(std::move(__i), make_index_sequence<_Np>{}) {}
196199

197200
_LIBCPP_HIDE_FROM_ABI constexpr auto operator*() const {
198-
return ranges::__tuple_transform([](auto& __i) -> decltype(auto) { return *__i; }, __current_);
201+
return std::__tuple_transform([](auto& __i) -> decltype(auto) { return *__i; }, __current_);
199202
}
200203

201204
_LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {
@@ -249,7 +252,7 @@ class adjacent_view<_View, _Np>::__iterator {
249252
_LIBCPP_HIDE_FROM_ABI constexpr auto operator[](difference_type __n) const
250253
requires random_access_range<_Base>
251254
{
252-
return ranges::__tuple_transform([&](auto& __i) -> decltype(auto) { return __i[__n]; }, __current_);
255+
return std::__tuple_transform([&](auto& __i) -> decltype(auto) { return __i[__n]; }, __current_);
253256
}
254257

255258
_LIBCPP_HIDE_FROM_ABI friend constexpr bool operator==(const __iterator& __x, const __iterator& __y) {
@@ -317,7 +320,7 @@ class adjacent_view<_View, _Np>::__iterator {
317320
_LIBCPP_HIDE_FROM_ABI friend constexpr auto iter_move(const __iterator& __i) noexcept(
318321
noexcept(ranges::iter_move(std::declval<const iterator_t<_Base>&>())) &&
319322
is_nothrow_move_constructible_v<range_rvalue_reference_t<_Base>>) {
320-
return ranges::__tuple_transform(ranges::iter_move, __i.__current_);
323+
return std::__tuple_transform(ranges::iter_move, __i.__current_);
321324
}
322325

323326
_LIBCPP_HIDE_FROM_ABI friend constexpr void iter_swap(const __iterator& __l, const __iterator& __r) noexcept(

libcxx/include/__ranges/zip_view.h

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10-
#ifndef _LIBCPP___RANGES_ZIP_UTILS_H
11-
#define _LIBCPP___RANGES_ZIP_UTILS_H
10+
#ifndef _LIBCPP___RANGES_ZIP_VIEW_H
11+
#define _LIBCPP___RANGES_ZIP_VIEW_H
1212

1313
#include <__config>
1414

@@ -31,7 +31,7 @@
3131
#include <__ranges/enable_borrowed_range.h>
3232
#include <__ranges/size.h>
3333
#include <__ranges/view_interface.h>
34-
#include <__ranges/zip_utils.h>
34+
#include <__tuple/tuple_transform.h>
3535
#include <__type_traits/is_nothrow_constructible.h>
3636
#include <__type_traits/make_unsigned.h>
3737
#include <__utility/declval.h>
@@ -137,36 +137,36 @@ class zip_view : public view_interface<zip_view<_Views...>> {
137137
_LIBCPP_HIDE_FROM_ABI constexpr auto begin()
138138
requires(!(__simple_view<_Views> && ...))
139139
{
140-
return __iterator<false>(ranges::__tuple_transform(ranges::begin, __views_));
140+
return __iterator<false>(std::__tuple_transform(ranges::begin, __views_));
141141
}
142142

143143
_LIBCPP_HIDE_FROM_ABI constexpr auto begin() const
144144
requires(range<const _Views> && ...)
145145
{
146-
return __iterator<true>(ranges::__tuple_transform(ranges::begin, __views_));
146+
return __iterator<true>(std::__tuple_transform(ranges::begin, __views_));
147147
}
148148

149149
_LIBCPP_HIDE_FROM_ABI constexpr auto end()
150150
requires(!(__simple_view<_Views> && ...))
151151
{
152152
if constexpr (!__zip_is_common<_Views...>) {
153-
return __sentinel<false>(ranges::__tuple_transform(ranges::end, __views_));
153+
return __sentinel<false>(std::__tuple_transform(ranges::end, __views_));
154154
} else if constexpr ((random_access_range<_Views> && ...)) {
155155
return begin() + iter_difference_t<__iterator<false>>(size());
156156
} else {
157-
return __iterator<false>(ranges::__tuple_transform(ranges::end, __views_));
157+
return __iterator<false>(std::__tuple_transform(ranges::end, __views_));
158158
}
159159
}
160160

161161
_LIBCPP_HIDE_FROM_ABI constexpr auto end() const
162162
requires(range<const _Views> && ...)
163163
{
164164
if constexpr (!__zip_is_common<const _Views...>) {
165-
return __sentinel<true>(ranges::__tuple_transform(ranges::end, __views_));
165+
return __sentinel<true>(std::__tuple_transform(ranges::end, __views_));
166166
} else if constexpr ((random_access_range<const _Views> && ...)) {
167167
return begin() + iter_difference_t<__iterator<true>>(size());
168168
} else {
169-
return __iterator<true>(ranges::__tuple_transform(ranges::end, __views_));
169+
return __iterator<true>(std::__tuple_transform(ranges::end, __views_));
170170
}
171171
}
172172

@@ -178,7 +178,7 @@ class zip_view : public view_interface<zip_view<_Views...>> {
178178
using _CT = make_unsigned_t<common_type_t<decltype(__sizes)...>>;
179179
return ranges::min({_CT(__sizes)...});
180180
},
181-
ranges::__tuple_transform(ranges::size, __views_));
181+
std::__tuple_transform(ranges::size, __views_));
182182
}
183183

184184
_LIBCPP_HIDE_FROM_ABI constexpr auto size() const
@@ -189,7 +189,7 @@ class zip_view : public view_interface<zip_view<_Views...>> {
189189
using _CT = make_unsigned_t<common_type_t<decltype(__sizes)...>>;
190190
return ranges::min({_CT(__sizes)...});
191191
},
192-
ranges::__tuple_transform(ranges::size, __views_));
192+
std::__tuple_transform(ranges::size, __views_));
193193
}
194194
};
195195

@@ -268,7 +268,7 @@ class zip_view<_Views...>::__iterator : public __zip_view_iterator_category_base
268268
: __current_(std::move(__i.__current_)) {}
269269

270270
_LIBCPP_HIDE_FROM_ABI constexpr auto operator*() const {
271-
return ranges::__tuple_transform([](auto& __i) -> decltype(auto) { return *__i; }, __current_);
271+
return std::__tuple_transform([](auto& __i) -> decltype(auto) { return *__i; }, __current_);
272272
}
273273

274274
_LIBCPP_HIDE_FROM_ABI constexpr __iterator& operator++() {
@@ -318,7 +318,7 @@ class zip_view<_Views...>::__iterator : public __zip_view_iterator_category_base
318318
_LIBCPP_HIDE_FROM_ABI constexpr auto operator[](difference_type __n) const
319319
requires __zip_all_random_access<_Const, _Views...>
320320
{
321-
return ranges::__tuple_transform(
321+
return std::__tuple_transform(
322322
[&]<class _Iter>(_Iter& __i) -> decltype(auto) { return __i[iter_difference_t<_Iter>(__n)]; }, __current_);
323323
}
324324

@@ -377,7 +377,7 @@ class zip_view<_Views...>::__iterator : public __zip_view_iterator_category_base
377377
_LIBCPP_HIDE_FROM_ABI friend constexpr auto iter_move(const __iterator& __i) noexcept(
378378
(noexcept(ranges::iter_move(std::declval<const iterator_t<__maybe_const<_Const, _Views>>&>())) && ...) &&
379379
(is_nothrow_move_constructible_v<range_rvalue_reference_t<__maybe_const<_Const, _Views>>> && ...)) {
380-
return ranges::__tuple_transform(ranges::iter_move, __i.__current_);
380+
return std::__tuple_transform(ranges::iter_move, __i.__current_);
381381
}
382382

383383
_LIBCPP_HIDE_FROM_ABI friend constexpr void iter_swap(const __iterator& __l, const __iterator& __r) noexcept(
@@ -496,4 +496,4 @@ _LIBCPP_END_NAMESPACE_STD
496496

497497
_LIBCPP_POP_MACROS
498498

499-
#endif // _LIBCPP___RANGES_ZIP_UTILS_H
499+
#endif // _LIBCPP___RANGES_ZIP_VIEW_H
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
//
88
//===----------------------------------------------------------------------===//
99

10-
#ifndef _LIBCPP___RANGES_ZIP_VIEW_H
11-
#define _LIBCPP___RANGES_ZIP_VIEW_H
10+
#ifndef _LIBCPP___TUPLE_TUPLE_TRANSFORM_H
11+
#define _LIBCPP___TUPLE_TUPLE_TRANSFORM_H
1212

1313
#include <__config>
1414

@@ -27,8 +27,6 @@ _LIBCPP_BEGIN_NAMESPACE_STD
2727

2828
#if _LIBCPP_STD_VER >= 23
2929

30-
namespace ranges {
31-
3230
template <class _Fun, class _Tuple>
3331
_LIBCPP_HIDE_FROM_ABI constexpr auto __tuple_transform(_Fun&& __f, _Tuple&& __tuple) {
3432
return std::apply(
@@ -38,11 +36,10 @@ _LIBCPP_HIDE_FROM_ABI constexpr auto __tuple_transform(_Fun&& __f, _Tuple&& __tu
3836
std::forward<_Tuple>(__tuple));
3937
}
4038

41-
} // namespace ranges
4239
#endif // _LIBCPP_STD_VER >= 23
4340

4441
_LIBCPP_END_NAMESPACE_STD
4542

4643
_LIBCPP_POP_MACROS
4744

48-
#endif // _LIBCPP___RANGES_ZIP_VIEW_H
45+
#endif // _LIBCPP___TUPLE_TUPLE_TRANSFORM_H

libcxx/include/module.modulemap.in

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1948,9 +1948,6 @@ module std [system] {
19481948
header "__ranges/zip_view.h"
19491949
export std.utility.pair
19501950
}
1951-
module zip_utils {
1952-
header "__ranges/zip_utils.h"
1953-
}
19541951
module zip_transform_view {
19551952
header "__ranges/zip_transform_view.h"
19561953
}
@@ -2122,6 +2119,7 @@ module std [system] {
21222119
module tuple_like_no_subrange { header "__tuple/tuple_like_no_subrange.h" }
21232120
module tuple_like { header "__tuple/tuple_like.h" }
21242121
module tuple_size { header "__tuple/tuple_size.h" }
2122+
module tuple_trasnform { header "__tuple/tuple_transform.h" }
21252123

21262124
header "tuple"
21272125
export *
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
10+
// ADDITIONAL_COMPILE_FLAGS: -O3
11+
#include <algorithm>
12+
#include <deque>
13+
#include <ranges>
14+
#include <vector>
15+
16+
#include "benchmark/benchmark.h"
17+
18+
namespace {
19+
20+
template <size_t N>
21+
void BM_adjacent_full(benchmark::State& state) {
22+
const std::vector<int> inputs(1000000, 42);
23+
auto view = inputs | std::views::adjacent<N>;
24+
for (auto _ : state) {
25+
auto it = view.begin();
26+
benchmark::DoNotOptimize(it);
27+
}
28+
}
29+
30+
BENCHMARK(BM_adjacent_full<2>);
31+
BENCHMARK(BM_adjacent_full<3>);
32+
BENCHMARK(BM_adjacent_full<4>);
33+
BENCHMARK(BM_adjacent_full<5>);
34+
BENCHMARK(BM_adjacent_full<6>);
35+
BENCHMARK(BM_adjacent_full<7>);
36+
BENCHMARK(BM_adjacent_full<8>);
37+
BENCHMARK(BM_adjacent_full<9>);
38+
BENCHMARK(BM_adjacent_full<10>);
39+
BENCHMARK(BM_adjacent_full<100>);
40+
BENCHMARK(BM_adjacent_full<1000>);
41+
42+
template <size_t N>
43+
void BM_adjacent_empty(benchmark::State& state) {
44+
const std::vector<int> inputs;
45+
auto view = inputs | std::views::adjacent<N>;
46+
for (auto _ : state) {
47+
auto it = view.begin();
48+
benchmark::DoNotOptimize(it);
49+
}
50+
}
51+
52+
BENCHMARK(BM_adjacent_empty<2>);
53+
BENCHMARK(BM_adjacent_empty<3>);
54+
BENCHMARK(BM_adjacent_empty<4>);
55+
BENCHMARK(BM_adjacent_empty<5>);
56+
BENCHMARK(BM_adjacent_empty<6>);
57+
BENCHMARK(BM_adjacent_empty<7>);
58+
BENCHMARK(BM_adjacent_empty<8>);
59+
BENCHMARK(BM_adjacent_empty<9>);
60+
BENCHMARK(BM_adjacent_empty<10>);
61+
BENCHMARK(BM_adjacent_empty<100>);
62+
BENCHMARK(BM_adjacent_empty<1000>);
63+
64+
} // namespace
65+
66+
BENCHMARK_MAIN();

libcxx/test/std/ranges/range.adaptors/range.adjacent/adaptor.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ constexpr void test_all_constraints() {
4848
test_constraints<5>();
4949
}
5050

51-
constexpr void test_zero_case() {
51+
static constexpr void test_zero_case() {
5252
// N == 0 is a special case that always results in an empty range
5353
int buffer[8] = {1, 2, 3, 4, 5, 6, 7, 8};
5454

libcxx/test/std/ranges/range.adaptors/range.adjacent/begin.pass.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -91,26 +91,21 @@ constexpr void test_one() {
9191
}
9292
}
9393

94-
template <std::size_t N>
95-
constexpr void test_simple() {
96-
test_one<SimpleCommon, N>();
97-
98-
using View = std::ranges::adjacent_view<SimpleCommon, N>;
99-
static_assert(std::is_same_v<std::ranges::iterator_t<View>, std::ranges::iterator_t<const View>>);
100-
}
101-
102-
template <std::size_t N>
103-
constexpr void test_non_simple() {
104-
test_one<NonSimpleCommon, N>();
105-
106-
using View = std::ranges::adjacent_view<NonSimpleCommon, N>;
107-
static_assert(!std::is_same_v<std::ranges::iterator_t<View>, std::ranges::iterator_t<const View>>);
108-
}
109-
11094
template <std::size_t N>
11195
constexpr void test() {
112-
test_simple<N>();
113-
test_non_simple<N>();
96+
{
97+
// Test with simple view
98+
test_one<SimpleCommon, N>();
99+
using View = std::ranges::adjacent_view<SimpleCommon, N>;
100+
static_assert(std::is_same_v<std::ranges::iterator_t<View>, std::ranges::iterator_t<const View>>);
101+
}
102+
103+
{
104+
// Test with non-simple view
105+
test_one<NonSimpleCommon, N>();
106+
using View = std::ranges::adjacent_view<NonSimpleCommon, N>;
107+
static_assert(!std::is_same_v<std::ranges::iterator_t<View>, std::ranges::iterator_t<const View>>);
108+
}
114109

115110
// Test with view that doesn't support const begin()
116111
using ViewWithNoConstBegin = std::ranges::adjacent_view<NoConstBeginView, N>;

0 commit comments

Comments
 (0)