@@ -819,13 +819,13 @@ impl<BorrowType, K, V, NodeType> Handle<NodeRef<BorrowType, K, V, NodeType>, mar
819
819
}
820
820
}
821
821
822
- impl < ' a , K , V > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > {
822
+ impl < ' a , K , V , NodeType > Handle < NodeRef < marker:: Mut < ' a > , K , V , NodeType > , marker:: Edge > {
823
+ /// Helps implementations of `insert_fit` for a particular `NodeType`,
824
+ /// by taking care of leaf data.
823
825
/// Inserts a new key/value pair between the key/value pairs to the right and left of
824
826
/// this edge. This method assumes that there is enough space in the node for the new
825
827
/// pair to fit.
826
- ///
827
- /// The returned pointer points to the inserted value.
828
- fn insert_fit ( & mut self , key : K , val : V ) -> * mut V {
828
+ fn leafy_insert_fit ( & mut self , key : K , val : V ) {
829
829
// Necessary for correctness, but in a private module
830
830
debug_assert ! ( self . node. len( ) < CAPACITY ) ;
831
831
@@ -834,11 +834,23 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::Edge
834
834
slice_insert ( self . node . vals_mut ( ) , self . idx , val) ;
835
835
836
836
( * self . node . as_leaf_mut ( ) ) . len += 1 ;
837
-
838
- self . node . vals_mut ( ) . get_unchecked_mut ( self . idx )
839
837
}
840
838
}
839
+ }
841
840
841
+ impl < ' a , K , V > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > {
842
+ /// Inserts a new key/value pair between the key/value pairs to the right and left of
843
+ /// this edge. This method assumes that there is enough space in the node for the new
844
+ /// pair to fit.
845
+ ///
846
+ /// The returned pointer points to the inserted value.
847
+ fn insert_fit ( & mut self , key : K , val : V ) -> * mut V {
848
+ self . leafy_insert_fit ( key, val) ;
849
+ unsafe { self . node . vals_mut ( ) . get_unchecked_mut ( self . idx ) }
850
+ }
851
+ }
852
+
853
+ impl < ' a , K , V > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > {
842
854
/// Inserts a new key/value pair between the key/value pairs to the right and left of
843
855
/// this edge. This method splits the node if there isn't enough room.
844
856
///
@@ -880,14 +892,6 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
880
892
}
881
893
}
882
894
883
- /// Unsafely asserts to the compiler some static information about whether the underlying
884
- /// node of this handle is a `Leaf` or an `Internal`.
885
- unsafe fn cast_unchecked < NewType > (
886
- & mut self ,
887
- ) -> Handle < NodeRef < marker:: Mut < ' _ > , K , V , NewType > , marker:: Edge > {
888
- unsafe { Handle :: new_edge ( self . node . cast_unchecked ( ) , self . idx ) }
889
- }
890
-
891
895
/// Inserts a new key/value pair and an edge that will go to the right of that new pair
892
896
/// between this edge and the key/value pair to the right of this edge. This method assumes
893
897
/// that there is enough space in the node for the new pair to fit.
@@ -897,8 +901,7 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
897
901
debug_assert ! ( edge. height == self . node. height - 1 ) ;
898
902
899
903
unsafe {
900
- // This cast is a lie, but it allows us to reuse the key/value insertion logic.
901
- self . cast_unchecked :: < marker:: Leaf > ( ) . insert_fit ( key, val) ;
904
+ self . leafy_insert_fit ( key, val) ;
902
905
903
906
slice_insert (
904
907
slice:: from_raw_parts_mut (
@@ -994,18 +997,11 @@ impl<'a, K, V, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>, marker
994
997
}
995
998
}
996
999
997
- impl < ' a , K , V > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: KV > {
998
- /// Splits the underlying node into three parts:
999
- ///
1000
- /// - The node is truncated to only contain the key/value pairs to the right of
1001
- /// this handle.
1002
- /// - The key and value pointed to by this handle and extracted.
1003
- /// - All the key/value pairs to the right of this handle are put into a newly
1004
- /// allocated node.
1005
- pub fn split ( mut self ) -> ( NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , K , V , Root < K , V > ) {
1000
+ impl < ' a , K , V , NodeType > Handle < NodeRef < marker:: Mut < ' a > , K , V , NodeType > , marker:: KV > {
1001
+ /// Helps implementations of `split` for a particular `NodeType`,
1002
+ /// by taking care of leaf data.
1003
+ fn leafy_split ( & mut self , new_node : & mut LeafNode < K , V > ) -> ( K , V , usize ) {
1006
1004
unsafe {
1007
- let mut new_node = Box :: new ( LeafNode :: new ( ) ) ;
1008
-
1009
1005
let k = ptr:: read ( self . node . keys ( ) . get_unchecked ( self . idx ) ) ;
1010
1006
let v = ptr:: read ( self . node . vals ( ) . get_unchecked ( self . idx ) ) ;
1011
1007
@@ -1024,6 +1020,24 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
1024
1020
1025
1021
( * self . node . as_leaf_mut ( ) ) . len = self . idx as u16 ;
1026
1022
new_node. len = new_len as u16 ;
1023
+ ( k, v, new_len)
1024
+ }
1025
+ }
1026
+ }
1027
+
1028
+ impl < ' a , K , V > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: KV > {
1029
+ /// Splits the underlying node into three parts:
1030
+ ///
1031
+ /// - The node is truncated to only contain the key/value pairs to the right of
1032
+ /// this handle.
1033
+ /// - The key and value pointed to by this handle and extracted.
1034
+ /// - All the key/value pairs to the right of this handle are put into a newly
1035
+ /// allocated node.
1036
+ pub fn split ( mut self ) -> ( NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , K , V , Root < K , V > ) {
1037
+ unsafe {
1038
+ let mut new_node = Box :: new ( LeafNode :: new ( ) ) ;
1039
+
1040
+ let ( k, v, _) = self . leafy_split ( & mut new_node) ;
1027
1041
1028
1042
( self . node , k, v, Root { node : BoxedNode :: from_leaf ( new_node) , height : 0 } )
1029
1043
}
@@ -1055,31 +1069,15 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
1055
1069
unsafe {
1056
1070
let mut new_node = Box :: new ( InternalNode :: new ( ) ) ;
1057
1071
1058
- let k = ptr:: read ( self . node . keys ( ) . get_unchecked ( self . idx ) ) ;
1059
- let v = ptr:: read ( self . node . vals ( ) . get_unchecked ( self . idx ) ) ;
1060
-
1072
+ let ( k, v, new_len) = self . leafy_split ( & mut new_node. data ) ;
1061
1073
let height = self . node . height ;
1062
- let new_len = self . node . len ( ) - self . idx - 1 ;
1063
1074
1064
- ptr:: copy_nonoverlapping (
1065
- self . node . keys ( ) . as_ptr ( ) . add ( self . idx + 1 ) ,
1066
- new_node. data . keys . as_mut_ptr ( ) as * mut K ,
1067
- new_len,
1068
- ) ;
1069
- ptr:: copy_nonoverlapping (
1070
- self . node . vals ( ) . as_ptr ( ) . add ( self . idx + 1 ) ,
1071
- new_node. data . vals . as_mut_ptr ( ) as * mut V ,
1072
- new_len,
1073
- ) ;
1074
1075
ptr:: copy_nonoverlapping (
1075
1076
self . node . as_internal ( ) . edges . as_ptr ( ) . add ( self . idx + 1 ) ,
1076
1077
new_node. edges . as_mut_ptr ( ) ,
1077
1078
new_len + 1 ,
1078
1079
) ;
1079
1080
1080
- ( * self . node . as_leaf_mut ( ) ) . len = self . idx as u16 ;
1081
- new_node. data . len = new_len as u16 ;
1082
-
1083
1081
let mut new_root = Root { node : BoxedNode :: from_internal ( new_node) , height } ;
1084
1082
1085
1083
for i in 0 ..( new_len + 1 ) {
0 commit comments