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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the BastionOS freestanding C++ standard library.
//
//===----------------------------------------------------------------------===//
#ifndef _LIBBASTION_UTILITY_PAIR_H
#define _LIBBASTION_UTILITY_PAIR_H
#include <__config>
#include <__type_traits/integral_constant.h>
#include <__type_traits/type_modifications.h>
#include <__type_traits/construction_traits.h>
#include <__utility/move.h>
#include <__utility/swap.h>
#include <cstddef>
_LIBBASTION_BEGIN_NAMESPACE_STD
template<class _T1, class _T2>
struct pair {
using first_type = _T1;
using second_type = _T2;
_T1 first;
_T2 second;
// Default constructor
constexpr pair()
noexcept(is_nothrow_default_constructible_v<_T1> && is_nothrow_default_constructible_v<_T2>)
requires is_default_constructible_v<_T1> && is_default_constructible_v<_T2>
: first(), second() {}
// Copy from elements
constexpr pair(const _T1& __a, const _T2& __b)
noexcept(is_nothrow_copy_constructible_v<_T1> && is_nothrow_copy_constructible_v<_T2>)
requires is_copy_constructible_v<_T1> && is_copy_constructible_v<_T2>
: first(__a), second(__b) {}
// Converting constructor
template<class _U1, class _U2>
requires is_constructible_v<_T1, _U1> && is_constructible_v<_T2, _U2>
constexpr pair(_U1&& __a, _U2&& __b)
noexcept(is_nothrow_constructible_v<_T1, _U1> && is_nothrow_constructible_v<_T2, _U2>)
: first(std::forward<_U1>(__a)), second(std::forward<_U2>(__b)) {}
// Converting copy constructor
template<class _U1, class _U2>
requires is_constructible_v<_T1, const _U1&> && is_constructible_v<_T2, const _U2&>
constexpr pair(const pair<_U1, _U2>& __p)
noexcept(is_nothrow_constructible_v<_T1, const _U1&> && is_nothrow_constructible_v<_T2, const _U2&>)
: first(__p.first), second(__p.second) {}
// Converting move constructor
template<class _U1, class _U2>
requires is_constructible_v<_T1, _U1> && is_constructible_v<_T2, _U2>
constexpr pair(pair<_U1, _U2>&& __p)
noexcept(is_nothrow_constructible_v<_T1, _U1> && is_nothrow_constructible_v<_T2, _U2>)
: first(std::forward<_U1>(__p.first)), second(std::forward<_U2>(__p.second)) {}
pair(const pair&) = default;
pair(pair&&) = default;
// Assignment
constexpr pair& operator=(const pair& __p)
noexcept(is_nothrow_copy_assignable_v<_T1> && is_nothrow_copy_assignable_v<_T2>)
requires is_copy_assignable_v<_T1> && is_copy_assignable_v<_T2>
{
first = __p.first;
second = __p.second;
return *this;
}
constexpr pair& operator=(pair&& __p)
noexcept(is_nothrow_move_assignable_v<_T1> && is_nothrow_move_assignable_v<_T2>)
requires is_move_assignable_v<_T1> && is_move_assignable_v<_T2>
{
first = std::move(__p.first);
second = std::move(__p.second);
return *this;
}
template<class _U1, class _U2>
requires is_assignable_v<_T1&, const _U1&> && is_assignable_v<_T2&, const _U2&>
constexpr pair& operator=(const pair<_U1, _U2>& __p) {
first = __p.first;
second = __p.second;
return *this;
}
template<class _U1, class _U2>
requires is_assignable_v<_T1&, _U1> && is_assignable_v<_T2&, _U2>
constexpr pair& operator=(pair<_U1, _U2>&& __p) {
first = std::forward<_U1>(__p.first);
second = std::forward<_U2>(__p.second);
return *this;
}
constexpr void swap(pair& __p)
noexcept(is_nothrow_swappable_v<_T1> && is_nothrow_swappable_v<_T2>)
{
using std::swap;
swap(first, __p.first);
swap(second, __p.second);
}
};
// Deduction guide
template<class _T1, class _T2>
pair(_T1, _T2) -> pair<_T1, _T2>;
// Non-member swap
template<class _T1, class _T2>
inline constexpr void swap(pair<_T1, _T2>& __x, pair<_T1, _T2>& __y)
noexcept(noexcept(__x.swap(__y)))
{
__x.swap(__y);
}
// Comparison operators
template<class _T1, class _T2>
_LIBBASTION_NODISCARD inline constexpr bool operator==(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) {
return __x.first == __y.first && __x.second == __y.second;
}
template<class _T1, class _T2>
_LIBBASTION_NODISCARD inline constexpr bool operator!=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) {
return !(__x == __y);
}
template<class _T1, class _T2>
_LIBBASTION_NODISCARD inline constexpr bool operator<(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) {
return __x.first < __y.first || (!(__y.first < __x.first) && __x.second < __y.second);
}
template<class _T1, class _T2>
_LIBBASTION_NODISCARD inline constexpr bool operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) {
return __y < __x;
}
template<class _T1, class _T2>
_LIBBASTION_NODISCARD inline constexpr bool operator<=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) {
return !(__y < __x);
}
template<class _T1, class _T2>
_LIBBASTION_NODISCARD inline constexpr bool operator>=(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y) {
return !(__x < __y);
}
// make_pair
template<class _T1, class _T2>
_LIBBASTION_NODISCARD inline constexpr pair<__decay(_T1), __decay(_T2)> make_pair(_T1&& __a, _T2&& __b) {
return pair<__decay(_T1), __decay(_T2)>(std::forward<_T1>(__a), std::forward<_T2>(__b));
}
// Structured bindings support (tuple-like access)
template<class _T1, class _T2>
struct tuple_size<pair<_T1, _T2>> : integral_constant<size_t, 2> {};
template<size_t _Ip, class _T1, class _T2> struct tuple_element;
template<class _T1, class _T2> struct tuple_element<0, pair<_T1, _T2>> { using type = _T1; };
template<class _T1, class _T2> struct tuple_element<1, pair<_T1, _T2>> { using type = _T2; };
template<size_t _Ip, class _T1, class _T2>
using tuple_element_t = typename tuple_element<_Ip, pair<_T1, _T2>>::type;
template<size_t _Ip, class _T1, class _T2>
_LIBBASTION_NODISCARD inline constexpr auto& get(pair<_T1, _T2>& __p) noexcept {
if constexpr (_Ip == 0) return __p.first;
else return __p.second;
}
template<size_t _Ip, class _T1, class _T2>
_LIBBASTION_NODISCARD inline constexpr const auto& get(const pair<_T1, _T2>& __p) noexcept {
if constexpr (_Ip == 0) return __p.first;
else return __p.second;
}
template<size_t _Ip, class _T1, class _T2>
_LIBBASTION_NODISCARD inline constexpr auto&& get(pair<_T1, _T2>&& __p) noexcept {
if constexpr (_Ip == 0) return std::move(__p.first);
else return std::move(__p.second);
}
template<size_t _Ip, class _T1, class _T2>
_LIBBASTION_NODISCARD inline constexpr const auto&& get(const pair<_T1, _T2>&& __p) noexcept {
if constexpr (_Ip == 0) return std::move(__p.first);
else return std::move(__p.second);
}
_LIBBASTION_END_NAMESPACE_STD
#endif // _LIBBASTION_UTILITY_PAIR_H
|