@@ -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:: { 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)
@@ -902,6 +866,15 @@ impl<K, V, S> OrderMap<K, V, S>
902
866
self . find_using ( h, move |entry| { Q :: equivalent ( key, & entry. key ) } )
903
867
}
904
868
869
+ /// FIXME Same as .swap_remove
870
+ ///
871
+ /// Computes in **O(1)** time (average).
872
+ pub fn remove < Q : ?Sized > ( & mut self , key : & Q ) -> Option < V >
873
+ where Q : Hash + Equivalent < K > ,
874
+ {
875
+ self . swap_remove ( key)
876
+ }
877
+
905
878
/// Remove the key-value pair equivalent to `key` and return
906
879
/// its value.
907
880
///
@@ -915,33 +888,26 @@ impl<K, V, S> OrderMap<K, V, S>
915
888
pub fn swap_remove < Q : ?Sized > ( & mut self , key : & Q ) -> Option < V >
916
889
where Q : Hash + Equivalent < K > ,
917
890
{
918
- self . swap_remove_pair ( key) . map ( second )
891
+ self . swap_remove_full ( key) . map ( third )
919
892
}
920
893
921
- /// FIXME Same as .swap_remove
922
- ///
923
- /// Computes in **O(1)** time (average).
924
- pub fn remove < Q : ?Sized > ( & mut self , key : & Q ) -> Option < V >
925
- where Q : Hash + Equivalent < K > ,
926
- {
927
- self . swap_remove ( key)
928
- }
929
-
930
- /// Remove the key-value pair equivalent to `key` and return it.
894
+ /// Remove the key-value pair equivalent to `key` and return it and
895
+ /// the index it had.
931
896
///
932
897
/// Like `Vec::swap_remove`, the pair is removed by swapping it with the
933
898
/// last element of the map and popping it off. **This perturbs
934
899
/// the postion of what used to be the last element!**
935
900
///
936
901
/// Return `None` if `key` is not in map.
937
- pub fn swap_remove_pair < Q : ?Sized > ( & mut self , key : & Q ) -> Option < ( K , V ) >
902
+ pub fn swap_remove_full < Q : ?Sized > ( & mut self , key : & Q ) -> Option < ( usize , K , V ) >
938
903
where Q : Hash + Equivalent < K > ,
939
904
{
940
905
let ( probe, found) = match self . find ( key) {
941
906
None => return None ,
942
907
Some ( t) => t,
943
908
} ;
944
- Some ( self . remove_found ( probe, found) )
909
+ let ( k, v) = self . remove_found ( probe, found) ;
910
+ Some ( ( found, k, v) )
945
911
}
946
912
947
913
/// Remove the last key-value pair
@@ -958,22 +924,9 @@ impl<K, V, S> OrderMap<K, V, S>
958
924
///
959
925
/// Computes in **O(n)** time (average).
960
926
pub fn retain < F > ( & mut self , mut keep : F )
961
- where F : FnMut ( & mut K , & mut V ) -> bool ,
927
+ where F : FnMut ( & K , & mut V ) -> bool ,
962
928
{
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
- }
929
+ self . retain2 ( move |k, v| keep ( & * k, v) )
977
930
}
978
931
979
932
/// Sort the key-value pairs of the map and return a by value iterator of
@@ -1712,12 +1665,13 @@ mod tests {
1712
1665
let remove = [ 4 , 12 , 8 , 7 ] ;
1713
1666
1714
1667
for & key in & remove_fail {
1715
- assert ! ( map. swap_remove_pair ( & key) . is_none( ) ) ;
1668
+ assert ! ( map. swap_remove_full ( & key) . is_none( ) ) ;
1716
1669
}
1717
1670
println ! ( "{:?}" , map) ;
1718
1671
for & key in & remove {
1719
1672
//println!("{:?}", map);
1720
- assert_eq ! ( map. swap_remove_pair( & key) , Some ( ( key, key) ) ) ;
1673
+ let index = map. get_full ( & key) . unwrap ( ) . 0 ;
1674
+ assert_eq ! ( map. swap_remove_full( & key) , Some ( ( index, key, key) ) ) ;
1721
1675
}
1722
1676
println ! ( "{:?}" , map) ;
1723
1677
0 commit comments