@@ -6,6 +6,7 @@ mod macros;
6
6
mod serde;
7
7
mod util;
8
8
mod equivalent;
9
+ mod mutable_keys;
9
10
10
11
use std:: hash:: Hash ;
11
12
use std:: hash:: BuildHasher ;
@@ -18,8 +19,9 @@ use std::fmt;
18
19
use std:: mem:: { replace} ;
19
20
use std:: marker:: PhantomData ;
20
21
21
- use util:: { second, ptrdistance, enumerate} ;
22
+ use util:: { second, third , ptrdistance, enumerate} ;
22
23
pub use equivalent:: Equivalent ;
24
+ pub use mutable_keys:: MutableKeys ;
23
25
24
26
fn hash_elem_using < B : BuildHasher , K : ?Sized + Hash > ( build : & B , k : & K ) -> HashValue {
25
27
let mut h = build. build_hasher ( ) ;
@@ -210,16 +212,6 @@ impl<Sz> ShortHashProxy<Sz>
210
212
///
211
213
/// All iterators traverse the map in *the order*.
212
214
///
213
- /// # Mutable Keys
214
- ///
215
- /// Some methods expose `&mut K`, mutable references to the key as it is stored
216
- /// in the map. The key is allowed to be modified, but *only in a way that
217
- /// preserves its hash and equality* (it is only useful for composite key
218
- /// structs).
219
- ///
220
- /// This is sound (memory safe) but a logical error hazard (just like
221
- /// implementing PartialEq, Eq, or Hash incorrectly would be).
222
- ///
223
215
/// # Examples
224
216
///
225
217
/// ```
@@ -837,22 +829,11 @@ impl<K, V, S> OrderMap<K, V, S>
837
829
pub fn get < Q : ?Sized > ( & self , key : & Q ) -> Option < & V >
838
830
where Q : Hash + Equivalent < K > ,
839
831
{
840
- self . get_pair ( key) . map ( second)
841
- }
842
-
843
- pub fn get_pair < Q : ?Sized > ( & self , key : & Q ) -> Option < ( & K , & V ) >
844
- where Q : Hash + Equivalent < K > ,
845
- {
846
- if let Some ( ( _, found) ) = self . find ( key) {
847
- let entry = & self . entries [ found] ;
848
- Some ( ( & entry. key , & entry. value ) )
849
- } else {
850
- None
851
- }
832
+ self . get_full ( key) . map ( third)
852
833
}
853
834
854
835
/// Return item index, key and value
855
- pub fn get_pair_index < Q : ?Sized > ( & self , key : & Q ) -> Option < ( usize , & K , & V ) >
836
+ pub fn get_full < Q : ?Sized > ( & self , key : & Q ) -> Option < ( usize , & K , & V ) >
856
837
where Q : Hash + Equivalent < K > ,
857
838
{
858
839
if let Some ( ( _, found) ) = self . find ( key) {
@@ -866,31 +847,14 @@ impl<K, V, S> OrderMap<K, V, S>
866
847
pub fn get_mut < Q : ?Sized > ( & mut self , key : & Q ) -> Option < & mut V >
867
848
where Q : Hash + Equivalent < K > ,
868
849
{
869
- self . get_pair_mut ( key) . map ( second )
850
+ self . get_full_mut ( key) . map ( third )
870
851
}
871
852
872
- pub fn get_pair_mut < Q : ?Sized > ( & mut self , key : & Q )
873
- -> Option < ( & mut K , & mut V ) >
853
+ pub fn get_full_mut < Q : ?Sized > ( & mut self , key : & Q )
854
+ -> Option < ( usize , & K , & mut V ) >
874
855
where Q : Hash + Equivalent < K > ,
875
856
{
876
- if let Some ( ( _, found) ) = self . find ( key) {
877
- let entry = & mut self . entries [ found] ;
878
- Some ( ( & mut entry. key , & mut entry. value ) )
879
- } else {
880
- None
881
- }
882
- }
883
-
884
- pub fn get_pair_index_mut < Q : ?Sized > ( & mut self , key : & Q )
885
- -> Option < ( usize , & mut K , & mut V ) >
886
- where Q : Hash + Equivalent < K > ,
887
- {
888
- if let Some ( ( _, found) ) = self . find ( key) {
889
- let entry = & mut self . entries [ found] ;
890
- Some ( ( found, & mut entry. key , & mut entry. value ) )
891
- } else {
892
- None
893
- }
857
+ self . get_full_mut2 ( key) . map ( |( i, k, v) | ( i, & * k, v) )
894
858
}
895
859
896
860
/// Return probe (indices) and position (entries)
@@ -958,22 +922,9 @@ impl<K, V, S> OrderMap<K, V, S>
958
922
///
959
923
/// Computes in **O(n)** time (average).
960
924
pub fn retain < F > ( & mut self , mut keep : F )
961
- where F : FnMut ( & mut K , & mut V ) -> bool ,
925
+ where F : FnMut ( & K , & mut V ) -> bool ,
962
926
{
963
- // We can use either forward or reverse scan, but forward was
964
- // faster in a microbenchmark
965
- let mut i = 0 ;
966
- while i < self . len ( ) {
967
- {
968
- let entry = & mut self . entries [ i] ;
969
- if keep ( & mut entry. key , & mut entry. value ) {
970
- i += 1 ;
971
- continue ;
972
- }
973
- }
974
- self . swap_remove_index ( i) ;
975
- // skip increment on remove
976
- }
927
+ self . retain_mut2 ( move |k, v| keep ( & * k, v) )
977
928
}
978
929
979
930
/// Sort the key-value pairs of the map and return a by value iterator of
0 commit comments