@@ -47,8 +47,7 @@ const KV_IDX_CENTER: usize = B - 1;
47
47
const EDGE_IDX_LEFT_OF_CENTER : usize = B - 1 ;
48
48
const EDGE_IDX_RIGHT_OF_CENTER : usize = B ;
49
49
50
- /// The underlying representation of leaf nodes.
51
- #[ repr( C ) ]
50
+ /// The underlying representation of leaf nodes and part of the representation of internal nodes.
52
51
struct LeafNode < K , V > {
53
52
/// We want to be covariant in `K` and `V`.
54
53
parent : Option < NonNull < InternalNode < K , V > > > ,
@@ -59,9 +58,6 @@ struct LeafNode<K, V> {
59
58
parent_idx : MaybeUninit < u16 > ,
60
59
61
60
/// The number of keys and values this node stores.
62
- ///
63
- /// This next to `parent_idx` to encourage the compiler to join `len` and
64
- /// `parent_idx` into the same 32-bit word, reducing space overhead.
65
61
len : u16 ,
66
62
67
63
/// The arrays storing the actual data of the node. Only the first `len` elements of each
@@ -92,7 +88,9 @@ impl<K, V> LeafNode<K, V> {
92
88
/// node, allowing code to act on leaf and internal nodes generically without having to even check
93
89
/// which of the two a pointer is pointing at. This property is enabled by the use of `repr(C)`.
94
90
#[ repr( C ) ]
91
+ // gdb_providers.py uses this type name for introspection.
95
92
struct InternalNode < K , V > {
93
+ // gdb_providers.py uses this field name for introspection.
96
94
data : LeafNode < K , V > ,
97
95
98
96
/// The pointers to the children of this node. `len + 1` of these are considered
@@ -183,9 +181,9 @@ impl<K, V> Root<K, V> {
183
181
NodeRef { height : self . height , node : self . node . as_ptr ( ) , _marker : PhantomData }
184
182
}
185
183
186
- /// Adds a new internal node with a single edge, pointing to the previous root, and make that
187
- /// new node the root. This increases the height by 1 and is the opposite of
188
- /// `pop_internal_level`.
184
+ /// Adds a new internal node with a single edge pointing to the previous root node,
185
+ /// make that new node the root node, and return it . This increases the height by 1
186
+ /// and is the opposite of `pop_internal_level`.
189
187
pub fn push_internal_level ( & mut self ) -> NodeRef < marker:: Mut < ' _ > , K , V , marker:: Internal > {
190
188
let mut new_node = Box :: new ( unsafe { InternalNode :: new ( ) } ) ;
191
189
new_node. edges [ 0 ] . write ( unsafe { BoxedNode :: from_ptr ( self . node . as_ptr ( ) ) } ) ;
@@ -322,7 +320,7 @@ impl<BorrowType, K, V, Type> NodeRef<BorrowType, K, V, Type> {
322
320
NodeRef { height : self . height , node : self . node , _marker : PhantomData }
323
321
}
324
322
325
- /// Exposes the leaf " portion" of any leaf or internal node.
323
+ /// Exposes the leaf portion of any leaf or internal node.
326
324
/// If the node is a leaf, this function simply opens up its data.
327
325
/// If the node is an internal node, so not a leaf, it does have all the data a leaf has
328
326
/// (header, keys and values), and this function exposes that.
@@ -472,7 +470,7 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
472
470
NodeRef { height : self . height , node : self . node , _marker : PhantomData }
473
471
}
474
472
475
- /// Exposes the leaf " portion" of any leaf or internal node for writing.
473
+ /// Exposes the leaf portion of any leaf or internal node for writing.
476
474
/// If the node is a leaf, this function simply opens up its data.
477
475
/// If the node is an internal node, so not a leaf, it does have all the data a leaf has
478
476
/// (header, keys and values), and this function exposes that.
@@ -498,18 +496,38 @@ impl<'a, K, V, Type> NodeRef<marker::Mut<'a>, K, V, Type> {
498
496
unsafe { self . reborrow_mut ( ) . into_val_mut_at ( idx) }
499
497
}
500
498
501
- fn keys_mut ( & mut self ) -> & mut [ K ] {
499
+ fn keys_mut ( & mut self ) -> & mut [ K ]
500
+ where
501
+ K : ' a ,
502
+ V : ' a ,
503
+ {
502
504
// SAFETY: the caller will not be able to call further methods on self
503
505
// until the key slice reference is dropped, as we have unique access
504
506
// for the lifetime of the borrow.
505
- unsafe { self . reborrow_mut ( ) . into_key_slice_mut ( ) }
507
+ // SAFETY: The keys of a node must always be initialized up to length.
508
+ unsafe {
509
+ slice:: from_raw_parts_mut (
510
+ MaybeUninit :: slice_as_mut_ptr ( & mut self . as_leaf_mut ( ) . keys ) ,
511
+ self . len ( ) ,
512
+ )
513
+ }
506
514
}
507
515
508
- fn vals_mut ( & mut self ) -> & mut [ V ] {
516
+ fn vals_mut ( & mut self ) -> & mut [ V ]
517
+ where
518
+ K : ' a ,
519
+ V : ' a ,
520
+ {
509
521
// SAFETY: the caller will not be able to call further methods on self
510
522
// until the value slice reference is dropped, as we have unique access
511
523
// for the lifetime of the borrow.
512
- unsafe { self . reborrow_mut ( ) . into_val_slice_mut ( ) }
524
+ // SAFETY: The values of a node must always be initialized up to length.
525
+ unsafe {
526
+ slice:: from_raw_parts_mut (
527
+ MaybeUninit :: slice_as_mut_ptr ( & mut self . as_leaf_mut ( ) . vals ) ,
528
+ self . len ( ) ,
529
+ )
530
+ }
513
531
}
514
532
}
515
533
@@ -539,26 +557,6 @@ impl<'a, K: 'a, V: 'a, Type> NodeRef<marker::Immut<'a>, K, V, Type> {
539
557
}
540
558
541
559
impl < ' a , K : ' a , V : ' a , Type > NodeRef < marker:: Mut < ' a > , K , V , Type > {
542
- fn into_key_slice_mut ( mut self ) -> & ' a mut [ K ] {
543
- // SAFETY: The keys of a node must always be initialized up to length.
544
- unsafe {
545
- slice:: from_raw_parts_mut (
546
- MaybeUninit :: slice_as_mut_ptr ( & mut self . as_leaf_mut ( ) . keys ) ,
547
- self . len ( ) ,
548
- )
549
- }
550
- }
551
-
552
- fn into_val_slice_mut ( mut self ) -> & ' a mut [ V ] {
553
- // SAFETY: The values of a node must always be initialized up to length.
554
- unsafe {
555
- slice:: from_raw_parts_mut (
556
- MaybeUninit :: slice_as_mut_ptr ( & mut self . as_leaf_mut ( ) . vals ) ,
557
- self . len ( ) ,
558
- )
559
- }
560
- }
561
-
562
560
/// # Safety
563
561
/// The node has more than `idx` initialized elements.
564
562
unsafe fn into_key_mut_at ( mut self , idx : usize ) -> & ' a mut K {
@@ -610,8 +608,8 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
610
608
}
611
609
612
610
/// Adds a key/value pair to the beginning of the node.
613
- pub fn push_front ( & mut self , key : K , val : V ) {
614
- assert ! ( self . len( ) < CAPACITY ) ;
611
+ fn push_front ( & mut self , key : K , val : V ) {
612
+ debug_assert ! ( self . len( ) < CAPACITY ) ;
615
613
616
614
unsafe {
617
615
slice_insert ( self . keys_mut ( ) , 0 , key) ;
@@ -683,10 +681,9 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
683
681
impl < ' a , K : ' a , V : ' a > NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > {
684
682
/// Removes a key/value pair from the end of this node and returns the pair.
685
683
/// If this is an internal node, also removes the edge that was to the right
686
- /// of that pair and returns the orphaned node that this edge owned with its
687
- /// parent erased.
688
- pub fn pop ( & mut self ) -> ( K , V , Option < Root < K , V > > ) {
689
- assert ! ( self . len( ) > 0 ) ;
684
+ /// of that pair and returns the orphaned node that this edge owned.
685
+ fn pop ( & mut self ) -> ( K , V , Option < Root < K , V > > ) {
686
+ debug_assert ! ( self . len( ) > 0 ) ;
690
687
691
688
let idx = self . len ( ) - 1 ;
692
689
@@ -708,10 +705,11 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
708
705
}
709
706
}
710
707
711
- /// Removes a key/value pair from the beginning of this node. If this is an internal node,
712
- /// also removes the edge that was to the left of that pair.
713
- pub fn pop_front ( & mut self ) -> ( K , V , Option < Root < K , V > > ) {
714
- assert ! ( self . len( ) > 0 ) ;
708
+ /// Removes a key/value pair from the beginning of this node and returns the pair.
709
+ /// If this is an internal node, also removes the edge that was to the left
710
+ /// of that pair and returns the orphaned node that this edge owned.
711
+ fn pop_front ( & mut self ) -> ( K , V , Option < Root < K , V > > ) {
712
+ debug_assert ! ( self . len( ) > 0 ) ;
715
713
716
714
let old_len = self . len ( ) ;
717
715
@@ -913,7 +911,6 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>
913
911
/// this edge. This method assumes that there is enough space in the node for the new
914
912
/// pair to fit.
915
913
fn leafy_insert_fit ( & mut self , key : K , val : V ) {
916
- // Necessary for correctness, but in a private module
917
914
debug_assert ! ( self . node. len( ) < CAPACITY ) ;
918
915
919
916
unsafe {
@@ -951,18 +948,18 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
951
948
let ( middle_kv_idx, insertion) = splitpoint ( self . idx ) ;
952
949
let middle = unsafe { Handle :: new_kv ( self . node , middle_kv_idx) } ;
953
950
let ( mut left, k, v, mut right) = middle. split ( ) ;
954
- let val_ptr = match insertion {
951
+ let mut insertion_edge = match insertion {
955
952
InsertionPlace :: Left ( insert_idx) => unsafe {
956
- Handle :: new_edge ( left. reborrow_mut ( ) , insert_idx) . insert_fit ( key , val )
953
+ Handle :: new_edge ( left. reborrow_mut ( ) , insert_idx)
957
954
} ,
958
955
InsertionPlace :: Right ( insert_idx) => unsafe {
959
956
Handle :: new_edge (
960
957
right. node_as_mut ( ) . cast_unchecked :: < marker:: Leaf > ( ) ,
961
958
insert_idx,
962
959
)
963
- . insert_fit ( key, val)
964
960
} ,
965
961
} ;
962
+ let val_ptr = insertion_edge. insert_fit ( key, val) ;
966
963
( InsertResult :: Split ( SplitResult { left : left. forget_type ( ) , k, v, right } ) , val_ptr)
967
964
}
968
965
}
@@ -985,8 +982,6 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
985
982
/// between this edge and the key/value pair to the right of this edge. This method assumes
986
983
/// that there is enough space in the node for the new pair to fit.
987
984
fn insert_fit ( & mut self , key : K , val : V , edge : Root < K , V > ) {
988
- // Necessary for correctness, but in an internal module
989
- debug_assert ! ( self . node. len( ) < CAPACITY ) ;
990
985
debug_assert ! ( edge. height == self . node. height - 1 ) ;
991
986
992
987
unsafe {
@@ -1136,12 +1131,12 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>
1136
1131
1137
1132
ptr:: copy_nonoverlapping (
1138
1133
self . node . key_at ( self . idx + 1 ) ,
1139
- new_node. keys . as_mut_ptr ( ) as * mut K ,
1134
+ MaybeUninit :: slice_as_mut_ptr ( & mut new_node. keys ) ,
1140
1135
new_len,
1141
1136
) ;
1142
1137
ptr:: copy_nonoverlapping (
1143
1138
self . node . val_at ( self . idx + 1 ) ,
1144
- new_node. vals . as_mut_ptr ( ) as * mut V ,
1139
+ MaybeUninit :: slice_as_mut_ptr ( & mut new_node. vals ) ,
1145
1140
new_len,
1146
1141
) ;
1147
1142
@@ -1376,9 +1371,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
1376
1371
move_edges ( left, new_left_len + 1 , right, 0 , count) ;
1377
1372
}
1378
1373
( ForceResult :: Leaf ( _) , ForceResult :: Leaf ( _) ) => { }
1379
- _ => {
1380
- unreachable ! ( ) ;
1381
- }
1374
+ _ => unreachable ! ( ) ,
1382
1375
}
1383
1376
}
1384
1377
}
@@ -1433,9 +1426,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
1433
1426
right. correct_childrens_parent_links ( 0 ..=new_right_len) ;
1434
1427
}
1435
1428
( ForceResult :: Leaf ( _) , ForceResult :: Leaf ( _) ) => { }
1436
- _ => {
1437
- unreachable ! ( ) ;
1438
- }
1429
+ _ => unreachable ! ( ) ,
1439
1430
}
1440
1431
}
1441
1432
}
@@ -1568,9 +1559,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal>, ma
1568
1559
move_edges ( left, left_new_len + 1 , right, 1 , right_new_len) ;
1569
1560
}
1570
1561
( ForceResult :: Leaf ( _) , ForceResult :: Leaf ( _) ) => { }
1571
- _ => {
1572
- unreachable ! ( ) ;
1573
- }
1562
+ _ => unreachable ! ( ) ,
1574
1563
}
1575
1564
}
1576
1565
}
0 commit comments