@@ -611,7 +611,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
611
611
612
612
/// Adds a key/value pair to the beginning of the node.
613
613
fn push_front ( & mut self , key : K , val : V ) {
614
- debug_assert ! ( self . len( ) < CAPACITY ) ;
614
+ assert ! ( self . len( ) < CAPACITY ) ;
615
615
616
616
unsafe {
617
617
slice_insert ( self . keys_mut ( ) , 0 , key) ;
@@ -664,14 +664,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Internal> {
664
664
unsafe {
665
665
slice_insert ( self . keys_mut ( ) , 0 , key) ;
666
666
slice_insert ( self . vals_mut ( ) , 0 , val) ;
667
- slice_insert (
668
- slice:: from_raw_parts_mut (
669
- MaybeUninit :: slice_as_mut_ptr ( & mut self . as_internal_mut ( ) . edges ) ,
670
- self . len ( ) + 1 ,
671
- ) ,
672
- 0 ,
673
- edge. node ,
674
- ) ;
667
+ slice_insert ( self . edges_mut ( ) , 0 , edge. node ) ;
675
668
}
676
669
677
670
self . as_leaf_mut ( ) . len += 1 ;
@@ -921,33 +914,22 @@ fn splitpoint(edge_idx: usize) -> (usize, InsertionPlace) {
921
914
}
922
915
}
923
916
924
- impl < ' a , K : ' a , V : ' a , NodeType > Handle < NodeRef < marker:: Mut < ' a > , K , V , NodeType > , marker:: Edge > {
925
- /// Helps implementations of `insert_fit` for a particular `NodeType`,
926
- /// by taking care of leaf data.
917
+ impl < ' a , K : ' a , V : ' a > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > {
927
918
/// Inserts a new key/value pair between the key/value pairs to the right and left of
928
919
/// this edge. This method assumes that there is enough space in the node for the new
929
920
/// pair to fit.
930
- fn leafy_insert_fit ( & mut self , key : K , val : V ) {
921
+ ///
922
+ /// The returned pointer points to the inserted value.
923
+ fn insert_fit ( & mut self , key : K , val : V ) -> * mut V {
931
924
debug_assert ! ( self . node. len( ) < CAPACITY ) ;
932
925
933
926
unsafe {
934
927
slice_insert ( self . node . keys_mut ( ) , self . idx , key) ;
935
928
slice_insert ( self . node . vals_mut ( ) , self . idx , val) ;
936
-
937
929
self . node . as_leaf_mut ( ) . len += 1 ;
938
- }
939
- }
940
- }
941
930
942
- impl < ' a , K : ' a , V : ' a > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > {
943
- /// Inserts a new key/value pair between the key/value pairs to the right and left of
944
- /// this edge. This method assumes that there is enough space in the node for the new
945
- /// pair to fit.
946
- ///
947
- /// The returned pointer points to the inserted value.
948
- fn insert_fit ( & mut self , key : K , val : V ) -> * mut V {
949
- self . leafy_insert_fit ( key, val) ;
950
- unsafe { self . node . val_mut_at ( self . idx ) }
931
+ self . node . val_mut_at ( self . idx )
932
+ }
951
933
}
952
934
}
953
935
@@ -996,11 +978,14 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
996
978
/// between this edge and the key/value pair to the right of this edge. This method assumes
997
979
/// that there is enough space in the node for the new pair to fit.
998
980
fn insert_fit ( & mut self , key : K , val : V , edge : Root < K , V > ) {
981
+ debug_assert ! ( self . node. len( ) < CAPACITY ) ;
999
982
debug_assert ! ( edge. height == self . node. height - 1 ) ;
1000
983
1001
984
unsafe {
985
+ slice_insert ( self . node . keys_mut ( ) , self . idx , key) ;
986
+ slice_insert ( self . node . vals_mut ( ) , self . idx , val) ;
1002
987
slice_insert ( self . node . edges_mut ( ) , self . idx + 1 , edge. node ) ;
1003
- self . leafy_insert_fit ( key , val ) ;
988
+ self . node . as_leaf_mut ( ) . len += 1 ;
1004
989
1005
990
self . node . correct_childrens_parent_links ( ( self . idx + 1 ) ..=self . node . len ( ) ) ;
1006
991
}
@@ -1131,15 +1116,21 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>
1131
1116
}
1132
1117
1133
1118
impl < ' a , K : ' a , V : ' a , NodeType > Handle < NodeRef < marker:: Mut < ' a > , K , V , NodeType > , marker:: KV > {
1119
+ /// Helps implementations of `split` for a particular `NodeType`,
1120
+ /// by calculating the length of the new node.
1121
+ fn split_new_node_len ( & self ) -> usize {
1122
+ debug_assert ! ( self . idx < self . node. len( ) ) ;
1123
+ self . node . len ( ) - self . idx - 1
1124
+ }
1125
+
1134
1126
/// Helps implementations of `split` for a particular `NodeType`,
1135
1127
/// by taking care of leaf data.
1136
- fn leafy_split ( & mut self , new_node : & mut LeafNode < K , V > ) -> ( K , V , usize ) {
1128
+ fn split_leaf_data ( & mut self , new_node : & mut LeafNode < K , V > ) -> ( K , V ) {
1129
+ let new_len = self . split_new_node_len ( ) ;
1137
1130
unsafe {
1138
1131
let k = ptr:: read ( self . node . key_at ( self . idx ) ) ;
1139
1132
let v = ptr:: read ( self . node . val_at ( self . idx ) ) ;
1140
1133
1141
- let new_len = self . node . len ( ) - self . idx - 1 ;
1142
-
1143
1134
ptr:: copy_nonoverlapping (
1144
1135
self . node . key_at ( self . idx + 1 ) ,
1145
1136
MaybeUninit :: slice_as_mut_ptr ( & mut new_node. keys ) ,
@@ -1153,15 +1144,15 @@ impl<'a, K: 'a, V: 'a, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>
1153
1144
1154
1145
self . node . as_leaf_mut ( ) . len = self . idx as u16 ;
1155
1146
new_node. len = new_len as u16 ;
1156
- ( k, v, new_len )
1147
+ ( k, v)
1157
1148
}
1158
1149
}
1159
1150
}
1160
1151
1161
1152
impl < ' a , K : ' a , V : ' a > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: KV > {
1162
1153
/// Splits the underlying node into three parts:
1163
1154
///
1164
- /// - The node is truncated to only contain the key/value pairs to the right of
1155
+ /// - The node is truncated to only contain the key/value pairs to the left of
1165
1156
/// this handle.
1166
1157
/// - The key and value pointed to by this handle are extracted.
1167
1158
/// - All the key/value pairs to the right of this handle are put into a newly
@@ -1170,9 +1161,10 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
1170
1161
unsafe {
1171
1162
let mut new_node = Box :: new ( LeafNode :: new ( ) ) ;
1172
1163
1173
- let ( k, v, _ ) = self . leafy_split ( & mut new_node) ;
1164
+ let ( k, v) = self . split_leaf_data ( & mut new_node) ;
1174
1165
1175
- ( self . node , k, v, Root { node : BoxedNode :: from_leaf ( new_node) , height : 0 } )
1166
+ let right = Root { node : BoxedNode :: from_leaf ( new_node) , height : 0 } ;
1167
+ ( self . node , k, v, right)
1176
1168
}
1177
1169
}
1178
1170
@@ -1206,29 +1198,28 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
1206
1198
/// Splits the underlying node into three parts:
1207
1199
///
1208
1200
/// - The node is truncated to only contain the edges and key/value pairs to the
1209
- /// right of this handle.
1201
+ /// left of this handle.
1210
1202
/// - The key and value pointed to by this handle are extracted.
1211
1203
/// - All the edges and key/value pairs to the right of this handle are put into
1212
1204
/// a newly allocated node.
1213
1205
pub fn split ( mut self ) -> ( NodeRef < marker:: Mut < ' a > , K , V , marker:: Internal > , K , V , Root < K , V > ) {
1214
1206
unsafe {
1215
1207
let mut new_node = Box :: new ( InternalNode :: new ( ) ) ;
1216
-
1217
- let ( k, v, new_len) = self . leafy_split ( & mut new_node. data ) ;
1218
- let height = self . node . height ;
1219
- let old_node = & * self . node . as_internal_ptr ( ) ;
1220
-
1208
+ // Move edges out before reducing length:
1209
+ let new_len = self . split_new_node_len ( ) ;
1221
1210
ptr:: copy_nonoverlapping (
1222
- old_node . edges . as_ptr ( ) . add ( self . idx + 1 ) ,
1223
- new_node. edges . as_mut_ptr ( ) ,
1211
+ self . node . edge_at ( self . idx + 1 ) ,
1212
+ MaybeUninit :: slice_as_mut_ptr ( & mut new_node. edges ) ,
1224
1213
new_len + 1 ,
1225
1214
) ;
1215
+ let ( k, v) = self . split_leaf_data ( & mut new_node. data ) ;
1226
1216
1227
- let mut new_root = Root { node : BoxedNode :: from_internal ( new_node) , height } ;
1217
+ let height = self . node . height ;
1218
+ let mut right = Root { node : BoxedNode :: from_internal ( new_node) , height } ;
1228
1219
1229
- new_root . internal_node_as_mut ( ) . correct_childrens_parent_links ( 0 ..=new_len) ;
1220
+ right . internal_node_as_mut ( ) . correct_childrens_parent_links ( 0 ..=new_len) ;
1230
1221
1231
- ( self . node , k, v, new_root )
1222
+ ( self . node , k, v, right )
1232
1223
}
1233
1224
}
1234
1225
0 commit comments