Skip to content

Commit 11ec4bc

Browse files
initial commit
1 parent 59c609c commit 11ec4bc

File tree

14 files changed

+1539
-0
lines changed

14 files changed

+1539
-0
lines changed

libcxx/include/__ranges/concat_view.h

Lines changed: 624 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
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
10+
11+
// std::views::filter
12+
13+
#include <ranges>
14+
15+
#include <cassert>
16+
#include <concepts>
17+
#include <initializer_list>
18+
#include <type_traits>
19+
#include <utility>
20+
#include <vector>
21+
22+
#include "test_iterators.h"
23+
#include "test_range.h"
24+
25+
struct Range : std::ranges::view_base {
26+
using Iterator = forward_iterator<int*>;
27+
using Sentinel = sentinel_wrapper<Iterator>;
28+
constexpr explicit Range(int* b, int* e) : begin_(b), end_(e) {}
29+
constexpr Iterator begin() const { return Iterator(begin_); }
30+
constexpr Sentinel end() const { return Sentinel(Iterator(end_)); }
31+
32+
private:
33+
int* begin_;
34+
int* end_;
35+
};
36+
37+
template <typename View>
38+
constexpr void compareViews(View v, std::initializer_list<int> list) {
39+
auto b1 = v.begin();
40+
auto e1 = v.end();
41+
auto b2 = list.begin();
42+
auto e2 = list.end();
43+
for (; b1 != e1 && b2 != e2; ++b1, ++b2) {
44+
assert(*b1 == *b2);
45+
}
46+
assert(b1 == e1);
47+
assert(b2 == e2);
48+
}
49+
50+
constexpr bool test() {
51+
int buff[] = {0, 1, 2, 3, 4, 5, 6, 7};
52+
53+
{
54+
using Result = std::ranges::concat_view<Range>;
55+
Range range(buff, buff + 8);
56+
57+
{
58+
decltype(auto) result = std::views::concat(range);
59+
compareViews(result, {0, 1, 2, 3, 4, 5, 6, 7});
60+
}
61+
}
62+
63+
return true;
64+
}
65+
66+
int main(int, char**) {
67+
test();
68+
static_assert(test());
69+
70+
return 0;
71+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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
10+
11+
#include <ranges>
12+
#include <vector>
13+
14+
#include <cassert>
15+
#include "test_iterators.h"
16+
17+
constexpr void general_tests() {
18+
std::vector<int> v1 = {1, 2, 3, 4, 5, 6, 7, 8};
19+
std::vector<int> v2 = {1, 2, 3, 4, 5, 6, 7, 8};
20+
using type = std::vector<int>;
21+
// Check the return type of `.begin()`
22+
{
23+
std::ranges::concat_view view(v1, v2);
24+
using FilterIterator = std::ranges::iterator_t<decltype(view)>;
25+
ASSERT_SAME_TYPE(FilterIterator, decltype(view.begin()));
26+
}
27+
}
28+
29+
constexpr bool test() {
30+
general_tests();
31+
return true;
32+
}
33+
34+
int main(int, char**) {
35+
test();
36+
static_assert(test());
37+
return 0;
38+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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
10+
11+
#include <ranges>
12+
13+
#include <cassert>
14+
#include <type_traits>
15+
#include "test_iterators.h"
16+
17+
struct View : std::ranges::view_base {
18+
View() = default;
19+
forward_iterator<int*> begin() const;
20+
sentinel_wrapper<forward_iterator<int*>> end() const;
21+
};
22+
static_assert(std::ranges::view<View>);
23+
24+
// A range that is not a view
25+
struct Range {
26+
Range() = default;
27+
forward_iterator<int*> begin() const;
28+
sentinel_wrapper<forward_iterator<int*>> end() const;
29+
};
30+
static_assert(std::ranges::range<Range> && !std::ranges::view<Range>);
31+
32+
constexpr bool test() {
33+
{
34+
View v;
35+
std::ranges::concat_view view(v);
36+
static_assert(std::is_same_v<decltype(view), std::ranges::concat_view<View>>);
37+
}
38+
39+
// Test with a range that isn't a view, to make sure we properly use views::all_t in the implementation.
40+
{
41+
Range r;
42+
std::ranges::concat_view view(r);
43+
static_assert(std::is_same_v<decltype(view), std::ranges::concat_view<std::ranges::ref_view<Range>>>);
44+
}
45+
46+
return true;
47+
}
48+
49+
int main(int, char**) {
50+
test();
51+
static_assert(test());
52+
53+
return 0;
54+
}
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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
10+
11+
// filter_view() requires std::default_initializable<View> &&
12+
// std::default_initializable<Pred> = default;
13+
14+
#include <ranges>
15+
16+
#include <cassert>
17+
#include <type_traits>
18+
19+
constexpr int buff[] = {1, 2, 3, 4};
20+
21+
struct DefaultConstructibleView : std::ranges::view_base {
22+
constexpr DefaultConstructibleView() : begin_(buff), end_(buff + 4) {}
23+
constexpr int const* begin() const { return begin_; }
24+
constexpr int const* end() const { return end_; }
25+
26+
private:
27+
int const* begin_;
28+
int const* end_;
29+
};
30+
31+
struct NoDefaultView : std::ranges::view_base {
32+
NoDefaultView() = delete;
33+
int* begin() const;
34+
int* end() const;
35+
};
36+
37+
struct NoexceptView : std::ranges::view_base {
38+
NoexceptView() noexcept;
39+
int const* begin() const;
40+
int const* end() const;
41+
};
42+
43+
constexpr bool test() {
44+
{
45+
using View = std::ranges::concat_view<DefaultConstructibleView>;
46+
View view;
47+
auto it = view.begin();
48+
auto end = view.end();
49+
assert(*it++ == 1);
50+
assert(*it++ == 2);
51+
assert(*it++ == 3);
52+
assert(*it++ == 4);
53+
assert(it == end);
54+
}
55+
56+
// Check cases where the default constructor isn't provided
57+
{
58+
static_assert(!std::is_default_constructible_v<std::ranges::concat_view<NoDefaultView >>);
59+
}
60+
61+
// Check noexcept-ness
62+
{
63+
{
64+
using View = std::ranges::concat_view<DefaultConstructibleView>;
65+
static_assert(!noexcept(View()));
66+
}
67+
{
68+
using View = std::ranges::concat_view<NoexceptView>;
69+
static_assert(noexcept(View()));
70+
}
71+
}
72+
73+
return true;
74+
}
75+
76+
int main(int, char**) {
77+
test();
78+
static_assert(test());
79+
80+
return 0;
81+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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
10+
11+
// constexpr filter_view(View, Pred); // explicit since C++23
12+
13+
#include <cassert>
14+
#include <ranges>
15+
#include <utility>
16+
17+
#include "test_convertible.h"
18+
#include "test_macros.h"
19+
#include "types.h"
20+
21+
struct Range : std::ranges::view_base {
22+
constexpr explicit Range(int* b, int* e) : begin_(b), end_(e) {}
23+
constexpr int* begin() const { return begin_; }
24+
constexpr int* end() const { return end_; }
25+
26+
private:
27+
int* begin_;
28+
int* end_;
29+
};
30+
31+
struct TrackingRange : TrackInitialization, std::ranges::view_base {
32+
using TrackInitialization::TrackInitialization;
33+
int* begin() const;
34+
int* end() const;
35+
};
36+
37+
constexpr bool test() {
38+
int buff[] = {1, 2, 3, 4};
39+
40+
// Test explicit syntax
41+
{
42+
Range range(buff, buff + 4);
43+
std::ranges::concat_view<Range> view(range);
44+
auto it = view.begin();
45+
auto end = view.end();
46+
assert(*it++ == 1);
47+
assert(*it++ == 2);
48+
assert(*it++ == 3);
49+
assert(*it++ == 4);
50+
assert(it == end);
51+
}
52+
53+
// Make sure we move the view
54+
{
55+
bool moved = false, copied = false;
56+
TrackingRange range(&moved, &copied);
57+
[[maybe_unused]] std::ranges::concat_view<TrackingRange> view(std::move(range));
58+
assert(moved);
59+
assert(!copied);
60+
}
61+
62+
return true;
63+
}
64+
65+
int main(int, char**) {
66+
test();
67+
static_assert(test());
68+
69+
return 0;
70+
}

0 commit comments

Comments
 (0)