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