blob: ac8470e269d0ca4352bd126e0ba1bd49b599a6a1 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
Louis Dionne9bd93882021-11-17 16:25:01 -05002//===----------------------------------------------------------------------===//
Howard Hinnantc51e1022010-05-11 19:42:16 +00003//
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
Konstantin Varlamovebb31192021-10-28 00:36:19 -070098template<class T1, class T2> pair(T1, T2) -> pair<T1, T2>;
99
Marshall Clowf00c56c2013-07-16 17:45:44 +0000100template <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 -0700101template <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> bool operator>=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
105template <class T1, class T2> bool operator<=(const pair<T1,T2>&, const pair<T1,T2>&); // constexpr in C++14, removed in C++20
106template <class T1, class T2>
107 constexpr common_comparison_type_t<synth-three-way-result<T1>,
108 synth-three-way-result<T2>>
109 operator<=>(const pair<T1,T2>&, const pair<T1,T2>&); // C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000110
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500111template <class T1, class T2> pair<V1, V2> make_pair(T1&&, T2&&); // constexpr in C++14
Howard Hinnantdbfd4b42011-05-27 15:04:19 +0000112template <class T1, class T2>
113void
Arthur O'Dwyer20638cc2021-02-09 19:12:16 -0500114swap(pair<T1, T2>& x, pair<T1, T2>& y) noexcept(noexcept(x.swap(y))); // constexpr in C++20
Howard Hinnantc51e1022010-05-11 19:42:16 +0000115
Louis Dionnea7a2beb2019-09-26 14:51:10 +0000116struct piecewise_construct_t { explicit piecewise_construct_t() = default; };
Marshall Clowf1bf62f2018-01-02 17:17:01 +0000117inline constexpr piecewise_construct_t piecewise_construct = piecewise_construct_t();
Howard Hinnantc51e1022010-05-11 19:42:16 +0000118
Marshall Clowce62b4d2019-01-11 21:57:12 +0000119template <class T> struct tuple_size;
Louis Dionnee684aa62019-04-01 16:39:34 +0000120template <size_t I, class T> struct tuple_element;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000121
Marshall Clowf50d66f2014-03-03 06:18:11 +0000122template <class T1, class T2> struct tuple_size<pair<T1, T2> >;
123template <class T1, class T2> struct tuple_element<0, pair<T1, T2> >;
124template <class T1, class T2> struct tuple_element<1, pair<T1, T2> >;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000125
126template<size_t I, class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000127 typename tuple_element<I, pair<T1, T2> >::type&
128 get(pair<T1, T2>&) noexcept; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000129
130template<size_t I, class T1, class T2>
Marshall Clow36907512015-11-19 19:45:29 +0000131 const typename tuple_element<I, pair<T1, T2> >::type&
Marshall Clowf50d66f2014-03-03 06:18:11 +0000132 get(const pair<T1, T2>&) noexcept; // constexpr in C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +0000133
Howard Hinnant22e97242010-11-17 19:52:17 +0000134template<size_t I, class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000135 typename tuple_element<I, pair<T1, T2> >::type&&
136 get(pair<T1, T2>&&) noexcept; // constexpr in C++14
Howard Hinnant22e97242010-11-17 19:52:17 +0000137
Eric Fiselier6dea8092015-12-18 00:36:55 +0000138template<size_t I, class T1, class T2>
139 const typename tuple_element<I, pair<T1, T2> >::type&&
140 get(const pair<T1, T2>&&) noexcept; // constexpr in C++14
141
Marshall Clow15b02e02013-07-13 02:54:05 +0000142template<class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000143 constexpr T1& get(pair<T1, T2>&) noexcept; // C++14
Marshall Clow15b02e02013-07-13 02:54:05 +0000144
Eric Fiselier6dea8092015-12-18 00:36:55 +0000145template<class T1, class T2>
Marshall Clow36907512015-11-19 19:45:29 +0000146 constexpr const T1& get(const pair<T1, T2>&) noexcept; // C++14
Marshall Clow15b02e02013-07-13 02:54:05 +0000147
Eric Fiselier6dea8092015-12-18 00:36:55 +0000148template<class T1, class T2>
Marshall Clowf50d66f2014-03-03 06:18:11 +0000149 constexpr T1&& get(pair<T1, T2>&&) noexcept; // C++14
Marshall Clow15b02e02013-07-13 02:54:05 +0000150
Eric Fiselier6dea8092015-12-18 00:36:55 +0000151template<class T1, class T2>
152 constexpr const T1&& get(const pair<T1, T2>&&) noexcept; // C++14
153
154template<class T1, class T2>
155 constexpr T1& get(pair<T2, T1>&) noexcept; // C++14
156
157template<class T1, class T2>
158 constexpr const T1& get(const pair<T2, T1>&) noexcept; // C++14
159
160template<class T1, class T2>
161 constexpr T1&& get(pair<T2, T1>&&) noexcept; // C++14
162
163template<class T1, class T2>
164 constexpr const T1&& get(const pair<T2, T1>&&) noexcept; // C++14
165
Marshall Clow9a970d82013-07-01 16:26:55 +0000166// C++14
167
168template<class T, T... I>
169struct integer_sequence
170{
171 typedef T value_type;
172
173 static constexpr size_t size() noexcept;
174};
175
176template<size_t... I>
177 using index_sequence = integer_sequence<size_t, I...>;
178
179template<class T, T N>
180 using make_integer_sequence = integer_sequence<T, 0, 1, ..., N-1>;
181template<size_t N>
182 using make_index_sequence = make_integer_sequence<size_t, N>;
183
184template<class... T>
185 using index_sequence_for = make_index_sequence<sizeof...(T)>;
186
Marshall Clowfa8ccca2016-06-19 19:29:52 +0000187template<class T, class U=T>
Joe Loserff0abf42021-10-11 14:34:37 -0400188 constexpr T exchange(T& obj, U&& new_value)
189 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 +0000190
191// 20.2.7, in-place construction // C++17
Eric Fiselier99b81712016-11-17 19:24:04 +0000192struct in_place_t {
193 explicit in_place_t() = default;
194};
195inline constexpr in_place_t in_place{};
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000196template <class T>
Eric Fiselier99b81712016-11-17 19:24:04 +0000197 struct in_place_type_t {
198 explicit in_place_type_t() = default;
199 };
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000200template <class T>
Eric Fiselier99b81712016-11-17 19:24:04 +0000201 inline constexpr in_place_type_t<T> in_place_type{};
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000202template <size_t I>
Eric Fiselier99b81712016-11-17 19:24:04 +0000203 struct in_place_index_t {
204 explicit in_place_index_t() = default;
205 };
206template <size_t I>
207 inline constexpr in_place_index_t<I> in_place_index{};
Eric Fiseliera8a4f6a2016-07-23 22:19:19 +0000208
Marek Kurdej4fc4b4c2021-03-05 09:19:39 +0100209// [utility.underlying], to_underlying
210template <class T>
211 constexpr underlying_type_t<T> to_underlying( T value ) noexcept; // C++2b
212
Howard Hinnantc51e1022010-05-11 19:42:16 +0000213} // std
214
215*/
216
217#include <__config>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400218#include <__debug>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000219#include <__tuple>
Christopher Di Bella599a6c62021-06-09 23:10:17 +0000220#include <__utility/as_const.h>
221#include <__utility/cmp.h>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000222#include <__utility/declval.h>
Christopher Di Bella599a6c62021-06-09 23:10:17 +0000223#include <__utility/exchange.h>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000224#include <__utility/forward.h>
Christopher Di Bella599a6c62021-06-09 23:10:17 +0000225#include <__utility/in_place.h>
226#include <__utility/integer_sequence.h>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000227#include <__utility/move.h>
Christopher Di Bella599a6c62021-06-09 23:10:17 +0000228#include <__utility/pair.h>
229#include <__utility/piecewise_construct.h>
Arthur O'Dwyerb85724e2021-07-28 22:04:18 -0400230#include <__utility/priority_tag.h>
Christopher Di Bella599a6c62021-06-09 23:10:17 +0000231#include <__utility/rel_ops.h>
Christopher Di Bella41f26e82021-06-05 02:47:47 +0000232#include <__utility/swap.h>
Mark de Wevera8b93aa2021-04-24 17:28:35 +0200233#include <__utility/to_underlying.h>
Arthur O'Dwyer7deec122021-03-24 18:19:12 -0400234#include <compare>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400235#include <initializer_list>
Marshall Clow0a1e7502018-09-12 19:41:40 +0000236#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237
Arthur O'Dwyere3874622021-12-05 13:08:36 -0500238#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
239#pragma GCC system_header
240#endif
241
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400242#endif // _LIBCPP_UTILITY