| 1 | // Debugging set implementation -*- C++ -*- |
| 2 | |
| 3 | // Copyright (C) 2003-2026 Free Software Foundation, Inc. |
| 4 | // |
| 5 | // This file is part of the GNU ISO C++ Library. This library is free |
| 6 | // software; you can redistribute it and/or modify it under the |
| 7 | // terms of the GNU General Public License as published by the |
| 8 | // Free Software Foundation; either version 3, or (at your option) |
| 9 | // any later version. |
| 10 | |
| 11 | // This library is distributed in the hope that it will be useful, |
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | // GNU General Public License for more details. |
| 15 | |
| 16 | // Under Section 7 of GPL version 3, you are granted additional |
| 17 | // permissions described in the GCC Runtime Library Exception, version |
| 18 | // 3.1, as published by the Free Software Foundation. |
| 19 | |
| 20 | // You should have received a copy of the GNU General Public License and |
| 21 | // a copy of the GCC Runtime Library Exception along with this program; |
| 22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
| 23 | // <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | /** @file debug/set.h |
| 26 | * This file is a GNU debug extension to the Standard C++ Library. |
| 27 | */ |
| 28 | |
| 29 | #ifndef _GLIBCXX_DEBUG_SET_H |
| 30 | #define _GLIBCXX_DEBUG_SET_H 1 |
| 31 | |
| 32 | #include <debug/safe_sequence.h> |
| 33 | #include <debug/safe_container.h> |
| 34 | #include <debug/safe_iterator.h> |
| 35 | #include <bits/stl_pair.h> |
| 36 | |
| 37 | namespace std _GLIBCXX_VISIBILITY(default) |
| 38 | { |
| 39 | namespace __debug |
| 40 | { |
| 41 | /// Class std::set with safety/checking/debug instrumentation. |
| 42 | template<typename _Key, typename _Compare = std::less<_Key>, |
| 43 | typename _Allocator = std::allocator<_Key> > |
| 44 | class set |
| 45 | : public __gnu_debug::_Safe_container< |
| 46 | set<_Key, _Compare, _Allocator>, _Allocator, |
| 47 | __gnu_debug::_Safe_node_sequence>, |
| 48 | public _GLIBCXX_STD_C::set<_Key,_Compare,_Allocator> |
| 49 | { |
| 50 | typedef _GLIBCXX_STD_C::set<_Key, _Compare, _Allocator> _Base; |
| 51 | typedef __gnu_debug::_Safe_container< |
| 52 | set, _Allocator, __gnu_debug::_Safe_node_sequence> _Safe; |
| 53 | |
| 54 | typedef typename _Base::const_iterator _Base_const_iterator; |
| 55 | typedef typename _Base::iterator _Base_iterator; |
| 56 | typedef __gnu_debug::_Equal_to<_Base_const_iterator> _Equal; |
| 57 | |
| 58 | template<typename _ItT, typename _SeqT, typename _CatT> |
| 59 | friend class ::__gnu_debug::_Safe_iterator; |
| 60 | |
| 61 | // Reference wrapper for base class. Disambiguates set(const _Base&) |
| 62 | // from copy constructor by requiring a user-defined conversion. |
| 63 | // See PR libstdc++/90102. |
| 64 | struct _Base_ref |
| 65 | { |
| 66 | _Base_ref(const _Base& __r) : _M_ref(__r) { } |
| 67 | |
| 68 | const _Base& _M_ref; |
| 69 | }; |
| 70 | |
| 71 | public: |
| 72 | // types: |
| 73 | typedef _Key key_type; |
| 74 | typedef _Key value_type; |
| 75 | typedef _Compare key_compare; |
| 76 | typedef _Compare value_compare; |
| 77 | typedef _Allocator allocator_type; |
| 78 | typedef typename _Base::reference reference; |
| 79 | typedef typename _Base::const_reference const_reference; |
| 80 | |
| 81 | typedef __gnu_debug::_Safe_iterator<_Base_iterator, set> |
| 82 | iterator; |
| 83 | typedef __gnu_debug::_Safe_iterator<_Base_const_iterator, set> |
| 84 | const_iterator; |
| 85 | |
| 86 | typedef typename _Base::size_type size_type; |
| 87 | typedef typename _Base::difference_type difference_type; |
| 88 | typedef typename _Base::pointer pointer; |
| 89 | typedef typename _Base::const_pointer const_pointer; |
| 90 | typedef std::reverse_iterator<iterator> reverse_iterator; |
| 91 | typedef std::reverse_iterator<const_iterator> const_reverse_iterator; |
| 92 | |
| 93 | // 23.3.3.1 construct/copy/destroy: |
| 94 | |
| 95 | #if __cplusplus < 201103L |
| 96 | set() : _Base() { } |
| 97 | |
| 98 | set(const set& __x) |
| 99 | : _Base(__x) { } |
| 100 | |
| 101 | ~set() { } |
| 102 | #else |
| 103 | set() = default; |
| 104 | set(const set&) = default; |
| 105 | set(set&&) = default; |
| 106 | |
| 107 | set(initializer_list<value_type> __l, |
| 108 | const _Compare& __comp = _Compare(), |
| 109 | const allocator_type& __a = allocator_type()) |
| 110 | : _Base(__l, __comp, __a) { } |
| 111 | |
| 112 | explicit |
| 113 | set(const allocator_type& __a) |
| 114 | : _Base(__a) { } |
| 115 | |
| 116 | set(const set& __x, const __type_identity_t<allocator_type>& __a) |
| 117 | : _Base(__x, __a) { } |
| 118 | |
| 119 | set(set&& __x, const __type_identity_t<allocator_type>& __a) |
| 120 | noexcept( noexcept(_Base(std::move(__x), __a)) ) |
| 121 | : _Safe(std::move(__x), __a), |
| 122 | _Base(std::move(__x), __a) { } |
| 123 | |
| 124 | set(initializer_list<value_type> __l, const allocator_type& __a) |
| 125 | : _Base(__l, __a) { } |
| 126 | |
| 127 | template<typename _InputIterator> |
| 128 | set(_InputIterator __first, _InputIterator __last, |
| 129 | const allocator_type& __a) |
| 130 | : _Base(__gnu_debug::__base( |
| 131 | __glibcxx_check_valid_constructor_range(__first, __last)), |
| 132 | __gnu_debug::__base(__last), __a) { } |
| 133 | |
| 134 | #if __glibcxx_containers_ranges // C++ >= 23 |
| 135 | /** |
| 136 | * @brief Construct a set from a range. |
| 137 | * @since C++23 |
| 138 | */ |
| 139 | template<std::__detail::__container_compatible_range<_Key> _Rg> |
| 140 | set(std::from_range_t __t, _Rg&& __rg, |
| 141 | const _Compare& __c, |
| 142 | const allocator_type& __a = allocator_type()) |
| 143 | : _Base(__t, std::forward<_Rg>(__rg), __c, __a) |
| 144 | { } |
| 145 | |
| 146 | template<std::__detail::__container_compatible_range<_Key> _Rg> |
| 147 | set(std::from_range_t __t, _Rg&& __rg, |
| 148 | const allocator_type& __a = allocator_type()) |
| 149 | : _Base(__t, std::forward<_Rg>(__rg), __a) |
| 150 | { } |
| 151 | #endif |
| 152 | |
| 153 | ~set() = default; |
| 154 | #endif |
| 155 | |
| 156 | explicit set(const _Compare& __comp, |
| 157 | const _Allocator& __a = _Allocator()) |
| 158 | : _Base(__comp, __a) { } |
| 159 | |
| 160 | template<typename _InputIterator> |
| 161 | set(_InputIterator __first, _InputIterator __last, |
| 162 | const _Compare& __comp = _Compare(), |
| 163 | const _Allocator& __a = _Allocator()) |
| 164 | : _Base(__gnu_debug::__base( |
| 165 | __glibcxx_check_valid_constructor_range(__first, __last)), |
| 166 | __gnu_debug::__base(__last), |
| 167 | __comp, __a) { } |
| 168 | |
| 169 | set(_Base_ref __x) |
| 170 | : _Base(__x._M_ref) { } |
| 171 | |
| 172 | #if __cplusplus >= 201103L |
| 173 | set& |
| 174 | operator=(const set&) = default; |
| 175 | |
| 176 | set& |
| 177 | operator=(set&&) = default; |
| 178 | |
| 179 | set& |
| 180 | operator=(initializer_list<value_type> __l) |
| 181 | { |
| 182 | _Base::operator=(__l); |
| 183 | this->_M_invalidate_all(); |
| 184 | return *this; |
| 185 | } |
| 186 | #endif |
| 187 | |
| 188 | using _Base::get_allocator; |
| 189 | |
| 190 | // iterators: |
| 191 | iterator |
| 192 | begin() _GLIBCXX_NOEXCEPT |
| 193 | { return iterator(_Base::begin(), this); } |
| 194 | |
| 195 | const_iterator |
| 196 | begin() const _GLIBCXX_NOEXCEPT |
| 197 | { return const_iterator(_Base::begin(), this); } |
| 198 | |
| 199 | iterator |
| 200 | end() _GLIBCXX_NOEXCEPT |
| 201 | { return iterator(_Base::end(), this); } |
| 202 | |
| 203 | const_iterator |
| 204 | end() const _GLIBCXX_NOEXCEPT |
| 205 | { return const_iterator(_Base::end(), this); } |
| 206 | |
| 207 | reverse_iterator |
| 208 | rbegin() _GLIBCXX_NOEXCEPT |
| 209 | { return reverse_iterator(end()); } |
| 210 | |
| 211 | const_reverse_iterator |
| 212 | rbegin() const _GLIBCXX_NOEXCEPT |
| 213 | { return const_reverse_iterator(end()); } |
| 214 | |
| 215 | reverse_iterator |
| 216 | rend() _GLIBCXX_NOEXCEPT |
| 217 | { return reverse_iterator(begin()); } |
| 218 | |
| 219 | const_reverse_iterator |
| 220 | rend() const _GLIBCXX_NOEXCEPT |
| 221 | { return const_reverse_iterator(begin()); } |
| 222 | |
| 223 | #if __cplusplus >= 201103L |
| 224 | const_iterator |
| 225 | cbegin() const noexcept |
| 226 | { return const_iterator(_Base::begin(), this); } |
| 227 | |
| 228 | const_iterator |
| 229 | cend() const noexcept |
| 230 | { return const_iterator(_Base::end(), this); } |
| 231 | |
| 232 | const_reverse_iterator |
| 233 | crbegin() const noexcept |
| 234 | { return const_reverse_iterator(end()); } |
| 235 | |
| 236 | const_reverse_iterator |
| 237 | crend() const noexcept |
| 238 | { return const_reverse_iterator(begin()); } |
| 239 | #endif |
| 240 | |
| 241 | // capacity: |
| 242 | using _Base::empty; |
| 243 | using _Base::size; |
| 244 | using _Base::max_size; |
| 245 | |
| 246 | // modifiers: |
| 247 | #if __cplusplus >= 201103L |
| 248 | template<typename... _Args> |
| 249 | std::pair<iterator, bool> |
| 250 | emplace(_Args&&... __args) |
| 251 | { |
| 252 | auto __res = _Base::emplace(std::forward<_Args>(__args)...); |
| 253 | return { { __res.first, this }, __res.second }; |
| 254 | } |
| 255 | |
| 256 | template<typename... _Args> |
| 257 | iterator |
| 258 | emplace_hint(const_iterator __pos, _Args&&... __args) |
| 259 | { |
| 260 | __glibcxx_check_insert(__pos); |
| 261 | return |
| 262 | { |
| 263 | _Base::emplace_hint(__pos.base(), std::forward<_Args>(__args)...), |
| 264 | this |
| 265 | }; |
| 266 | } |
| 267 | #endif |
| 268 | |
| 269 | std::pair<iterator, bool> |
| 270 | insert(const value_type& __x) |
| 271 | { |
| 272 | std::pair<_Base_iterator, bool> __res = _Base::insert(__x); |
| 273 | return std::pair<iterator, bool>(iterator(__res.first, this), |
| 274 | __res.second); |
| 275 | } |
| 276 | |
| 277 | #if __cplusplus >= 201103L |
| 278 | std::pair<iterator, bool> |
| 279 | insert(value_type&& __x) |
| 280 | { |
| 281 | auto __res = _Base::insert(std::move(__x)); |
| 282 | return { { __res.first, this }, __res.second }; |
| 283 | } |
| 284 | #endif |
| 285 | |
| 286 | #ifdef __glibcxx_associative_heterogeneous_insertion |
| 287 | template <__heterogeneous_tree_key<set> _Kt> |
| 288 | std::pair<iterator, bool> |
| 289 | insert(_Kt&& __x) |
| 290 | { |
| 291 | auto __res = _Base::insert(std::forward<_Kt>(__x)); |
| 292 | return { { __res.first, this }, __res.second }; |
| 293 | } |
| 294 | #endif |
| 295 | |
| 296 | iterator |
| 297 | insert(const_iterator __position, const value_type& __x) |
| 298 | { |
| 299 | __glibcxx_check_insert(__position); |
| 300 | return iterator(_Base::insert(__position.base(), __x), this); |
| 301 | } |
| 302 | |
| 303 | #if __cplusplus >= 201103L |
| 304 | iterator |
| 305 | insert(const_iterator __position, value_type&& __x) |
| 306 | { |
| 307 | __glibcxx_check_insert(__position); |
| 308 | return { _Base::insert(__position.base(), std::move(__x)), this }; |
| 309 | } |
| 310 | #endif |
| 311 | |
| 312 | #ifdef __glibcxx_associative_heterogeneous_insertion |
| 313 | template <__heterogeneous_tree_key<set> _Kt> |
| 314 | iterator |
| 315 | insert(const_iterator __position, _Kt&& __x) |
| 316 | { |
| 317 | __glibcxx_check_insert(__position); |
| 318 | auto __it = _Base::insert(__position.base(), std::forward<_Kt>(__x)); |
| 319 | return { __it, this }; |
| 320 | } |
| 321 | #endif |
| 322 | |
| 323 | template <typename _InputIterator> |
| 324 | void |
| 325 | insert(_InputIterator __first, _InputIterator __last) |
| 326 | { |
| 327 | typename __gnu_debug::_Distance_traits<_InputIterator>::__type __dist; |
| 328 | __glibcxx_check_valid_range2(__first, __last, __dist); |
| 329 | |
| 330 | if (__dist.second >= __gnu_debug::__dp_sign) |
| 331 | _Base::insert(__gnu_debug::__unsafe(__first), |
| 332 | __gnu_debug::__unsafe(__last)); |
| 333 | else |
| 334 | _Base::insert(__first, __last); |
| 335 | } |
| 336 | |
| 337 | #if __cplusplus >= 201103L |
| 338 | void |
| 339 | insert(initializer_list<value_type> __l) |
| 340 | { _Base::insert(__l); } |
| 341 | #endif |
| 342 | |
| 343 | #ifdef __glibcxx_node_extract // >= C++17 && HOSTED |
| 344 | using node_type = typename _Base::node_type; |
| 345 | using insert_return_type = _Node_insert_return<iterator, node_type>; |
| 346 | |
| 347 | node_type |
| 348 | (const_iterator __position) |
| 349 | { |
| 350 | __glibcxx_check_erase(__position); |
| 351 | this->_M_invalidate_if(_Equal(__position.base())); |
| 352 | return _Base::extract(__position.base()); |
| 353 | } |
| 354 | |
| 355 | node_type |
| 356 | (const key_type& __key) |
| 357 | { |
| 358 | const auto __position = find(__key); |
| 359 | if (__position != end()) |
| 360 | return extract(__position); |
| 361 | return {}; |
| 362 | } |
| 363 | |
| 364 | # ifdef __glibcxx_associative_heterogeneous_erasure |
| 365 | template <__heterogeneous_tree_key<set> _Kt> |
| 366 | node_type |
| 367 | extract(_Kt&& __key) |
| 368 | { |
| 369 | const auto __position = find(__key); |
| 370 | if (__position != end()) |
| 371 | return extract(__position); |
| 372 | return {}; |
| 373 | } |
| 374 | #endif |
| 375 | |
| 376 | insert_return_type |
| 377 | insert(node_type&& __nh) |
| 378 | { |
| 379 | auto __ret = _Base::insert(std::move(__nh)); |
| 380 | iterator __pos = iterator(__ret.position, this); |
| 381 | return { __pos, __ret.inserted, std::move(__ret.node) }; |
| 382 | } |
| 383 | |
| 384 | iterator |
| 385 | insert(const_iterator __hint, node_type&& __nh) |
| 386 | { |
| 387 | __glibcxx_check_insert(__hint); |
| 388 | return { _Base::insert(__hint.base(), std::move(__nh)), this }; |
| 389 | } |
| 390 | |
| 391 | using _Base::merge; |
| 392 | #endif // C++17 |
| 393 | |
| 394 | #if __cplusplus >= 201103L |
| 395 | _GLIBCXX_ABI_TAG_CXX11 |
| 396 | iterator |
| 397 | erase(const_iterator __position) |
| 398 | { |
| 399 | __glibcxx_check_erase(__position); |
| 400 | return { erase(__position.base()), this }; |
| 401 | } |
| 402 | |
| 403 | _Base_iterator |
| 404 | erase(_Base_const_iterator __position) |
| 405 | { |
| 406 | __glibcxx_check_erase2(__position); |
| 407 | this->_M_invalidate_if(_Equal(__position)); |
| 408 | return _Base::erase(__position); |
| 409 | } |
| 410 | #else |
| 411 | void |
| 412 | erase(iterator __position) |
| 413 | { |
| 414 | __glibcxx_check_erase(__position); |
| 415 | this->_M_invalidate_if(_Equal(__position.base())); |
| 416 | _Base::erase(__position.base()); |
| 417 | } |
| 418 | #endif |
| 419 | |
| 420 | size_type |
| 421 | erase(const key_type& __x) |
| 422 | { |
| 423 | _Base_iterator __victim = _Base::find(__x); |
| 424 | if (__victim == _Base::end()) |
| 425 | return 0; |
| 426 | else |
| 427 | { |
| 428 | this->_M_invalidate_if(_Equal(__victim)); |
| 429 | _Base::erase(__victim); |
| 430 | return 1; |
| 431 | } |
| 432 | } |
| 433 | |
| 434 | # ifdef __glibcxx_associative_heterogeneous_erasure |
| 435 | // Note that for some types _Kt this may erase more than |
| 436 | // one element, such as if _Kt::operator< checks only part |
| 437 | // of the key. |
| 438 | template <__heterogeneous_tree_key<set> _Kt> |
| 439 | size_type |
| 440 | erase(_Kt&& __x) |
| 441 | { |
| 442 | auto __victims = _Base::equal_range(__x); |
| 443 | size_type __count = 0; |
| 444 | for (auto __victim = __victims.first; __victim != __victims.second;) |
| 445 | { |
| 446 | this->_M_invalidate_if(_Equal(__victim)); |
| 447 | _Base::erase(__victim++); |
| 448 | ++__count; |
| 449 | } |
| 450 | return __count; |
| 451 | } |
| 452 | #endif |
| 453 | |
| 454 | #if __cplusplus >= 201103L |
| 455 | _GLIBCXX_ABI_TAG_CXX11 |
| 456 | iterator |
| 457 | erase(const_iterator __first, const_iterator __last) |
| 458 | { |
| 459 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 460 | // 151. can't currently clear() empty container |
| 461 | __glibcxx_check_erase_range(__first, __last); |
| 462 | for (_Base_const_iterator __victim = __first.base(); |
| 463 | __victim != __last.base(); ++__victim) |
| 464 | { |
| 465 | _GLIBCXX_DEBUG_VERIFY(__victim != _Base::cend(), |
| 466 | _M_message(__gnu_debug::__msg_valid_range) |
| 467 | ._M_iterator(__first, "first" ) |
| 468 | ._M_iterator(__last, "last" )); |
| 469 | this->_M_invalidate_if(_Equal(__victim)); |
| 470 | } |
| 471 | |
| 472 | return { _Base::erase(__first.base(), __last.base()), this }; |
| 473 | } |
| 474 | #else |
| 475 | void |
| 476 | erase(iterator __first, iterator __last) |
| 477 | { |
| 478 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 479 | // 151. can't currently clear() empty container |
| 480 | __glibcxx_check_erase_range(__first, __last); |
| 481 | for (_Base_iterator __victim = __first.base(); |
| 482 | __victim != __last.base(); ++__victim) |
| 483 | { |
| 484 | _GLIBCXX_DEBUG_VERIFY(__victim != _Base::end(), |
| 485 | _M_message(__gnu_debug::__msg_valid_range) |
| 486 | ._M_iterator(__first, "first" ) |
| 487 | ._M_iterator(__last, "last" )); |
| 488 | this->_M_invalidate_if(_Equal(__victim)); |
| 489 | } |
| 490 | _Base::erase(__first.base(), __last.base()); |
| 491 | } |
| 492 | #endif |
| 493 | |
| 494 | void |
| 495 | swap(set& __x) |
| 496 | _GLIBCXX_NOEXCEPT_IF( noexcept(declval<_Base&>().swap(__x)) ) |
| 497 | { |
| 498 | _Safe::_M_swap(__x); |
| 499 | _Base::swap(__x); |
| 500 | } |
| 501 | |
| 502 | void |
| 503 | clear() _GLIBCXX_NOEXCEPT |
| 504 | { |
| 505 | this->_M_invalidate_all(); |
| 506 | _Base::clear(); |
| 507 | } |
| 508 | |
| 509 | // observers: |
| 510 | using _Base::key_comp; |
| 511 | using _Base::value_comp; |
| 512 | |
| 513 | // set operations: |
| 514 | iterator |
| 515 | find(const key_type& __x) |
| 516 | { return iterator(_Base::find(__x), this); } |
| 517 | |
| 518 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 519 | // 214. set::find() missing const overload |
| 520 | const_iterator |
| 521 | find(const key_type& __x) const |
| 522 | { return const_iterator(_Base::find(__x), this); } |
| 523 | |
| 524 | #ifdef __glibcxx_generic_associative_lookup // C++ >= 14 |
| 525 | template<typename _Kt, |
| 526 | typename _Req = |
| 527 | typename __has_is_transparent<_Compare, _Kt>::type> |
| 528 | iterator |
| 529 | find(const _Kt& __x) |
| 530 | { return { _Base::find(__x), this }; } |
| 531 | |
| 532 | template<typename _Kt, |
| 533 | typename _Req = |
| 534 | typename __has_is_transparent<_Compare, _Kt>::type> |
| 535 | const_iterator |
| 536 | find(const _Kt& __x) const |
| 537 | { return { _Base::find(__x), this }; } |
| 538 | #endif |
| 539 | |
| 540 | using _Base::count; |
| 541 | |
| 542 | iterator |
| 543 | lower_bound(const key_type& __x) |
| 544 | { return iterator(_Base::lower_bound(__x), this); } |
| 545 | |
| 546 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 547 | // 214. set::find() missing const overload |
| 548 | const_iterator |
| 549 | lower_bound(const key_type& __x) const |
| 550 | { return const_iterator(_Base::lower_bound(__x), this); } |
| 551 | |
| 552 | #ifdef __glibcxx_generic_associative_lookup // C++ >= 14 |
| 553 | template<typename _Kt, |
| 554 | typename _Req = |
| 555 | typename __has_is_transparent<_Compare, _Kt>::type> |
| 556 | iterator |
| 557 | lower_bound(const _Kt& __x) |
| 558 | { return { _Base::lower_bound(__x), this }; } |
| 559 | |
| 560 | template<typename _Kt, |
| 561 | typename _Req = |
| 562 | typename __has_is_transparent<_Compare, _Kt>::type> |
| 563 | const_iterator |
| 564 | lower_bound(const _Kt& __x) const |
| 565 | { return { _Base::lower_bound(__x), this }; } |
| 566 | #endif |
| 567 | |
| 568 | iterator |
| 569 | upper_bound(const key_type& __x) |
| 570 | { return iterator(_Base::upper_bound(__x), this); } |
| 571 | |
| 572 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 573 | // 214. set::find() missing const overload |
| 574 | const_iterator |
| 575 | upper_bound(const key_type& __x) const |
| 576 | { return const_iterator(_Base::upper_bound(__x), this); } |
| 577 | |
| 578 | #ifdef __glibcxx_generic_associative_lookup // C++ >= 14 |
| 579 | template<typename _Kt, |
| 580 | typename _Req = |
| 581 | typename __has_is_transparent<_Compare, _Kt>::type> |
| 582 | iterator |
| 583 | upper_bound(const _Kt& __x) |
| 584 | { return { _Base::upper_bound(__x), this }; } |
| 585 | |
| 586 | template<typename _Kt, |
| 587 | typename _Req = |
| 588 | typename __has_is_transparent<_Compare, _Kt>::type> |
| 589 | const_iterator |
| 590 | upper_bound(const _Kt& __x) const |
| 591 | { return { _Base::upper_bound(__x), this }; } |
| 592 | #endif |
| 593 | |
| 594 | std::pair<iterator, iterator> |
| 595 | equal_range(const key_type& __x) |
| 596 | { |
| 597 | std::pair<_Base_iterator, _Base_iterator> __res = |
| 598 | _Base::equal_range(__x); |
| 599 | return std::make_pair(iterator(__res.first, this), |
| 600 | iterator(__res.second, this)); |
| 601 | } |
| 602 | |
| 603 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
| 604 | // 214. set::find() missing const overload |
| 605 | std::pair<const_iterator, const_iterator> |
| 606 | equal_range(const key_type& __x) const |
| 607 | { |
| 608 | std::pair<_Base_const_iterator, _Base_const_iterator> __res = |
| 609 | _Base::equal_range(__x); |
| 610 | return std::make_pair(const_iterator(__res.first, this), |
| 611 | const_iterator(__res.second, this)); |
| 612 | } |
| 613 | |
| 614 | #ifdef __glibcxx_generic_associative_lookup // C++ >= 14 |
| 615 | template<typename _Kt, |
| 616 | typename _Req = |
| 617 | typename __has_is_transparent<_Compare, _Kt>::type> |
| 618 | std::pair<iterator, iterator> |
| 619 | equal_range(const _Kt& __x) |
| 620 | { |
| 621 | auto __res = _Base::equal_range(__x); |
| 622 | return { { __res.first, this }, { __res.second, this } }; |
| 623 | } |
| 624 | |
| 625 | template<typename _Kt, |
| 626 | typename _Req = |
| 627 | typename __has_is_transparent<_Compare, _Kt>::type> |
| 628 | std::pair<const_iterator, const_iterator> |
| 629 | equal_range(const _Kt& __x) const |
| 630 | { |
| 631 | auto __res = _Base::equal_range(__x); |
| 632 | return { { __res.first, this }, { __res.second, this } }; |
| 633 | } |
| 634 | #endif |
| 635 | |
| 636 | _Base& |
| 637 | _M_base() _GLIBCXX_NOEXCEPT { return *this; } |
| 638 | |
| 639 | const _Base& |
| 640 | _M_base() const _GLIBCXX_NOEXCEPT { return *this; } |
| 641 | }; |
| 642 | |
| 643 | #if __cpp_deduction_guides >= 201606 |
| 644 | |
| 645 | template<typename _InputIterator, |
| 646 | typename _Compare = |
| 647 | less<typename iterator_traits<_InputIterator>::value_type>, |
| 648 | typename _Allocator = |
| 649 | allocator<typename iterator_traits<_InputIterator>::value_type>, |
| 650 | typename = _RequireInputIter<_InputIterator>, |
| 651 | typename = _RequireNotAllocator<_Compare>, |
| 652 | typename = _RequireAllocator<_Allocator>> |
| 653 | set(_InputIterator, _InputIterator, |
| 654 | _Compare = _Compare(), _Allocator = _Allocator()) |
| 655 | -> set<typename iterator_traits<_InputIterator>::value_type, |
| 656 | _Compare, _Allocator>; |
| 657 | |
| 658 | template<typename _Key, typename _Compare = less<_Key>, |
| 659 | typename _Allocator = allocator<_Key>, |
| 660 | typename = _RequireNotAllocator<_Compare>, |
| 661 | typename = _RequireAllocator<_Allocator>> |
| 662 | set(initializer_list<_Key>, |
| 663 | _Compare = _Compare(), _Allocator = _Allocator()) |
| 664 | -> set<_Key, _Compare, _Allocator>; |
| 665 | |
| 666 | template<typename _InputIterator, typename _Allocator, |
| 667 | typename = _RequireInputIter<_InputIterator>, |
| 668 | typename = _RequireAllocator<_Allocator>> |
| 669 | set(_InputIterator, _InputIterator, _Allocator) |
| 670 | -> set<typename iterator_traits<_InputIterator>::value_type, |
| 671 | less<typename iterator_traits<_InputIterator>::value_type>, |
| 672 | _Allocator>; |
| 673 | |
| 674 | template<typename _Key, typename _Allocator, |
| 675 | typename = _RequireAllocator<_Allocator>> |
| 676 | set(initializer_list<_Key>, _Allocator) |
| 677 | -> set<_Key, less<_Key>, _Allocator>; |
| 678 | |
| 679 | #if __glibcxx_containers_ranges // C++ >= 23 |
| 680 | template<ranges::input_range _Rg, |
| 681 | __not_allocator_like _Compare = less<ranges::range_value_t<_Rg>>, |
| 682 | __allocator_like _Alloc = std::allocator<ranges::range_value_t<_Rg>>> |
| 683 | set(from_range_t, _Rg&&, _Compare = _Compare(), _Alloc = _Alloc()) |
| 684 | -> set<ranges::range_value_t<_Rg>, _Compare, _Alloc>; |
| 685 | |
| 686 | template<ranges::input_range _Rg, __allocator_like _Alloc> |
| 687 | set(from_range_t, _Rg&&, _Alloc) |
| 688 | -> set<ranges::range_value_t<_Rg>, less<ranges::range_value_t<_Rg>>, _Alloc>; |
| 689 | #endif |
| 690 | #endif // deduction guides |
| 691 | |
| 692 | template<typename _Key, typename _Compare, typename _Allocator> |
| 693 | inline bool |
| 694 | operator==(const set<_Key, _Compare, _Allocator>& __lhs, |
| 695 | const set<_Key, _Compare, _Allocator>& __rhs) |
| 696 | { return __lhs._M_base() == __rhs._M_base(); } |
| 697 | |
| 698 | #if __cpp_lib_three_way_comparison |
| 699 | template<typename _Key, typename _Compare, typename _Alloc> |
| 700 | inline __detail::__synth3way_t<_Key> |
| 701 | operator<=>(const set<_Key, _Compare, _Alloc>& __lhs, |
| 702 | const set<_Key, _Compare, _Alloc>& __rhs) |
| 703 | { return __lhs._M_base() <=> __rhs._M_base(); } |
| 704 | #else |
| 705 | template<typename _Key, typename _Compare, typename _Allocator> |
| 706 | inline bool |
| 707 | operator!=(const set<_Key, _Compare, _Allocator>& __lhs, |
| 708 | const set<_Key, _Compare, _Allocator>& __rhs) |
| 709 | { return __lhs._M_base() != __rhs._M_base(); } |
| 710 | |
| 711 | template<typename _Key, typename _Compare, typename _Allocator> |
| 712 | inline bool |
| 713 | operator<(const set<_Key, _Compare, _Allocator>& __lhs, |
| 714 | const set<_Key, _Compare, _Allocator>& __rhs) |
| 715 | { return __lhs._M_base() < __rhs._M_base(); } |
| 716 | |
| 717 | template<typename _Key, typename _Compare, typename _Allocator> |
| 718 | inline bool |
| 719 | operator<=(const set<_Key, _Compare, _Allocator>& __lhs, |
| 720 | const set<_Key, _Compare, _Allocator>& __rhs) |
| 721 | { return __lhs._M_base() <= __rhs._M_base(); } |
| 722 | |
| 723 | template<typename _Key, typename _Compare, typename _Allocator> |
| 724 | inline bool |
| 725 | operator>=(const set<_Key, _Compare, _Allocator>& __lhs, |
| 726 | const set<_Key, _Compare, _Allocator>& __rhs) |
| 727 | { return __lhs._M_base() >= __rhs._M_base(); } |
| 728 | |
| 729 | template<typename _Key, typename _Compare, typename _Allocator> |
| 730 | inline bool |
| 731 | operator>(const set<_Key, _Compare, _Allocator>& __lhs, |
| 732 | const set<_Key, _Compare, _Allocator>& __rhs) |
| 733 | { return __lhs._M_base() > __rhs._M_base(); } |
| 734 | #endif // three-way comparison |
| 735 | |
| 736 | template<typename _Key, typename _Compare, typename _Allocator> |
| 737 | void |
| 738 | swap(set<_Key, _Compare, _Allocator>& __x, |
| 739 | set<_Key, _Compare, _Allocator>& __y) |
| 740 | _GLIBCXX_NOEXCEPT_IF(noexcept(__x.swap(__y))) |
| 741 | { return __x.swap(__y); } |
| 742 | |
| 743 | } // namespace __debug |
| 744 | } // namespace std |
| 745 | |
| 746 | #endif |
| 747 | |