@@ -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 (
@@ -1030,18 +1033,11 @@ impl<'a, K, V, NodeType> Handle<NodeRef<marker::Mut<'a>, K, V, NodeType>, marker
1030
1033
}
1031
1034
}
1032
1035
1033
- impl < ' a , K , V > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: KV > {
1034
- /// Splits the underlying node into three parts:
1035
- ///
1036
- /// - The node is truncated to only contain the key/value pairs to the right of
1037
- /// this handle.
1038
- /// - The key and value pointed to by this handle and extracted.
1039
- /// - All the key/value pairs to the right of this handle are put into a newly
1040
- /// allocated node.
1041
- pub fn split ( mut self ) -> ( NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , K , V , Root < K , V > ) {
1036
+ impl < ' a , K , V , NodeType > Handle < NodeRef < marker:: Mut < ' a > , K , V , NodeType > , marker:: KV > {
1037
+ /// Helps implementations of `split` for a particular `NodeType`,
1038
+ /// by taking care of leaf data.
1039
+ fn leafy_split ( & mut self , new_node : & mut LeafNode < K , V > ) -> ( K , V , usize ) {
1042
1040
unsafe {
1043
- let mut new_node = Box :: new ( LeafNode :: new ( ) ) ;
1044
-
1045
1041
let k = ptr:: read ( self . node . keys ( ) . get_unchecked ( self . idx ) ) ;
1046
1042
let v = ptr:: read ( self . node . vals ( ) . get_unchecked ( self . idx ) ) ;
1047
1043
@@ -1060,6 +1056,24 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, marker::KV>
1060
1056
1061
1057
( * self . node . as_leaf_mut ( ) ) . len = self . idx as u16 ;
1062
1058
new_node. len = new_len as u16 ;
1059
+ ( k, v, new_len)
1060
+ }
1061
+ }
1062
+ }
1063
+
1064
+ impl < ' a , K , V > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: KV > {
1065
+ /// Splits the underlying node into three parts:
1066
+ ///
1067
+ /// - The node is truncated to only contain the key/value pairs to the right of
1068
+ /// this handle.
1069
+ /// - The key and value pointed to by this handle and extracted.
1070
+ /// - All the key/value pairs to the right of this handle are put into a newly
1071
+ /// allocated node.
1072
+ pub fn split ( mut self ) -> ( NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , K , V , Root < K , V > ) {
1073
+ unsafe {
1074
+ let mut new_node = Box :: new ( LeafNode :: new ( ) ) ;
1075
+
1076
+ let ( k, v, _) = self . leafy_split ( & mut new_node) ;
1063
1077
1064
1078
( self . node , k, v, Root { node : BoxedNode :: from_leaf ( new_node) , height : 0 } )
1065
1079
}
@@ -1091,31 +1105,15 @@ impl<'a, K, V> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>, marker::
1091
1105
unsafe {
1092
1106
let mut new_node = Box :: new ( InternalNode :: new ( ) ) ;
1093
1107
1094
- let k = ptr:: read ( self . node . keys ( ) . get_unchecked ( self . idx ) ) ;
1095
- let v = ptr:: read ( self . node . vals ( ) . get_unchecked ( self . idx ) ) ;
1096
-
1108
+ let ( k, v, new_len) = self . leafy_split ( & mut new_node. data ) ;
1097
1109
let height = self . node . height ;
1098
- let new_len = self . node . len ( ) - self . idx - 1 ;
1099
1110
1100
- ptr:: copy_nonoverlapping (
1101
- self . node . keys ( ) . as_ptr ( ) . add ( self . idx + 1 ) ,
1102
- new_node. data . keys . as_mut_ptr ( ) as * mut K ,
1103
- new_len,
1104
- ) ;
1105
- ptr:: copy_nonoverlapping (
1106
- self . node . vals ( ) . as_ptr ( ) . add ( self . idx + 1 ) ,
1107
- new_node. data . vals . as_mut_ptr ( ) as * mut V ,
1108
- new_len,
1109
- ) ;
1110
1111
ptr:: copy_nonoverlapping (
1111
1112
self . node . as_internal ( ) . edges . as_ptr ( ) . add ( self . idx + 1 ) ,
1112
1113
new_node. edges . as_mut_ptr ( ) ,
1113
1114
new_len + 1 ,
1114
1115
) ;
1115
1116
1116
- ( * self . node . as_leaf_mut ( ) ) . len = self . idx as u16 ;
1117
- new_node. data . len = new_len as u16 ;
1118
-
1119
1117
let mut new_root = Root { node : BoxedNode :: from_internal ( new_node) , height } ;
1120
1118
1121
1119
for i in 0 ..( new_len + 1 ) {
0 commit comments