@@ -1213,6 +1213,80 @@ private:
1213
1213
__node_pointer __cache_root_;
1214
1214
__node_pointer __cache_elem_;
1215
1215
};
1216
+
1217
+ class __tree_deleter {
1218
+ __node_allocator& __alloc_;
1219
+
1220
+ public:
1221
+ using pointer = __node_pointer;
1222
+
1223
+ _LIBCPP_HIDE_FROM_ABI __tree_deleter (__node_allocator& __alloc) : __alloc_(__alloc) {}
1224
+
1225
+ void operator ()(__node_pointer __ptr) {
1226
+ if (!__ptr)
1227
+ return ;
1228
+
1229
+ (*this )(static_cast <__node_pointer>(__ptr->__left_ ));
1230
+
1231
+ auto __right = __ptr->__right_ ;
1232
+
1233
+ __node_traits::destroy (__alloc_, std::addressof (__ptr->__value_ ));
1234
+ __node_traits::deallocate (__alloc_, __ptr, 1 );
1235
+
1236
+ (*this )(static_cast <__node_pointer>(__right));
1237
+ }
1238
+ };
1239
+
1240
+ _LIBCPP_HIDE_FROM_ABI __node_pointer __copy_construct_tree (__node_pointer __src) {
1241
+ if (!__src)
1242
+ return nullptr ;
1243
+
1244
+ __node_holder __new_node = __construct_node (__src->__value_ );
1245
+
1246
+ unique_ptr<__node, __tree_deleter> __left (
1247
+ __copy_construct_tree (static_cast <__node_pointer>(__src->__left_ )), __node_alloc_);
1248
+ __node_pointer __right = __copy_construct_tree (static_cast <__node_pointer>(__src->__right_ ));
1249
+
1250
+ __node_pointer __new_node_ptr = __new_node.release ();
1251
+
1252
+ __new_node_ptr->__is_black_ = __src->__is_black_ ;
1253
+ __new_node_ptr->__left_ = static_cast <__node_base_pointer>(__left.release ());
1254
+ __new_node_ptr->__right_ = static_cast <__node_base_pointer>(__right);
1255
+ if (__new_node_ptr->__left_ )
1256
+ __new_node_ptr->__left_ ->__parent_ = static_cast <__end_node_pointer>(__new_node_ptr);
1257
+ if (__new_node_ptr->__right_ )
1258
+ __new_node_ptr->__right_ ->__parent_ = static_cast <__end_node_pointer>(__new_node_ptr);
1259
+ return __new_node_ptr;
1260
+ }
1261
+
1262
+ _LIBCPP_HIDE_FROM_ABI __node_pointer __copy_assign_tree (__node_pointer __assign_to, __node_pointer __src) {
1263
+ if (!__src) {
1264
+ destroy (__assign_to);
1265
+ return nullptr ;
1266
+ }
1267
+
1268
+ __assign_value (__assign_to->__value_ , __src->__value_ );
1269
+
1270
+ if (__assign_to->__left_ ) {
1271
+ __assign_to->__left_ = static_cast <__node_base_pointer>(__copy_assign_tree (
1272
+ static_cast <__node_pointer>(__assign_to->__left_ ), static_cast <__node_pointer>(__src->__left_ )));
1273
+ } else if (__src->__left_ ) {
1274
+ auto __new_left = __copy_construct_tree (static_cast <__node_pointer>(__src->__left_ ));
1275
+ __assign_to->__left_ = static_cast <__node_base_pointer>(__new_left);
1276
+ __new_left->__parent_ = static_cast <__end_node_pointer>(__assign_to);
1277
+ }
1278
+
1279
+ if (__assign_to->__right_ ) {
1280
+ __assign_to->__right_ = static_cast <__node_base_pointer>(__copy_assign_tree (
1281
+ static_cast <__node_pointer>(__assign_to->__right_ ), static_cast <__node_pointer>(__src->__right_ )));
1282
+ } else if (__src->__right_ ) {
1283
+ auto __new_right = __copy_construct_tree (static_cast <__node_pointer>(__src->__right_ ));
1284
+ __assign_to->__right_ = static_cast <__node_base_pointer>(__new_right);
1285
+ __new_right->__parent_ = static_cast <__end_node_pointer>(__assign_to);
1286
+ }
1287
+
1288
+ return __assign_to;
1289
+ }
1216
1290
};
1217
1291
1218
1292
template <class _Tp , class _Compare , class _Allocator >
@@ -1277,11 +1351,28 @@ __tree<_Tp, _Compare, _Allocator>::_DetachedTreeCache::__detach_next(__node_poin
1277
1351
1278
1352
template <class _Tp , class _Compare , class _Allocator >
1279
1353
__tree<_Tp, _Compare, _Allocator>& __tree<_Tp, _Compare, _Allocator>::operator =(const __tree& __t ) {
1280
- if (this != std::addressof (__t )) {
1281
- value_comp () = __t .value_comp ();
1282
- __copy_assign_alloc (__t );
1283
- __assign_multi (__t .begin (), __t .end ());
1354
+ if (this == std::addressof (__t ))
1355
+ return *this ;
1356
+
1357
+ value_comp () = __t .value_comp ();
1358
+ __copy_assign_alloc (__t );
1359
+
1360
+ if (__t .size () == 0 ) {
1361
+ clear ();
1362
+ return *this ;
1363
+ }
1364
+
1365
+ if (__size_ != 0 ) {
1366
+ __end_node_.__left_ = static_cast <__node_base_pointer>(__copy_assign_tree (
1367
+ static_cast <__node_pointer>(__end_node_.__left_ ), static_cast <__node_pointer>(__t .__end_node_ .__left_ )));
1368
+ } else {
1369
+ __end_node_.__left_ =
1370
+ static_cast <__node_base_pointer>(__copy_construct_tree (static_cast <__node_pointer>(__t .__end_node_ .__left_ )));
1371
+ __end_node_.__left_ ->__parent_ = __end_node ();
1284
1372
}
1373
+ __begin_node_ = static_cast <__end_node_pointer>(std::__tree_min (static_cast <__node_base_pointer>(__end_node ())));
1374
+ __size_ = __t .__size_ ;
1375
+
1285
1376
return *this ;
1286
1377
}
1287
1378
@@ -1327,11 +1418,18 @@ void __tree<_Tp, _Compare, _Allocator>::__assign_multi(_InputIterator __first, _
1327
1418
1328
1419
template <class _Tp , class _Compare , class _Allocator >
1329
1420
__tree<_Tp, _Compare, _Allocator>::__tree(const __tree& __t )
1330
- : __begin_node_(),
1421
+ : __begin_node_(__end_node() ),
1331
1422
__node_alloc_ (__node_traits::select_on_container_copy_construction(__t .__node_alloc())),
1332
1423
__size_(0 ),
1333
1424
__value_comp_(__t .value_comp()) {
1334
- __begin_node_ = __end_node ();
1425
+ if (__t .__size_ == 0 )
1426
+ return ;
1427
+
1428
+ __end_node_.__left_ =
1429
+ static_cast <__node_base_pointer>(__copy_construct_tree (static_cast <__node_pointer>(__t .__end_node_ .__left_ )));
1430
+ __end_node_.__left_ ->__parent_ = __end_node ();
1431
+ __begin_node_ = static_cast <__end_node_pointer>(std::__tree_min (static_cast <__node_base_pointer>(__end_node ())));
1432
+ __size_ = __t .__size_ ;
1335
1433
}
1336
1434
1337
1435
template <class _Tp , class _Compare , class _Allocator >
@@ -1430,13 +1528,7 @@ __tree<_Tp, _Compare, _Allocator>::~__tree() {
1430
1528
1431
1529
template <class _Tp , class _Compare , class _Allocator >
1432
1530
void __tree<_Tp, _Compare, _Allocator>::destroy(__node_pointer __nd) _NOEXCEPT {
1433
- if (__nd != nullptr ) {
1434
- destroy (static_cast <__node_pointer>(__nd->__left_ ));
1435
- destroy (static_cast <__node_pointer>(__nd->__right_ ));
1436
- __node_allocator& __na = __node_alloc ();
1437
- __node_traits::destroy (__na, std::addressof (__nd->__value_ ));
1438
- __node_traits::deallocate (__na, __nd, 1 );
1439
- }
1531
+ (__tree_deleter (__node_alloc_))(__nd);
1440
1532
}
1441
1533
1442
1534
template <class _Tp , class _Compare , class _Allocator >
0 commit comments