@@ -459,7 +459,7 @@ impl<K: Ord, V> BTreeMap<K, V> {
459459 }
460460 } ) ;
461461 match result {
462- Finished ( ret) => return ret,
462+ Finished ( ret) => return ret. map ( | ( _ , v ) | v ) ,
463463 Continue ( new_stack) => stack = new_stack
464464 }
465465 }
@@ -693,16 +693,16 @@ mod stack {
693693 impl < ' a , K , V > SearchStack < ' a , K , V , handle:: KV , handle:: Leaf > {
694694 /// Removes the key and value in the top element of the stack, then handles underflows as
695695 /// described in BTree's pop function.
696- fn remove_leaf ( mut self ) -> V {
696+ fn remove_leaf ( mut self ) -> ( K , V ) {
697697 self . map . length -= 1 ;
698698
699699 // Remove the key-value pair from the leaf that this search stack points to.
700700 // Then, note if the leaf is underfull, and promptly forget the leaf and its ptr
701701 // 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 ( ) ;
704704 let underflow = self . top . from_raw ( ) . node ( ) . is_underfull ( ) ;
705- ( value , underflow)
705+ ( key_val , underflow)
706706 } ;
707707
708708 loop {
@@ -717,7 +717,7 @@ mod stack {
717717 self . map . depth -= 1 ;
718718 self . map . root . hoist_lone_child ( ) ;
719719 }
720- return value ;
720+ return key_val ;
721721 }
722722 Some ( mut handle) => {
723723 if underflow {
@@ -728,7 +728,7 @@ mod stack {
728728 }
729729 } else {
730730 // All done!
731- return value ;
731+ return key_val ;
732732 }
733733 }
734734 }
@@ -739,7 +739,7 @@ mod stack {
739739 impl < ' a , K , V > SearchStack < ' a , K , V , handle:: KV , handle:: LeafOrInternal > {
740740 /// Removes the key and value in the top element of the stack, then handles underflows as
741741 /// described in BTree's pop function.
742- pub fn remove ( self ) -> V {
742+ pub fn remove ( self ) -> ( K , V ) {
743743 // Ensure that the search stack goes to a leaf. This is necessary to perform deletion
744744 // in a BTree. Note that this may put the tree in an inconsistent state (further
745745 // 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> {
12081208 /// Takes the value of the entry out of the map, and returns it.
12091209 #[ stable( feature = "rust1" , since = "1.0.0" ) ]
12101210 pub fn remove ( self ) -> V {
1211- self . stack . remove ( )
1211+ self . stack . remove ( ) . 1
12121212 }
12131213}
12141214
@@ -1609,3 +1609,86 @@ impl<K: Ord, V> BTreeMap<K, V> {
16091609 }
16101610 }
16111611}
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