@@ -597,18 +597,17 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::Leaf> {
597
597
598
598
impl < ' a , K , V > NodeRef < marker:: Mut < ' a > , K , V , marker:: Internal > {
599
599
/// # Safety
600
- /// 'first' and 'after_last' must be in range.
601
- unsafe fn correct_childrens_parent_links ( & mut self , first : usize , after_last : usize ) {
602
- debug_assert ! ( first <= self . len( ) ) ;
603
- debug_assert ! ( after_last <= self . len( ) + 1 ) ;
604
- for i in first..after_last {
600
+ /// Every item returned by `range` is a valid edge index for the node.
601
+ unsafe fn correct_childrens_parent_links < R : Iterator < Item = usize > > ( & mut self , range : R ) {
602
+ for i in range {
603
+ debug_assert ! ( i <= self . len( ) ) ;
605
604
unsafe { Handle :: new_edge ( self . reborrow_mut ( ) , i) } . correct_parent_link ( ) ;
606
605
}
607
606
}
608
607
609
608
fn correct_all_childrens_parent_links ( & mut self ) {
610
609
let len = self . len ( ) ;
611
- unsafe { self . correct_childrens_parent_links ( 0 , len + 1 ) } ;
610
+ unsafe { self . correct_childrens_parent_links ( 0 ..= len) } ;
612
611
}
613
612
}
614
613
@@ -708,9 +707,7 @@ impl<'a, K: 'a, V: 'a> NodeRef<marker::Mut<'a>, K, V, marker::LeafOrInternal> {
708
707
let mut new_root = Root { node : edge, height : internal. height - 1 } ;
709
708
new_root. node_as_mut ( ) . as_leaf_mut ( ) . parent = None ;
710
709
711
- for i in 0 ..old_len {
712
- Handle :: new_edge ( internal. reborrow_mut ( ) , i) . correct_parent_link ( ) ;
713
- }
710
+ internal. correct_childrens_parent_links ( 0 ..old_len) ;
714
711
715
712
Some ( new_root)
716
713
}
@@ -986,9 +983,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
986
983
edge. node ,
987
984
) ;
988
985
989
- for i in ( self . idx + 1 ) ..( self . node . len ( ) + 1 ) {
990
- Handle :: new_edge ( self . node . reborrow_mut ( ) , i) . correct_parent_link ( ) ;
991
- }
986
+ self . node . correct_childrens_parent_links ( ( self . idx + 1 ) ..=self . node . len ( ) ) ;
992
987
}
993
988
}
994
989
@@ -1215,9 +1210,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
1215
1210
1216
1211
let mut new_root = Root { node : BoxedNode :: from_internal ( new_node) , height } ;
1217
1212
1218
- for i in 0 ..( new_len + 1 ) {
1219
- Handle :: new_edge ( new_root. node_as_mut ( ) . cast_unchecked ( ) , i) . correct_parent_link ( ) ;
1220
- }
1213
+ new_root. node_as_mut ( ) . cast_unchecked ( ) . correct_childrens_parent_links ( 0 ..=new_len) ;
1221
1214
1222
1215
( self . node , k, v, new_root)
1223
1216
}
@@ -1261,27 +1254,24 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
1261
1254
) ;
1262
1255
1263
1256
slice_remove ( & mut self . node . as_internal_mut ( ) . edges , self . idx + 1 ) ;
1264
- for i in self . idx + 1 ..self . node . len ( ) {
1265
- Handle :: new_edge ( self . node . reborrow_mut ( ) , i) . correct_parent_link ( ) ;
1266
- }
1257
+ let self_len = self . node . len ( ) ;
1258
+ self . node . correct_childrens_parent_links ( self . idx + 1 ..self_len) ;
1267
1259
self . node . as_leaf_mut ( ) . len -= 1 ;
1268
1260
1269
1261
left_node. as_leaf_mut ( ) . len += right_len as u16 + 1 ;
1270
1262
1271
1263
if self . node . height > 1 {
1272
1264
// SAFETY: the height of the nodes being merged is one below the height
1273
1265
// of the node of this edge, thus above zero, so they are internal.
1274
- let mut left_node = left_node. cast_unchecked ( ) ;
1275
- let mut right_node = right_node. cast_unchecked ( ) ;
1266
+ let mut left_node = left_node. cast_unchecked :: < marker :: Internal > ( ) ;
1267
+ let mut right_node = right_node. cast_unchecked :: < marker :: Internal > ( ) ;
1276
1268
ptr:: copy_nonoverlapping (
1277
1269
right_node. as_internal ( ) . edges . as_ptr ( ) ,
1278
1270
left_node. as_internal_mut ( ) . edges . as_mut_ptr ( ) . add ( left_len + 1 ) ,
1279
1271
right_len + 1 ,
1280
1272
) ;
1281
1273
1282
- for i in left_len + 1 ..left_len + right_len + 2 {
1283
- Handle :: new_edge ( left_node. reborrow_mut ( ) , i) . correct_parent_link ( ) ;
1284
- }
1274
+ left_node. correct_childrens_parent_links ( left_len + 1 ..=left_len + 1 + right_len) ;
1285
1275
1286
1276
Global . dealloc ( right_node. node . cast ( ) , Layout :: new :: < InternalNode < K , V > > ( ) ) ;
1287
1277
} else {
@@ -1371,7 +1361,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
1371
1361
// Make room for stolen edges.
1372
1362
let right_edges = right. reborrow_mut ( ) . as_internal_mut ( ) . edges . as_mut_ptr ( ) ;
1373
1363
ptr:: copy ( right_edges, right_edges. add ( count) , right_len + 1 ) ;
1374
- right. correct_childrens_parent_links ( count, count + right_len + 1 ) ;
1364
+ right. correct_childrens_parent_links ( count.. count + right_len + 1 ) ;
1375
1365
1376
1366
move_edges ( left, new_left_len + 1 , right, 0 , count) ;
1377
1367
}
@@ -1430,7 +1420,7 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
1430
1420
// Fix right indexing.
1431
1421
let right_edges = right. reborrow_mut ( ) . as_internal_mut ( ) . edges . as_mut_ptr ( ) ;
1432
1422
ptr:: copy ( right_edges. add ( count) , right_edges, new_right_len + 1 ) ;
1433
- right. correct_childrens_parent_links ( 0 , new_right_len + 1 ) ;
1423
+ right. correct_childrens_parent_links ( 0 ..= new_right_len) ;
1434
1424
}
1435
1425
( ForceResult :: Leaf ( _) , ForceResult :: Leaf ( _) ) => { }
1436
1426
_ => {
@@ -1466,7 +1456,7 @@ unsafe fn move_edges<K, V>(
1466
1456
let dest_ptr = dest. as_internal_mut ( ) . edges . as_mut_ptr ( ) ;
1467
1457
unsafe {
1468
1458
ptr:: copy_nonoverlapping ( source_ptr. add ( source_offset) , dest_ptr. add ( dest_offset) , count) ;
1469
- dest. correct_childrens_parent_links ( dest_offset, dest_offset + count) ;
1459
+ dest. correct_childrens_parent_links ( dest_offset.. dest_offset + count) ;
1470
1460
}
1471
1461
}
1472
1462
0 commit comments