2021-05-01 06:24:28 +08:00
|
|
|
// -*- 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_VIEW_INTERFACE_H
|
|
|
|
#define _LIBCPP___RANGES_VIEW_INTERFACE_H
|
|
|
|
|
2022-02-15 02:41:09 +08:00
|
|
|
#include <__assert>
|
2022-02-10 03:31:21 +08:00
|
|
|
#include <__concepts/derived_from.h>
|
|
|
|
#include <__concepts/same_as.h>
|
2021-05-01 06:24:28 +08:00
|
|
|
#include <__config>
|
|
|
|
#include <__iterator/concepts.h>
|
|
|
|
#include <__iterator/iterator_traits.h>
|
|
|
|
#include <__iterator/prev.h>
|
2021-06-12 14:13:44 +08:00
|
|
|
#include <__memory/pointer_traits.h>
|
2021-05-01 06:24:28 +08:00
|
|
|
#include <__ranges/access.h>
|
2021-06-12 14:13:44 +08:00
|
|
|
#include <__ranges/concepts.h>
|
2021-05-01 06:24:28 +08:00
|
|
|
#include <__ranges/empty.h>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-02 09:16:40 +08:00
|
|
|
# pragma GCC system_header
|
2021-05-01 06:24:28 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
2022-03-12 23:46:57 +08:00
|
|
|
#if _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
|
2021-05-01 06:24:28 +08:00
|
|
|
|
|
|
|
namespace ranges {
|
|
|
|
|
|
|
|
template<class _Derived>
|
|
|
|
requires is_class_v<_Derived> && same_as<_Derived, remove_cv_t<_Derived>>
|
2022-01-20 04:16:15 +08:00
|
|
|
class view_interface {
|
2021-07-20 00:34:56 +08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2021-05-01 06:24:28 +08:00
|
|
|
constexpr _Derived& __derived() noexcept {
|
2021-10-30 07:04:32 +08:00
|
|
|
static_assert(sizeof(_Derived) && derived_from<_Derived, view_interface> && view<_Derived>);
|
2021-05-01 06:24:28 +08:00
|
|
|
return static_cast<_Derived&>(*this);
|
|
|
|
}
|
|
|
|
|
2021-07-20 00:34:56 +08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2021-05-01 06:24:28 +08:00
|
|
|
constexpr _Derived const& __derived() const noexcept {
|
2021-10-30 07:04:32 +08:00
|
|
|
static_assert(sizeof(_Derived) && derived_from<_Derived, view_interface> && view<_Derived>);
|
2021-05-01 06:24:28 +08:00
|
|
|
return static_cast<_Derived const&>(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
|
|
|
template<class _D2 = _Derived>
|
2021-07-20 00:34:56 +08:00
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty()
|
2021-05-01 06:24:28 +08:00
|
|
|
requires forward_range<_D2>
|
|
|
|
{
|
|
|
|
return ranges::begin(__derived()) == ranges::end(__derived());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class _D2 = _Derived>
|
2021-07-20 00:34:56 +08:00
|
|
|
[[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr bool empty() const
|
2021-05-01 06:24:28 +08:00
|
|
|
requires forward_range<const _D2>
|
|
|
|
{
|
|
|
|
return ranges::begin(__derived()) == ranges::end(__derived());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class _D2 = _Derived>
|
2021-07-20 00:34:56 +08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2021-05-01 06:24:28 +08:00
|
|
|
constexpr explicit operator bool()
|
2022-02-10 03:31:21 +08:00
|
|
|
requires requires (_D2& __t) { ranges::empty(__t); }
|
2021-05-01 06:24:28 +08:00
|
|
|
{
|
|
|
|
return !ranges::empty(__derived());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class _D2 = _Derived>
|
2021-07-20 00:34:56 +08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2021-05-01 06:24:28 +08:00
|
|
|
constexpr explicit operator bool() const
|
2022-02-10 03:31:21 +08:00
|
|
|
requires requires (const _D2& __t) { ranges::empty(__t); }
|
2021-05-01 06:24:28 +08:00
|
|
|
{
|
|
|
|
return !ranges::empty(__derived());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class _D2 = _Derived>
|
2021-07-20 00:34:56 +08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2021-05-01 06:24:28 +08:00
|
|
|
constexpr auto data()
|
|
|
|
requires contiguous_iterator<iterator_t<_D2>>
|
|
|
|
{
|
2022-02-11 08:01:17 +08:00
|
|
|
return std::to_address(ranges::begin(__derived()));
|
2021-05-01 06:24:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class _D2 = _Derived>
|
2021-07-20 00:34:56 +08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2021-05-01 06:24:28 +08:00
|
|
|
constexpr auto data() const
|
|
|
|
requires range<const _D2> && contiguous_iterator<iterator_t<const _D2>>
|
|
|
|
{
|
2022-02-11 08:01:17 +08:00
|
|
|
return std::to_address(ranges::begin(__derived()));
|
2021-05-01 06:24:28 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<class _D2 = _Derived>
|
2021-07-20 00:34:56 +08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2021-05-01 06:24:28 +08:00
|
|
|
constexpr auto size()
|
2022-02-10 03:31:21 +08:00
|
|
|
requires forward_range<_D2> && sized_sentinel_for<sentinel_t<_D2>, iterator_t<_D2>>
|
2021-05-01 06:24:28 +08:00
|
|
|
{
|
|
|
|
return ranges::end(__derived()) - ranges::begin(__derived());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class _D2 = _Derived>
|
2021-07-20 00:34:56 +08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2021-05-01 06:24:28 +08:00
|
|
|
constexpr auto size() const
|
2022-02-10 03:31:21 +08:00
|
|
|
requires forward_range<const _D2> && sized_sentinel_for<sentinel_t<const _D2>, iterator_t<const _D2>>
|
2021-05-01 06:24:28 +08:00
|
|
|
{
|
|
|
|
return ranges::end(__derived()) - ranges::begin(__derived());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class _D2 = _Derived>
|
2021-07-20 00:34:56 +08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2021-05-01 06:24:28 +08:00
|
|
|
constexpr decltype(auto) front()
|
|
|
|
requires forward_range<_D2>
|
|
|
|
{
|
|
|
|
_LIBCPP_ASSERT(!empty(),
|
|
|
|
"Precondition `!empty()` not satisfied. `.front()` called on an empty view.");
|
|
|
|
return *ranges::begin(__derived());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class _D2 = _Derived>
|
2021-07-20 00:34:56 +08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2021-05-01 06:24:28 +08:00
|
|
|
constexpr decltype(auto) front() const
|
|
|
|
requires forward_range<const _D2>
|
|
|
|
{
|
|
|
|
_LIBCPP_ASSERT(!empty(),
|
|
|
|
"Precondition `!empty()` not satisfied. `.front()` called on an empty view.");
|
|
|
|
return *ranges::begin(__derived());
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class _D2 = _Derived>
|
2021-07-20 00:34:56 +08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2021-05-01 06:24:28 +08:00
|
|
|
constexpr decltype(auto) back()
|
|
|
|
requires bidirectional_range<_D2> && common_range<_D2>
|
|
|
|
{
|
|
|
|
_LIBCPP_ASSERT(!empty(),
|
|
|
|
"Precondition `!empty()` not satisfied. `.back()` called on an empty view.");
|
|
|
|
return *ranges::prev(ranges::end(__derived()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<class _D2 = _Derived>
|
2021-07-20 00:34:56 +08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2021-05-01 06:24:28 +08:00
|
|
|
constexpr decltype(auto) back() const
|
|
|
|
requires bidirectional_range<const _D2> && common_range<const _D2>
|
|
|
|
{
|
|
|
|
_LIBCPP_ASSERT(!empty(),
|
|
|
|
"Precondition `!empty()` not satisfied. `.back()` called on an empty view.");
|
|
|
|
return *ranges::prev(ranges::end(__derived()));
|
|
|
|
}
|
|
|
|
|
|
|
|
template<random_access_range _RARange = _Derived>
|
2021-07-20 00:34:56 +08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2021-05-01 06:24:28 +08:00
|
|
|
constexpr decltype(auto) operator[](range_difference_t<_RARange> __index)
|
|
|
|
{
|
|
|
|
return ranges::begin(__derived())[__index];
|
|
|
|
}
|
|
|
|
|
|
|
|
template<random_access_range _RARange = const _Derived>
|
2021-07-20 00:34:56 +08:00
|
|
|
_LIBCPP_HIDE_FROM_ABI
|
2021-05-01 06:24:28 +08:00
|
|
|
constexpr decltype(auto) operator[](range_difference_t<_RARange> __index) const
|
|
|
|
{
|
|
|
|
return ranges::begin(__derived())[__index];
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-02-02 01:11:49 +08:00
|
|
|
} // namespace ranges
|
2021-05-01 06:24:28 +08:00
|
|
|
|
2022-03-12 23:46:57 +08:00
|
|
|
#endif // _LIBCPP_STD_VER > 17 && !defined(_LIBCPP_HAS_NO_INCOMPLETE_RANGES)
|
2021-05-01 06:24:28 +08:00
|
|
|
|
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
|
|
|
#endif // _LIBCPP___RANGES_VIEW_INTERFACE_H
|