Skip to content

Commit 97e9af8

Browse files
committed
Implement and cleanup
1 parent 51d2b41 commit 97e9af8

File tree

6 files changed

+210
-55
lines changed

6 files changed

+210
-55
lines changed

libcxx/include/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,6 @@ set(files
713713
__ranges/enable_view.h
714714
__ranges/filter_view.h
715715
__ranges/from_range.h
716-
__ranges/indices_view.h
717716
__ranges/iota_view.h
718717
__ranges/istream_view.h
719718
__ranges/join_view.h

libcxx/include/__ranges/indices_view.h

Lines changed: 0 additions & 49 deletions
This file was deleted.

libcxx/include/__ranges/iota_view.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ template <class _Start, class _BoundSentinel>
371371
inline constexpr bool enable_borrowed_range<iota_view<_Start, _BoundSentinel>> = true;
372372

373373
namespace views {
374+
374375
namespace __iota {
375376
struct __fn {
376377
template <class _Start>
@@ -392,6 +393,16 @@ struct __fn {
392393
inline namespace __cpo {
393394
inline constexpr auto iota = __iota::__fn{};
394395
} // namespace __cpo
396+
397+
398+
# if _LIBCPP_STD_VER >= 26
399+
400+
inline constexpr auto indices = [](__integer_like auto __size) {
401+
return ranges::views::iota(decltype(__size){}, __size);
402+
};
403+
404+
# endif
405+
395406
} // namespace views
396407
} // namespace ranges
397408

libcxx/include/module.modulemap.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1887,7 +1887,6 @@ module std [system] {
18871887
export std.functional.bind_back
18881888
}
18891889
module from_range { header "__ranges/from_range.h" }
1890-
module indices_view { header "__ranges/indices_view.h" }
18911890
module iota_view { header "__ranges/iota_view.h" }
18921891
module istream_view { header "__ranges/istream_view.h" }
18931892
module join_view { header "__ranges/join_view.h" }

libcxx/include/ranges

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,10 +453,6 @@ namespace std {
453453
# include <__ranges/zip_view.h>
454454
# endif
455455

456-
# if _LIBCPP_STD_VER >= 26
457-
# include <__ranges/indeces_view.h>
458-
# endif
459-
460456
# include <version>
461457

462458
// standard-mandated includes
Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
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+
// REQUIRES: std-at-least-c++26
10+
11+
#include <cassert>
12+
#include <cstddef>
13+
#include <ranges>
14+
#include <vector>
15+
16+
#include "test_macros.h"
17+
#define TEST_HAS_NO_INT128 // Size cannot be larger than 64 bits
18+
#include "type_algorithms.h"
19+
20+
#include "types.h"
21+
22+
// #include <compare>
23+
24+
// class IntegerLike {
25+
// int value;
26+
27+
// public:
28+
// // Constructor
29+
// IntegerLike(int v = 0) : value(v) {}
30+
31+
// // Conversion to int
32+
// operator int() const { return value; }
33+
34+
// // Conversion to std::size_t
35+
// // This is necessary for std::ranges::views::indices to work with IntegerLike
36+
// operator std::size_t() const { return static_cast<std::size_t>(value); }
37+
38+
// // Equality and comparison
39+
// auto operator<=>(const IntegerLike&) const = default;
40+
41+
// // Arithmetic
42+
// IntegerLike operator+(const IntegerLike& other) const { return IntegerLike(value + other.value); }
43+
44+
// IntegerLike operator-(const IntegerLike& other) const { return IntegerLike(value - other.value); }
45+
46+
// IntegerLike operator*(const IntegerLike& other) const { return IntegerLike(value * other.value); }
47+
48+
// IntegerLike operator/(const IntegerLike& other) const { return IntegerLike(value / other.value); }
49+
50+
// // Compound assignment
51+
// IntegerLike& operator+=(const IntegerLike& other) {
52+
// value += other.value;
53+
// return *this;
54+
// }
55+
56+
// IntegerLike& operator-=(const IntegerLike& other) {
57+
// value -= other.value;
58+
// return *this;
59+
// }
60+
61+
// IntegerLike& operator*=(const IntegerLike& other) {
62+
// value *= other.value;
63+
// return *this;
64+
// }
65+
66+
// IntegerLike& operator/=(const IntegerLike& other) {
67+
// value /= other.value;
68+
// return *this;
69+
// }
70+
71+
// // Increment / Decrement
72+
// IntegerLike& operator++() {
73+
// ++value;
74+
// return *this;
75+
// }
76+
77+
// IntegerLike operator++(int) {
78+
// IntegerLike tmp = *this;
79+
// ++(*this);
80+
// return tmp;
81+
// }
82+
83+
// IntegerLike& operator--() {
84+
// --value;
85+
// return *this;
86+
// }
87+
88+
// IntegerLike operator--(int) {
89+
// IntegerLike tmp = *this;
90+
// --(*this);
91+
// return tmp;
92+
// }
93+
// };
94+
95+
// Test SFINAE.
96+
97+
template <typename SizeType>
98+
concept HasIndices = requires(SizeType s) { std::ranges::views::indices(s); };
99+
100+
struct NotIntegerLike {};
101+
102+
struct IntegerTypesTest {
103+
template <class T>
104+
constexpr void operator()() {
105+
static_assert(HasIndices<T>);
106+
}
107+
};
108+
109+
void test_SFIANE() {
110+
static_assert(HasIndices<std::size_t>);
111+
types::for_each(types::integer_types(), IntegerTypesTest{});
112+
113+
static_assert(!HasIndices<bool>);
114+
static_assert(!HasIndices<float>);
115+
static_assert(!HasIndices<void>);
116+
static_assert(!HasIndices<SomeInt>); // Does satisfy is_integer_like, but not the conversion to std::size_t
117+
static_assert(!HasIndices<NotIntegerLike>);
118+
}
119+
120+
constexpr bool test() {
121+
{
122+
// Check that the indices view works as expected
123+
auto indices_view = std::ranges::views::indices(5);
124+
assert(indices_view.size() == 5);
125+
126+
// This should be valid, as indices_view is a range of integers
127+
assert(indices_view[0] == 0);
128+
assert(indices_view[1] == 1);
129+
assert(indices_view[2] == 2);
130+
assert(indices_view[3] == 3);
131+
assert(indices_view[4] == 4);
132+
133+
// Check that the view is a range
134+
static_assert(std::ranges::range<decltype(indices_view)>);
135+
}
136+
137+
// {
138+
// // Check that the indices view works as expected
139+
// auto indices_view = std::ranges::views::indices(IntegerLike{5});
140+
// assert(indices_view.size() == 5);
141+
142+
// // Check that the view is a range
143+
// static_assert(std::ranges::range<decltype(indices_view)>);
144+
145+
// // This should be valid, as indices_view is a range of integers
146+
// assert(indices_view[0] == 0);
147+
// assert(indices_view[1] == 1);
148+
// assert(indices_view[2] == 2);
149+
// assert(indices_view[3] == 3);
150+
// assert(indices_view[4] == 4);
151+
// }
152+
153+
{
154+
std::vector v(5, 0);
155+
156+
// Check that the indices view works as expected
157+
auto indices_view = std::ranges::views::indices(std::ranges::size(v));
158+
assert(indices_view.size() == 5);
159+
160+
// Check that the view is a range
161+
static_assert(std::ranges::range<decltype(indices_view)>);
162+
163+
// This should be valid, as indices_view is a range of integers
164+
assert(indices_view[0] == 0);
165+
assert(indices_view[1] == 1);
166+
assert(indices_view[2] == 2);
167+
assert(indices_view[3] == 3);
168+
assert(indices_view[4] == 4);
169+
}
170+
171+
{
172+
std::vector v(5, SomeInt{});
173+
174+
// Check that the indices view works as expected
175+
auto indices_view = std::ranges::views::indices(std::ranges::size(v));
176+
assert(indices_view.size() == 5);
177+
178+
// Check that the view is a range
179+
static_assert(std::ranges::range<decltype(indices_view)>);
180+
181+
// This should be valid, as indices_view is a range of integers
182+
assert(indices_view[0] == 0);
183+
assert(indices_view[1] == 1);
184+
assert(indices_view[2] == 2);
185+
assert(indices_view[3] == 3);
186+
assert(indices_view[4] == 4);
187+
}
188+
189+
return true;
190+
}
191+
192+
int main(int, char**) {
193+
test_SFIANE();
194+
195+
test();
196+
static_assert(test());
197+
198+
return 0;
199+
}

0 commit comments

Comments
 (0)