@@ -1352,66 +1352,65 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
1352
1352
///
1353
1353
/// Panics unless we `.can_merge()`.
1354
1354
pub fn merge (
1355
- mut self ,
1355
+ self ,
1356
1356
track_edge_idx : Option < LeftOrRight < usize > > ,
1357
1357
) -> Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > , marker:: Edge > {
1358
+ let Handle { node : mut parent_node, idx : parent_idx, _marker } = self . parent ;
1359
+ let old_parent_len = parent_node. len ( ) ;
1358
1360
let mut left_node = self . left_child ;
1359
- let left_len = left_node. len ( ) ;
1361
+ let old_left_len = left_node. len ( ) ;
1360
1362
let right_node = self . right_child ;
1361
1363
let right_len = right_node. len ( ) ;
1364
+ let new_left_len = old_left_len + 1 + right_len;
1362
1365
1363
- assert ! ( left_len + right_len < CAPACITY ) ;
1366
+ assert ! ( new_left_len <= CAPACITY ) ;
1364
1367
assert ! ( match track_edge_idx {
1365
1368
None => true ,
1366
- Some ( LeftOrRight :: Left ( idx) ) => idx <= left_len ,
1369
+ Some ( LeftOrRight :: Left ( idx) ) => idx <= old_left_len ,
1367
1370
Some ( LeftOrRight :: Right ( idx) ) => idx <= right_len,
1368
1371
} ) ;
1369
1372
1370
1373
unsafe {
1371
- * left_node. reborrow_mut ( ) . into_len_mut ( ) += right_len as u16 + 1 ;
1374
+ * left_node. reborrow_mut ( ) . into_len_mut ( ) = new_left_len as u16 ;
1372
1375
1373
- let parent_key = slice_remove (
1374
- self . parent . node . reborrow_mut ( ) . into_key_area_slice ( ) ,
1375
- self . parent . idx ,
1376
- ) ;
1377
- left_node. reborrow_mut ( ) . into_key_area_mut_at ( left_len) . write ( parent_key) ;
1376
+ let parent_key =
1377
+ slice_remove ( parent_node. reborrow_mut ( ) . into_key_area_slice ( ) , parent_idx) ;
1378
+ left_node. reborrow_mut ( ) . into_key_area_mut_at ( old_left_len) . write ( parent_key) ;
1378
1379
ptr:: copy_nonoverlapping (
1379
1380
right_node. reborrow ( ) . key_area ( ) . as_ptr ( ) ,
1380
- left_node. reborrow_mut ( ) . into_key_area_slice ( ) . as_mut_ptr ( ) . add ( left_len + 1 ) ,
1381
+ left_node. reborrow_mut ( ) . into_key_area_slice ( ) . as_mut_ptr ( ) . add ( old_left_len + 1 ) ,
1381
1382
right_len,
1382
1383
) ;
1383
1384
1384
- let parent_val = slice_remove (
1385
- self . parent . node . reborrow_mut ( ) . into_val_area_slice ( ) ,
1386
- self . parent . idx ,
1387
- ) ;
1388
- left_node. reborrow_mut ( ) . into_val_area_mut_at ( left_len) . write ( parent_val) ;
1385
+ let parent_val =
1386
+ slice_remove ( parent_node. reborrow_mut ( ) . into_val_area_slice ( ) , parent_idx) ;
1387
+ left_node. reborrow_mut ( ) . into_val_area_mut_at ( old_left_len) . write ( parent_val) ;
1389
1388
ptr:: copy_nonoverlapping (
1390
1389
right_node. reborrow ( ) . val_area ( ) . as_ptr ( ) ,
1391
- left_node. reborrow_mut ( ) . into_val_area_slice ( ) . as_mut_ptr ( ) . add ( left_len + 1 ) ,
1390
+ left_node. reborrow_mut ( ) . into_val_area_slice ( ) . as_mut_ptr ( ) . add ( old_left_len + 1 ) ,
1392
1391
right_len,
1393
1392
) ;
1394
1393
1395
- slice_remove (
1396
- & mut self . parent . node . reborrow_mut ( ) . into_edge_area_slice ( ) ,
1397
- self . parent . idx + 1 ,
1398
- ) ;
1399
- let parent_old_len = self . parent . node . len ( ) ;
1400
- self . parent . node . correct_childrens_parent_links ( self . parent . idx + 1 ..parent_old_len) ;
1401
- * self . parent . node . reborrow_mut ( ) . into_len_mut ( ) -= 1 ;
1394
+ slice_remove ( & mut parent_node. reborrow_mut ( ) . into_edge_area_slice ( ) , parent_idx + 1 ) ;
1395
+ parent_node. correct_childrens_parent_links ( parent_idx + 1 ..old_parent_len) ;
1396
+ * parent_node. reborrow_mut ( ) . into_len_mut ( ) -= 1 ;
1402
1397
1403
- if self . parent . node . height > 1 {
1398
+ if parent_node . height > 1 {
1404
1399
// SAFETY: the height of the nodes being merged is one below the height
1405
1400
// of the node of this edge, thus above zero, so they are internal.
1406
1401
let mut left_node = left_node. reborrow_mut ( ) . cast_to_internal_unchecked ( ) ;
1407
1402
let right_node = right_node. cast_to_internal_unchecked ( ) ;
1408
1403
ptr:: copy_nonoverlapping (
1409
1404
right_node. reborrow ( ) . edge_area ( ) . as_ptr ( ) ,
1410
- left_node. reborrow_mut ( ) . into_edge_area_slice ( ) . as_mut_ptr ( ) . add ( left_len + 1 ) ,
1405
+ left_node
1406
+ . reborrow_mut ( )
1407
+ . into_edge_area_slice ( )
1408
+ . as_mut_ptr ( )
1409
+ . add ( old_left_len + 1 ) ,
1411
1410
right_len + 1 ,
1412
1411
) ;
1413
1412
1414
- left_node. correct_childrens_parent_links ( left_len + 1 ..=left_len + 1 + right_len ) ;
1413
+ left_node. correct_childrens_parent_links ( old_left_len + 1 ..new_left_len + 1 ) ;
1415
1414
1416
1415
Global . deallocate ( right_node. node . cast ( ) , Layout :: new :: < InternalNode < K , V > > ( ) ) ;
1417
1416
} else {
@@ -1421,7 +1420,7 @@ impl<'a, K: 'a, V: 'a> BalancingContext<'a, K, V> {
1421
1420
let new_idx = match track_edge_idx {
1422
1421
None => 0 ,
1423
1422
Some ( LeftOrRight :: Left ( idx) ) => idx,
1424
- Some ( LeftOrRight :: Right ( idx) ) => left_len + 1 + idx,
1423
+ Some ( LeftOrRight :: Right ( idx) ) => old_left_len + 1 + idx,
1425
1424
} ;
1426
1425
Handle :: new_edge ( left_node, new_idx)
1427
1426
}
0 commit comments