@@ -626,16 +626,16 @@ impl<K, V, Type> NodeRef<marker::Owned, K, V, Type> {
626
626
}
627
627
628
628
impl < ' a , K : ' a , V : ' a > NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > {
629
- /// Adds a key-value pair to the end of the node, and returns
630
- /// the mutable reference of the inserted value.
631
- pub fn push ( & mut self , key : K , val : V ) -> & mut V {
629
+ /// Adds a key to the end of the node, and returns an exclusive reference
630
+ /// to the space for the corresponding value.
631
+ pub fn push ( & mut self , key : K ) -> & mut MaybeUninit < V > {
632
632
let len = self . len_mut ( ) ;
633
633
let idx = usize:: from ( * len) ;
634
634
assert ! ( idx < CAPACITY ) ;
635
635
* len += 1 ;
636
636
unsafe {
637
637
self . key_area_mut ( idx) . write ( key) ;
638
- self . val_area_mut ( idx) . write ( val )
638
+ self . val_area_mut ( idx)
639
639
}
640
640
}
641
641
}
@@ -845,39 +845,35 @@ fn splitpoint(edge_idx: usize) -> (usize, LeftOrRight<usize>) {
845
845
}
846
846
847
847
impl < ' a , K : ' a , V : ' a > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > {
848
- /// Inserts a new key-value pair between the key-value pairs to the right and left of
849
- /// this edge. This method assumes that there is enough space in the node for the new
850
- /// pair to fit.
848
+ /// Inserts a new key between the keys to the left and right of this edge.
849
+ /// This method assumes that the node is not yet filled to capacity.
851
850
///
852
851
/// The returned pointer points to the inserted value.
853
- fn insert_fit ( & mut self , key : K , val : V ) -> * mut V {
852
+ fn insert_fit ( & mut self , key : K ) -> & mut MaybeUninit < V > {
854
853
debug_assert ! ( self . node. len( ) < CAPACITY ) ;
855
854
let new_len = self . node . len ( ) + 1 ;
856
855
857
856
unsafe {
858
- slice_insert ( self . node . key_area_mut ( ..new_len) , self . idx , key) ;
859
- slice_insert ( self . node . val_area_mut ( ..new_len) , self . idx , val) ;
860
857
* self . node . len_mut ( ) = new_len as u16 ;
861
-
862
- self . node . val_area_mut ( self . idx ) . assume_init_mut ( )
858
+ slice_insert ( self . node . key_area_mut ( ..new_len ) , self . idx ) . write ( key ) ;
859
+ slice_insert ( self . node . val_area_mut ( ..new_len ) , self . idx )
863
860
}
864
861
}
865
862
}
866
863
867
864
impl < ' a , K : ' a , V : ' a > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > {
868
- /// Inserts a new key-value pair between the key-value pairs to the right and left of
869
- /// this edge. This method splits the node if there isn't enough room.
865
+ /// Inserts a new key between the keys to the right and left of this edge.
866
+ /// This method splits the node if there isn't enough room.
870
867
///
871
- /// The returned pointer points to the inserted value.
868
+ /// The returned pointer points to the space for the value paired with the key .
872
869
fn insert < A : Allocator > (
873
870
mut self ,
874
871
key : K ,
875
- val : V ,
876
872
alloc : & A ,
877
- ) -> ( Option < SplitResult < ' a , K , V , marker:: Leaf > > , * mut V ) {
873
+ ) -> ( * mut MaybeUninit < V > , Option < SplitResult < ' a , K , V , marker:: Leaf > > ) {
878
874
if self . node . len ( ) < CAPACITY {
879
- let val_ptr = self . insert_fit ( key, val ) ;
880
- ( None , val_ptr )
875
+ let val_ptr = self . insert_fit ( key) ;
876
+ ( val_ptr , None )
881
877
} else {
882
878
let ( middle_kv_idx, insertion) = splitpoint ( self . idx ) ;
883
879
let middle = unsafe { Handle :: new_kv ( self . node , middle_kv_idx) } ;
@@ -890,8 +886,8 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Leaf>, mark
890
886
Handle :: new_edge ( result. right . borrow_mut ( ) , insert_idx)
891
887
} ,
892
888
} ;
893
- let val_ptr = insertion_edge. insert_fit ( key, val ) ;
894
- ( Some ( result) , val_ptr )
889
+ let val_ptr = insertion_edge. insert_fit ( key) ;
890
+ ( val_ptr , Some ( result) )
895
891
}
896
892
}
897
893
}
@@ -918,11 +914,10 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
918
914
let new_len = self . node . len ( ) + 1 ;
919
915
920
916
unsafe {
921
- slice_insert ( self . node . key_area_mut ( ..new_len) , self . idx , key) ;
922
- slice_insert ( self . node . val_area_mut ( ..new_len) , self . idx , val) ;
923
- slice_insert ( self . node . edge_area_mut ( ..new_len + 1 ) , self . idx + 1 , edge. node ) ;
924
917
* self . node . len_mut ( ) = new_len as u16 ;
925
-
918
+ slice_insert ( self . node . key_area_mut ( ..new_len) , self . idx ) . write ( key) ;
919
+ slice_insert ( self . node . val_area_mut ( ..new_len) , self . idx ) . write ( val) ;
920
+ slice_insert ( self . node . edge_area_mut ( ..new_len + 1 ) , self . idx + 1 ) . write ( edge. node ) ;
926
921
self . node . correct_childrens_parent_links ( self . idx + 1 ..new_len + 1 ) ;
927
922
}
928
923
}
@@ -961,31 +956,30 @@ impl<'a, K: 'a, V: 'a> Handle<NodeRef<marker::Mut<'a>, K, V, marker::Internal>,
961
956
}
962
957
963
958
impl < ' a , K : ' a , V : ' a > Handle < NodeRef < marker:: Mut < ' a > , K , V , marker:: Leaf > , marker:: Edge > {
964
- /// Inserts a new key-value pair between the key-value pairs to the right and left of
965
- /// this edge. This method splits the node if there isn't enough room, and tries to
959
+ /// Inserts a new key between the keys to the left and right of this edge.
960
+ /// This method splits the node if there isn't enough room, and tries to
966
961
/// insert the split off portion into the parent node recursively, until the root is reached.
967
962
///
968
963
/// If the returned result is some `SplitResult`, the `left` field will be the root node.
969
- /// The returned pointer points to the inserted value, which in the case of `SplitResult`
970
- /// is in the `left` or `right` tree.
964
+ /// The returned pointer points to the space for the value paired with the key,
965
+ /// which in the case of `SplitResult` is either in the `left` or `right` tree.
971
966
pub fn insert_recursing < A : Allocator > (
972
967
self ,
973
968
key : K ,
974
- value : V ,
975
969
alloc : & A ,
976
- ) -> ( Option < SplitResult < ' a , K , V , marker:: LeafOrInternal > > , * mut V ) {
977
- let ( mut split, val_ptr ) = match self . insert ( key, value , alloc) {
978
- ( None , val_ptr ) => return ( None , val_ptr ) ,
979
- ( Some ( split) , val_ptr ) => ( split. forget_node_type ( ) , val_ptr ) ,
970
+ ) -> ( * mut MaybeUninit < V > , Option < SplitResult < ' a , K , V , marker:: LeafOrInternal > > ) {
971
+ let ( val_ptr , mut split) = match self . insert ( key, alloc) {
972
+ ( val_ptr , None ) => return ( val_ptr , None ) ,
973
+ ( val_ptr , Some ( split) ) => ( val_ptr , split. forget_node_type ( ) ) ,
980
974
} ;
981
975
982
976
loop {
983
977
split = match split. left . ascend ( ) {
984
978
Ok ( parent) => match parent. insert ( split. kv . 0 , split. kv . 1 , split. right , alloc) {
985
- None => return ( None , val_ptr) ,
986
979
Some ( split) => split. forget_node_type ( ) ,
980
+ None => return ( val_ptr, None ) ,
987
981
} ,
988
- Err ( root) => return ( Some ( SplitResult { left : root, ..split } ) , val_ptr ) ,
982
+ Err ( root) => return ( val_ptr , Some ( SplitResult { left : root, ..split } ) ) ,
989
983
} ;
990
984
}
991
985
}
@@ -1680,19 +1674,18 @@ pub mod marker {
1680
1674
pub enum Edge { }
1681
1675
}
1682
1676
1683
- /// Inserts a value into a slice of initialized elements followed by one uninitialized element.
1677
+ /// Shifts values in a slice, where all elements but the last are initialized,
1678
+ /// to make room for a new value, and returns that room.
1684
1679
///
1685
1680
/// # Safety
1686
1681
/// The slice has more than `idx` elements.
1687
- unsafe fn slice_insert < T > ( slice : & mut [ MaybeUninit < T > ] , idx : usize , val : T ) {
1682
+ unsafe fn slice_insert < T > ( slice : & mut [ MaybeUninit < T > ] , idx : usize ) -> & mut MaybeUninit < T > {
1688
1683
unsafe {
1689
1684
let len = slice. len ( ) ;
1690
- debug_assert ! ( len > idx) ;
1691
- let slice_ptr = slice. as_mut_ptr ( ) ;
1692
- if len > idx + 1 {
1693
- ptr:: copy ( slice_ptr. add ( idx) , slice_ptr. add ( idx + 1 ) , len - idx - 1 ) ;
1694
- }
1695
- ( * slice_ptr. add ( idx) ) . write ( val) ;
1685
+ debug_assert ! ( idx < len) ;
1686
+ let slice_ptr = slice. as_mut_ptr ( ) . add ( idx) ;
1687
+ ptr:: copy ( slice_ptr, slice_ptr. add ( 1 ) , len - idx - 1 ) ;
1688
+ & mut * slice_ptr
1696
1689
}
1697
1690
}
1698
1691
@@ -1705,9 +1698,9 @@ unsafe fn slice_remove<T>(slice: &mut [MaybeUninit<T>], idx: usize) -> T {
1705
1698
unsafe {
1706
1699
let len = slice. len ( ) ;
1707
1700
debug_assert ! ( idx < len) ;
1708
- let slice_ptr = slice. as_mut_ptr ( ) ;
1709
- let ret = ( * slice_ptr. add ( idx ) ) . assume_init_read ( ) ;
1710
- ptr:: copy ( slice_ptr. add ( idx + 1 ) , slice_ptr. add ( idx ) , len - idx - 1 ) ;
1701
+ let slice_ptr = slice. as_mut_ptr ( ) . add ( idx ) ;
1702
+ let ret = ( * slice_ptr) . assume_init_read ( ) ;
1703
+ ptr:: copy ( slice_ptr. add ( 1 ) , slice_ptr, len - idx - 1 ) ;
1711
1704
ret
1712
1705
}
1713
1706
}
0 commit comments