1 | /*
|
---|
2 | * Copyright (c) 2017 Jaroslav Jindrak
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | #ifndef LIBCPP_UTILITY
|
---|
30 | #define LIBCPP_UTILITY
|
---|
31 |
|
---|
32 | #include <cstdint>
|
---|
33 | #include <internal/type_transformation.hpp>
|
---|
34 | #include <type_traits>
|
---|
35 |
|
---|
36 | namespace std
|
---|
37 | {
|
---|
38 | /**
|
---|
39 | * 20.2.1, operators:
|
---|
40 | */
|
---|
41 |
|
---|
42 | namespace rel_ops
|
---|
43 | {
|
---|
44 | template<typename T>
|
---|
45 | bool operator!=(const T& lhs, const T& rhs)
|
---|
46 | {
|
---|
47 | return !(lhs == rhs);
|
---|
48 | }
|
---|
49 |
|
---|
50 | template<typename T>
|
---|
51 | bool operator>(const T& lhs, const T& rhs)
|
---|
52 | {
|
---|
53 | return (rhs < lhs);
|
---|
54 | }
|
---|
55 |
|
---|
56 | template<typename T>
|
---|
57 | bool operator<=(const T& lhs, const T& rhs)
|
---|
58 | {
|
---|
59 | return !(rhs < lhs);
|
---|
60 | }
|
---|
61 |
|
---|
62 | template<typename T>
|
---|
63 | bool operator>=(const T& lhs, const T& rhs)
|
---|
64 | {
|
---|
65 | return !(lhs < rhs);
|
---|
66 | }
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * 20.2.4, forward/move helpers:
|
---|
71 | */
|
---|
72 |
|
---|
73 | template<class T>
|
---|
74 | constexpr T&& forward(remove_reference_t<T>& t) noexcept
|
---|
75 | {
|
---|
76 | return static_cast<T&&>(t);
|
---|
77 | }
|
---|
78 |
|
---|
79 | template<class T>
|
---|
80 | constexpr T&& forward(remove_reference_t<T>&& t) noexcept
|
---|
81 | {
|
---|
82 | return static_cast<T&&>(t);
|
---|
83 | }
|
---|
84 |
|
---|
85 | template<class T>
|
---|
86 | constexpr remove_reference_t<T>&& move(T&& t) noexcept
|
---|
87 | {
|
---|
88 | return static_cast<remove_reference_t<T>&&>(t);
|
---|
89 | }
|
---|
90 |
|
---|
91 | /**
|
---|
92 | * 20.2.2, swap:
|
---|
93 | */
|
---|
94 |
|
---|
95 | template<class T>
|
---|
96 | void swap(T& x, T& y)
|
---|
97 | /* noexcept(is_nothrow_move_constructible<T>::value && */
|
---|
98 | /* is_nothrow_move_assignable<T>::value) */
|
---|
99 | {
|
---|
100 | T tmp{move(x)};
|
---|
101 | x = move(y);
|
---|
102 | y = move(tmp);
|
---|
103 | }
|
---|
104 |
|
---|
105 | template<class T, size_t N>
|
---|
106 | void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)))
|
---|
107 | {
|
---|
108 | // TODO: Use swap_ranges(a, a + N, b); when implemented.
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * 20.2.3, exchange:
|
---|
113 | */
|
---|
114 |
|
---|
115 | template<class T, class U = T>
|
---|
116 | T exchange(T& obj, U&& new_val)
|
---|
117 | {
|
---|
118 | T old_val = move(obj);
|
---|
119 | obj = forward<U>(new_val);
|
---|
120 |
|
---|
121 | return old_val;
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * 20.2.5, function template declval:
|
---|
126 | * Note: This function only needs declaration, not
|
---|
127 | * implementation.
|
---|
128 | */
|
---|
129 |
|
---|
130 | template<class T>
|
---|
131 | add_rvalue_reference_t<T> declval() noexcept;
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * 20.3, pairs:
|
---|
135 | */
|
---|
136 |
|
---|
137 | struct piecewise_construct_t
|
---|
138 | {
|
---|
139 | explicit piecewise_construct_t() = default;
|
---|
140 | };
|
---|
141 |
|
---|
142 | template<typename T1, typename T2>
|
---|
143 | struct pair
|
---|
144 | {
|
---|
145 | using first_type = T1;
|
---|
146 | using second_type = T2;
|
---|
147 |
|
---|
148 | T1 first;
|
---|
149 | T2 second;
|
---|
150 |
|
---|
151 | pair(const pair&) = default;
|
---|
152 | pair(pair&&) = default;
|
---|
153 |
|
---|
154 | constexpr pair()
|
---|
155 | : first{}, second{}
|
---|
156 | { /* DUMMY BODY */ }
|
---|
157 |
|
---|
158 | constexpr pair(const T1& x, const T2& y)
|
---|
159 | : first{x}, second{y}
|
---|
160 | { /* DUMMY BODY */ }
|
---|
161 |
|
---|
162 | template<typename U, typename V>
|
---|
163 | constexpr pair(U&& x, V&& y)
|
---|
164 | : first(x), second(y)
|
---|
165 | { /* DUMMY BODY */ }
|
---|
166 |
|
---|
167 | template<typename U, typename V>
|
---|
168 | constexpr pair(const pair<U, V>& other)
|
---|
169 | : first(other.first), second(other.second)
|
---|
170 | { /* DUMMY BODY */ }
|
---|
171 |
|
---|
172 | template<typename U, typename V>
|
---|
173 | constexpr pair(pair<U, V>&& other)
|
---|
174 | : first(forward<first_type>(other.first)),
|
---|
175 | second(forward<second_type>(other.second))
|
---|
176 | { /* DUMMY BODY */ }
|
---|
177 |
|
---|
178 | /* TODO: need tuple, piecewise_construct_t
|
---|
179 | template<class... Args1, class... Args2>
|
---|
180 | pair(piecewise_construct_t, tuple<Args1...> first_args, tuple<Args2...> second_args)
|
---|
181 | {
|
---|
182 | // TODO:
|
---|
183 | }
|
---|
184 | */
|
---|
185 |
|
---|
186 | pair& operator=(const pair& other)
|
---|
187 | {
|
---|
188 | first = other.first;
|
---|
189 | second = other.second;
|
---|
190 |
|
---|
191 | return *this;
|
---|
192 | }
|
---|
193 |
|
---|
194 | template<typename U, typename V>
|
---|
195 | pair& operator=(const pair<U, V>& other)
|
---|
196 | {
|
---|
197 | first = other.first;
|
---|
198 | second = other.second;
|
---|
199 |
|
---|
200 | return *this;
|
---|
201 | }
|
---|
202 |
|
---|
203 | pair& operator=(pair&& other) noexcept
|
---|
204 | {
|
---|
205 | first = forward<first_type>(other.first);
|
---|
206 | second = forward<second_type>(other.second);
|
---|
207 |
|
---|
208 | return *this;
|
---|
209 | }
|
---|
210 | };
|
---|
211 |
|
---|
212 | /**
|
---|
213 | * 20.3.3, specialized algorithms:
|
---|
214 | */
|
---|
215 |
|
---|
216 | template<class T1, class T2>
|
---|
217 | constexpr bool operator==(const pair<T1, T2>& lhs,
|
---|
218 | const pair<T1, T2>& rhs)
|
---|
219 | {
|
---|
220 | return lhs.first == rhs.first && lhs.second == rhs.second;
|
---|
221 | }
|
---|
222 |
|
---|
223 | template<class T1, class T2>
|
---|
224 | constexpr bool operator<(const pair<T1, T2>& lhs,
|
---|
225 | const pair<T1, T2>& rhs)
|
---|
226 | {
|
---|
227 | return lhs.first < rhs.first ||
|
---|
228 | (!(rhs.first < lhs.first) && lhs.second < rhs.second);
|
---|
229 | }
|
---|
230 |
|
---|
231 | template<class T1, class T2>
|
---|
232 | constexpr bool operator!=(const pair<T1, T2>& lhs,
|
---|
233 | const pair<T1, T2>& rhs)
|
---|
234 | {
|
---|
235 | return !(lhs == rhs);
|
---|
236 | }
|
---|
237 |
|
---|
238 | template<class T1, class T2>
|
---|
239 | constexpr bool operator>(const pair<T1, T2>& lhs,
|
---|
240 | const pair<T1, T2>& rhs)
|
---|
241 | {
|
---|
242 | return rhs < lhs;
|
---|
243 | }
|
---|
244 |
|
---|
245 | template<class T1, class T2>
|
---|
246 | constexpr bool operator>=(const pair<T1, T2>& lhs,
|
---|
247 | const pair<T1, T2>& rhs)
|
---|
248 | {
|
---|
249 | return !(lhs < rhs);
|
---|
250 | }
|
---|
251 |
|
---|
252 | template<class T1, class T2>
|
---|
253 | constexpr bool operator<=(const pair<T1, T2>& lhs,
|
---|
254 | const pair<T1, T2>& rhs)
|
---|
255 | {
|
---|
256 | return !(rhs < lhs);
|
---|
257 | }
|
---|
258 |
|
---|
259 | template<class T1, class T2>
|
---|
260 | constexpr void swap(pair<T1, T2>& lhs, pair<T1, T2>& rhs)
|
---|
261 | noexcept(noexcept(lhs.swap(rhs)))
|
---|
262 | {
|
---|
263 | lhs.swap(rhs);
|
---|
264 | }
|
---|
265 |
|
---|
266 | template<class T1, class T2>
|
---|
267 | constexpr auto make_pair(T1&& t1, T2&& t2)
|
---|
268 | {
|
---|
269 | return pair<
|
---|
270 | aux::transform_tuple_types_t<T1>,
|
---|
271 | aux::transform_tuple_types_t<T2>
|
---|
272 | >{
|
---|
273 | forward<T1>(t1), forward<T2>(t2)
|
---|
274 | };
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * 20.3.4, tuple-like access to pair:
|
---|
279 | */
|
---|
280 |
|
---|
281 | template<class>
|
---|
282 | struct tuple_size;
|
---|
283 |
|
---|
284 | template<class T1, class T2>
|
---|
285 | struct tuple_size<pair<T1, T2>>
|
---|
286 | : integral_constant<size_t, 2>
|
---|
287 | { /* DUMMY BODY */ };
|
---|
288 |
|
---|
289 | template<size_t, class>
|
---|
290 | struct tuple_element;
|
---|
291 |
|
---|
292 | template<class T1, class T2>
|
---|
293 | struct tuple_element<0, pair<T1, T2>>
|
---|
294 | : aux::type_is<T1>
|
---|
295 | { /* DUMMY BODY */ };
|
---|
296 |
|
---|
297 | template<class T1, class T2>
|
---|
298 | struct tuple_element<1, pair<T1, T2>>
|
---|
299 | : aux::type_is<T2>
|
---|
300 | { /* DUMMY BODY */ };
|
---|
301 |
|
---|
302 | template<size_t I, class T>
|
---|
303 | using tuple_element_t = typename tuple_element<I, T>::type;
|
---|
304 |
|
---|
305 | template<size_t I, class T1, class T2>
|
---|
306 | constexpr tuple_element_t<I, pair<T1, T2>>&
|
---|
307 | get(pair<T1, T2>& p) noexcept
|
---|
308 | {
|
---|
309 | if constexpr (I == 0)
|
---|
310 | return p.first;
|
---|
311 | else
|
---|
312 | return p.second;
|
---|
313 | }
|
---|
314 |
|
---|
315 | template<size_t I, class T1, class T2>
|
---|
316 | constexpr const tuple_element_t<I, pair<T1, T2>>&
|
---|
317 | get(const pair<T1, T2>& p) noexcept
|
---|
318 | {
|
---|
319 | if constexpr (I == 0)
|
---|
320 | return p.first;
|
---|
321 | else
|
---|
322 | return p.second;
|
---|
323 | }
|
---|
324 |
|
---|
325 | template<size_t I, class T1, class T2>
|
---|
326 | constexpr tuple_element_t<I, pair<T1, T2>>&&
|
---|
327 | get(pair<T1, T2>&& p) noexcept
|
---|
328 | {
|
---|
329 | if constexpr (I == 0)
|
---|
330 | return forward<T1>(p.first);
|
---|
331 | else
|
---|
332 | return forward<T2>(p.second);
|
---|
333 | }
|
---|
334 |
|
---|
335 | template<class T, class U>
|
---|
336 | constexpr T& get(pair<T, U>& p) noexcept
|
---|
337 | {
|
---|
338 | static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
|
---|
339 |
|
---|
340 | return get<0>(p);
|
---|
341 | }
|
---|
342 |
|
---|
343 | template<class T, class U>
|
---|
344 | constexpr const T& get(const pair<T, U>& p) noexcept
|
---|
345 | {
|
---|
346 | static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
|
---|
347 |
|
---|
348 | return get<0>(p);
|
---|
349 | }
|
---|
350 |
|
---|
351 | template<class T, class U>
|
---|
352 | constexpr T&& get(pair<T, U>&& p) noexcept
|
---|
353 | {
|
---|
354 | static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
|
---|
355 |
|
---|
356 | return get<0>(move(p));
|
---|
357 | }
|
---|
358 |
|
---|
359 | template<class T, class U>
|
---|
360 | constexpr T& get(pair<U, T>& p) noexcept
|
---|
361 | {
|
---|
362 | static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
|
---|
363 |
|
---|
364 | return get<1>(p);
|
---|
365 | }
|
---|
366 |
|
---|
367 | template<class T, class U>
|
---|
368 | constexpr const T& get(const pair<U, T>& p) noexcept
|
---|
369 | {
|
---|
370 | static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
|
---|
371 |
|
---|
372 | return get<1>(p);
|
---|
373 | }
|
---|
374 |
|
---|
375 | template<class T, class U>
|
---|
376 | constexpr T&& get(pair<U, T>&& p) noexcept
|
---|
377 | {
|
---|
378 | static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
|
---|
379 |
|
---|
380 | return get<1>(move(p));
|
---|
381 | }
|
---|
382 |
|
---|
383 | /**
|
---|
384 | * 20.5.2, class template integer_sequence:
|
---|
385 | */
|
---|
386 |
|
---|
387 | template<class T, T... Is>
|
---|
388 | struct integer_sequence
|
---|
389 | {
|
---|
390 | using value_type = T;
|
---|
391 |
|
---|
392 | static constexpr size_t size() noexcept
|
---|
393 | {
|
---|
394 | return sizeof...(Is);
|
---|
395 | }
|
---|
396 |
|
---|
397 | using next = integer_sequence<T, Is..., sizeof...(Is)>;
|
---|
398 | };
|
---|
399 |
|
---|
400 | template<std::size_t... Is>
|
---|
401 | using index_sequence = integer_sequence<std::size_t, Is...>;
|
---|
402 |
|
---|
403 | /**
|
---|
404 | * 20.5.3, alias template make_integer_sequence:
|
---|
405 | */
|
---|
406 |
|
---|
407 | namespace aux
|
---|
408 | {
|
---|
409 | template<class T, uintmax_t N>
|
---|
410 | struct make_integer_sequence
|
---|
411 | {
|
---|
412 | /**
|
---|
413 | * Recursive to the bottom case below, appends sizeof...(Is) in
|
---|
414 | * every next "call", building the sequence.
|
---|
415 | */
|
---|
416 | using type = typename make_integer_sequence<T, N - 1>::type::next;
|
---|
417 | };
|
---|
418 |
|
---|
419 | template<class T>
|
---|
420 | struct make_integer_sequence<T, std::uintmax_t(0)>
|
---|
421 | {
|
---|
422 | using type = integer_sequence<T>;
|
---|
423 | };
|
---|
424 | }
|
---|
425 |
|
---|
426 |
|
---|
427 | /**
|
---|
428 | * Problem: We can't specialize the N parameter because it is a value parameter
|
---|
429 | * depending on a type parameter.
|
---|
430 | * Solution: According to the standard: if N is negative, the program is ill-formed,
|
---|
431 | * so we just recast it to uintmax_t :)
|
---|
432 | */
|
---|
433 | template<class T, T N>
|
---|
434 | using make_integer_sequence = typename aux::make_integer_sequence<T, std::uintmax_t(N)>::type;
|
---|
435 |
|
---|
436 | template<size_t N>
|
---|
437 | using make_index_sequence = make_integer_sequence<std::size_t, N>;
|
---|
438 | }
|
---|
439 |
|
---|
440 | #endif
|
---|