node_handle.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. // Node handles for containers -*- C++ -*-
  2. // Copyright (C) 2016-2023 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/node_handle.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly.
  23. * @headername{map,set,unordered_map,unordered_set}
  24. */
  25. #ifndef _NODE_HANDLE
  26. #define _NODE_HANDLE 1
  27. #pragma GCC system_header
  28. #if __cplusplus >= 201703L
  29. # define __cpp_lib_node_extract 201606L
  30. #include <new>
  31. #include <bits/alloc_traits.h>
  32. #include <bits/ptr_traits.h>
  33. namespace std _GLIBCXX_VISIBILITY(default)
  34. {
  35. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  36. /**
  37. * @defgroup node_handles Node handles
  38. * @ingroup associative_containers
  39. * @since C++17
  40. *
  41. * The associative containers (`map`, `set`, `multimap` and `multiset`)
  42. * support extracting and re-inserting nodes from the container. Those
  43. * operations use the container's `node_handle` type, which is an alias
  44. * for a `_Node_handle<...>` type. You should always use the container's
  45. * `node_handle` type (e.g. `std::set<int>::node_handle`) to refer to
  46. * these types, not the non-standard internal `_Node_handle` names.
  47. *
  48. * @{
  49. */
  50. /// Base class for node handle types of maps and sets.
  51. template<typename _Val, typename _NodeAlloc>
  52. class _Node_handle_common
  53. {
  54. using _AllocTraits = allocator_traits<_NodeAlloc>;
  55. public:
  56. using allocator_type = __alloc_rebind<_NodeAlloc, _Val>;
  57. allocator_type
  58. get_allocator() const noexcept
  59. {
  60. __glibcxx_assert(!this->empty());
  61. return allocator_type(_M_alloc._M_alloc);
  62. }
  63. explicit operator bool() const noexcept { return _M_ptr != nullptr; }
  64. [[nodiscard]] bool empty() const noexcept { return _M_ptr == nullptr; }
  65. /// @cond undocumented
  66. protected:
  67. constexpr _Node_handle_common() noexcept : _M_ptr() { }
  68. ~_Node_handle_common()
  69. {
  70. if (!empty())
  71. _M_reset();
  72. }
  73. _Node_handle_common(_Node_handle_common&& __nh) noexcept
  74. : _M_ptr(__nh._M_ptr)
  75. {
  76. if (_M_ptr)
  77. _M_move(std::move(__nh));
  78. }
  79. _Node_handle_common&
  80. operator=(_Node_handle_common&& __nh) noexcept
  81. {
  82. if (empty())
  83. {
  84. if (!__nh.empty())
  85. _M_move(std::move(__nh));
  86. }
  87. else if (__nh.empty())
  88. _M_reset();
  89. else
  90. {
  91. // Free the current node before replacing the allocator.
  92. _AllocTraits::destroy(*_M_alloc, _M_ptr->_M_valptr());
  93. _AllocTraits::deallocate(*_M_alloc, _M_ptr, 1);
  94. _M_alloc = __nh._M_alloc.release(); // assigns if POCMA
  95. _M_ptr = __nh._M_ptr;
  96. __nh._M_ptr = nullptr;
  97. }
  98. return *this;
  99. }
  100. _Node_handle_common(typename _AllocTraits::pointer __ptr,
  101. const _NodeAlloc& __alloc)
  102. : _M_ptr(__ptr), _M_alloc(__alloc)
  103. {
  104. __glibcxx_assert(__ptr != nullptr);
  105. }
  106. void
  107. _M_swap(_Node_handle_common& __nh) noexcept
  108. {
  109. if (empty())
  110. {
  111. if (!__nh.empty())
  112. _M_move(std::move(__nh));
  113. }
  114. else if (__nh.empty())
  115. __nh._M_move(std::move(*this));
  116. else
  117. {
  118. using std::swap;
  119. swap(_M_ptr, __nh._M_ptr);
  120. _M_alloc.swap(__nh._M_alloc); // swaps if POCS
  121. }
  122. }
  123. private:
  124. // Moves the pointer and allocator from __nh to *this.
  125. // Precondition: empty() && !__nh.empty()
  126. // Postcondition: !empty() && __nh.empty()
  127. void
  128. _M_move(_Node_handle_common&& __nh) noexcept
  129. {
  130. ::new (std::__addressof(_M_alloc)) _NodeAlloc(__nh._M_alloc.release());
  131. _M_ptr = __nh._M_ptr;
  132. __nh._M_ptr = nullptr;
  133. }
  134. // Deallocates the node, destroys the allocator.
  135. // Precondition: !empty()
  136. // Postcondition: empty()
  137. void
  138. _M_reset() noexcept
  139. {
  140. _NodeAlloc __alloc = _M_alloc.release();
  141. _AllocTraits::destroy(__alloc, _M_ptr->_M_valptr());
  142. _AllocTraits::deallocate(__alloc, _M_ptr, 1);
  143. _M_ptr = nullptr;
  144. }
  145. // Destroys the allocator. Does not deallocate or destroy the node.
  146. // Precondition: !empty()
  147. // Postcondition: empty()
  148. void
  149. release() noexcept
  150. {
  151. _M_alloc.release();
  152. _M_ptr = nullptr;
  153. }
  154. protected:
  155. typename _AllocTraits::pointer _M_ptr;
  156. private:
  157. // A simplified, non-copyable std::optional<_NodeAlloc>.
  158. // Call release() before destruction iff the allocator member is active.
  159. union _Optional_alloc
  160. {
  161. _Optional_alloc() { }
  162. ~_Optional_alloc() { }
  163. _Optional_alloc(_Optional_alloc&&) = delete;
  164. _Optional_alloc& operator=(_Optional_alloc&&) = delete;
  165. _Optional_alloc(const _NodeAlloc& __alloc) noexcept
  166. : _M_alloc(__alloc)
  167. { }
  168. // Precondition: _M_alloc is the active member of the union.
  169. void
  170. operator=(_NodeAlloc&& __alloc) noexcept
  171. {
  172. using _ATr = _AllocTraits;
  173. if constexpr (_ATr::propagate_on_container_move_assignment::value)
  174. _M_alloc = std::move(__alloc);
  175. else if constexpr (!_AllocTraits::is_always_equal::value)
  176. __glibcxx_assert(_M_alloc == __alloc);
  177. }
  178. // Precondition: _M_alloc is the active member of both unions.
  179. void
  180. swap(_Optional_alloc& __other) noexcept
  181. {
  182. using std::swap;
  183. if constexpr (_AllocTraits::propagate_on_container_swap::value)
  184. swap(_M_alloc, __other._M_alloc);
  185. else if constexpr (!_AllocTraits::is_always_equal::value)
  186. __glibcxx_assert(_M_alloc == __other._M_alloc);
  187. }
  188. // Precondition: _M_alloc is the active member of the union.
  189. _NodeAlloc& operator*() noexcept { return _M_alloc; }
  190. // Precondition: _M_alloc is the active member of the union.
  191. _NodeAlloc release() noexcept
  192. {
  193. _NodeAlloc __tmp = std::move(_M_alloc);
  194. _M_alloc.~_NodeAlloc();
  195. return __tmp;
  196. }
  197. [[__no_unique_address__]] _NodeAlloc _M_alloc;
  198. };
  199. [[__no_unique_address__]] _Optional_alloc _M_alloc;
  200. template<typename _Key2, typename _Value2, typename _KeyOfValue,
  201. typename _Compare, typename _ValueAlloc>
  202. friend class _Rb_tree;
  203. template<typename _Key2, typename _Value2, typename _ValueAlloc,
  204. typename _ExtractKey, typename _Equal,
  205. typename _Hash, typename _RangeHash, typename _Unused,
  206. typename _RehashPolicy, typename _Traits>
  207. friend class _Hashtable;
  208. /// @endcond
  209. };
  210. /// Node handle type for maps.
  211. template<typename _Key, typename _Value, typename _NodeAlloc>
  212. class _Node_handle : public _Node_handle_common<_Value, _NodeAlloc>
  213. {
  214. public:
  215. constexpr _Node_handle() noexcept = default;
  216. ~_Node_handle() = default;
  217. _Node_handle(_Node_handle&&) noexcept = default;
  218. _Node_handle&
  219. operator=(_Node_handle&&) noexcept = default;
  220. using key_type = _Key;
  221. using mapped_type = typename _Value::second_type;
  222. key_type&
  223. key() const noexcept
  224. {
  225. __glibcxx_assert(!this->empty());
  226. return *_M_pkey;
  227. }
  228. mapped_type&
  229. mapped() const noexcept
  230. {
  231. __glibcxx_assert(!this->empty());
  232. return *_M_pmapped;
  233. }
  234. void
  235. swap(_Node_handle& __nh) noexcept
  236. {
  237. this->_M_swap(__nh);
  238. using std::swap;
  239. swap(_M_pkey, __nh._M_pkey);
  240. swap(_M_pmapped, __nh._M_pmapped);
  241. }
  242. friend void
  243. swap(_Node_handle& __x, _Node_handle& __y)
  244. noexcept(noexcept(__x.swap(__y)))
  245. { __x.swap(__y); }
  246. private:
  247. using _AllocTraits = allocator_traits<_NodeAlloc>;
  248. _Node_handle(typename _AllocTraits::pointer __ptr,
  249. const _NodeAlloc& __alloc)
  250. : _Node_handle_common<_Value, _NodeAlloc>(__ptr, __alloc)
  251. {
  252. if (__ptr)
  253. {
  254. auto& __key = const_cast<_Key&>(__ptr->_M_valptr()->first);
  255. _M_pkey = _S_pointer_to(__key);
  256. _M_pmapped = _S_pointer_to(__ptr->_M_valptr()->second);
  257. }
  258. else
  259. {
  260. _M_pkey = nullptr;
  261. _M_pmapped = nullptr;
  262. }
  263. }
  264. template<typename _Tp>
  265. using __pointer
  266. = __ptr_rebind<typename _AllocTraits::pointer,
  267. remove_reference_t<_Tp>>;
  268. __pointer<_Key> _M_pkey = nullptr;
  269. __pointer<typename _Value::second_type> _M_pmapped = nullptr;
  270. template<typename _Tp>
  271. __pointer<_Tp>
  272. _S_pointer_to(_Tp& __obj)
  273. { return pointer_traits<__pointer<_Tp>>::pointer_to(__obj); }
  274. const key_type&
  275. _M_key() const noexcept { return key(); }
  276. template<typename _Key2, typename _Value2, typename _KeyOfValue,
  277. typename _Compare, typename _ValueAlloc>
  278. friend class _Rb_tree;
  279. template<typename _Key2, typename _Value2, typename _ValueAlloc,
  280. typename _ExtractKey, typename _Equal,
  281. typename _Hash, typename _RangeHash, typename _Unused,
  282. typename _RehashPolicy, typename _Traits>
  283. friend class _Hashtable;
  284. };
  285. /// Node handle type for sets.
  286. template<typename _Value, typename _NodeAlloc>
  287. class _Node_handle<_Value, _Value, _NodeAlloc>
  288. : public _Node_handle_common<_Value, _NodeAlloc>
  289. {
  290. public:
  291. constexpr _Node_handle() noexcept = default;
  292. ~_Node_handle() = default;
  293. _Node_handle(_Node_handle&&) noexcept = default;
  294. _Node_handle&
  295. operator=(_Node_handle&&) noexcept = default;
  296. using value_type = _Value;
  297. value_type&
  298. value() const noexcept
  299. {
  300. __glibcxx_assert(!this->empty());
  301. return *this->_M_ptr->_M_valptr();
  302. }
  303. void
  304. swap(_Node_handle& __nh) noexcept
  305. { this->_M_swap(__nh); }
  306. friend void
  307. swap(_Node_handle& __x, _Node_handle& __y)
  308. noexcept(noexcept(__x.swap(__y)))
  309. { __x.swap(__y); }
  310. private:
  311. using _AllocTraits = allocator_traits<_NodeAlloc>;
  312. _Node_handle(typename _AllocTraits::pointer __ptr,
  313. const _NodeAlloc& __alloc)
  314. : _Node_handle_common<_Value, _NodeAlloc>(__ptr, __alloc) { }
  315. const value_type&
  316. _M_key() const noexcept { return value(); }
  317. template<typename _Key, typename _Val, typename _KeyOfValue,
  318. typename _Compare, typename _Alloc>
  319. friend class _Rb_tree;
  320. template<typename _Key2, typename _Value2, typename _ValueAlloc,
  321. typename _ExtractKey, typename _Equal,
  322. typename _Hash, typename _RangeHash, typename _Unused,
  323. typename _RehashPolicy, typename _Traits>
  324. friend class _Hashtable;
  325. };
  326. /// Return type of insert(node_handle&&) on unique maps/sets.
  327. template<typename _Iterator, typename _NodeHandle>
  328. struct _Node_insert_return
  329. {
  330. _Iterator position = _Iterator();
  331. bool inserted = false;
  332. _NodeHandle node;
  333. };
  334. /// @}
  335. _GLIBCXX_END_NAMESPACE_VERSION
  336. } // namespace std
  337. #endif // C++17
  338. #endif