node_handle.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. // Node handles for containers -*- C++ -*-
  2. // Copyright (C) 2016-2022 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. protected:
  146. typename _AllocTraits::pointer _M_ptr;
  147. private:
  148. // A simplified, non-copyable std::optional<_NodeAlloc>.
  149. // Call release() before destruction iff the allocator member is active.
  150. union _Optional_alloc
  151. {
  152. _Optional_alloc() { }
  153. ~_Optional_alloc() { }
  154. _Optional_alloc(_Optional_alloc&&) = delete;
  155. _Optional_alloc& operator=(_Optional_alloc&&) = delete;
  156. _Optional_alloc(const _NodeAlloc& __alloc) noexcept
  157. : _M_alloc(__alloc)
  158. { }
  159. // Precondition: _M_alloc is the active member of the union.
  160. void
  161. operator=(_NodeAlloc&& __alloc) noexcept
  162. {
  163. using _ATr = _AllocTraits;
  164. if constexpr (_ATr::propagate_on_container_move_assignment::value)
  165. _M_alloc = std::move(__alloc);
  166. else if constexpr (!_AllocTraits::is_always_equal::value)
  167. __glibcxx_assert(_M_alloc == __alloc);
  168. }
  169. // Precondition: _M_alloc is the active member of both unions.
  170. void
  171. swap(_Optional_alloc& __other) noexcept
  172. {
  173. using std::swap;
  174. if constexpr (_AllocTraits::propagate_on_container_swap::value)
  175. swap(_M_alloc, __other._M_alloc);
  176. else if constexpr (!_AllocTraits::is_always_equal::value)
  177. __glibcxx_assert(_M_alloc == __other._M_alloc);
  178. }
  179. // Precondition: _M_alloc is the active member of the union.
  180. _NodeAlloc& operator*() noexcept { return _M_alloc; }
  181. // Precondition: _M_alloc is the active member of the union.
  182. _NodeAlloc release() noexcept
  183. {
  184. _NodeAlloc __tmp = std::move(_M_alloc);
  185. _M_alloc.~_NodeAlloc();
  186. return __tmp;
  187. }
  188. struct _Empty { };
  189. [[__no_unique_address__]] _Empty _M_empty;
  190. [[__no_unique_address__]] _NodeAlloc _M_alloc;
  191. };
  192. [[__no_unique_address__]] _Optional_alloc _M_alloc;
  193. template<typename _Key2, typename _Value2, typename _KeyOfValue,
  194. typename _Compare, typename _ValueAlloc>
  195. friend class _Rb_tree;
  196. /// @endcond
  197. };
  198. /// Node handle type for maps.
  199. template<typename _Key, typename _Value, typename _NodeAlloc>
  200. class _Node_handle : public _Node_handle_common<_Value, _NodeAlloc>
  201. {
  202. public:
  203. constexpr _Node_handle() noexcept = default;
  204. ~_Node_handle() = default;
  205. _Node_handle(_Node_handle&&) noexcept = default;
  206. _Node_handle&
  207. operator=(_Node_handle&&) noexcept = default;
  208. using key_type = _Key;
  209. using mapped_type = typename _Value::second_type;
  210. key_type&
  211. key() const noexcept
  212. {
  213. __glibcxx_assert(!this->empty());
  214. return *_M_pkey;
  215. }
  216. mapped_type&
  217. mapped() const noexcept
  218. {
  219. __glibcxx_assert(!this->empty());
  220. return *_M_pmapped;
  221. }
  222. void
  223. swap(_Node_handle& __nh) noexcept
  224. {
  225. this->_M_swap(__nh);
  226. using std::swap;
  227. swap(_M_pkey, __nh._M_pkey);
  228. swap(_M_pmapped, __nh._M_pmapped);
  229. }
  230. friend void
  231. swap(_Node_handle& __x, _Node_handle& __y)
  232. noexcept(noexcept(__x.swap(__y)))
  233. { __x.swap(__y); }
  234. private:
  235. using _AllocTraits = allocator_traits<_NodeAlloc>;
  236. _Node_handle(typename _AllocTraits::pointer __ptr,
  237. const _NodeAlloc& __alloc)
  238. : _Node_handle_common<_Value, _NodeAlloc>(__ptr, __alloc)
  239. {
  240. if (__ptr)
  241. {
  242. auto& __key = const_cast<_Key&>(__ptr->_M_valptr()->first);
  243. _M_pkey = _S_pointer_to(__key);
  244. _M_pmapped = _S_pointer_to(__ptr->_M_valptr()->second);
  245. }
  246. else
  247. {
  248. _M_pkey = nullptr;
  249. _M_pmapped = nullptr;
  250. }
  251. }
  252. template<typename _Tp>
  253. using __pointer
  254. = __ptr_rebind<typename _AllocTraits::pointer,
  255. remove_reference_t<_Tp>>;
  256. __pointer<_Key> _M_pkey = nullptr;
  257. __pointer<typename _Value::second_type> _M_pmapped = nullptr;
  258. template<typename _Tp>
  259. __pointer<_Tp>
  260. _S_pointer_to(_Tp& __obj)
  261. { return pointer_traits<__pointer<_Tp>>::pointer_to(__obj); }
  262. const key_type&
  263. _M_key() const noexcept { return key(); }
  264. template<typename _Key2, typename _Value2, typename _KeyOfValue,
  265. typename _Compare, typename _ValueAlloc>
  266. friend class _Rb_tree;
  267. template<typename _Key2, typename _Value2, typename _ValueAlloc,
  268. typename _ExtractKey, typename _Equal,
  269. typename _Hash, typename _RangeHash, typename _Unused,
  270. typename _RehashPolicy, typename _Traits>
  271. friend class _Hashtable;
  272. };
  273. /// Node handle type for sets.
  274. template<typename _Value, typename _NodeAlloc>
  275. class _Node_handle<_Value, _Value, _NodeAlloc>
  276. : public _Node_handle_common<_Value, _NodeAlloc>
  277. {
  278. public:
  279. constexpr _Node_handle() noexcept = default;
  280. ~_Node_handle() = default;
  281. _Node_handle(_Node_handle&&) noexcept = default;
  282. _Node_handle&
  283. operator=(_Node_handle&&) noexcept = default;
  284. using value_type = _Value;
  285. value_type&
  286. value() const noexcept
  287. {
  288. __glibcxx_assert(!this->empty());
  289. return *this->_M_ptr->_M_valptr();
  290. }
  291. void
  292. swap(_Node_handle& __nh) noexcept
  293. { this->_M_swap(__nh); }
  294. friend void
  295. swap(_Node_handle& __x, _Node_handle& __y)
  296. noexcept(noexcept(__x.swap(__y)))
  297. { __x.swap(__y); }
  298. private:
  299. using _AllocTraits = allocator_traits<_NodeAlloc>;
  300. _Node_handle(typename _AllocTraits::pointer __ptr,
  301. const _NodeAlloc& __alloc)
  302. : _Node_handle_common<_Value, _NodeAlloc>(__ptr, __alloc) { }
  303. const value_type&
  304. _M_key() const noexcept { return value(); }
  305. template<typename _Key, typename _Val, typename _KeyOfValue,
  306. typename _Compare, typename _Alloc>
  307. friend class _Rb_tree;
  308. template<typename _Key2, typename _Value2, typename _ValueAlloc,
  309. typename _ExtractKey, typename _Equal,
  310. typename _Hash, typename _RangeHash, typename _Unused,
  311. typename _RehashPolicy, typename _Traits>
  312. friend class _Hashtable;
  313. };
  314. /// Return type of insert(node_handle&&) on unique maps/sets.
  315. template<typename _Iterator, typename _NodeHandle>
  316. struct _Node_insert_return
  317. {
  318. _Iterator position = _Iterator();
  319. bool inserted = false;
  320. _NodeHandle node;
  321. };
  322. /// @}
  323. _GLIBCXX_END_NAMESPACE_VERSION
  324. } // namespace std
  325. #endif // C++17
  326. #endif