node_handle.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. // Node handles for containers -*- C++ -*-
  2. // Copyright (C) 2016-2021 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 201606
  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. /// Base class for node handle types of maps and sets.
  37. template<typename _Val, typename _NodeAlloc>
  38. class _Node_handle_common
  39. {
  40. using _AllocTraits = allocator_traits<_NodeAlloc>;
  41. public:
  42. using allocator_type = __alloc_rebind<_NodeAlloc, _Val>;
  43. allocator_type
  44. get_allocator() const noexcept
  45. {
  46. __glibcxx_assert(!this->empty());
  47. return allocator_type(_M_alloc._M_alloc);
  48. }
  49. explicit operator bool() const noexcept { return _M_ptr != nullptr; }
  50. [[nodiscard]] bool empty() const noexcept { return _M_ptr == nullptr; }
  51. protected:
  52. constexpr _Node_handle_common() noexcept : _M_ptr() { }
  53. ~_Node_handle_common()
  54. {
  55. if (!empty())
  56. _M_reset();
  57. }
  58. _Node_handle_common(_Node_handle_common&& __nh) noexcept
  59. : _M_ptr(__nh._M_ptr)
  60. {
  61. if (_M_ptr)
  62. _M_move(std::move(__nh));
  63. }
  64. _Node_handle_common&
  65. operator=(_Node_handle_common&& __nh) noexcept
  66. {
  67. if (empty())
  68. {
  69. if (!__nh.empty())
  70. _M_move(std::move(__nh));
  71. }
  72. else if (__nh.empty())
  73. _M_reset();
  74. else
  75. {
  76. // Free the current node before replacing the allocator.
  77. _AllocTraits::destroy(*_M_alloc, _M_ptr->_M_valptr());
  78. _AllocTraits::deallocate(*_M_alloc, _M_ptr, 1);
  79. _M_alloc = __nh._M_alloc.release(); // assigns if POCMA
  80. _M_ptr = __nh._M_ptr;
  81. __nh._M_ptr = nullptr;
  82. }
  83. return *this;
  84. }
  85. _Node_handle_common(typename _AllocTraits::pointer __ptr,
  86. const _NodeAlloc& __alloc)
  87. : _M_ptr(__ptr), _M_alloc(__alloc)
  88. {
  89. __glibcxx_assert(__ptr != nullptr);
  90. }
  91. void
  92. _M_swap(_Node_handle_common& __nh) noexcept
  93. {
  94. if (empty())
  95. {
  96. if (!__nh.empty())
  97. _M_move(std::move(__nh));
  98. }
  99. else if (__nh.empty())
  100. __nh._M_move(std::move(*this));
  101. else
  102. {
  103. using std::swap;
  104. swap(_M_ptr, __nh._M_ptr);
  105. _M_alloc.swap(__nh._M_alloc); // swaps if POCS
  106. }
  107. }
  108. private:
  109. // Moves the pointer and allocator from __nh to *this.
  110. // Precondition: empty() && !__nh.empty()
  111. // Postcondition: !empty() && __nh.empty()
  112. void
  113. _M_move(_Node_handle_common&& __nh) noexcept
  114. {
  115. ::new (std::__addressof(_M_alloc)) _NodeAlloc(__nh._M_alloc.release());
  116. _M_ptr = __nh._M_ptr;
  117. __nh._M_ptr = nullptr;
  118. }
  119. // Deallocates the node, destroys the allocator.
  120. // Precondition: !empty()
  121. // Postcondition: empty()
  122. void
  123. _M_reset() noexcept
  124. {
  125. _NodeAlloc __alloc = _M_alloc.release();
  126. _AllocTraits::destroy(__alloc, _M_ptr->_M_valptr());
  127. _AllocTraits::deallocate(__alloc, _M_ptr, 1);
  128. _M_ptr = nullptr;
  129. }
  130. protected:
  131. typename _AllocTraits::pointer _M_ptr;
  132. private:
  133. // A simplified, non-copyable std::optional<_NodeAlloc>.
  134. // Call release() before destruction iff the allocator member is active.
  135. union _Optional_alloc
  136. {
  137. _Optional_alloc() { }
  138. ~_Optional_alloc() { }
  139. _Optional_alloc(_Optional_alloc&&) = delete;
  140. _Optional_alloc& operator=(_Optional_alloc&&) = delete;
  141. _Optional_alloc(const _NodeAlloc& __alloc) noexcept
  142. : _M_alloc(__alloc)
  143. { }
  144. // Precondition: _M_alloc is the active member of the union.
  145. void
  146. operator=(_NodeAlloc&& __alloc) noexcept
  147. {
  148. using _ATr = _AllocTraits;
  149. if constexpr (_ATr::propagate_on_container_move_assignment::value)
  150. _M_alloc = std::move(__alloc);
  151. else if constexpr (!_AllocTraits::is_always_equal::value)
  152. __glibcxx_assert(_M_alloc == __alloc);
  153. }
  154. // Precondition: _M_alloc is the active member of both unions.
  155. void
  156. swap(_Optional_alloc& __other) noexcept
  157. {
  158. using std::swap;
  159. if constexpr (_AllocTraits::propagate_on_container_swap::value)
  160. swap(_M_alloc, __other._M_alloc);
  161. else if constexpr (!_AllocTraits::is_always_equal::value)
  162. __glibcxx_assert(_M_alloc == __other._M_alloc);
  163. }
  164. // Precondition: _M_alloc is the active member of the union.
  165. _NodeAlloc& operator*() noexcept { return _M_alloc; }
  166. // Precondition: _M_alloc is the active member of the union.
  167. _NodeAlloc release() noexcept
  168. {
  169. _NodeAlloc __tmp = std::move(_M_alloc);
  170. _M_alloc.~_NodeAlloc();
  171. return __tmp;
  172. }
  173. struct _Empty { };
  174. [[__no_unique_address__]] _Empty _M_empty;
  175. [[__no_unique_address__]] _NodeAlloc _M_alloc;
  176. };
  177. [[__no_unique_address__]] _Optional_alloc _M_alloc;
  178. template<typename _Key2, typename _Value2, typename _KeyOfValue,
  179. typename _Compare, typename _ValueAlloc>
  180. friend class _Rb_tree;
  181. };
  182. /// Node handle type for maps.
  183. template<typename _Key, typename _Value, typename _NodeAlloc>
  184. class _Node_handle : public _Node_handle_common<_Value, _NodeAlloc>
  185. {
  186. public:
  187. constexpr _Node_handle() noexcept = default;
  188. ~_Node_handle() = default;
  189. _Node_handle(_Node_handle&&) noexcept = default;
  190. _Node_handle&
  191. operator=(_Node_handle&&) noexcept = default;
  192. using key_type = _Key;
  193. using mapped_type = typename _Value::second_type;
  194. key_type&
  195. key() const noexcept
  196. {
  197. __glibcxx_assert(!this->empty());
  198. return *_M_pkey;
  199. }
  200. mapped_type&
  201. mapped() const noexcept
  202. {
  203. __glibcxx_assert(!this->empty());
  204. return *_M_pmapped;
  205. }
  206. void
  207. swap(_Node_handle& __nh) noexcept
  208. {
  209. this->_M_swap(__nh);
  210. using std::swap;
  211. swap(_M_pkey, __nh._M_pkey);
  212. swap(_M_pmapped, __nh._M_pmapped);
  213. }
  214. friend void
  215. swap(_Node_handle& __x, _Node_handle& __y)
  216. noexcept(noexcept(__x.swap(__y)))
  217. { __x.swap(__y); }
  218. private:
  219. using _AllocTraits = allocator_traits<_NodeAlloc>;
  220. _Node_handle(typename _AllocTraits::pointer __ptr,
  221. const _NodeAlloc& __alloc)
  222. : _Node_handle_common<_Value, _NodeAlloc>(__ptr, __alloc)
  223. {
  224. if (__ptr)
  225. {
  226. auto& __key = const_cast<_Key&>(__ptr->_M_valptr()->first);
  227. _M_pkey = _S_pointer_to(__key);
  228. _M_pmapped = _S_pointer_to(__ptr->_M_valptr()->second);
  229. }
  230. else
  231. {
  232. _M_pkey = nullptr;
  233. _M_pmapped = nullptr;
  234. }
  235. }
  236. template<typename _Tp>
  237. using __pointer
  238. = __ptr_rebind<typename _AllocTraits::pointer,
  239. remove_reference_t<_Tp>>;
  240. __pointer<_Key> _M_pkey = nullptr;
  241. __pointer<typename _Value::second_type> _M_pmapped = nullptr;
  242. template<typename _Tp>
  243. __pointer<_Tp>
  244. _S_pointer_to(_Tp& __obj)
  245. { return pointer_traits<__pointer<_Tp>>::pointer_to(__obj); }
  246. const key_type&
  247. _M_key() const noexcept { return key(); }
  248. template<typename _Key2, typename _Value2, typename _KeyOfValue,
  249. typename _Compare, typename _ValueAlloc>
  250. friend class _Rb_tree;
  251. template<typename _Key2, typename _Value2, typename _ValueAlloc,
  252. typename _ExtractKey, typename _Equal,
  253. typename _Hash, typename _RangeHash, typename _Unused,
  254. typename _RehashPolicy, typename _Traits>
  255. friend class _Hashtable;
  256. };
  257. /// Node handle type for sets.
  258. template<typename _Value, typename _NodeAlloc>
  259. class _Node_handle<_Value, _Value, _NodeAlloc>
  260. : public _Node_handle_common<_Value, _NodeAlloc>
  261. {
  262. public:
  263. constexpr _Node_handle() noexcept = default;
  264. ~_Node_handle() = default;
  265. _Node_handle(_Node_handle&&) noexcept = default;
  266. _Node_handle&
  267. operator=(_Node_handle&&) noexcept = default;
  268. using value_type = _Value;
  269. value_type&
  270. value() const noexcept
  271. {
  272. __glibcxx_assert(!this->empty());
  273. return *this->_M_ptr->_M_valptr();
  274. }
  275. void
  276. swap(_Node_handle& __nh) noexcept
  277. { this->_M_swap(__nh); }
  278. friend void
  279. swap(_Node_handle& __x, _Node_handle& __y)
  280. noexcept(noexcept(__x.swap(__y)))
  281. { __x.swap(__y); }
  282. private:
  283. using _AllocTraits = allocator_traits<_NodeAlloc>;
  284. _Node_handle(typename _AllocTraits::pointer __ptr,
  285. const _NodeAlloc& __alloc)
  286. : _Node_handle_common<_Value, _NodeAlloc>(__ptr, __alloc) { }
  287. const value_type&
  288. _M_key() const noexcept { return value(); }
  289. template<typename _Key, typename _Val, typename _KeyOfValue,
  290. typename _Compare, typename _Alloc>
  291. friend class _Rb_tree;
  292. template<typename _Key2, typename _Value2, typename _ValueAlloc,
  293. typename _ExtractKey, typename _Equal,
  294. typename _Hash, typename _RangeHash, typename _Unused,
  295. typename _RehashPolicy, typename _Traits>
  296. friend class _Hashtable;
  297. };
  298. /// Return type of insert(node_handle&&) on unique maps/sets.
  299. template<typename _Iterator, typename _NodeHandle>
  300. struct _Node_insert_return
  301. {
  302. _Iterator position = _Iterator();
  303. bool inserted = false;
  304. _NodeHandle node;
  305. };
  306. _GLIBCXX_END_NAMESPACE_VERSION
  307. } // namespace std
  308. #endif // C++17
  309. #endif