aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/cxxsupp/libcxx/include/__ranges/size.h
blob: af0a8479f2ecaba839c3bcb9f42d43e58e78f1e9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// -*- 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_SIZE_H
#define _LIBCPP___RANGES_SIZE_H

#include <__config>
#include <__iterator/concepts.h>
#include <__iterator/iterator_traits.h>
#include <__ranges/access.h>
#include <__utility/decay_copy.h>
#include <__utility/forward.h>
#include <concepts>
#include <type_traits>

#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
#pragma GCC system_header
#endif

_LIBCPP_BEGIN_NAMESPACE_STD

#if !defined(_LIBCPP_HAS_NO_RANGES)

// clang-format off
namespace ranges {
template<class>
inline constexpr bool disable_sized_range = false;

// [range.prim.size]
namespace __size {
  void size(auto&) = delete;
  void size(const auto&) = delete;

  template <class _Tp>
  concept __size_enabled = !disable_sized_range<remove_cvref_t<_Tp>>;

  template <class _Tp>
  concept __member_size = __size_enabled<_Tp> && requires(_Tp&& __t) {
    { _VSTD::__decay_copy(_VSTD::forward<_Tp>(__t).size()) } -> __integer_like;
  };

  template <class _Tp>
  concept __unqualified_size =
    __size_enabled<_Tp> &&
    !__member_size<_Tp> &&
    __class_or_enum<remove_cvref_t<_Tp>> &&
    requires(_Tp&& __t) {
      { _VSTD::__decay_copy(size(_VSTD::forward<_Tp>(__t))) } -> __integer_like;
    };

  template <class _Tp>
  concept __difference =
    !__member_size<_Tp> &&
    !__unqualified_size<_Tp> &&
    __class_or_enum<remove_cvref_t<_Tp>> &&
    requires(_Tp&& __t) {
      { ranges::begin(__t) } -> forward_iterator;
      { ranges::end(__t) } -> sized_sentinel_for<decltype(ranges::begin(declval<_Tp>()))>;
    };

  struct __fn {
    template <class _Tp, size_t _Sz>
    [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_t operator()(_Tp (&&)[_Sz]) const noexcept {
      return _Sz;
    }

    template <class _Tp, size_t _Sz>
    [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr size_t operator()(_Tp (&)[_Sz]) const noexcept {
      return _Sz;
    }

    template <__member_size _Tp>
    [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __integer_like auto operator()(_Tp&& __t) const
        noexcept(noexcept(_VSTD::forward<_Tp>(__t).size())) {
      return _VSTD::forward<_Tp>(__t).size();
    }

    template <__unqualified_size _Tp>
    [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __integer_like auto operator()(_Tp&& __t) const
        noexcept(noexcept(size(_VSTD::forward<_Tp>(__t)))) {
      return size(_VSTD::forward<_Tp>(__t));
    }

    template<__difference _Tp>
    [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr __integer_like auto operator()(_Tp&& __t) const
        noexcept(noexcept(ranges::end(__t) - ranges::begin(__t))) {
      return _VSTD::__to_unsigned_like(ranges::end(__t) - ranges::begin(__t));
    }
  };
} // end namespace __size

inline namespace __cpo {
  inline constexpr auto size = __size::__fn{};
} // namespace __cpo

namespace __ssize {
  struct __fn {
    template<class _Tp>
      requires requires (_Tp&& __t) { ranges::size(__t); }
    [[nodiscard]] _LIBCPP_HIDE_FROM_ABI constexpr integral auto operator()(_Tp&& __t) const
        noexcept(noexcept(ranges::size(__t))) {
      using _Signed = make_signed_t<decltype(ranges::size(__t))>;
      if constexpr (sizeof(ptrdiff_t) > sizeof(_Signed))
        return static_cast<ptrdiff_t>(ranges::size(__t));
      else
        return static_cast<_Signed>(ranges::size(__t));
    }
  };
}

inline namespace __cpo {
  inline constexpr auto ssize = __ssize::__fn{};
} // namespace __cpo
} // namespace ranges

// clang-format off

#endif // !defined(_LIBCPP_HAS_NO_RANGES)

_LIBCPP_END_NAMESPACE_STD

#endif // _LIBCPP___RANGES_SIZE_H