blob: 7d021d9580299b88157de30ca61bd1a482e8104e [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- utility -----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5// See https://llvm.org/LICENSE.txt for license information.
6// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
8//===----------------------------------------------------------------------===//
9
10#ifndef _LIBCPP_UTILITY
11#define _LIBCPP_UTILITY
12
13/*
14 utility synopsis
15
Louis Dionneb5769a32018-07-05 16:16:03 +000016#include <initializer_list>
17
Howard Hinnantc51e1022010-05-11 19:42:16 +000018namespace std
19{
20
21template <class T>
22 void
23 swap(T& a, T& b);
24
25namespace rel_ops
26{
27 template<class T> bool operator!=(const T&, const T&);
28 template<class T> bool operator> (const T&, const T&);
29 template<class T> bool operator<=(const T&, const T&);
30 template<class T> bool operator>=(const T&, const T&);
31}
32
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000033template<class T>
34void
35swap(T& a, T& b) noexcept(is_nothrow_move_constructible<T>::value &&
36 is_nothrow_move_assignable<T>::value);
Howard Hinnantc51e1022010-05-11 19:42:16 +000037
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000038template <class T, size_t N>
39void
40swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)));
41
Marshall Clowfe9aa372013-07-15 20:46:11 +000042template <class T> T&& forward(typename remove_reference<T>::type& t) noexcept; // constexpr in C++14
43template <class T> T&& forward(typename remove_reference<T>::type&& t) noexcept; // constexpr in C++14
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000044
Marshall Clowfe9aa372013-07-15 20:46:11 +000045template <class T> typename remove_reference<T>::type&& move(T&&) noexcept; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000046
47template <class T>
48 typename conditional
49 <
Howard Hinnanta9a897e2010-11-19 22:17:28 +000050 !is_nothrow_move_constructible<T>::value && is_copy_constructible<T>::value,
Howard Hinnantc51e1022010-05-11 19:42:16 +000051 const T&,
52 T&&
53 >::type
Marshall Clowfe9aa372013-07-15 20:46:11 +000054 move_if_noexcept(T& x) noexcept; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000055
Marshall Clow5d305742018-02-11 21:51:49 +000056template <class T> constexpr add_const_t<T>& as_const(T& t) noexcept; // C++17
Marshall Clow845efe42015-11-17 00:08:08 +000057template <class T> void as_const(const T&&) = delete; // C++17
58
Howard Hinnantc51e1022010-05-11 19:42:16 +000059template <class T> typename add_rvalue_reference<T>::type declval() noexcept;
60
Kamlesh Kumarc1964592021-04-20 04:48:34 +053061template<class T, class U> constexpr bool cmp_equal(T t, U u) noexcept; // C++20
62template<class T, class U> constexpr bool cmp_not_equal(T t, U u) noexcept; // C++20
63template<class T, class U> constexpr bool cmp_less(T t, U u) noexcept; // C++20
64template<class T, class U> constexpr bool cmp_greater(T t, U u) noexcept; // C++20
65template<class T, class U> constexpr bool cmp_less_equal(T t, U u) noexcept; // C++20
66template<class T, class U> constexpr bool cmp_greater_equal(T t, U u) noexcept; // C++20
67template<class R, class T> constexpr bool in_range(T t) noexcept; // C++20
68
Howard Hinnantc51e1022010-05-11 19:42:16 +000069template <class T1, class T2>
70struct pair
71{
72 typedef T1 first_type;
73 typedef T2 second_type;
74
75 T1 first;
76 T2 second;
77
78 pair(const pair&) = default;
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000079 pair(pair&&) = default;
Louis Dionnea7a2beb2019-09-26 14:51:10 +000080 explicit(see-below) constexpr pair();
Louis Dionne1afad1d2019-07-22 20:45:23 +000081 explicit(see-below) pair(const T1& x, const T2& y); // constexpr in C++14
Louis Dionne29263ac2021-08-31 18:13:33 -040082 template <class U = T1, class V = T2> explicit(see-below) pair(U&&, V&&); // constexpr in C++14
Louis Dionne1afad1d2019-07-22 20:45:23 +000083 template <class U, class V> explicit(see-below) pair(const pair<U, V>& p); // constexpr in C++14
84 template <class U, class V> explicit(see-below) pair(pair<U, V>&& p); // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000085 template <class... Args1, class... Args2>
86 pair(piecewise_construct_t, tuple<Args1...> first_args,
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -050087 tuple<Args2...> second_args); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000088
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -050089 template <class U, class V> pair& operator=(const pair<U, V>& p); // constexpr in C++20
Howard Hinnantdbfd4b42011-05-27 15:04:19 +000090 pair& operator=(pair&& p) noexcept(is_nothrow_move_assignable<T1>::value &&
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -050091 is_nothrow_move_assignable<T2>::value); // constexpr in C++20
92 template <class U, class V> pair& operator=(pair<U, V>&& p); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000093
Eric Fiselier6bfed252016-04-21 23:38:59 +000094 void swap(pair& p) noexcept(is_nothrow_swappable_v<T1> &&
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -050095 is_nothrow_swappable_v<T2>); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +000096};
97
Marshall Clowf00c56c2013-07-16 17:45:44 +000098template <class T1, class T2> bool operator==(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14
Kent Ross47cc52e2021-09-22 22:36:23 -070099template <class T1, class T2> bool operator!=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
100template <class T1, class T2> bool operator< (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
101template <class T1, class T2> bool operator> (const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
102template <class T1, class T2> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
103template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
104template <class T1, class T2>
105 constexpr common_comparison_type_t<synth-three-way-result<T1>,
106 synth-three-way-result<T2>>
107 operator<=>(const pair<T1,T2>&, const pair<T1,T2>&); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000108
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500109template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&); // constexpr in C++14
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000110template <class T1, class T2>
111void
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500112swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y))); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000113
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000114struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000115inline constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000116
Marshall Clowce62b4d2019-01-11 21:57:12 +0000117template <class T> struct tuple_size;
Louis Dionnee684aa62019-04-01 16:39:34 +0000118template <size_t I, class T> struct tuple_element;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000119
Marshall Clowf50d66f2014-03-03 06:18:11 +0000120template <class T1, class T2> struct tuple_size<pair<T1, T2> >;
121template <class T1, class T2> struct tuple_element<0, pair<T1, T2> >;
122template <class T1, class T2> struct tuple_element<1, pair<T1, T2> >;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000123
124template<size_t I, class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000125 typename tuple_element<I, pair<T1, T2> >::type&
126 get(pair<T1, T2>&) noexcept; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000127
128template<size_t I, class T1, class T2>
Marshall Clow36907512015-11-19 19:45:29 +0000129 const typename tuple_element<I, pair<T1, T2> >::type&
Marshall Clowf50d66f2014-03-03 06:18:11 +0000130 get(const pair<T1, T2>&) noexcept; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131
Howard Hinnant22e97242010-11-17 19:52:17 +0000132template<size_t I, class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000133 typename tuple_element<I, pair<T1, T2> >::type&&
134 get(pair<T1, T2>&&) noexcept; // constexpr in C++14
Howard Hinnant22e97242010-11-17 19:52:17 +0000135
Eric Fiselier6dea8092015-12-18 00:36:55 +0000136template<size_t I, class T1, class T2>
137 const typename tuple_element<I, pair<T1, T2> >::type&&
138 get(const pair<T1, T2>&&) noexcept; // constexpr in C++14
139
Marshall Clow15b02e02013-07-13 02:54:05 +0000140template<class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000141 constexpr T1& get(pair<T1, T2>&) noexcept; // C++14
Marshall Clow15b02e02013-07-13 02:54:05 +0000142
Eric Fiselier6dea8092015-12-18 00:36:55 +0000143template<class T1, class T2>
Marshall Clow36907512015-11-19 19:45:29 +0000144 constexpr const T1& get(const pair<T1, T2>&) noexcept; // C++14
Marshall Clow15b02e02013-07-13 02:54:05 +0000145
Eric Fiselier6dea8092015-12-18 00:36:55 +0000146template<class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000147 constexpr T1&& get(pair<T1, T2>&&) noexcept; // C++14
Marshall Clow15b02e02013-07-13 02:54:05 +0000148
Eric Fiselier6dea8092015-12-18 00:36:55 +0000149template<class T1, class T2>
150 constexpr const T1&& get(const pair<T1, T2>&&) noexcept; // C++14
151
152template<class T1, class T2>
153 constexpr T1& get(pair<T2, T1>&) noexcept; // C++14
154
155template<class T1, class T2>
156 constexpr const T1& get(const pair<T2, T1>&) noexcept; // C++14
157
158template<class T1, class T2>
159 constexpr T1&& get(pair<T2, T1>&&) noexcept; // C++14
160
161template<class T1, class T2>
162 constexpr const T1&& get(const pair<T2, T1>&&) noexcept; // C++14
163
Marshall Clow9a970d82013-07-01 16:26:55 +0000164// C++14
165
166template<class T, T... I>
167struct integer_sequence
168{
169 typedef T value_type;
170
171 static constexpr size_t size() noexcept;
172};
173
174template<size_t... I>
175 using index_sequence = integer_sequence<size_t, I...>;
176
177template<class T, T N>
178 using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>;
179template<size_t N>
180 using make_index_sequence = make_integer_sequence<size_t, N>;
181
182template<class... T>
183 using index_sequence_for = make_index_sequence<sizeof...(T)>;
184
Marshall Clowfa8ccca2016-06-19 19:29:52 +0000185template<class T, class U=T>
Joe Loserff0abf42021-10-11 14:34:37 -0400186 constexpr T exchange(T& obj, U&& new_value)
187 noexcept(is_nothrow_move_constructible<T>::value && is_nothrow_assignable<T&, U>::value); // constexpr in C++17, noexcept in C++23
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000188
189// 20.2.7, in-place construction // C++17
Eric Fiselier99b81712016-11-17 19:24:04 +0000190struct in_place_t {
191 explicit in_place_t() = default;
192};
193inline constexpr in_place_t in_place{};
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000194template <class T>
Eric Fiselier99b81712016-11-17 19:24:04 +0000195 struct in_place_type_t {
196 explicit in_place_type_t() = default;
197 };
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000198template <class T>
Eric Fiselier99b81712016-11-17 19:24:04 +0000199 inline constexpr in_place_type_t<T> in_place_type{};
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000200template <size_t I>
Eric Fiselier99b81712016-11-17 19:24:04 +0000201 struct in_place_index_t {
202 explicit in_place_index_t() = default;
203 };
204template <size_t I>
205 inline constexpr in_place_index_t<I> in_place_index{};
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000206
Marek Kurdej4fc4b4c2021-03-05 09:19:39 +0100207// [utility.underlying], to_underlying
208template <class T>
209 constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++2b
210
Howard Hinnantc51e1022010-05-11 19:42:16 +0000211} // std
212
213*/
214
215#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400216#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000217#include <__tuple>
Christopher Di Bella599a6c62021-06-09 23:10:17 +0000218#include <__utility/as_const.h>
219#include <__utility/cmp.h>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000220#include <__utility/declval.h>
Christopher Di Bella599a6c62021-06-09 23:10:17 +0000221#include <__utility/exchange.h>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000222#include <__utility/forward.h>
Christopher Di Bella599a6c62021-06-09 23:10:17 +0000223#include <__utility/in_place.h>
224#include <__utility/integer_sequence.h>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000225#include <__utility/move.h>
Christopher Di Bella599a6c62021-06-09 23:10:17 +0000226#include <__utility/pair.h>
227#include <__utility/piecewise_construct.h>
228#include <__utility/rel_ops.h>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000229#include <__utility/swap.h>
Mark de Wevera8b93aa2021-04-24 17:28:35 +0200230#include <__utility/to_underlying.h>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400231#include <compare>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400232#include <initializer_list>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000233#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400235#endif // _LIBCPP_UTILITY