[libcxx][ranges] Add range.subrange.

Basically the title.

Differential Revision: https://reviews.llvm.org/D102006
This commit is contained in:
zoecarver 2021-05-03 14:37:42 -07:00
parent e0efebb8eb
commit 9106047ee3
14 changed files with 945 additions and 0 deletions

View File

@ -53,6 +53,7 @@ set(files
__ranges/enable_view.h
__ranges/view_interface.h
__ranges/size.h
__ranges/subrange.h
__split_buffer
__std_stream
__string

View File

@ -0,0 +1,237 @@
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef _LIBCPP___RANGES_SUBRANGE_H
#define _LIBCPP___RANGES_SUBRANGE_H
#include <__config>
#include <__iterator/concepts.h>
#include <__iterator/iterator_traits.h>
#include <__iterator/advance.h>
#include <__ranges/access.h>
#include <__ranges/enable_borrowed_range.h>
#include <__ranges/view_interface.h>
#include <type_traits>
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif
_LIBCPP_PUSH_MACROS
#include <__undef_macros>
_LIBCPP_BEGIN_NAMESPACE_STD
#if !defined(_LIBCPP_HAS_NO_RANGES)
// clang-format off
namespace ranges {
template<class _From, class _To>
concept __convertible_to_non_slicing =
convertible_to<_From, _To> &&
// If they're both pointers, they must have the same element type.
!(is_pointer_v<decay_t<_From>> &&
is_pointer_v<decay_t<_To>> &&
__different_from<remove_pointer_t<decay_t<_From>>, remove_pointer_t<decay_t<_To>>>);
template<class _Tp>
concept __pair_like =
!is_reference_v<_Tp> && requires(_Tp __t) {
typename tuple_size<_Tp>::type; // Ensures `tuple_size<T>` is complete.
requires derived_from<tuple_size<_Tp>, integral_constant<size_t, 2>>;
typename tuple_element_t<0, remove_const_t<_Tp>>;
typename tuple_element_t<1, remove_const_t<_Tp>>;
{ _VSTD::get<0>(__t) } -> convertible_to<const tuple_element_t<0, _Tp>&>;
{ _VSTD::get<1>(__t) } -> convertible_to<const tuple_element_t<1, _Tp>&>;
};
template<class _Pair, class _Iter, class _Sent>
concept __pair_like_convertible_from =
!range<_Pair> && __pair_like<_Pair> &&
constructible_from<_Pair, _Iter, _Sent> &&
__convertible_to_non_slicing<_Iter, tuple_element_t<0, _Pair>> &&
convertible_to<_Sent, tuple_element_t<1, _Pair>>;
enum class _LIBCPP_ENUM_VIS subrange_kind : bool { unsized, sized };
template<class _Iter, class _Sent, bool>
struct __subrange_base {
static constexpr bool __store_size = false;
_Iter __begin_ = _Iter();
_Sent __end_ = _Sent();
constexpr __subrange_base() = default;
constexpr __subrange_base(_Iter __iter, _Sent __sent, make_unsigned_t<iter_difference_t<_Iter>> = 0)
: __begin_(_VSTD::move(__iter)), __end_(__sent) { }
};
template<class _Iter, class _Sent>
struct __subrange_base<_Iter, _Sent, true> {
static constexpr bool __store_size = true;
_Iter __begin_ = _Iter();
_Sent __end_ = _Sent();
make_unsigned_t<iter_difference_t<_Iter>> __size_ = 0;
constexpr __subrange_base() = default;
constexpr __subrange_base(_Iter __iter, _Sent __sent, decltype(__size_) __size)
: __begin_(_VSTD::move(__iter)), __end_(__sent), __size_(__size) { }
};
template<input_or_output_iterator _Iter, sentinel_for<_Iter> _Sent = _Iter,
subrange_kind _Kind = sized_sentinel_for<_Sent, _Iter>
? subrange_kind::sized
: subrange_kind::unsized>
requires (_Kind == subrange_kind::sized || !sized_sentinel_for<_Sent, _Iter>)
struct _LIBCPP_TEMPLATE_VIS subrange
: public view_interface<subrange<_Iter, _Sent, _Kind>>,
private __subrange_base<_Iter, _Sent, _Kind == subrange_kind::sized && !sized_sentinel_for<_Sent, _Iter>> {
using _Base = __subrange_base<_Iter, _Sent, _Kind == subrange_kind::sized && !sized_sentinel_for<_Sent, _Iter>>;
subrange() requires default_initializable<_Iter> = default;
constexpr subrange(__convertible_to_non_slicing<_Iter> auto __iter, _Sent __sent)
requires (!_Base::__store_size)
: _Base(_VSTD::move(__iter), __sent) {}
constexpr subrange(__convertible_to_non_slicing<_Iter> auto __iter, _Sent __sent,
make_unsigned_t<iter_difference_t<_Iter>> __n)
requires (_Kind == subrange_kind::sized)
: _Base(_VSTD::move(__iter), __sent, __n) { }
template<__different_from<subrange> _Range>
requires borrowed_range<_Range> &&
__convertible_to_non_slicing<iterator_t<_Range>, _Iter> &&
convertible_to<sentinel_t<_Range>, _Sent>
constexpr subrange(_Range&& __range)
requires (!_Base::__store_size)
: subrange(ranges::begin(__range), ranges::end(__range)) { }
template<__different_from<subrange> _Range>
requires borrowed_range<_Range> &&
__convertible_to_non_slicing<iterator_t<_Range>, _Iter> &&
convertible_to<sentinel_t<_Range>, _Sent>
constexpr subrange(_Range&& __range)
requires _Base::__store_size && sized_range<_Range>
: subrange(__range, ranges::size(__range)) { }
template<borrowed_range _Range>
requires __convertible_to_non_slicing<iterator_t<_Range>, _Iter> &&
convertible_to<sentinel_t<_Range>, _Sent>
constexpr subrange(_Range&& __range, make_unsigned_t<iter_difference_t<_Iter>> __n)
requires (_Kind == subrange_kind::sized)
: subrange(ranges::begin(__range), ranges::end(__range), __n) { }
template<__different_from<subrange> _Pair>
requires __pair_like_convertible_from<_Pair, const _Iter&, const _Sent&>
constexpr operator _Pair() const { return _Pair(this->__begin_, this->__end_); }
constexpr _Iter begin() const requires copyable<_Iter> {
return this->__begin_;
}
[[nodiscard]] constexpr _Iter begin() requires (!copyable<_Iter>) {
return _VSTD::move(this->__begin_);
}
constexpr _Sent end() const { return this->__end_; }
[[nodiscard]] constexpr bool empty() const { return this->__begin_ == this->__end_; }
constexpr make_unsigned_t<iter_difference_t<_Iter>> size() const
requires (_Kind == subrange_kind::sized)
{
if constexpr (_Base::__store_size)
return this->__size_;
else
return __to_unsigned_like(this->__end_ - this->__begin_);
}
[[nodiscard]] constexpr subrange next(iter_difference_t<_Iter> __n = 1) const&
requires forward_iterator<_Iter> {
auto __tmp = *this;
__tmp.advance(__n);
return __tmp;
}
[[nodiscard]] constexpr subrange next(iter_difference_t<_Iter> __n = 1) && {
advance(__n);
return _VSTD::move(*this);
}
[[nodiscard]] constexpr subrange prev(iter_difference_t<_Iter> __n = 1) const
requires bidirectional_iterator<_Iter> {
auto __tmp = *this;
__tmp.advance(-__n);
return __tmp;
}
constexpr subrange& advance(iter_difference_t<_Iter> __n) {
if constexpr (bidirectional_iterator<_Iter>) {
if (__n < 0) {
ranges::advance(this->__begin_, __n);
if constexpr (_Base::__store_size)
this->__size_ += _VSTD::__to_unsigned_like(-__n);
return *this;
}
}
auto __d = __n - ranges::advance(this->__begin_, __n, this->__end_);
if constexpr (_Base::__store_size)
this->__size_ -= _VSTD::__to_unsigned_like(__d);
return *this;
}
};
template<input_or_output_iterator _Iter, sentinel_for<_Iter> _Sent>
subrange(_Iter, _Sent) -> subrange<_Iter, _Sent>;
template<input_or_output_iterator _Iter, sentinel_for<_Iter> _Sent>
subrange(_Iter, _Sent, make_unsigned_t<iter_difference_t<_Iter>>)
-> subrange<_Iter, _Sent, subrange_kind::sized>;
template<borrowed_range _Range>
subrange(_Range&&) -> subrange<iterator_t<_Range>, sentinel_t<_Range>,
(sized_range<_Range> || sized_sentinel_for<sentinel_t<_Range>, iterator_t<_Range>>)
? subrange_kind::sized : subrange_kind::unsized>;
template<borrowed_range _Range>
subrange(_Range&&, make_unsigned_t<range_difference_t<_Range>>)
-> subrange<iterator_t<_Range>, sentinel_t<_Range>, subrange_kind::sized>;
template<size_t _Index, class _Iter, class _Sent, subrange_kind _Kind>
requires (_Index < 2)
constexpr auto get(const subrange<_Iter, _Sent, _Kind>& __subrange) {
if constexpr (_Index == 0)
return __subrange.begin();
else
return __subrange.end();
}
template<size_t _Index, class _Iter, class _Sent, subrange_kind _Kind>
requires (_Index < 2)
constexpr auto get(subrange<_Iter, _Sent, _Kind>&& __subrange) {
if constexpr (_Index == 0)
return __subrange.begin();
else
return __subrange.end();
}
} // namespace ranges
using ranges::get;
// clang-format off
#endif // !defined(_LIBCPP_HAS_NO_RANGES)
_LIBCPP_END_NAMESPACE_STD
_LIBCPP_POP_MACROS
#endif // _LIBCPP___RANGES_SUBRANGE_H

View File

@ -101,6 +101,7 @@ namespace std::ranges {
#include <__ranges/enable_borrowed_range.h>
#include <__ranges/enable_view.h>
#include <__ranges/size.h>
#include <__ranges/subrange.h>
#include <__ranges/view_interface.h>
#include <compare> // Required by the standard.
#include <initializer_list> // Required by the standard.

View File

@ -0,0 +1,59 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: gcc-10
// class std::ranges::subrange;
#include <ranges>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
#include "../subrange_test_types.h"
constexpr bool test() {
std::ranges::subrange<int*> a(globalBuff, globalBuff + 8, 8);
auto a1 = a.next();
assert(a1.begin() == globalBuff + 1);
assert(a1.size() == 7);
auto a5 = a.next(5);
assert(a5.begin() == globalBuff + 5);
assert(a5.size() == 3);
auto a4 = a5.prev();
assert(a4.begin() == globalBuff + 4);
assert(a4.size() == 4);
std::ranges::subrange<InputIter> b(InputIter(globalBuff), InputIter(globalBuff + 8));
auto b1 = std::move(b).next();
assert(b1.begin().base() == globalBuff + 1);
std::ranges::subrange<BidirIter> c(BidirIter(globalBuff + 4), BidirIter(globalBuff + 8));
auto c1 = c.prev();
assert(c1.begin().base() == globalBuff + 3);
auto c2 = c.prev(4);
assert(c2.begin().base() == globalBuff);
std::ranges::subrange<BidirIter> d(BidirIter(globalBuff + 4), BidirIter(globalBuff + 8));
auto d1 = d.advance(4);
assert(d1.begin().base() == globalBuff + 8);
assert(d1.empty());
auto d2 = d1.advance(-4);
assert(d2.begin().base() == globalBuff + 4);
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}

View File

@ -0,0 +1,49 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: gcc-10
// class std::ranges::subrange;
#include <ranges>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
#include "../subrange_test_types.h"
template<size_t I, class S>
concept GetInvocable = requires {
std::get<I>(std::declval<S>());
};
static_assert( GetInvocable<0, std::ranges::subrange<int*>>);
static_assert( GetInvocable<1, std::ranges::subrange<int*>>);
static_assert(!GetInvocable<2, std::ranges::subrange<int*>>);
static_assert(!GetInvocable<3, std::ranges::subrange<int*>>);
constexpr bool test() {
std::ranges::subrange<int*> a(globalBuff, globalBuff + 8, 8);
assert(std::get<0>(a) == a.begin());
assert(std::get<1>(a) == a.end());
assert(a.begin() == std::get<0>(std::move(a)));
std::ranges::subrange<int*> b(globalBuff, globalBuff + 8, 8);
assert(b.end() == std::get<1>(std::move(b)));
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}

View File

@ -0,0 +1,57 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: gcc-10
// class std::ranges::subrange;
#include <ranges>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
#include "../subrange_test_types.h"
// Note: begin and end tested in range.subrange.ctor.pass.cpp.
constexpr bool test() {
std::ranges::subrange<MoveOnlyForwardIter, int*> a(MoveOnlyForwardIter(globalBuff), globalBuff + 8, 8);
assert(a.begin().base == globalBuff);
assert(!a.empty());
assert(a.size() == 8);
std::ranges::subrange<ForwardIter> b(ForwardIter(nullptr), ForwardIter(nullptr));
assert(b.empty());
std::ranges::subrange<ForwardIter> c{ForwardIter(globalBuff), ForwardIter(globalBuff)};
assert(c.empty());
std::ranges::subrange<ForwardIter> d(ForwardIter(globalBuff), ForwardIter(globalBuff + 1));
assert(!d.empty());
std::ranges::subrange<SizedSentinelForwardIter> e(SizedSentinelForwardIter(globalBuff),
SizedSentinelForwardIter(globalBuff + 8), 8);
assert(!e.empty());
assert(e.size() == 8);
// Make sure that operator- is used to calculate size when possible.
if (!std::is_constant_evaluated())
assert(SizedSentinelForwardIter::minusCount == 1);
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}

View File

@ -0,0 +1,51 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: gcc-10
// class std::ranges::subrange;
#include <ranges>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
using FI = forward_iterator<int*>;
FI fi{nullptr};
int *ptr = nullptr;
static_assert(std::same_as<decltype(std::ranges::subrange(fi, fi)),
std::ranges::subrange<FI, FI, std::ranges::subrange_kind::unsized>>);
static_assert(std::same_as<decltype(std::ranges::subrange(ptr, ptr, 0)),
std::ranges::subrange<int*, int*, std::ranges::subrange_kind::sized>>);
static_assert(std::same_as<decltype(std::ranges::subrange(ptr, nullptr, 0)),
std::ranges::subrange<int*, nullptr_t, std::ranges::subrange_kind::sized>>);
struct ForwardRange {
forward_iterator<int*> begin() const;
forward_iterator<int*> end() const;
};
template<>
inline constexpr bool std::ranges::enable_borrowed_range<ForwardRange> = true;
struct SizedRange {
int *begin();
int *end();
};
template<>
inline constexpr bool std::ranges::enable_borrowed_range<SizedRange> = true;
static_assert(std::same_as<decltype(std::ranges::subrange(ForwardRange())),
std::ranges::subrange<FI, FI, std::ranges::subrange_kind::unsized>>);
static_assert(std::same_as<decltype(std::ranges::subrange(SizedRange())),
std::ranges::subrange<int*, int*, std::ranges::subrange_kind::sized>>);
static_assert(std::same_as<decltype(std::ranges::subrange(SizedRange(), 8)),
std::ranges::subrange<int*, int*, std::ranges::subrange_kind::sized>>);

View File

@ -0,0 +1,53 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: gcc-10
// class std::ranges::subrange;
#include <ranges>
#include "../subrange_test_types.h"
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
// convertible-to-non-slicing cases:
// 1. Not convertible (fail)
// 2. Only one is a pointer (succeed)
// 3. Both are not pointers (succeed)
// 4. Pointer elements are different types (fail)
// 5. Pointer elements are same type (succeed)
// !StoreSize ctor.
static_assert( std::is_constructible_v<ForwardSubrange, ForwardIter, ForwardIter>); // Default case.
static_assert(!std::is_constructible_v<ForwardSubrange, Empty, ForwardIter>); // 1.
static_assert( std::is_constructible_v<ConvertibleForwardSubrange, ConvertibleForwardIter, int*>); // 2.
static_assert( std::is_constructible_v<ForwardSubrange, ForwardIter, ForwardIter>); // 3. (Same as default case.)
// 4. and 5. must be sized.
constexpr bool test() {
ForwardSubrange a(ForwardIter(globalBuff), ForwardIter(globalBuff + 8));
assert(a.begin().base() == globalBuff);
assert(a.end().base() == globalBuff + 8);
ConvertibleForwardSubrange b(ConvertibleForwardIter(globalBuff), globalBuff + 8);
assert(b.begin() == globalBuff);
assert(b.end() == globalBuff + 8);
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}

View File

@ -0,0 +1,60 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: gcc-10
// class std::ranges::subrange;
#include <ranges>
#include "../subrange_test_types.h"
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
// convertible-to-non-slicing cases:
// 1. Not convertible (fail)
// 2. Only one is a pointer (succeed)
// 3. Both are not pointers (succeed)
// 4. Pointer elements are different types (fail)
// 5. Pointer elements are same type (succeed)
static_assert( std::is_constructible_v<SizedSentinelForwardSubrange, ConditionallyConvertibleIter, ConditionallyConvertibleIter, ConditionallyConvertibleIter::udifference_type>); // Default case.
static_assert(!std::is_constructible_v<SizedSentinelForwardSubrange, Empty, ConditionallyConvertibleIter, ConditionallyConvertibleIter::udifference_type>); // 1.
static_assert( std::is_constructible_v<ConvertibleSizedSentinelForwardSubrange, ConvertibleSizedSentinelForwardIter, int*, ConvertibleSizedSentinelForwardIter::udifference_type>); // 2.
static_assert( std::is_constructible_v<SizedSentinelForwardSubrange, ConditionallyConvertibleIter, ConditionallyConvertibleIter, ConditionallyConvertibleIter::udifference_type>); // 3. (Same as default case.)
static_assert(!std::is_constructible_v<SizedIntPtrSubrange, long*, int*, size_t>); // 4.
static_assert( std::is_constructible_v<SizedIntPtrSubrange, int*, int*, size_t>); // 5.
constexpr bool test() {
SizedSentinelForwardSubrange a(ConditionallyConvertibleIter(globalBuff), ConditionallyConvertibleIter(globalBuff + 8), 8);
assert(a.begin().base() == globalBuff);
assert(a.end().base() == globalBuff + 8);
assert(a.size() == 8);
ConvertibleSizedSentinelForwardSubrange b(ConvertibleSizedSentinelForwardIter(globalBuff), ConvertibleSizedSentinelForwardIter(globalBuff + 8), 8);
assert(b.begin() == globalBuff);
assert(b.end() == globalBuff + 8);
assert(b.size() == 8);
SizedIntPtrSubrange c(globalBuff, globalBuff + 8, 8);
assert(c.begin() == globalBuff);
assert(c.end() == globalBuff + 8);
assert(c.size() == 8);
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}

View File

@ -0,0 +1,49 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: gcc-10
// class std::ranges::subrange;
#include <ranges>
#include "../subrange_test_types.h"
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
#include <tuple>
#include <utility>
static_assert( std::is_convertible_v<ForwardSubrange, std::pair<ForwardIter, ForwardIter>>);
static_assert( std::is_convertible_v<ForwardSubrange, std::tuple<ForwardIter, ForwardIter>>);
static_assert(!std::is_convertible_v<ForwardSubrange, std::tuple<ForwardIter, ForwardIter>&>);
static_assert(!std::is_convertible_v<ForwardSubrange, std::tuple<ForwardIter, ForwardIter, ForwardIter>>);
static_assert( std::is_convertible_v<ConvertibleForwardSubrange, std::tuple<ConvertibleForwardIter, int*>>);
static_assert(!std::is_convertible_v<SizedIntPtrSubrange, std::tuple<long*, int*>>);
static_assert( std::is_convertible_v<SizedIntPtrSubrange, std::tuple<int*, int*>>);
constexpr bool test() {
ForwardSubrange a(ForwardIter(globalBuff), ForwardIter(globalBuff + 8));
std::pair<ForwardIter, ForwardIter> aPair = a;
assert(aPair.first.base() == globalBuff);
assert(aPair.second.base() == globalBuff + 8);
std::tuple<ForwardIter, ForwardIter> aTuple = a;
assert(get<0>(aTuple).base() == globalBuff);
assert(get<1>(aTuple).base() == globalBuff + 8);
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}

View File

@ -0,0 +1,51 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: gcc-10
// class std::ranges::subrange;
#include <ranges>
#include "../subrange_test_types.h"
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
static_assert( std::is_constructible_v<ForwardSubrange, ForwardBorrowedRange>); // Default case.
static_assert(!std::is_constructible_v<ForwardSubrange, ForwardRange>); // Not borrowed.
// Iter convertible to sentinel (pointer) type.
static_assert( std::is_constructible_v<ConvertibleForwardSubrange, ConvertibleForwardBorrowedRange>);
// Where neither iter or sentinel are pointers, but they are different.
static_assert( std::is_constructible_v<DifferentSentinelSubrange, ForwardBorrowedRangeDifferentSentinel>);
static_assert( std::is_constructible_v<DifferentSentinelWithSizeMemberSubrange, DifferentSentinelWithSizeMember>);
constexpr bool test() {
ForwardSubrange a{ForwardBorrowedRange()};
assert(a.begin().base() == globalBuff);
assert(a.end().base() == globalBuff + 8);
ConvertibleForwardSubrange b{ConvertibleForwardBorrowedRange()};
assert(b.begin() == globalBuff);
assert(b.end() == globalBuff + 8);
DifferentSentinelSubrange c{ForwardBorrowedRangeDifferentSentinel()};
assert(c.begin().base() == globalBuff);
assert(c.end().value == globalBuff + 8);
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}

View File

@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: gcc-10
// class std::ranges::subrange;
#include <ranges>
// Tested in pair_like_conv.pass.cpp.
int main(int, char**) {
return 0;
}

View File

@ -0,0 +1,41 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-no-concepts
// UNSUPPORTED: gcc-10
// class std::ranges::subrange;
#include <ranges>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
template<std::ranges::subrange_kind K, class... Args>
concept ValidSubrangeKind = requires { typename std::ranges::subrange<Args..., K>; };
template<class... Args>
concept ValidSubrange = requires { typename std::ranges::subrange<Args...>; };
static_assert( ValidSubrange<forward_iterator<int*>>);
static_assert( ValidSubrange<forward_iterator<int*>, forward_iterator<int*>>);
static_assert( ValidSubrangeKind<std::ranges::subrange_kind::unsized, forward_iterator<int*>, forward_iterator<int*>>);
static_assert( ValidSubrangeKind<std::ranges::subrange_kind::sized, forward_iterator<int*>, forward_iterator<int*>>);
// Wrong sentinel type.
static_assert(!ValidSubrange<forward_iterator<int*>, int*>);
static_assert( ValidSubrange<int*>);
static_assert( ValidSubrange<int*, int*>);
// Must be sized.
static_assert(!ValidSubrangeKind<std::ranges::subrange_kind::unsized, int*, int*>);
static_assert( ValidSubrangeKind<std::ranges::subrange_kind::sized, int*, int*>);
// Wrong sentinel type.
static_assert(!ValidSubrange<int*, forward_iterator<int*>>);
// Not an iterator.
static_assert(!ValidSubrange<int>);

View File

@ -0,0 +1,215 @@
//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#ifndef SUBRANGE_TEST_TYPES_H
#define SUBRANGE_TEST_TYPES_H
#include "test_macros.h"
#include "test_iterators.h"
int globalBuff[8];
struct Empty {};
using InputIter = cpp17_input_iterator<int*>;
using ForwardIter = forward_iterator<int*>;
using BidirIter = bidirectional_iterator<int*>;
using ForwardSubrange = std::ranges::subrange<ForwardIter, ForwardIter, std::ranges::subrange_kind::unsized>;
using SizedIntPtrSubrange = std::ranges::subrange<int*, int*, std::ranges::subrange_kind::sized>;
struct MoveOnlyForwardIter {
typedef std::forward_iterator_tag iterator_category;
typedef int value_type;
typedef std::ptrdiff_t difference_type;
typedef int* pointer;
typedef int& reference;
typedef MoveOnlyForwardIter self;
int *base = nullptr;
MoveOnlyForwardIter() = default;
MoveOnlyForwardIter(MoveOnlyForwardIter &&) = default;
MoveOnlyForwardIter &operator=(MoveOnlyForwardIter&&) = default;
MoveOnlyForwardIter(MoveOnlyForwardIter const&) = delete;
constexpr MoveOnlyForwardIter(int *ptr) : base(ptr) { }
friend bool operator==(const self&, const self&);
friend constexpr bool operator==(const self& lhs, int* rhs) { return lhs.base == rhs; }
reference operator*() const;
pointer operator->() const;
self& operator++();
self operator++(int);
self& operator--();
self operator--(int);
constexpr operator pointer() const { return base; }
};
struct SizedSentinelForwardIter {
typedef std::forward_iterator_tag iterator_category;
typedef int value_type;
typedef std::ptrdiff_t difference_type;
typedef int* pointer;
typedef int& reference;
typedef std::make_unsigned_t<std::ptrdiff_t> udifference_type;
typedef SizedSentinelForwardIter self;
int *base;
SizedSentinelForwardIter() = default;
constexpr SizedSentinelForwardIter(int *ptr) : base(ptr) { }
friend constexpr bool operator==(const self& lhs, const self& rhs) { return lhs.base == rhs.base; }
reference operator*() const;
pointer operator->() const;
self& operator++();
self operator++(int);
self& operator--();
self operator--(int);
static int minusCount;
friend constexpr difference_type operator-(SizedSentinelForwardIter const&a,
SizedSentinelForwardIter const&b) {
if (!std::is_constant_evaluated())
minusCount++;
return a.base - b.base;
}
};
int SizedSentinelForwardIter::minusCount = 0;
struct ConvertibleForwardIter {
typedef std::forward_iterator_tag iterator_category;
typedef int value_type;
typedef std::ptrdiff_t difference_type;
typedef int* pointer;
typedef int& reference;
typedef ConvertibleForwardIter self;
int *base_ = nullptr;
constexpr ConvertibleForwardIter() = default;
constexpr explicit ConvertibleForwardIter(int *ptr) : base_(ptr) { }
friend bool operator==(const self&, const self&);
reference operator*() const;
pointer operator->() const;
self& operator++();
self operator++(int);
self& operator--();
self operator--(int);
constexpr operator pointer() const { return base_; }
// Explicitly deleted so this doesn't model sized_sentinel_for.
friend constexpr difference_type operator-(int *, self const&) = delete;
friend constexpr difference_type operator-(self const&, int*) = delete;
};
using ConvertibleForwardSubrange = std::ranges::subrange<ConvertibleForwardIter, int*,
std::ranges::subrange_kind::unsized>;
static_assert(std::is_convertible_v<ConvertibleForwardIter, int*>);
template<bool EnableConvertible>
struct ConditionallyConvertibleBase {
typedef std::forward_iterator_tag iterator_category;
typedef int value_type;
typedef std::ptrdiff_t difference_type;
typedef int* pointer;
typedef int& reference;
typedef std::make_unsigned_t<std::ptrdiff_t> udifference_type;
typedef ConditionallyConvertibleBase self;
int *base_ = nullptr;
constexpr ConditionallyConvertibleBase() = default;
constexpr explicit ConditionallyConvertibleBase(int *ptr) : base_(ptr) {}
constexpr int *base() const { return base_; }
friend bool operator==(const self&, const self&);
reference operator*() const;
pointer operator->() const;
self& operator++();
self operator++(int);
self& operator--();
self operator--(int);
template<bool E = EnableConvertible>
requires E
constexpr operator pointer() const { return base_; }
};
using ConditionallyConvertibleIter = ConditionallyConvertibleBase<false>;
using SizedSentinelForwardSubrange = std::ranges::subrange<ConditionallyConvertibleIter,
ConditionallyConvertibleIter,
std::ranges::subrange_kind::sized>;
using ConvertibleSizedSentinelForwardIter = ConditionallyConvertibleBase<true>;
using ConvertibleSizedSentinelForwardSubrange = std::ranges::subrange<ConvertibleSizedSentinelForwardIter, int*,
std::ranges::subrange_kind::sized>;
struct ForwardBorrowedRange {
constexpr ForwardIter begin() const { return ForwardIter(globalBuff); }
constexpr ForwardIter end() const { return ForwardIter(globalBuff + 8); }
};
template<>
inline constexpr bool std::ranges::enable_borrowed_range<ForwardBorrowedRange> = true;
struct ForwardRange {
ForwardIter begin() const;
ForwardIter end() const;
};
struct ConvertibleForwardBorrowedRange {
constexpr ConvertibleForwardIter begin() const { return ConvertibleForwardIter(globalBuff); }
constexpr int *end() const { return globalBuff + 8; }
};
template<>
inline constexpr bool std::ranges::enable_borrowed_range<ConvertibleForwardBorrowedRange> = true;
struct ForwardBorrowedRangeDifferentSentinel {
struct sentinel {
int *value;
friend bool operator==(sentinel s, ForwardIter i) { return s.value == i.base(); }
};
constexpr ForwardIter begin() const { return ForwardIter(globalBuff); }
constexpr sentinel end() const { return sentinel{globalBuff + 8}; }
};
template<>
inline constexpr bool std::ranges::enable_borrowed_range<ForwardBorrowedRangeDifferentSentinel> = true;
using DifferentSentinelSubrange = std::ranges::subrange<ForwardIter,
ForwardBorrowedRangeDifferentSentinel::sentinel,
std::ranges::subrange_kind::unsized>;
struct DifferentSentinelWithSizeMember {
struct sentinel {
int *value;
friend bool operator==(sentinel s, ForwardIter i) { return s.value == i.base(); }
};
constexpr ForwardIter begin() const { return ForwardIter(globalBuff); }
constexpr sentinel end() const { return sentinel{globalBuff + 8}; }
constexpr size_t size() const { return 8; }
};
template<>
inline constexpr bool std::ranges::enable_borrowed_range<DifferentSentinelWithSizeMember> = true;
using DifferentSentinelWithSizeMemberSubrange = std::ranges::subrange<ForwardIter,
DifferentSentinelWithSizeMember::sentinel,
std::ranges::subrange_kind::unsized>;
#endif // SUBRANGE_TEST_TYPES_H