@@ -712,13 +712,6 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
712
712
( key, val, edge)
713
713
}
714
714
}
715
-
716
- fn into_kv_pointers_mut ( mut self ) -> ( * mut K , * mut V ) {
717
- let leaf = self . as_leaf_mut ( ) ;
718
- let keys = MaybeUninit :: slice_as_mut_ptr ( & mut leaf. keys ) ;
719
- let vals = MaybeUninit :: slice_as_mut_ptr ( & mut leaf. vals ) ;
720
- ( keys, vals)
721
- }
722
715
}
723
716
724
717
impl < BorrowType , K , V > NodeRef < BorrowType , K , V , marker:: LeafOrInternal > {
@@ -1428,36 +1421,42 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
1428
1421
1429
1422
// Move leaf data.
1430
1423
{
1431
- let left_kv = left_node. reborrow_mut ( ) . into_kv_pointers_mut ( ) ;
1432
- let right_kv = right_node. reborrow_mut ( ) . into_kv_pointers_mut ( ) ;
1433
- let parent_kv = {
1434
- let kv = self . parent . kv_mut ( ) ;
1435
- ( kv. 0 as * mut K , kv. 1 as * mut V )
1436
- } ;
1437
-
1438
1424
// Make room for stolen elements in the right child.
1439
- ptr :: copy ( right_kv . 0 , right_kv . 0 . add ( count ) , old_right_len ) ;
1440
- ptr :: copy ( right_kv . 1 , right_kv . 1 . add ( count ) , old_right_len ) ;
1425
+ slice_shr ( right_node . key_area_mut ( ..new_right_len ) , count ) ;
1426
+ slice_shr ( right_node . val_area_mut ( ..new_right_len ) , count ) ;
1441
1427
1442
1428
// Move elements from the left child to the right one.
1443
- move_kv ( left_kv, new_left_len + 1 , right_kv, 0 , count - 1 ) ;
1444
-
1445
- // Move parent's key-value pair to the right child.
1446
- move_kv ( parent_kv, 0 , right_kv, count - 1 , 1 ) ;
1429
+ move_to_slice (
1430
+ left_node. key_area_mut ( new_left_len + 1 ..old_left_len) ,
1431
+ right_node. key_area_mut ( ..count - 1 ) ,
1432
+ ) ;
1433
+ move_to_slice (
1434
+ left_node. val_area_mut ( new_left_len + 1 ..old_left_len) ,
1435
+ right_node. val_area_mut ( ..count - 1 ) ,
1436
+ ) ;
1447
1437
1448
1438
// Move the left-most stolen pair to the parent.
1449
- move_kv ( left_kv, new_left_len, parent_kv, 0 , 1 ) ;
1439
+ let k = left_node. key_area_mut ( new_left_len) . assume_init_read ( ) ;
1440
+ let v = left_node. val_area_mut ( new_left_len) . assume_init_read ( ) ;
1441
+ let ( k, v) = self . parent . replace_kv ( k, v) ;
1442
+
1443
+ // Move parent's key-value pair to the right child.
1444
+ right_node. key_area_mut ( count - 1 ) . write ( k) ;
1445
+ right_node. val_area_mut ( count - 1 ) . write ( v) ;
1450
1446
}
1451
1447
1452
1448
match ( left_node. reborrow_mut ( ) . force ( ) , right_node. reborrow_mut ( ) . force ( ) ) {
1453
- ( ForceResult :: Internal ( left) , ForceResult :: Internal ( mut right) ) => {
1449
+ ( ForceResult :: Internal ( mut left) , ForceResult :: Internal ( mut right) ) => {
1454
1450
// Make room for stolen edges.
1455
- let right_edges = right. edge_area_mut ( ..) . as_mut_ptr ( ) ;
1456
- ptr:: copy ( right_edges, right_edges. add ( count) , old_right_len + 1 ) ;
1457
- right. correct_childrens_parent_links ( count..new_right_len + 1 ) ;
1451
+ slice_shr ( right. edge_area_mut ( ..new_right_len + 1 ) , count) ;
1458
1452
1459
1453
// Steal edges.
1460
- move_edges ( left, new_left_len + 1 , right, 0 , count) ;
1454
+ move_to_slice (
1455
+ left. edge_area_mut ( new_left_len + 1 ..old_left_len + 1 ) ,
1456
+ right. edge_area_mut ( ..count) ,
1457
+ ) ;
1458
+
1459
+ right. correct_childrens_parent_links ( 0 ..new_right_len + 1 ) ;
1461
1460
}
1462
1461
( ForceResult :: Leaf ( _) , ForceResult :: Leaf ( _) ) => { }
1463
1462
_ => unreachable ! ( ) ,
@@ -1485,36 +1484,43 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
1485
1484
1486
1485
// Move leaf data.
1487
1486
{
1488
- let left_kv = left_node. reborrow_mut ( ) . into_kv_pointers_mut ( ) ;
1489
- let right_kv = right_node. reborrow_mut ( ) . into_kv_pointers_mut ( ) ;
1490
- let parent_kv = {
1491
- let kv = self . parent . kv_mut ( ) ;
1492
- ( kv. 0 as * mut K , kv. 1 as * mut V )
1493
- } ;
1487
+ // Move the right-most stolen pair to the parent.
1488
+ let k = right_node. key_area_mut ( count - 1 ) . assume_init_read ( ) ;
1489
+ let v = right_node. val_area_mut ( count - 1 ) . assume_init_read ( ) ;
1490
+ let ( k, v) = self . parent . replace_kv ( k, v) ;
1494
1491
1495
1492
// Move parent's key-value pair to the left child.
1496
- move_kv ( parent_kv, 0 , left_kv, old_left_len, 1 ) ;
1493
+ left_node. key_area_mut ( old_left_len) . write ( k) ;
1494
+ left_node. val_area_mut ( old_left_len) . write ( v) ;
1497
1495
1498
1496
// Move elements from the right child to the left one.
1499
- move_kv ( right_kv, 0 , left_kv, old_left_len + 1 , count - 1 ) ;
1500
-
1501
- // Move the right-most stolen pair to the parent.
1502
- move_kv ( right_kv, count - 1 , parent_kv, 0 , 1 ) ;
1497
+ move_to_slice (
1498
+ right_node. key_area_mut ( ..count - 1 ) ,
1499
+ left_node. key_area_mut ( old_left_len + 1 ..new_left_len) ,
1500
+ ) ;
1501
+ move_to_slice (
1502
+ right_node. val_area_mut ( ..count - 1 ) ,
1503
+ left_node. val_area_mut ( old_left_len + 1 ..new_left_len) ,
1504
+ ) ;
1503
1505
1504
1506
// Fill gap where stolen elements used to be.
1505
- ptr :: copy ( right_kv . 0 . add ( count ) , right_kv . 0 , new_right_len ) ;
1506
- ptr :: copy ( right_kv . 1 . add ( count ) , right_kv . 1 , new_right_len ) ;
1507
+ slice_shl ( right_node . key_area_mut ( ..old_right_len ) , count ) ;
1508
+ slice_shl ( right_node . val_area_mut ( ..old_right_len ) , count ) ;
1507
1509
}
1508
1510
1509
1511
match ( left_node. reborrow_mut ( ) . force ( ) , right_node. reborrow_mut ( ) . force ( ) ) {
1510
- ( ForceResult :: Internal ( left) , ForceResult :: Internal ( mut right) ) => {
1512
+ ( ForceResult :: Internal ( mut left) , ForceResult :: Internal ( mut right) ) => {
1511
1513
// Steal edges.
1512
- move_edges ( right. reborrow_mut ( ) , 0 , left, old_left_len + 1 , count) ;
1514
+ move_to_slice (
1515
+ right. edge_area_mut ( ..count) ,
1516
+ left. edge_area_mut ( old_left_len + 1 ..new_left_len + 1 ) ,
1517
+ ) ;
1513
1518
1514
1519
// Fill gap where stolen edges used to be.
1515
- let right_edges = right. edge_area_mut ( ..) . as_mut_ptr ( ) ;
1516
- ptr:: copy ( right_edges. add ( count) , right_edges, new_right_len + 1 ) ;
1517
- right. correct_childrens_parent_links ( 0 ..=new_right_len) ;
1520
+ slice_shl ( right. edge_area_mut ( ..old_right_len + 1 ) , count) ;
1521
+
1522
+ left. correct_childrens_parent_links ( old_left_len + 1 ..new_left_len + 1 ) ;
1523
+ right. correct_childrens_parent_links ( 0 ..new_right_len + 1 ) ;
1518
1524
}
1519
1525
( ForceResult :: Leaf ( _) , ForceResult :: Leaf ( _) ) => { }
1520
1526
_ => unreachable ! ( ) ,
@@ -1523,35 +1529,6 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
1523
1529
}
1524
1530
}
1525
1531
1526
- unsafe fn move_kv < K , V > (
1527
- source : ( * mut K , * mut V ) ,
1528
- source_offset : usize ,
1529
- dest : ( * mut K , * mut V ) ,
1530
- dest_offset : usize ,
1531
- count : usize ,
1532
- ) {
1533
- unsafe {
1534
- ptr:: copy_nonoverlapping ( source. 0 . add ( source_offset) , dest. 0 . add ( dest_offset) , count) ;
1535
- ptr:: copy_nonoverlapping ( source. 1 . add ( source_offset) , dest. 1 . add ( dest_offset) , count) ;
1536
- }
1537
- }
1538
-
1539
- // Source and destination must have the same height.
1540
- unsafe fn move_edges < ' a , K : ' a , V : ' a > (
1541
- mut source : NodeRef < marker:: Mut < ' a > , K , V , marker:: Internal > ,
1542
- source_offset : usize ,
1543
- mut dest : NodeRef < marker:: Mut < ' a > , K , V , marker:: Internal > ,
1544
- dest_offset : usize ,
1545
- count : usize ,
1546
- ) {
1547
- unsafe {
1548
- let source_ptr = source. edge_area_mut ( ..) . as_ptr ( ) ;
1549
- let dest_ptr = dest. edge_area_mut ( dest_offset..) . as_mut_ptr ( ) ;
1550
- ptr:: copy_nonoverlapping ( source_ptr. add ( source_offset) , dest_ptr, count) ;
1551
- dest. correct_childrens_parent_links ( dest_offset..dest_offset + count) ;
1552
- }
1553
- }
1554
-
1555
1532
impl < BorrowType , K , V > NodeRef < BorrowType , K , V , marker:: Leaf > {
1556
1533
/// Removes any static information asserting that this node is a `Leaf` node.
1557
1534
pub fn forget_type ( self ) -> NodeRef < BorrowType , K , V , marker:: LeafOrInternal > {
@@ -1629,25 +1606,33 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, ma
1629
1606
unsafe {
1630
1607
let new_left_len = self . idx ;
1631
1608
let mut left_node = self . reborrow_mut ( ) . into_node ( ) ;
1609
+ let old_left_len = left_node. len ( ) ;
1632
1610
1633
- let new_right_len = left_node . len ( ) - new_left_len;
1611
+ let new_right_len = old_left_len - new_left_len;
1634
1612
let mut right_node = right. reborrow_mut ( ) ;
1635
1613
1636
1614
assert ! ( right_node. len( ) == 0 ) ;
1637
1615
assert ! ( left_node. height == right_node. height) ;
1638
1616
1639
1617
if new_right_len > 0 {
1640
- let left_kv = left_node. reborrow_mut ( ) . into_kv_pointers_mut ( ) ;
1641
- let right_kv = right_node. reborrow_mut ( ) . into_kv_pointers_mut ( ) ;
1642
-
1643
- move_kv ( left_kv, new_left_len, right_kv, 0 , new_right_len) ;
1644
-
1645
1618
* left_node. len_mut ( ) = new_left_len as u16 ;
1646
1619
* right_node. len_mut ( ) = new_right_len as u16 ;
1647
1620
1621
+ move_to_slice (
1622
+ left_node. key_area_mut ( new_left_len..old_left_len) ,
1623
+ right_node. key_area_mut ( ..new_right_len) ,
1624
+ ) ;
1625
+ move_to_slice (
1626
+ left_node. val_area_mut ( new_left_len..old_left_len) ,
1627
+ right_node. val_area_mut ( ..new_right_len) ,
1628
+ ) ;
1648
1629
match ( left_node. force ( ) , right_node. force ( ) ) {
1649
- ( ForceResult :: Internal ( left) , ForceResult :: Internal ( right) ) => {
1650
- move_edges ( left, new_left_len + 1 , right, 1 , new_right_len) ;
1630
+ ( ForceResult :: Internal ( mut left) , ForceResult :: Internal ( mut right) ) => {
1631
+ move_to_slice (
1632
+ left. edge_area_mut ( new_left_len + 1 ..old_left_len + 1 ) ,
1633
+ right. edge_area_mut ( 1 ..new_right_len + 1 ) ,
1634
+ ) ;
1635
+ right. correct_childrens_parent_links ( 1 ..new_right_len + 1 ) ;
1651
1636
}
1652
1637
( ForceResult :: Leaf ( _) , ForceResult :: Leaf ( _) ) => { }
1653
1638
_ => unreachable ! ( ) ,
@@ -1737,6 +1722,28 @@ unsafe fn slice_remove<T>(slice: &mut [MaybeUninit<T>], idx: usize) -> T {
1737
1722
}
1738
1723
}
1739
1724
1725
+ /// Shifts the elements in a slice `distance` positions to the left.
1726
+ ///
1727
+ /// # Safety
1728
+ /// The slice has at least `distance` elements.
1729
+ unsafe fn slice_shl < T > ( slice : & mut [ MaybeUninit < T > ] , distance : usize ) {
1730
+ unsafe {
1731
+ let slice_ptr = slice. as_mut_ptr ( ) ;
1732
+ ptr:: copy ( slice_ptr. add ( distance) , slice_ptr, slice. len ( ) - distance) ;
1733
+ }
1734
+ }
1735
+
1736
+ /// Shifts the elements in a slice `distance` positions to the right.
1737
+ ///
1738
+ /// # Safety
1739
+ /// The slice has at least `distance` elements.
1740
+ unsafe fn slice_shr < T > ( slice : & mut [ MaybeUninit < T > ] , distance : usize ) {
1741
+ unsafe {
1742
+ let slice_ptr = slice. as_mut_ptr ( ) ;
1743
+ ptr:: copy ( slice_ptr, slice_ptr. add ( distance) , slice. len ( ) - distance) ;
1744
+ }
1745
+ }
1746
+
1740
1747
/// Moves all values from a slice of initialized elements to a slice
1741
1748
/// of uninitialized elements, leaving behind `src` as all uninitialized.
1742
1749
/// Works like `dst.copy_from_slice(src)` but does not require `T` to be `Copy`.
0 commit comments