@@ -5,12 +5,12 @@ mod macros;
5
5
#[ cfg( feature = "serde-1" ) ]
6
6
mod serde;
7
7
mod util;
8
+ mod equivalent;
8
9
9
10
use std:: hash:: Hash ;
10
11
use std:: hash:: BuildHasher ;
11
12
use std:: hash:: Hasher ;
12
13
use std:: collections:: hash_map:: RandomState ;
13
- use std:: borrow:: Borrow ;
14
14
use std:: ops:: RangeFull ;
15
15
16
16
use std:: cmp:: { max, Ordering } ;
@@ -19,6 +19,7 @@ use std::mem::{replace};
19
19
use std:: marker:: PhantomData ;
20
20
21
21
use util:: { second, ptrdistance, enumerate} ;
22
+ pub use equivalent:: Equivalent ;
22
23
23
24
fn hash_elem_using < B : BuildHasher , K : ?Sized + Hash > ( build : & B , k : & K ) -> HashValue {
24
25
let mut h = build. build_hasher ( ) ;
@@ -824,8 +825,7 @@ impl<K, V, S> OrderMap<K, V, S>
824
825
///
825
826
/// Computes in **O(1)** time (average).
826
827
pub fn contains_key < Q : ?Sized > ( & self , key : & Q ) -> bool
827
- where K : Borrow < Q > ,
828
- Q : Eq + Hash ,
828
+ where Q : Hash + Equivalent < K > ,
829
829
{
830
830
self . find ( key) . is_some ( )
831
831
}
@@ -835,15 +835,13 @@ impl<K, V, S> OrderMap<K, V, S>
835
835
///
836
836
/// Computes in **O(1)** time (average).
837
837
pub fn get < Q : ?Sized > ( & self , key : & Q ) -> Option < & V >
838
- where K : Borrow < Q > ,
839
- Q : Eq + Hash ,
838
+ where Q : Hash + Equivalent < K > ,
840
839
{
841
840
self . get_pair ( key) . map ( second)
842
841
}
843
842
844
843
pub fn get_pair < Q : ?Sized > ( & self , key : & Q ) -> Option < ( & K , & V ) >
845
- where K : Borrow < Q > ,
846
- Q : Eq + Hash ,
844
+ where Q : Hash + Equivalent < K > ,
847
845
{
848
846
if let Some ( ( _, found) ) = self . find ( key) {
849
847
let entry = & self . entries [ found] ;
@@ -855,8 +853,7 @@ impl<K, V, S> OrderMap<K, V, S>
855
853
856
854
/// Return item index, key and value
857
855
pub fn get_pair_index < Q : ?Sized > ( & self , key : & Q ) -> Option < ( usize , & K , & V ) >
858
- where K : Borrow < Q > ,
859
- Q : Eq + Hash ,
856
+ where Q : Hash + Equivalent < K > ,
860
857
{
861
858
if let Some ( ( _, found) ) = self . find ( key) {
862
859
let entry = & self . entries [ found] ;
@@ -867,16 +864,14 @@ impl<K, V, S> OrderMap<K, V, S>
867
864
}
868
865
869
866
pub fn get_mut < Q : ?Sized > ( & mut self , key : & Q ) -> Option < & mut V >
870
- where K : Borrow < Q > ,
871
- Q : Eq + Hash ,
867
+ where Q : Hash + Equivalent < K > ,
872
868
{
873
869
self . get_pair_mut ( key) . map ( second)
874
870
}
875
871
876
872
pub fn get_pair_mut < Q : ?Sized > ( & mut self , key : & Q )
877
873
-> Option < ( & mut K , & mut V ) >
878
- where K : Borrow < Q > ,
879
- Q : Eq + Hash ,
874
+ where Q : Hash + Equivalent < K > ,
880
875
{
881
876
if let Some ( ( _, found) ) = self . find ( key) {
882
877
let entry = & mut self . entries [ found] ;
@@ -888,8 +883,7 @@ impl<K, V, S> OrderMap<K, V, S>
888
883
889
884
pub fn get_pair_index_mut < Q : ?Sized > ( & mut self , key : & Q )
890
885
-> Option < ( usize , & mut K , & mut V ) >
891
- where K : Borrow < Q > ,
892
- Q : Eq + Hash ,
886
+ where Q : Hash + Equivalent < K > ,
893
887
{
894
888
if let Some ( ( _, found) ) = self . find ( key) {
895
889
let entry = & mut self . entries [ found] ;
@@ -901,12 +895,11 @@ impl<K, V, S> OrderMap<K, V, S>
901
895
902
896
/// Return probe (indices) and position (entries)
903
897
fn find < Q : ?Sized > ( & self , key : & Q ) -> Option < ( usize , usize ) >
904
- where K : Borrow < Q > ,
905
- Q : Eq + Hash ,
898
+ where Q : Hash + Equivalent < K > ,
906
899
{
907
900
if self . len ( ) == 0 { return None ; }
908
901
let h = hash_elem_using ( & self . hash_builder , key) ;
909
- self . find_using ( h, move |entry| { * entry. key . borrow ( ) == * key } )
902
+ self . find_using ( h, move |entry| { Q :: equivalent ( key , & entry. key ) } )
910
903
}
911
904
912
905
/// Remove the key-value pair equivalent to `key` and return
@@ -920,8 +913,7 @@ impl<K, V, S> OrderMap<K, V, S>
920
913
///
921
914
/// Computes in **O(1)** time (average).
922
915
pub fn swap_remove < Q : ?Sized > ( & mut self , key : & Q ) -> Option < V >
923
- where K : Borrow < Q > ,
924
- Q : Eq + Hash ,
916
+ where Q : Hash + Equivalent < K > ,
925
917
{
926
918
self . swap_remove_pair ( key) . map ( second)
927
919
}
@@ -930,8 +922,7 @@ impl<K, V, S> OrderMap<K, V, S>
930
922
///
931
923
/// Computes in **O(1)** time (average).
932
924
pub fn remove < Q : ?Sized > ( & mut self , key : & Q ) -> Option < V >
933
- where K : Borrow < Q > ,
934
- Q : Eq + Hash ,
925
+ where Q : Hash + Equivalent < K > ,
935
926
{
936
927
self . swap_remove ( key)
937
928
}
@@ -944,8 +935,7 @@ impl<K, V, S> OrderMap<K, V, S>
944
935
///
945
936
/// Return `None` if `key` is not in map.
946
937
pub fn swap_remove_pair < Q : ?Sized > ( & mut self , key : & Q ) -> Option < ( K , V ) >
947
- where K : Borrow < Q > ,
948
- Q : Eq + Hash ,
938
+ where Q : Hash + Equivalent < K > ,
949
939
{
950
940
let ( probe, found) = match self . find ( key) {
951
941
None => return None ,
@@ -1470,9 +1460,8 @@ impl<K, V, S> IntoIterator for OrderMap<K, V, S>
1470
1460
use std:: ops:: { Index , IndexMut } ;
1471
1461
1472
1462
impl < ' a , K , V , Q : ?Sized , S > Index < & ' a Q > for OrderMap < K , V , S >
1473
- where K : Eq + Hash ,
1474
- K : Borrow < Q > ,
1475
- Q : Eq + Hash ,
1463
+ where Q : Hash + Equivalent < K > ,
1464
+ K : Hash + Eq ,
1476
1465
S : BuildHasher ,
1477
1466
{
1478
1467
type Output = V ;
@@ -1492,9 +1481,8 @@ impl<'a, K, V, Q: ?Sized, S> Index<&'a Q> for OrderMap<K, V, S>
1492
1481
///
1493
1482
/// You can **not** insert new pairs with index syntax, use `.insert()`.
1494
1483
impl < ' a , K , V , Q : ?Sized , S > IndexMut < & ' a Q > for OrderMap < K , V , S >
1495
- where K : Eq + Hash ,
1496
- K : Borrow < Q > ,
1497
- Q : Eq + Hash ,
1484
+ where Q : Hash + Equivalent < K > ,
1485
+ K : Hash + Eq ,
1498
1486
S : BuildHasher ,
1499
1487
{
1500
1488
/// ***Panics*** if `key` is not present in the map.
0 commit comments