Skip to content

Commit 2dc8392

Browse files
committed
bounded_iterator
1 parent fcc71b5 commit 2dc8392

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed

libcxx/include/__iterator/bounded_iter.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#define _LIBCPP___ITERATOR_BOUNDED_ITER_H
1212

1313
#include <__assert>
14+
#include <__compare/ordering.h>
1415
#include <__config>
1516
#include <__iterator/iterator_traits.h>
1617
#include <__memory/pointer_traits.h>
@@ -201,6 +202,7 @@ struct __bounded_iter {
201202
operator==(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {
202203
return __x.__current_ == __y.__current_;
203204
}
205+
#if _LIBCPP_STD_VER <= 17
204206
_LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR friend bool
205207
operator!=(__bounded_iter const& __x, __bounded_iter const& __y) _NOEXCEPT {
206208
return __x.__current_ != __y.__current_;
@@ -222,6 +224,15 @@ struct __bounded_iter {
222224
return __x.__current_ >= __y.__current_;
223225
}
224226

227+
#else // _LIBCPP_STD_VER <= 17
228+
229+
_LIBCPP_HIDE_FROM_ABI constexpr friend strong_ordering
230+
operator<=>(__bounded_iter const& __x, __bounded_iter const& __y) noexcept {
231+
return __x.__current_ <=> __y.__current_;
232+
}
233+
234+
#endif // _LIBCPP_STD_VER <= 17
235+
225236
private:
226237
template <class>
227238
friend struct pointer_traits;
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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+
// <span>
12+
13+
// class iterator
14+
15+
#include <span>
16+
#include <cassert>
17+
#include <string>
18+
#include <version> // __cpp_lib_ranges_as_const is not defined in span.
19+
20+
#include "test_macros.h"
21+
22+
template <class T>
23+
TEST_CONSTEXPR void test_type() {
24+
#
25+
26+
using C = std::span<T>;
27+
typename C::iterator ii1{}, ii2{};
28+
typename C::iterator ii4 = ii1;
29+
// TODO Test against C++23 after implementing
30+
// P2278R4 cbegin should always return a constant iterator
31+
#ifdef __cpp_lib_ranges_as_const
32+
typename C::const_iterator cii{};
33+
#endif
34+
assert(ii1 == ii2);
35+
assert(ii1 == ii4);
36+
#ifdef __cpp_lib_ranges_as_const
37+
assert(ii1 == cii);
38+
#endif
39+
40+
assert(!(ii1 != ii2));
41+
#ifdef __cpp_lib_ranges_as_const
42+
assert(!(ii1 != cii));
43+
#endif
44+
45+
T v;
46+
C c{&v, 1};
47+
assert(c.begin() == std::begin(c));
48+
assert(c.rbegin() == std::rbegin(c));
49+
#ifdef __cpp_lib_ranges_as_const
50+
assert(c.cbegin() == std::cbegin(c));
51+
assert(c.crbegin() == std::crbegin(c));
52+
#endif
53+
54+
assert(c.end() == std::end(c));
55+
assert(c.rend() == std::rend(c));
56+
#ifdef __cpp_lib_ranges_as_const
57+
assert(c.cend() == std::cend(c));
58+
assert(c.crend() == std::crend(c));
59+
#endif
60+
61+
assert(std::begin(c) != std::end(c));
62+
assert(std::rbegin(c) != std::rend(c));
63+
#ifdef __cpp_lib_ranges_as_const
64+
assert(std::cbegin(c) != std::cend(c));
65+
assert(std::crbegin(c) != std::crend(c));
66+
#endif
67+
68+
// P1614 + LWG3352
69+
std::same_as<std::strong_ordering> decltype(auto) r1 = ii1 <=> ii2;
70+
assert(r1 == std::strong_ordering::equal);
71+
72+
std::same_as<std::strong_ordering> decltype(auto) r2 = ii1 <=> ii2;
73+
assert(r2 == std::strong_ordering::equal);
74+
}
75+
76+
TEST_CONSTEXPR bool test() {
77+
test_type<char>();
78+
test_type<int>();
79+
test_type<std::string>();
80+
81+
return true;
82+
}
83+
84+
int main(int, char**) {
85+
test();
86+
static_assert(test(), "");
87+
88+
return 0;
89+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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: !stdlib=libc++ && (c++03 || c++11 || c++14)
10+
11+
// <string_view>
12+
13+
// class iterator
14+
15+
#include <string_view>
16+
#include <cassert>
17+
#include <iterator>
18+
19+
#include "test_macros.h"
20+
#include "make_string.h"
21+
22+
template <class CharT>
23+
TEST_CONSTEXPR_CXX14 void test_type() {
24+
using C = std::basic_string_view<CharT>;
25+
typename C::iterator ii1 = typename C::iterator(), ii2 = typename C::iterator();
26+
typename C::iterator ii4 = ii1;
27+
typename C::const_iterator cii = typename C::const_iterator();
28+
assert(ii1 == ii2);
29+
assert(ii1 == ii4);
30+
assert(ii1 == cii);
31+
32+
assert(!(ii1 != ii2));
33+
assert(!(ii1 != cii));
34+
35+
#if TEST_STD_VER >= 11
36+
C c = MAKE_STRING_VIEW(CharT, "abc");
37+
assert(c.begin() == std::begin(c));
38+
assert(c.rbegin() == std::rbegin(c));
39+
assert(c.cbegin() == std::cbegin(c));
40+
assert(c.crbegin() == std::crbegin(c));
41+
42+
assert(c.end() == std::end(c));
43+
assert(c.rend() == std::rend(c));
44+
assert(c.cend() == std::cend(c));
45+
assert(c.crend() == std::crend(c));
46+
47+
assert(std::begin(c) != std::end(c));
48+
assert(std::rbegin(c) != std::rend(c));
49+
assert(std::cbegin(c) != std::cend(c));
50+
assert(std::crbegin(c) != std::crend(c));
51+
#endif
52+
53+
#if TEST_STD_VER >= 20
54+
// P1614 + LWG3352
55+
std::same_as<std::strong_ordering> decltype(auto) r1 = ii1 <=> ii2;
56+
assert(r1 == std::strong_ordering::equal);
57+
58+
std::same_as<std::strong_ordering> decltype(auto) r2 = ii1 <=> ii2;
59+
assert(r2 == std::strong_ordering::equal);
60+
#endif
61+
}
62+
63+
TEST_CONSTEXPR_CXX14 bool test() {
64+
test_type<char>();
65+
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
66+
test_type<wchar_t>();
67+
#endif
68+
#ifndef TEST_HAS_NO_CHAR8_T
69+
test_type<char8_t>();
70+
#endif
71+
test_type<char16_t>();
72+
test_type<char32_t>();
73+
74+
return true;
75+
}
76+
77+
int main(int, char**) {
78+
test();
79+
#if TEST_STD_VER >= 14
80+
static_assert(test(), "");
81+
#endif
82+
83+
return 0;
84+
}

0 commit comments

Comments
 (0)