@@ -35,6 +35,7 @@ use core::cmp::Ordering;
35
35
use core:: marker:: PhantomData ;
36
36
use core:: mem:: { self , MaybeUninit } ;
37
37
use core:: ptr:: { self , NonNull } ;
38
+ use core:: slice:: SliceIndex ;
38
39
39
40
use crate :: alloc:: { Allocator , Global , Layout } ;
40
41
use crate :: boxed:: Box ;
@@ -507,30 +508,45 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
507
508
/// Borrows exclusive access to an element of the key storage area.
508
509
///
509
510
/// # Safety
510
- /// The node has more than `idx` initialized elements.
511
- unsafe fn key_area_mut_at ( & mut self , idx : usize ) -> & mut MaybeUninit < K > {
512
- debug_assert ! ( idx < self . len( ) ) ;
513
- unsafe { self . as_leaf_mut ( ) . keys . get_unchecked_mut ( idx) }
511
+ /// `index` is in bounds of 0..CAPACITY
512
+ unsafe fn key_area_mut_at < I , Output : ?Sized > ( & mut self , index : I ) -> & mut Output
513
+ where
514
+ I : SliceIndex < [ MaybeUninit < K > ] , Output = Output > ,
515
+ {
516
+ // SAFETY: the caller will not be able to call further methods on self
517
+ // until the key slice reference is dropped, as we have unique access
518
+ // for the lifetime of the borrow.
519
+ unsafe { self . as_leaf_mut ( ) . keys . as_mut_slice ( ) . get_unchecked_mut ( index) }
514
520
}
515
521
516
- /// Borrows exclusive access to an element of the value storage area.
522
+ /// Borrows exclusive access to an element or slice of the node's value storage area.
517
523
///
518
524
/// # Safety
519
- /// The node has more than `idx` initialized elements.
520
- unsafe fn val_area_mut_at ( & mut self , idx : usize ) -> & mut MaybeUninit < V > {
521
- debug_assert ! ( idx < self . len( ) ) ;
522
- unsafe { self . as_leaf_mut ( ) . vals . get_unchecked_mut ( idx) }
525
+ /// `index` is in bounds of 0..CAPACITY
526
+ unsafe fn val_area_mut_at < I , Output : ?Sized > ( & mut self , index : I ) -> & mut Output
527
+ where
528
+ I : SliceIndex < [ MaybeUninit < V > ] , Output = Output > ,
529
+ {
530
+ // SAFETY: the caller will not be able to call further methods on self
531
+ // until the value slice reference is dropped, as we have unique access
532
+ // for the lifetime of the borrow.
533
+ unsafe { self . as_leaf_mut ( ) . vals . as_mut_slice ( ) . get_unchecked_mut ( index) }
523
534
}
524
535
}
525
536
526
537
impl < ' a , K : ' a , V : ' a > NodeRef < marker:: Mut < ' a > , K , V , marker:: Internal > {
527
- /// Borrows exclusive access to an element of the storage area for edge contents.
538
+ /// Borrows exclusive access to an element or slice of the node's storage area for edge contents.
528
539
///
529
540
/// # Safety
530
- /// The node has at least `idx` initialized elements.
531
- unsafe fn edge_area_mut_at ( & mut self , idx : usize ) -> & mut MaybeUninit < BoxedNode < K , V > > {
532
- debug_assert ! ( idx <= self . len( ) ) ;
533
- unsafe { self . as_internal_mut ( ) . edges . get_unchecked_mut ( idx) }
541
+ /// `index` is in bounds of 0..CAPACITY + 1
542
+ unsafe fn edge_area_mut_at < I , Output : ?Sized > ( & mut self , index : I ) -> & mut Output
543
+ where
544
+ I : SliceIndex < [ MaybeUninit < BoxedNode < K , V > > ] , Output = Output > ,
545
+ {
546
+ // SAFETY: the caller will not be able to call further methods on self
547
+ // until the edge slice reference is dropped, as we have unique access
548
+ // for the lifetime of the borrow.
549
+ unsafe { self . as_internal_mut ( ) . edges . as_mut_slice ( ) . get_unchecked_mut ( index) }
534
550
}
535
551
}
536
552
@@ -559,37 +575,6 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Immut<'a>, K, V, marker::Internal> {
559
575
}
560
576
}
561
577
562
- impl < ' a , K : ' a , V : ' a , Type > NodeRef < marker:: Mut < ' a > , K , V , Type > {
563
- /// Borrows exclusive access to a sized slice of key storage area in the node.
564
- unsafe fn key_area_slice ( & mut self ) -> & mut [ MaybeUninit < K > ] {
565
- let len = self . len ( ) ;
566
- // SAFETY: the caller will not be able to call further methods on self
567
- // until the key slice reference is dropped, as we have unique access
568
- // for the lifetime of the borrow.
569
- unsafe { self . as_leaf_mut ( ) . keys . get_unchecked_mut ( ..len) }
570
- }
571
-
572
- /// Borrows exclusive access to a sized slice of value storage area in the node.
573
- unsafe fn val_area_slice ( & mut self ) -> & mut [ MaybeUninit < V > ] {
574
- let len = self . len ( ) ;
575
- // SAFETY: the caller will not be able to call further methods on self
576
- // until the value slice reference is dropped, as we have unique access
577
- // for the lifetime of the borrow.
578
- unsafe { self . as_leaf_mut ( ) . vals . get_unchecked_mut ( ..len) }
579
- }
580
- }
581
-
582
- impl < ' a , K : ' a , V : ' a > NodeRef < marker:: Mut < ' a > , K , V , marker:: Internal > {
583
- /// Borrows exclusive access to a sized slice of storage area for edge contents in the node.
584
- unsafe fn edge_area_slice ( & mut self ) -> & mut [ MaybeUninit < BoxedNode < K , V > > ] {
585
- let len = self . len ( ) ;
586
- // SAFETY: the caller will not be able to call further methods on self
587
- // until the edge slice reference is dropped, as we have unique access
588
- // for the lifetime of the borrow.
589
- unsafe { self . as_internal_mut ( ) . edges . get_unchecked_mut ( ..len + 1 ) }
590
- }
591
- }
592
-
593
578
impl < ' a , K , V , Type > NodeRef < marker:: ValMut < ' a > , K , V , Type > {
594
579
/// # Safety
595
580
/// - The node has more than `idx` initialized elements.
@@ -650,12 +635,12 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
650
635
651
636
/// Adds a key-value pair to the beginning of the node.
652
637
fn push_front ( & mut self , key : K , val : V ) {
653
- assert ! ( self . len( ) < CAPACITY ) ;
654
-
638
+ let new_len = self . len ( ) + 1 ;
639
+ assert ! ( new_len <= CAPACITY ) ;
655
640
unsafe {
656
- * self . len_mut ( ) += 1 ;
657
- slice_insert ( self . key_area_slice ( ) , 0 , key ) ;
658
- slice_insert ( self . val_area_slice ( ) , 0 , val ) ;
641
+ slice_insert ( self . key_area_mut_at ( ..new_len ) , 0 , key ) ;
642
+ slice_insert ( self . val_area_mut_at ( ..new_len ) , 0 , val ) ;
643
+ * self . len_mut ( ) = new_len as u16 ;
659
644
}
660
645
}
661
646
}
@@ -697,14 +682,15 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
697
682
/// Adds a key-value pair, and an edge to go to the left of that pair,
698
683
/// to the beginning of the node.
699
684
fn push_front ( & mut self , key : K , val : V , edge : Root < K , V > ) {
685
+ let new_len = self . len ( ) + 1 ;
700
686
assert ! ( edge. height == self . height - 1 ) ;
701
- assert ! ( self . len ( ) < CAPACITY ) ;
687
+ assert ! ( new_len <= CAPACITY ) ;
702
688
703
689
unsafe {
704
- * self . len_mut ( ) += 1 ;
705
- slice_insert ( self . key_area_slice ( ) , 0 , key ) ;
706
- slice_insert ( self . val_area_slice ( ) , 0 , val ) ;
707
- slice_insert ( self . edge_area_slice ( ) , 0 , edge . node ) ;
690
+ slice_insert ( self . key_area_mut_at ( ..new_len ) , 0 , key ) ;
691
+ slice_insert ( self . val_area_mut_at ( ..new_len ) , 0 , val ) ;
692
+ slice_insert ( self . edge_area_mut_at ( ..new_len + 1 ) , 0 , edge . node ) ;
693
+ * self . len_mut ( ) = new_len as u16 ;
708
694
}
709
695
710
696
self . correct_all_childrens_parent_links ( ) ;
@@ -749,12 +735,12 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
749
735
let old_len = self . len ( ) ;
750
736
751
737
unsafe {
752
- let key = slice_remove ( self . key_area_slice ( ) , 0 ) ;
753
- let val = slice_remove ( self . val_area_slice ( ) , 0 ) ;
738
+ let key = slice_remove ( self . key_area_mut_at ( ..old_len ) , 0 ) ;
739
+ let val = slice_remove ( self . val_area_mut_at ( ..old_len ) , 0 ) ;
754
740
let edge = match self . reborrow_mut ( ) . force ( ) {
755
741
ForceResult :: Leaf ( _) => None ,
756
742
ForceResult :: Internal ( mut internal) => {
757
- let node = slice_remove ( internal. edge_area_slice ( ) , 0 ) ;
743
+ let node = slice_remove ( internal. edge_area_mut_at ( ..old_len + 1 ) , 0 ) ;
758
744
let mut edge = Root { node, height : internal. height - 1 , _marker : PhantomData } ;
759
745
// Currently, clearing the parent link is superfluous, because we will
760
746
// insert the node elsewhere and set its parent link again.
@@ -975,11 +961,12 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
975
961
/// The returned pointer points to the inserted value.
976
962
fn insert_fit ( & mut self , key : K , val : V ) -> * mut V {
977
963
debug_assert ! ( self . node. len( ) < CAPACITY ) ;
964
+ let new_len = self . node . len ( ) + 1 ;
978
965
979
966
unsafe {
980
- * self . node . len_mut ( ) += 1 ;
981
- slice_insert ( self . node . key_area_slice ( ) , self . idx , key ) ;
982
- slice_insert ( self . node . val_area_slice ( ) , self . idx , val ) ;
967
+ slice_insert ( self . node . key_area_mut_at ( ..new_len ) , self . idx , key ) ;
968
+ slice_insert ( self . node . val_area_mut_at ( ..new_len ) , self . idx , val ) ;
969
+ * self . node . len_mut ( ) = new_len as u16 ;
983
970
984
971
self . node . val_area_mut_at ( self . idx ) . assume_init_mut ( )
985
972
}
@@ -1033,14 +1020,15 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
1033
1020
fn insert_fit ( & mut self , key : K , val : V , edge : Root < K , V > ) {
1034
1021
debug_assert ! ( self . node. len( ) < CAPACITY ) ;
1035
1022
debug_assert ! ( edge. height == self . node. height - 1 ) ;
1023
+ let new_len = self . node . len ( ) + 1 ;
1036
1024
1037
1025
unsafe {
1038
- * self . node . len_mut ( ) += 1 ;
1039
- slice_insert ( self . node . key_area_slice ( ) , self . idx , key ) ;
1040
- slice_insert ( self . node . val_area_slice ( ) , self . idx , val ) ;
1041
- slice_insert ( self . node . edge_area_slice ( ) , self . idx + 1 , edge . node ) ;
1026
+ slice_insert ( self . node . key_area_mut_at ( ..new_len ) , self . idx , key ) ;
1027
+ slice_insert ( self . node . val_area_mut_at ( ..new_len ) , self . idx , val ) ;
1028
+ slice_insert ( self . node . edge_area_mut_at ( ..new_len + 1 ) , self . idx + 1 , edge . node ) ;
1029
+ * self . node . len_mut ( ) = new_len as u16 ;
1042
1030
1043
- self . node . correct_childrens_parent_links ( ( self . idx + 1 ) ..= self . node . len ( ) ) ;
1031
+ self . node . correct_childrens_parent_links ( self . idx + 1 ..new_len + 1 ) ;
1044
1032
}
1045
1033
}
1046
1034
@@ -1177,17 +1165,11 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>
1177
1165
}
1178
1166
1179
1167
impl < ' a , K : ' a , V : ' a , NodeType > Handle < NodeRef < marker:: Mut < ' a > , K , V , NodeType > , marker:: KV > {
1180
- /// Helps implementations of `split` for a particular `NodeType`,
1181
- /// by calculating the length of the new node.
1182
- fn split_new_node_len ( & self ) -> usize {
1183
- debug_assert ! ( self . idx < self . node. len( ) ) ;
1184
- self . node . len ( ) - self . idx - 1
1185
- }
1186
-
1187
1168
/// Helps implementations of `split` for a particular `NodeType`,
1188
1169
/// by taking care of leaf data.
1189
1170
fn split_leaf_data ( & mut self , new_node : & mut LeafNode < K , V > ) -> ( K , V ) {
1190
- let new_len = self . split_new_node_len ( ) ;
1171
+ debug_assert ! ( self . idx < self . node. len( ) ) ;
1172
+ let new_len = self . node . len ( ) - self . idx - 1 ;
1191
1173
new_node. len = new_len as u16 ;
1192
1174
unsafe {
1193
1175
let k = ptr:: read ( self . node . reborrow ( ) . key_at ( self . idx ) ) ;
@@ -1234,10 +1216,11 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
1234
1216
pub fn remove (
1235
1217
mut self ,
1236
1218
) -> ( ( K , V ) , Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > ) {
1219
+ let old_len = self . node . len ( ) ;
1237
1220
unsafe {
1238
- let k = slice_remove ( self . node . key_area_slice ( ) , self . idx ) ;
1239
- let v = slice_remove ( self . node . val_area_slice ( ) , self . idx ) ;
1240
- * self . node . len_mut ( ) -= 1 ;
1221
+ let k = slice_remove ( self . node . key_area_mut_at ( ..old_len ) , self . idx ) ;
1222
+ let v = slice_remove ( self . node . val_area_mut_at ( ..old_len ) , self . idx ) ;
1223
+ * self . node . len_mut ( ) = ( old_len - 1 ) as u16 ;
1241
1224
( ( k, v) , self . left_edge ( ) )
1242
1225
}
1243
1226
}
@@ -1254,14 +1237,13 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
1254
1237
pub fn split ( mut self ) -> SplitResult < ' a , K , V , marker:: Internal > {
1255
1238
unsafe {
1256
1239
let mut new_node = Box :: new ( InternalNode :: new ( ) ) ;
1257
- let new_len = self . split_new_node_len ( ) ;
1258
- // Move edges out before reducing length:
1240
+ let kv = self . split_leaf_data ( & mut new_node . data ) ;
1241
+ let new_len = usize :: from ( new_node . data . len ) ;
1259
1242
ptr:: copy_nonoverlapping (
1260
1243
self . node . reborrow ( ) . edge_area ( ) . as_ptr ( ) . add ( self . idx + 1 ) ,
1261
1244
new_node. edges . as_mut_ptr ( ) ,
1262
1245
new_len + 1 ,
1263
1246
) ;
1264
- let kv = self . split_leaf_data ( & mut new_node. data ) ;
1265
1247
1266
1248
let height = self . node . height ;
1267
1249
let mut right = NodeRef :: from_new_internal ( new_node, height) ;
@@ -1384,23 +1366,25 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
1384
1366
unsafe {
1385
1367
* left_node. len_mut ( ) = new_left_len as u16 ;
1386
1368
1387
- let parent_key = slice_remove ( parent_node. key_area_slice ( ) , parent_idx) ;
1369
+ let parent_key =
1370
+ slice_remove ( parent_node. key_area_mut_at ( ..old_parent_len) , parent_idx) ;
1388
1371
left_node. key_area_mut_at ( old_left_len) . write ( parent_key) ;
1389
1372
ptr:: copy_nonoverlapping (
1390
1373
right_node. reborrow ( ) . key_area ( ) . as_ptr ( ) ,
1391
- left_node. key_area_slice ( ) . as_mut_ptr ( ) . add ( old_left_len + 1 ) ,
1374
+ left_node. key_area_mut_at ( old_left_len + 1 .. ) . as_mut_ptr ( ) ,
1392
1375
right_len,
1393
1376
) ;
1394
1377
1395
- let parent_val = slice_remove ( parent_node. val_area_slice ( ) , parent_idx) ;
1378
+ let parent_val =
1379
+ slice_remove ( parent_node. val_area_mut_at ( ..old_parent_len) , parent_idx) ;
1396
1380
left_node. val_area_mut_at ( old_left_len) . write ( parent_val) ;
1397
1381
ptr:: copy_nonoverlapping (
1398
1382
right_node. reborrow ( ) . val_area ( ) . as_ptr ( ) ,
1399
- left_node. val_area_slice ( ) . as_mut_ptr ( ) . add ( old_left_len + 1 ) ,
1383
+ left_node. val_area_mut_at ( old_left_len + 1 .. ) . as_mut_ptr ( ) ,
1400
1384
right_len,
1401
1385
) ;
1402
1386
1403
- slice_remove ( & mut parent_node. edge_area_slice ( ) , parent_idx + 1 ) ;
1387
+ slice_remove ( & mut parent_node. edge_area_mut_at ( ..old_parent_len + 1 ) , parent_idx + 1 ) ;
1404
1388
parent_node. correct_childrens_parent_links ( parent_idx + 1 ..old_parent_len) ;
1405
1389
* parent_node. len_mut ( ) -= 1 ;
1406
1390
@@ -1411,7 +1395,7 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
1411
1395
let right_node = right_node. cast_to_internal_unchecked ( ) ;
1412
1396
ptr:: copy_nonoverlapping (
1413
1397
right_node. reborrow ( ) . edge_area ( ) . as_ptr ( ) ,
1414
- left_node. edge_area_slice ( ) . as_mut_ptr ( ) . add ( old_left_len + 1 ) ,
1398
+ left_node. edge_area_mut_at ( old_left_len + 1 .. ) . as_mut_ptr ( ) ,
1415
1399
right_len + 1 ,
1416
1400
) ;
1417
1401
@@ -1489,6 +1473,9 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
1489
1473
assert ! ( old_left_len >= count) ;
1490
1474
1491
1475
let new_left_len = old_left_len - count;
1476
+ let new_right_len = old_right_len + count;
1477
+ * left_node. len_mut ( ) = new_left_len as u16 ;
1478
+ * right_node. len_mut ( ) = new_right_len as u16 ;
1492
1479
1493
1480
// Move leaf data.
1494
1481
{
@@ -1513,16 +1500,13 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
1513
1500
move_kv ( left_kv, new_left_len, parent_kv, 0 , 1 ) ;
1514
1501
}
1515
1502
1516
- * left_node. len_mut ( ) -= count as u16 ;
1517
- * right_node. len_mut ( ) += count as u16 ;
1518
-
1519
1503
match ( left_node. reborrow_mut ( ) . force ( ) , right_node. reborrow_mut ( ) . force ( ) ) {
1520
1504
( ForceResult :: Internal ( left) , ForceResult :: Internal ( mut right) ) => {
1521
1505
// Make room for stolen edges.
1522
1506
let left = left. reborrow ( ) ;
1523
- let right_edges = right. edge_area_slice ( ) . as_mut_ptr ( ) ;
1507
+ let right_edges = right. edge_area_mut_at ( .. ) . as_mut_ptr ( ) ;
1524
1508
ptr:: copy ( right_edges, right_edges. add ( count) , old_right_len + 1 ) ;
1525
- right. correct_childrens_parent_links ( count..count + old_right_len + 1 ) ;
1509
+ right. correct_childrens_parent_links ( count..new_right_len + 1 ) ;
1526
1510
1527
1511
// Steal edges.
1528
1512
move_edges ( left, new_left_len + 1 , right, 0 , count) ;
@@ -1546,7 +1530,10 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
1546
1530
assert ! ( old_left_len + count <= CAPACITY ) ;
1547
1531
assert ! ( old_right_len >= count) ;
1548
1532
1533
+ let new_left_len = old_left_len + count;
1549
1534
let new_right_len = old_right_len - count;
1535
+ * left_node. len_mut ( ) = new_left_len as u16 ;
1536
+ * right_node. len_mut ( ) = new_right_len as u16 ;
1550
1537
1551
1538
// Move leaf data.
1552
1539
{
@@ -1571,16 +1558,13 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
1571
1558
ptr:: copy ( right_kv. 1 . add ( count) , right_kv. 1 , new_right_len) ;
1572
1559
}
1573
1560
1574
- * left_node. len_mut ( ) += count as u16 ;
1575
- * right_node. len_mut ( ) -= count as u16 ;
1576
-
1577
1561
match ( left_node. reborrow_mut ( ) . force ( ) , right_node. reborrow_mut ( ) . force ( ) ) {
1578
1562
( ForceResult :: Internal ( left) , ForceResult :: Internal ( mut right) ) => {
1579
1563
// Steal edges.
1580
1564
move_edges ( right. reborrow ( ) , 0 , left, old_left_len + 1 , count) ;
1581
1565
1582
1566
// Fill gap where stolen edges used to be.
1583
- let right_edges = right. edge_area_slice ( ) . as_mut_ptr ( ) ;
1567
+ let right_edges = right. edge_area_mut_at ( .. ) . as_mut_ptr ( ) ;
1584
1568
ptr:: copy ( right_edges. add ( count) , right_edges, new_right_len + 1 ) ;
1585
1569
right. correct_childrens_parent_links ( 0 ..=new_right_len) ;
1586
1570
}
@@ -1614,8 +1598,8 @@ unsafe fn move_edges<'a, K: 'a, V: 'a>(
1614
1598
) {
1615
1599
unsafe {
1616
1600
let source_ptr = source. edge_area ( ) . as_ptr ( ) ;
1617
- let dest_ptr = dest. edge_area_slice ( ) . as_mut_ptr ( ) ;
1618
- ptr:: copy_nonoverlapping ( source_ptr. add ( source_offset) , dest_ptr. add ( dest_offset ) , count) ;
1601
+ let dest_ptr = dest. edge_area_mut_at ( dest_offset.. ) . as_mut_ptr ( ) ;
1602
+ ptr:: copy_nonoverlapping ( source_ptr. add ( source_offset) , dest_ptr, count) ;
1619
1603
dest. correct_childrens_parent_links ( dest_offset..dest_offset + count) ;
1620
1604
}
1621
1605
}
0 commit comments