@@ -459,7 +459,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
459
459
}
460
460
} ) ;
461
461
match result {
462
- Finished ( ret) => return ret,
462
+ Finished ( ret) => return ret. map ( | ( _ , v ) | v ) ,
463
463
Continue ( new_stack) => stack = new_stack
464
464
}
465
465
}
@@ -693,16 +693,16 @@ mod stack {
693
693
impl < ' a , K , V > SearchStack < ' a , K , V , handle:: KV , handle:: Leaf > {
694
694
/// Removes the key and value in the top element of the stack, then handles underflows as
695
695
/// described in BTree's pop function.
696
- fn remove_leaf ( mut self ) -> V {
696
+ fn remove_leaf ( mut self ) -> ( K , V ) {
697
697
self . map . length -= 1 ;
698
698
699
699
// Remove the key-value pair from the leaf that this search stack points to.
700
700
// Then, note if the leaf is underfull, and promptly forget the leaf and its ptr
701
701
// to avoid ownership issues.
702
- let ( value , mut underflow) = unsafe {
703
- let ( _ , value ) = self . top . from_raw_mut ( ) . remove_as_leaf ( ) ;
702
+ let ( key_val , mut underflow) = unsafe {
703
+ let key_val = self . top . from_raw_mut ( ) . remove_as_leaf ( ) ;
704
704
let underflow = self . top . from_raw ( ) . node ( ) . is_underfull ( ) ;
705
- ( value , underflow)
705
+ ( key_val , underflow)
706
706
} ;
707
707
708
708
loop {
@@ -717,7 +717,7 @@ mod stack {
717
717
self . map . depth -= 1 ;
718
718
self . map . root . hoist_lone_child ( ) ;
719
719
}
720
- return value ;
720
+ return key_val ;
721
721
}
722
722
Some ( mut handle) => {
723
723
if underflow {
@@ -728,7 +728,7 @@ mod stack {
728
728
}
729
729
} else {
730
730
// All done!
731
- return value ;
731
+ return key_val ;
732
732
}
733
733
}
734
734
}
@@ -739,7 +739,7 @@ mod stack {
739
739
impl < ' a , K , V > SearchStack < ' a , K , V , handle:: KV , handle:: LeafOrInternal > {
740
740
/// Removes the key and value in the top element of the stack, then handles underflows as
741
741
/// described in BTree's pop function.
742
- pub fn remove ( self ) -> V {
742
+ pub fn remove ( self ) -> ( K , V ) {
743
743
// Ensure that the search stack goes to a leaf. This is necessary to perform deletion
744
744
// in a BTree. Note that this may put the tree in an inconsistent state (further
745
745
// described in into_leaf's comments), but this is immediately fixed by the
@@ -1208,7 +1208,7 @@ impl<'a, K: Ord, V> OccupiedEntry<'a, K, V> {
1208
1208
/// Takes the value of the entry out of the map, and returns it.
1209
1209
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1210
1210
pub fn remove ( self ) -> V {
1211
- self . stack . remove ( )
1211
+ self . stack . remove ( ) . 1
1212
1212
}
1213
1213
}
1214
1214
@@ -1609,3 +1609,86 @@ impl<K: Ord, V> BTreeMap<K, V> {
1609
1609
}
1610
1610
}
1611
1611
}
1612
+
1613
+ impl < K , Q : ?Sized > super :: Recover < Q > for BTreeMap < K , ( ) > where K : Borrow < Q > + Ord , Q : Ord {
1614
+ type Key = K ;
1615
+
1616
+ fn get ( & self , key : & Q ) -> Option < & K > {
1617
+ let mut cur_node = & self . root ;
1618
+ loop {
1619
+ match Node :: search ( cur_node, key) {
1620
+ Found ( handle) => return Some ( handle. into_kv ( ) . 0 ) ,
1621
+ GoDown ( handle) => match handle. force ( ) {
1622
+ Leaf ( _) => return None ,
1623
+ Internal ( internal_handle) => {
1624
+ cur_node = internal_handle. into_edge ( ) ;
1625
+ continue ;
1626
+ }
1627
+ }
1628
+ }
1629
+ }
1630
+ }
1631
+
1632
+ fn take ( & mut self , key : & Q ) -> Option < K > {
1633
+ // See `remove` for an explanation of this.
1634
+
1635
+ let mut stack = stack:: PartialSearchStack :: new ( self ) ;
1636
+ loop {
1637
+ let result = stack. with ( move |pusher, node| {
1638
+ match Node :: search ( node, key) {
1639
+ Found ( handle) => {
1640
+ // Perfect match. Terminate the stack here, and remove the entry
1641
+ Finished ( Some ( pusher. seal ( handle) . remove ( ) ) )
1642
+ } ,
1643
+ GoDown ( handle) => {
1644
+ // We need to keep searching, try to go down the next edge
1645
+ match handle. force ( ) {
1646
+ // We're at a leaf; the key isn't in here
1647
+ Leaf ( _) => Finished ( None ) ,
1648
+ Internal ( internal_handle) => Continue ( pusher. push ( internal_handle) )
1649
+ }
1650
+ }
1651
+ }
1652
+ } ) ;
1653
+ match result {
1654
+ Finished ( ret) => return ret. map ( |( k, _) | k) ,
1655
+ Continue ( new_stack) => stack = new_stack
1656
+ }
1657
+ }
1658
+ }
1659
+
1660
+ fn replace ( & mut self , mut key : K ) -> Option < K > {
1661
+ // See `insert` for an explanation of this.
1662
+
1663
+ let mut stack = stack:: PartialSearchStack :: new ( self ) ;
1664
+
1665
+ loop {
1666
+ let result = stack. with ( move |pusher, node| {
1667
+ match Node :: search :: < K , _ > ( node, & key) {
1668
+ Found ( mut handle) => {
1669
+ mem:: swap ( handle. key_mut ( ) , & mut key) ;
1670
+ Finished ( Some ( key) )
1671
+ } ,
1672
+ GoDown ( handle) => {
1673
+ match handle. force ( ) {
1674
+ Leaf ( leaf_handle) => {
1675
+ pusher. seal ( leaf_handle) . insert ( key, ( ) ) ;
1676
+ Finished ( None )
1677
+ }
1678
+ Internal ( internal_handle) => {
1679
+ Continue ( ( pusher. push ( internal_handle) , key, ( ) ) )
1680
+ }
1681
+ }
1682
+ }
1683
+ }
1684
+ } ) ;
1685
+ match result {
1686
+ Finished ( ret) => return ret,
1687
+ Continue ( ( new_stack, renewed_key, _) ) => {
1688
+ stack = new_stack;
1689
+ key = renewed_key;
1690
+ }
1691
+ }
1692
+ }
1693
+ }
1694
+ }
0 commit comments