libstdc++
|
00001 // Node handles for containers -*- C++ -*- 00002 00003 // Copyright (C) 2016-2017 Free Software Foundation, Inc. 00004 // 00005 // This file is part of the GNU ISO C++ Library. This library is free 00006 // software; you can redistribute it and/or modify it under the 00007 // terms of the GNU General Public License as published by the 00008 // Free Software Foundation; either version 3, or (at your option) 00009 // any later version. 00010 00011 // This library is distributed in the hope that it will be useful, 00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00014 // GNU General Public License for more details. 00015 00016 // Under Section 7 of GPL version 3, you are granted additional 00017 // permissions described in the GCC Runtime Library Exception, version 00018 // 3.1, as published by the Free Software Foundation. 00019 00020 // You should have received a copy of the GNU General Public License and 00021 // a copy of the GCC Runtime Library Exception along with this program; 00022 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 00023 // <http://www.gnu.org/licenses/>. 00024 00025 /** @file bits/node_handle.h 00026 * This is an internal header file, included by other library headers. 00027 * Do not attempt to use it directly. 00028 * @headername{map,set,unordered_map,unordered_set} 00029 */ 00030 00031 #ifndef _NODE_HANDLE 00032 #define _NODE_HANDLE 1 00033 00034 #pragma GCC system_header 00035 00036 #if __cplusplus > 201402L 00037 # define __cpp_lib_node_extract 201606 00038 00039 #include <optional> 00040 #include <tuple> 00041 #include <bits/alloc_traits.h> 00042 #include <bits/ptr_traits.h> 00043 00044 namespace std _GLIBCXX_VISIBILITY(default) 00045 { 00046 _GLIBCXX_BEGIN_NAMESPACE_VERSION 00047 00048 /// Base class for node handle types of maps and sets. 00049 template<typename _Val, typename _NodeAlloc> 00050 class _Node_handle_common 00051 { 00052 using _AllocTraits = allocator_traits<_NodeAlloc>; 00053 00054 public: 00055 using allocator_type = __alloc_rebind<_NodeAlloc, _Val>; 00056 00057 allocator_type 00058 get_allocator() const noexcept 00059 { 00060 __glibcxx_assert(!this->empty()); 00061 return allocator_type(*_M_alloc); 00062 } 00063 00064 explicit operator bool() const noexcept { return _M_ptr != nullptr; } 00065 00066 bool empty() const noexcept { return _M_ptr == nullptr; } 00067 00068 protected: 00069 constexpr _Node_handle_common() noexcept : _M_ptr(), _M_alloc() {} 00070 00071 ~_Node_handle_common() { _M_destroy(); } 00072 00073 _Node_handle_common(_Node_handle_common&& __nh) noexcept 00074 : _M_ptr(__nh._M_ptr), _M_alloc(std::move(__nh._M_alloc)) 00075 { 00076 __nh._M_ptr = nullptr; 00077 __nh._M_alloc = nullopt; 00078 } 00079 00080 _Node_handle_common& 00081 operator=(_Node_handle_common&& __nh) noexcept 00082 { 00083 _M_destroy(); 00084 _M_ptr = __nh._M_ptr; 00085 if constexpr (is_move_assignable_v<_NodeAlloc>) 00086 { 00087 if (_AllocTraits::propagate_on_container_move_assignment::value 00088 || !this->_M_alloc) 00089 this->_M_alloc = std::move(__nh._M_alloc); 00090 else 00091 __glibcxx_assert(this->_M_alloc == __nh._M_alloc); 00092 } 00093 else 00094 __glibcxx_assert(_M_alloc); 00095 __nh._M_ptr = nullptr; 00096 __nh._M_alloc = nullopt; 00097 return *this; 00098 } 00099 00100 _Node_handle_common(typename _AllocTraits::pointer __ptr, 00101 const _NodeAlloc& __alloc) 00102 : _M_ptr(__ptr), _M_alloc(__alloc) { } 00103 00104 void 00105 _M_swap(_Node_handle_common& __nh) noexcept 00106 { 00107 using std::swap; 00108 swap(_M_ptr, __nh._M_ptr); 00109 if (_AllocTraits::propagate_on_container_swap 00110 || !_M_alloc || !__nh._M_alloc) 00111 _M_alloc.swap(__nh._M_alloc); 00112 else 00113 __glibcxx_assert(_M_alloc == __nh._M_alloc); 00114 } 00115 00116 private: 00117 void 00118 _M_destroy() noexcept 00119 { 00120 if (_M_ptr != nullptr) 00121 { 00122 allocator_type __alloc(*_M_alloc); 00123 allocator_traits<allocator_type>::destroy(__alloc, 00124 _M_ptr->_M_valptr()); 00125 _AllocTraits::deallocate(*_M_alloc, _M_ptr, 1); 00126 } 00127 } 00128 00129 protected: 00130 typename _AllocTraits::pointer _M_ptr; 00131 private: 00132 optional<_NodeAlloc> _M_alloc; 00133 00134 template<typename _Key2, typename _Value2, typename _KeyOfValue, 00135 typename _Compare, typename _ValueAlloc> 00136 friend class _Rb_tree; 00137 }; 00138 00139 /// Node handle type for maps. 00140 template<typename _Key, typename _Value, typename _NodeAlloc> 00141 class _Node_handle : public _Node_handle_common<_Value, _NodeAlloc> 00142 { 00143 public: 00144 constexpr _Node_handle() noexcept = default; 00145 ~_Node_handle() = default; 00146 _Node_handle(_Node_handle&&) noexcept = default; 00147 00148 _Node_handle& 00149 operator=(_Node_handle&&) noexcept = default; 00150 00151 using key_type = _Key; 00152 using mapped_type = typename _Value::second_type; 00153 00154 key_type& 00155 key() const noexcept 00156 { 00157 __glibcxx_assert(!this->empty()); 00158 return *_M_pkey; 00159 } 00160 00161 mapped_type& 00162 mapped() const noexcept 00163 { 00164 __glibcxx_assert(!this->empty()); 00165 return *_M_pmapped; 00166 } 00167 00168 void 00169 swap(_Node_handle& __nh) noexcept 00170 { 00171 this->_M_swap(__nh); 00172 using std::swap; 00173 swap(_M_pkey, __nh._M_pkey); 00174 swap(_M_pmapped, __nh._M_pmapped); 00175 } 00176 00177 friend void 00178 swap(_Node_handle& __x, _Node_handle& __y) 00179 noexcept(noexcept(__x.swap(__y))) 00180 { __x.swap(__y); } 00181 00182 private: 00183 using _AllocTraits = allocator_traits<_NodeAlloc>; 00184 00185 _Node_handle(typename _AllocTraits::pointer __ptr, 00186 const _NodeAlloc& __alloc) 00187 : _Node_handle_common<_Value, _NodeAlloc>(__ptr, __alloc) 00188 { 00189 if (__ptr) 00190 { 00191 auto& __key = const_cast<_Key&>(__ptr->_M_valptr()->first); 00192 _M_pkey = _S_pointer_to(__key); 00193 _M_pmapped = _S_pointer_to(__ptr->_M_valptr()->second); 00194 } 00195 else 00196 { 00197 _M_pkey = nullptr; 00198 _M_pmapped = nullptr; 00199 } 00200 } 00201 00202 template<typename _Tp> 00203 using __pointer = __ptr_rebind<typename _AllocTraits::pointer, _Tp>; 00204 00205 __pointer<_Key> _M_pkey = nullptr; 00206 __pointer<typename _Value::second_type> _M_pmapped = nullptr; 00207 00208 template<typename _Tp> 00209 __pointer<_Tp> 00210 _S_pointer_to(_Tp& __obj) 00211 { return pointer_traits<__pointer<_Tp>>::pointer_to(__obj); } 00212 00213 const key_type& 00214 _M_key() const noexcept { return key(); } 00215 00216 template<typename _Key2, typename _Value2, typename _KeyOfValue, 00217 typename _Compare, typename _ValueAlloc> 00218 friend class _Rb_tree; 00219 00220 template<typename _Key2, typename _Value2, typename _ValueAlloc, 00221 typename _ExtractKey, typename _Equal, 00222 typename _H1, typename _H2, typename _Hash, 00223 typename _RehashPolicy, typename _Traits> 00224 friend class _Hashtable; 00225 }; 00226 00227 /// Node handle type for sets. 00228 template<typename _Value, typename _NodeAlloc> 00229 class _Node_handle<_Value, _Value, _NodeAlloc> 00230 : public _Node_handle_common<_Value, _NodeAlloc> 00231 { 00232 public: 00233 constexpr _Node_handle() noexcept = default; 00234 ~_Node_handle() = default; 00235 _Node_handle(_Node_handle&&) noexcept = default; 00236 00237 _Node_handle& 00238 operator=(_Node_handle&&) noexcept = default; 00239 00240 using value_type = _Value; 00241 00242 value_type& 00243 value() const noexcept 00244 { 00245 __glibcxx_assert(!this->empty()); 00246 return *this->_M_ptr->_M_valptr(); 00247 } 00248 00249 void 00250 swap(_Node_handle& __nh) noexcept 00251 { this->_M_swap(__nh); } 00252 00253 friend void 00254 swap(_Node_handle& __x, _Node_handle& __y) 00255 noexcept(noexcept(__x.swap(__y))) 00256 { __x.swap(__y); } 00257 00258 private: 00259 using _AllocTraits = allocator_traits<_NodeAlloc>; 00260 00261 _Node_handle(typename _AllocTraits::pointer __ptr, 00262 const _NodeAlloc& __alloc) 00263 : _Node_handle_common<_Value, _NodeAlloc>(__ptr, __alloc) { } 00264 00265 const value_type& 00266 _M_key() const noexcept { return value(); } 00267 00268 template<typename _Key, typename _Val, typename _KeyOfValue, 00269 typename _Compare, typename _Alloc> 00270 friend class _Rb_tree; 00271 00272 template<typename _Key2, typename _Value2, typename _ValueAlloc, 00273 typename _ExtractKey, typename _Equal, 00274 typename _H1, typename _H2, typename _Hash, 00275 typename _RehashPolicy, typename _Traits> 00276 friend class _Hashtable; 00277 }; 00278 00279 /// Return type of insert(node_handle&&) on unique maps/sets. 00280 template<typename _Iterator, typename _NodeHandle> 00281 struct _Node_insert_return 00282 { 00283 _Iterator position = _Iterator(); 00284 bool inserted = false; 00285 _NodeHandle node; 00286 00287 template<size_t _Idx> 00288 decltype(auto) get() & 00289 { return std::get<_Idx>(std::tie(inserted, position, node)); } 00290 00291 template<size_t _Idx> 00292 decltype(auto) get() const & 00293 { return std::get<_Idx>(std::tie(inserted, position, node)); } 00294 00295 template<size_t _Idx> 00296 decltype(auto) get() && 00297 { 00298 return std::move(std::get<_Idx>(std::tie(inserted, position, node))); 00299 } 00300 00301 template<size_t _Idx> 00302 decltype(auto) get() const && 00303 { 00304 return std::move(std::get<_Idx>(std::tie(inserted, position, node))); 00305 } 00306 }; 00307 00308 _GLIBCXX_END_NAMESPACE_VERSION 00309 } // namespace std 00310 00311 #endif // C++17 00312 #endif