11use core:: hash:: { BuildHasher , Hash } ;
22
3- use super :: { Equivalent , IndexMap } ;
3+ use super :: { Bucket , Entries , Equivalent , IndexMap } ;
44
55pub struct PrivateMarker { }
66
@@ -21,13 +21,22 @@ pub trait MutableKeys {
2121 type Value ;
2222
2323 /// Return item index, mutable reference to key and value
24+ ///
25+ /// Computes in **O(1)** time (average).
2426 fn get_full_mut2 < Q : ?Sized > (
2527 & mut self ,
2628 key : & Q ,
2729 ) -> Option < ( usize , & mut Self :: Key , & mut Self :: Value ) >
2830 where
2931 Q : Hash + Equivalent < Self :: Key > ;
3032
33+ /// Return mutable reference to key and value at an index.
34+ ///
35+ /// Valid indices are *0 <= index < self.len()*
36+ ///
37+ /// Computes in **O(1)** time.
38+ fn get_index_mut2 ( & mut self , index : usize ) -> Option < ( & mut Self :: Key , & mut Self :: Value ) > ;
39+
3140 /// Scan through each key-value pair in the map and keep those where the
3241 /// closure `keep` returns `true`.
3342 ///
@@ -39,6 +48,7 @@ pub trait MutableKeys {
3948 where
4049 F : FnMut ( & mut Self :: Key , & mut Self :: Value ) -> bool ;
4150
51+ #[ doc( hidden) ]
4252 /// This method is not useful in itself – it is there to “seal” the trait
4353 /// for external implementation, so that we can add methods without
4454 /// causing breaking changes.
@@ -55,11 +65,21 @@ where
5565{
5666 type Key = K ;
5767 type Value = V ;
68+
5869 fn get_full_mut2 < Q : ?Sized > ( & mut self , key : & Q ) -> Option < ( usize , & mut K , & mut V ) >
5970 where
6071 Q : Hash + Equivalent < K > ,
6172 {
62- self . get_full_mut2_impl ( key)
73+ if let Some ( i) = self . get_index_of ( key) {
74+ let entry = & mut self . as_entries_mut ( ) [ i] ;
75+ Some ( ( i, & mut entry. key , & mut entry. value ) )
76+ } else {
77+ None
78+ }
79+ }
80+
81+ fn get_index_mut2 ( & mut self , index : usize ) -> Option < ( & mut K , & mut V ) > {
82+ self . as_entries_mut ( ) . get_mut ( index) . map ( Bucket :: muts)
6383 }
6484
6585 fn retain2 < F > ( & mut self , keep : F )
0 commit comments