[b1cd380c] | 1 | /*
|
---|
[9594c0c6] | 2 | * Copyright (c) 2018 Jaroslav Jindrak
|
---|
[b1cd380c] | 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 |
|
---|
[de53138] | 32 | #include <cstdint>
|
---|
[7a666789] | 33 | #include <internal/type_transformation.hpp>
|
---|
[c866a83] | 34 | #include <internal/utility/forward_move.hpp>
|
---|
[2084bfcd] | 35 | #include <type_traits>
|
---|
| 36 |
|
---|
[b1cd380c] | 37 | namespace std
|
---|
| 38 | {
|
---|
| 39 | /**
|
---|
| 40 | * 20.2.1, operators:
|
---|
| 41 | */
|
---|
[aab972f] | 42 |
|
---|
[f041811] | 43 | namespace rel_ops
|
---|
[b1cd380c] | 44 | {
|
---|
[f041811] | 45 | template<typename T>
|
---|
| 46 | bool operator!=(const T& lhs, const T& rhs)
|
---|
| 47 | {
|
---|
| 48 | return !(lhs == rhs);
|
---|
| 49 | }
|
---|
[b1cd380c] | 50 |
|
---|
[f041811] | 51 | template<typename T>
|
---|
| 52 | bool operator>(const T& lhs, const T& rhs)
|
---|
| 53 | {
|
---|
| 54 | return (rhs < lhs);
|
---|
| 55 | }
|
---|
[b1cd380c] | 56 |
|
---|
[f041811] | 57 | template<typename T>
|
---|
| 58 | bool operator<=(const T& lhs, const T& rhs)
|
---|
| 59 | {
|
---|
| 60 | return !(rhs < lhs);
|
---|
| 61 | }
|
---|
[b1cd380c] | 62 |
|
---|
[f041811] | 63 | template<typename T>
|
---|
| 64 | bool operator>=(const T& lhs, const T& rhs)
|
---|
| 65 | {
|
---|
| 66 | return !(lhs < rhs);
|
---|
| 67 | }
|
---|
[b1cd380c] | 68 | }
|
---|
| 69 |
|
---|
[5abc7fd] | 70 | /**
|
---|
| 71 | * 20.2.2, swap:
|
---|
| 72 | */
|
---|
| 73 |
|
---|
| 74 | template<class T>
|
---|
| 75 | void swap(T& x, T& y)
|
---|
[18944e0] | 76 | /* noexcept(is_nothrow_move_constructible<T>::value && */
|
---|
| 77 | /* is_nothrow_move_assignable<T>::value) */
|
---|
[5abc7fd] | 78 | {
|
---|
[add816c7] | 79 | T tmp{move(x)};
|
---|
| 80 | x = move(y);
|
---|
| 81 | y = move(tmp);
|
---|
[5abc7fd] | 82 | }
|
---|
| 83 |
|
---|
[9594c0c6] | 84 | template<class F1, class F2>
|
---|
| 85 | F2 swap_ranges(F1, F1, F2);
|
---|
| 86 |
|
---|
[5abc7fd] | 87 | template<class T, size_t N>
|
---|
| 88 | void swap(T (&a)[N], T (&b)[N]) noexcept(noexcept(swap(*a, *b)))
|
---|
| 89 | {
|
---|
[9594c0c6] | 90 | swap_ranges(a, a + N, b);
|
---|
[5abc7fd] | 91 | }
|
---|
| 92 |
|
---|
| 93 | /**
|
---|
| 94 | * 20.2.3, exchange:
|
---|
| 95 | */
|
---|
| 96 |
|
---|
| 97 | template<class T, class U = T>
|
---|
| 98 | T exchange(T& obj, U&& new_val)
|
---|
| 99 | {
|
---|
[add816c7] | 100 | T old_val = move(obj);
|
---|
| 101 | obj = forward<U>(new_val);
|
---|
[5abc7fd] | 102 |
|
---|
| 103 | return old_val;
|
---|
| 104 | }
|
---|
[b1cd380c] | 105 |
|
---|
[aa0fa86a] | 106 | /**
|
---|
| 107 | * 20.5.2, class template integer_sequence:
|
---|
| 108 | */
|
---|
| 109 |
|
---|
| 110 | template<class T, T... Is>
|
---|
| 111 | struct integer_sequence
|
---|
| 112 | {
|
---|
| 113 | using value_type = T;
|
---|
| 114 |
|
---|
| 115 | static constexpr size_t size() noexcept
|
---|
| 116 | {
|
---|
| 117 | return sizeof...(Is);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | using next = integer_sequence<T, Is..., sizeof...(Is)>;
|
---|
| 121 | };
|
---|
| 122 |
|
---|
| 123 | template<std::size_t... Is>
|
---|
| 124 | using index_sequence = integer_sequence<std::size_t, Is...>;
|
---|
| 125 |
|
---|
| 126 | /**
|
---|
| 127 | * 20.5.3, alias template make_integer_sequence:
|
---|
| 128 | */
|
---|
| 129 |
|
---|
| 130 | namespace aux
|
---|
| 131 | {
|
---|
| 132 | template<class T, uintmax_t N>
|
---|
| 133 | struct make_integer_sequence
|
---|
| 134 | {
|
---|
| 135 | /**
|
---|
| 136 | * Recursive to the bottom case below, appends sizeof...(Is) in
|
---|
| 137 | * every next "call", building the sequence.
|
---|
| 138 | */
|
---|
| 139 | using type = typename make_integer_sequence<T, N - 1>::type::next;
|
---|
| 140 | };
|
---|
| 141 |
|
---|
| 142 | template<class T>
|
---|
| 143 | struct make_integer_sequence<T, std::uintmax_t(0)>
|
---|
| 144 | {
|
---|
| 145 | using type = integer_sequence<T>;
|
---|
| 146 | };
|
---|
| 147 | }
|
---|
| 148 |
|
---|
| 149 |
|
---|
| 150 | /**
|
---|
| 151 | * Problem: We can't specialize the N parameter because it is a value parameter
|
---|
| 152 | * depending on a type parameter.
|
---|
| 153 | * Solution: According to the standard: if N is negative, the program is ill-formed,
|
---|
| 154 | * so we just recast it to uintmax_t :)
|
---|
| 155 | */
|
---|
| 156 | template<class T, T N>
|
---|
| 157 | using make_integer_sequence = typename aux::make_integer_sequence<T, std::uintmax_t(N)>::type;
|
---|
| 158 |
|
---|
| 159 | template<size_t N>
|
---|
| 160 | using make_index_sequence = make_integer_sequence<std::size_t, N>;
|
---|
| 161 |
|
---|
[b1cd380c] | 162 | /**
|
---|
| 163 | * 20.3, pairs:
|
---|
| 164 | */
|
---|
| 165 |
|
---|
[aa0fa86a] | 166 | template<size_t, class>
|
---|
| 167 | class tuple_element;
|
---|
| 168 |
|
---|
| 169 | template<size_t I, class T>
|
---|
| 170 | using tuple_element_t = typename tuple_element<I, T>::type;
|
---|
| 171 |
|
---|
| 172 | template<class...>
|
---|
| 173 | class tuple;
|
---|
| 174 |
|
---|
| 175 | template<size_t I, class... Ts>
|
---|
| 176 | constexpr tuple_element_t<I, tuple<Ts...>>&& get(tuple<Ts...>&&) noexcept;
|
---|
| 177 |
|
---|
| 178 | namespace aux
|
---|
| 179 | {
|
---|
| 180 | template<class T, class... Args, size_t... Is>
|
---|
| 181 | T from_tuple(tuple<Args...>&& tpl, index_sequence<Is...>)
|
---|
| 182 | {
|
---|
| 183 | return T{get<Is>(move(tpl))...};
|
---|
| 184 | }
|
---|
| 185 |
|
---|
| 186 | template<class T, class... Args>
|
---|
| 187 | T from_tuple(tuple<Args...>&& tpl)
|
---|
| 188 | {
|
---|
| 189 | return from_tuple<T>(move(tpl), make_index_sequence<sizeof...(Args)>{});
|
---|
| 190 | }
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[b1cd380c] | 193 | struct piecewise_construct_t
|
---|
| 194 | {
|
---|
| 195 | explicit piecewise_construct_t() = default;
|
---|
| 196 | };
|
---|
| 197 |
|
---|
[aa0fa86a] | 198 | inline constexpr piecewise_construct_t piecewise_construct{};
|
---|
| 199 |
|
---|
[b1cd380c] | 200 | template<typename T1, typename T2>
|
---|
| 201 | struct pair
|
---|
| 202 | {
|
---|
| 203 | using first_type = T1;
|
---|
| 204 | using second_type = T2;
|
---|
| 205 |
|
---|
| 206 | T1 first;
|
---|
| 207 | T2 second;
|
---|
| 208 |
|
---|
| 209 | pair(const pair&) = default;
|
---|
| 210 | pair(pair&&) = default;
|
---|
| 211 |
|
---|
| 212 | constexpr pair()
|
---|
| 213 | : first{}, second{}
|
---|
| 214 | { /* DUMMY BODY */ }
|
---|
| 215 |
|
---|
| 216 | constexpr pair(const T1& x, const T2& y)
|
---|
| 217 | : first{x}, second{y}
|
---|
| 218 | { /* DUMMY BODY */ }
|
---|
| 219 |
|
---|
| 220 | template<typename U, typename V>
|
---|
| 221 | constexpr pair(U&& x, V&& y)
|
---|
| 222 | : first(x), second(y)
|
---|
| 223 | { /* DUMMY BODY */ }
|
---|
| 224 |
|
---|
| 225 | template<typename U, typename V>
|
---|
| 226 | constexpr pair(const pair<U, V>& other)
|
---|
| 227 | : first(other.first), second(other.second)
|
---|
| 228 | { /* DUMMY BODY */ }
|
---|
| 229 |
|
---|
| 230 | template<typename U, typename V>
|
---|
| 231 | constexpr pair(pair<U, V>&& other)
|
---|
| 232 | : first(forward<first_type>(other.first)),
|
---|
| 233 | second(forward<second_type>(other.second))
|
---|
| 234 | { /* DUMMY BODY */ }
|
---|
| 235 |
|
---|
| 236 | template<class... Args1, class... Args2>
|
---|
| 237 | pair(piecewise_construct_t, tuple<Args1...> first_args, tuple<Args2...> second_args)
|
---|
[aa0fa86a] | 238 | : first{aux::from_tuple<first_type>(move(first_args))},
|
---|
| 239 | second{aux::from_tuple<second_type>(move(second_args))}
|
---|
| 240 | { /* DUMMY BODY */ }
|
---|
[b1cd380c] | 241 |
|
---|
| 242 | pair& operator=(const pair& other)
|
---|
| 243 | {
|
---|
| 244 | first = other.first;
|
---|
| 245 | second = other.second;
|
---|
| 246 |
|
---|
| 247 | return *this;
|
---|
| 248 | }
|
---|
| 249 |
|
---|
| 250 | template<typename U, typename V>
|
---|
| 251 | pair& operator=(const pair<U, V>& other)
|
---|
| 252 | {
|
---|
| 253 | first = other.first;
|
---|
| 254 | second = other.second;
|
---|
| 255 |
|
---|
| 256 | return *this;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | pair& operator=(pair&& other) noexcept
|
---|
| 260 | {
|
---|
| 261 | first = forward<first_type>(other.first);
|
---|
| 262 | second = forward<second_type>(other.second);
|
---|
| 263 |
|
---|
| 264 | return *this;
|
---|
| 265 | }
|
---|
[adee838] | 266 |
|
---|
| 267 | template<typename U, typename V>
|
---|
| 268 | pair& operator=(pair<U, V>&& other)
|
---|
| 269 | {
|
---|
| 270 | first = forward<first_type>(other.first);
|
---|
| 271 | second = forward<second_type>(other.second);
|
---|
| 272 |
|
---|
| 273 | return *this;
|
---|
| 274 | }
|
---|
| 275 |
|
---|
| 276 | void swap(pair& other) noexcept(
|
---|
| 277 | noexcept(std::swap(first, other.first)) &&
|
---|
| 278 | noexcept(std::swap(second, other.second))
|
---|
| 279 | )
|
---|
| 280 | {
|
---|
| 281 | std::swap(first, other.first);
|
---|
| 282 | std::swap(second, other.second);
|
---|
| 283 | }
|
---|
[b1cd380c] | 284 | };
|
---|
[82ef902] | 285 |
|
---|
[7a666789] | 286 | /**
|
---|
| 287 | * 20.3.3, specialized algorithms:
|
---|
| 288 | */
|
---|
| 289 |
|
---|
[82d256e] | 290 | template<class T1, class T2>
|
---|
| 291 | constexpr bool operator==(const pair<T1, T2>& lhs,
|
---|
| 292 | const pair<T1, T2>& rhs)
|
---|
| 293 | {
|
---|
| 294 | return lhs.first == rhs.first && lhs.second == rhs.second;
|
---|
| 295 | }
|
---|
| 296 |
|
---|
| 297 | template<class T1, class T2>
|
---|
| 298 | constexpr bool operator<(const pair<T1, T2>& lhs,
|
---|
| 299 | const pair<T1, T2>& rhs)
|
---|
| 300 | {
|
---|
| 301 | return lhs.first < rhs.first ||
|
---|
| 302 | (!(rhs.first < lhs.first) && lhs.second < rhs.second);
|
---|
| 303 | }
|
---|
| 304 |
|
---|
| 305 | template<class T1, class T2>
|
---|
| 306 | constexpr bool operator!=(const pair<T1, T2>& lhs,
|
---|
| 307 | const pair<T1, T2>& rhs)
|
---|
| 308 | {
|
---|
| 309 | return !(lhs == rhs);
|
---|
| 310 | }
|
---|
| 311 |
|
---|
| 312 | template<class T1, class T2>
|
---|
| 313 | constexpr bool operator>(const pair<T1, T2>& lhs,
|
---|
| 314 | const pair<T1, T2>& rhs)
|
---|
| 315 | {
|
---|
| 316 | return rhs < lhs;
|
---|
| 317 | }
|
---|
| 318 |
|
---|
| 319 | template<class T1, class T2>
|
---|
| 320 | constexpr bool operator>=(const pair<T1, T2>& lhs,
|
---|
| 321 | const pair<T1, T2>& rhs)
|
---|
| 322 | {
|
---|
| 323 | return !(lhs < rhs);
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | template<class T1, class T2>
|
---|
| 327 | constexpr bool operator<=(const pair<T1, T2>& lhs,
|
---|
| 328 | const pair<T1, T2>& rhs)
|
---|
| 329 | {
|
---|
| 330 | return !(rhs < lhs);
|
---|
| 331 | }
|
---|
| 332 |
|
---|
| 333 | template<class T1, class T2>
|
---|
| 334 | constexpr void swap(pair<T1, T2>& lhs, pair<T1, T2>& rhs)
|
---|
| 335 | noexcept(noexcept(lhs.swap(rhs)))
|
---|
| 336 | {
|
---|
| 337 | lhs.swap(rhs);
|
---|
| 338 | }
|
---|
[7a666789] | 339 |
|
---|
| 340 | template<class T1, class T2>
|
---|
| 341 | constexpr auto make_pair(T1&& t1, T2&& t2)
|
---|
| 342 | {
|
---|
| 343 | return pair<
|
---|
| 344 | aux::transform_tuple_types_t<T1>,
|
---|
| 345 | aux::transform_tuple_types_t<T2>
|
---|
| 346 | >{
|
---|
| 347 | forward<T1>(t1), forward<T2>(t2)
|
---|
| 348 | };
|
---|
| 349 | }
|
---|
| 350 |
|
---|
| 351 | /**
|
---|
| 352 | * 20.3.4, tuple-like access to pair:
|
---|
| 353 | */
|
---|
| 354 |
|
---|
[016d86e] | 355 | template<class>
|
---|
| 356 | struct tuple_size;
|
---|
| 357 |
|
---|
| 358 | template<class T1, class T2>
|
---|
| 359 | struct tuple_size<pair<T1, T2>>
|
---|
| 360 | : integral_constant<size_t, 2>
|
---|
| 361 | { /* DUMMY BODY */ };
|
---|
| 362 |
|
---|
| 363 | template<size_t, class>
|
---|
| 364 | struct tuple_element;
|
---|
| 365 |
|
---|
| 366 | template<class T1, class T2>
|
---|
| 367 | struct tuple_element<0, pair<T1, T2>>
|
---|
| 368 | : aux::type_is<T1>
|
---|
| 369 | { /* DUMMY BODY */ };
|
---|
| 370 |
|
---|
| 371 | template<class T1, class T2>
|
---|
| 372 | struct tuple_element<1, pair<T1, T2>>
|
---|
| 373 | : aux::type_is<T2>
|
---|
| 374 | { /* DUMMY BODY */ };
|
---|
| 375 |
|
---|
| 376 | template<size_t I, class T>
|
---|
| 377 | using tuple_element_t = typename tuple_element<I, T>::type;
|
---|
| 378 |
|
---|
| 379 | template<size_t I, class T1, class T2>
|
---|
| 380 | constexpr tuple_element_t<I, pair<T1, T2>>&
|
---|
| 381 | get(pair<T1, T2>& p) noexcept
|
---|
| 382 | {
|
---|
| 383 | if constexpr (I == 0)
|
---|
| 384 | return p.first;
|
---|
| 385 | else
|
---|
| 386 | return p.second;
|
---|
| 387 | }
|
---|
| 388 |
|
---|
| 389 | template<size_t I, class T1, class T2>
|
---|
| 390 | constexpr const tuple_element_t<I, pair<T1, T2>>&
|
---|
| 391 | get(const pair<T1, T2>& p) noexcept
|
---|
| 392 | {
|
---|
| 393 | if constexpr (I == 0)
|
---|
| 394 | return p.first;
|
---|
| 395 | else
|
---|
| 396 | return p.second;
|
---|
| 397 | }
|
---|
| 398 |
|
---|
| 399 | template<size_t I, class T1, class T2>
|
---|
| 400 | constexpr tuple_element_t<I, pair<T1, T2>>&&
|
---|
| 401 | get(pair<T1, T2>&& p) noexcept
|
---|
| 402 | {
|
---|
| 403 | if constexpr (I == 0)
|
---|
| 404 | return forward<T1>(p.first);
|
---|
| 405 | else
|
---|
| 406 | return forward<T2>(p.second);
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | template<class T, class U>
|
---|
| 410 | constexpr T& get(pair<T, U>& p) noexcept
|
---|
| 411 | {
|
---|
| 412 | static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
|
---|
| 413 |
|
---|
| 414 | return get<0>(p);
|
---|
| 415 | }
|
---|
| 416 |
|
---|
| 417 | template<class T, class U>
|
---|
| 418 | constexpr const T& get(const pair<T, U>& p) noexcept
|
---|
| 419 | {
|
---|
| 420 | static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
|
---|
| 421 |
|
---|
| 422 | return get<0>(p);
|
---|
| 423 | }
|
---|
| 424 |
|
---|
| 425 | template<class T, class U>
|
---|
| 426 | constexpr T&& get(pair<T, U>&& p) noexcept
|
---|
| 427 | {
|
---|
| 428 | static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
|
---|
| 429 |
|
---|
| 430 | return get<0>(move(p));
|
---|
| 431 | }
|
---|
| 432 |
|
---|
| 433 | template<class T, class U>
|
---|
| 434 | constexpr T& get(pair<U, T>& p) noexcept
|
---|
| 435 | {
|
---|
| 436 | static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
|
---|
| 437 |
|
---|
| 438 | return get<1>(p);
|
---|
| 439 | }
|
---|
| 440 |
|
---|
| 441 | template<class T, class U>
|
---|
| 442 | constexpr const T& get(const pair<U, T>& p) noexcept
|
---|
| 443 | {
|
---|
| 444 | static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
|
---|
| 445 |
|
---|
| 446 | return get<1>(p);
|
---|
| 447 | }
|
---|
| 448 |
|
---|
| 449 | template<class T, class U>
|
---|
| 450 | constexpr T&& get(pair<U, T>&& p) noexcept
|
---|
| 451 | {
|
---|
| 452 | static_assert(!is_same_v<T, U>, "get(pair) requires distinct types");
|
---|
| 453 |
|
---|
| 454 | return get<1>(move(p));
|
---|
| 455 | }
|
---|
[b1cd380c] | 456 | }
|
---|
| 457 |
|
---|
| 458 | #endif
|
---|