@@ -843,7 +843,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge
843
843
/// this edge. This method splits the node if there isn't enough room.
844
844
///
845
845
/// The returned pointer points to the inserted value.
846
- pub fn insert ( mut self , key : K , val : V ) -> ( InsertResult < ' a , K , V , marker:: Leaf > , * mut V ) {
846
+ fn insert ( mut self , key : K , val : V ) -> ( InsertResult < ' a , K , V , marker:: Leaf > , * mut V ) {
847
847
if self . node . len ( ) < CAPACITY {
848
848
let ptr = self . insert_fit ( key, val) ;
849
849
let kv = unsafe { Handle :: new_kv ( self . node , self . idx ) } ;
@@ -862,7 +862,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge
862
862
. insert_fit ( key, val)
863
863
}
864
864
} ;
865
- ( InsertResult :: Split ( left, k, v, right) , ptr)
865
+ ( InsertResult :: Split ( SplitResult { left : left . forget_type ( ) , k, v, right } ) , ptr)
866
866
}
867
867
}
868
868
}
@@ -918,7 +918,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
918
918
/// Inserts a new key/value pair and an edge that will go to the right of that new pair
919
919
/// between this edge and the key/value pair to the right of this edge. This method splits
920
920
/// the node if there isn't enough room.
921
- pub fn insert (
921
+ fn insert (
922
922
mut self ,
923
923
key : K ,
924
924
val : V ,
@@ -946,7 +946,43 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
946
946
. insert_fit ( key, val, edge) ;
947
947
}
948
948
}
949
- InsertResult :: Split ( left, k, v, right)
949
+ InsertResult :: Split ( SplitResult { left : left. forget_type ( ) , k, v, right } )
950
+ }
951
+ }
952
+ }
953
+
954
+ impl < ' a , K : ' a , V > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > {
955
+ /// Inserts a new key/value pair between the key/value pairs to the right and left of
956
+ /// this edge. This method splits the node if there isn't enough room, and tries to
957
+ /// insert the split off portion into the parent node recursively, until the root is reached.
958
+ ///
959
+ /// If the returned result is a `Fit`, its handle's node can be this edge's node or an ancestor.
960
+ /// If the returned result is a `Split`, the `left` field will be the root node.
961
+ /// The returned pointer points to the inserted value.
962
+ pub fn insert_recursing (
963
+ self ,
964
+ key : K ,
965
+ value : V ,
966
+ ) -> ( InsertResult < ' a , K , V , marker:: LeafOrInternal > , * mut V ) {
967
+ let ( mut split, val_ptr) = match self . insert ( key, value) {
968
+ ( InsertResult :: Fit ( handle) , ptr) => {
969
+ return ( InsertResult :: Fit ( handle. forget_node_type ( ) ) , ptr) ;
970
+ }
971
+ ( InsertResult :: Split ( split) , val_ptr) => ( split, val_ptr) ,
972
+ } ;
973
+
974
+ loop {
975
+ split = match split. left . ascend ( ) {
976
+ Ok ( parent) => match parent. insert ( split. k , split. v , split. right ) {
977
+ InsertResult :: Fit ( handle) => {
978
+ return ( InsertResult :: Fit ( handle. forget_node_type ( ) ) , val_ptr) ;
979
+ }
980
+ InsertResult :: Split ( split) => split,
981
+ } ,
982
+ Err ( root) => {
983
+ return ( InsertResult :: Split ( SplitResult { left : root, ..split } ) , val_ptr) ;
984
+ }
985
+ } ;
950
986
}
951
987
}
952
988
}
@@ -1389,6 +1425,14 @@ impl<BorrowType, K, V> Handle<NodeRef<BorrowType, K, V, marker::Leaf>, marker::K
1389
1425
}
1390
1426
}
1391
1427
1428
+ impl < BorrowType , K , V > Handle < NodeRef < BorrowType , K , V , marker:: Internal > , marker:: KV > {
1429
+ pub fn forget_node_type (
1430
+ self ,
1431
+ ) -> Handle < NodeRef < BorrowType , K , V , marker:: LeafOrInternal > , marker:: KV > {
1432
+ unsafe { Handle :: new_kv ( self . node . forget_type ( ) , self . idx ) }
1433
+ }
1434
+ }
1435
+
1392
1436
impl < BorrowType , K , V , HandleType >
1393
1437
Handle < NodeRef < BorrowType , K , V , marker:: LeafOrInternal > , HandleType >
1394
1438
{
@@ -1455,9 +1499,21 @@ pub enum ForceResult<Leaf, Internal> {
1455
1499
Internal ( Internal ) ,
1456
1500
}
1457
1501
1502
+ /// Result of insertion, when a node needed to expand beyond its capacity.
1503
+ /// Does not distinguish between `Leaf` and `Internal` because `Root` doesn't.
1504
+ pub struct SplitResult < ' a , K , V > {
1505
+ // Altered node in existing tree with elements and edges that belong to the left of `k`.
1506
+ pub left : NodeRef < marker:: Mut < ' a > , K , V , marker:: LeafOrInternal > ,
1507
+ // Some key and value split off, to be inserted elsewhere.
1508
+ pub k : K ,
1509
+ pub v : V ,
1510
+ // Owned, unattached, new node with elements and edges that belong to the right of `k`.
1511
+ pub right : Root < K , V > ,
1512
+ }
1513
+
1458
1514
pub enum InsertResult < ' a , K , V , Type > {
1459
1515
Fit ( Handle < NodeRef < marker:: Mut < ' a > , K , V , Type > , marker:: KV > ) ,
1460
- Split ( NodeRef < marker :: Mut < ' a > , K , V , Type > , K , V , Root < K , V > ) ,
1516
+ Split ( SplitResult < ' a , K , V > ) ,
1461
1517
}
1462
1518
1463
1519
pub mod marker {
0 commit comments