13
13
/// Open addressing with linear probing.
14
14
pub mod linear {
15
15
use container:: { Container , Mutable , Map , Set } ;
16
- use cmp:: Eq ;
16
+ use cmp:: { Eq , Equiv } ;
17
17
use hash:: Hash ;
18
18
use to_bytes:: IterBytes ;
19
19
use iter:: BaseIter ;
@@ -107,6 +107,15 @@ pub mod linear {
107
107
self . bucket_for_key_with_hash ( hash, k)
108
108
}
109
109
110
+ #[ inline( always) ]
111
+ pure fn bucket_for_key_equiv < Q : Hash + IterBytes + Equiv < K > > (
112
+ & self ,
113
+ k : & Q )
114
+ -> SearchResult {
115
+ let hash = k. hash_keyed ( self . k0 , self . k1 ) as uint ;
116
+ self . bucket_for_key_with_hash_equiv ( hash, k)
117
+ }
118
+
110
119
#[ inline( always) ]
111
120
pure fn bucket_for_key_with_hash ( & self ,
112
121
hash : uint ,
@@ -122,6 +131,24 @@ pub mod linear {
122
131
TableFull
123
132
}
124
133
134
+ #[ inline( always) ]
135
+ pure fn bucket_for_key_with_hash_equiv < Q : Equiv < K > > ( & self ,
136
+ hash : uint ,
137
+ k : & Q )
138
+ -> SearchResult {
139
+ let _ = for self . bucket_sequence( hash) |i| {
140
+ match self . buckets [ i] {
141
+ Some ( ref bkt) => {
142
+ if bkt. hash == hash && k. equiv ( & bkt. key ) {
143
+ return FoundEntry ( i) ;
144
+ }
145
+ } ,
146
+ None => return FoundHole ( i)
147
+ }
148
+ } ;
149
+ TableFull
150
+ }
151
+
125
152
/// Expand the capacity of the array to the next power of two
126
153
/// and re-insert each of the existing buckets.
127
154
#[ inline( always) ]
@@ -450,6 +477,28 @@ pub mod linear {
450
477
None => fail!(fmt!(" No entry found for key: %?" , k) ) ,
451
478
}
452
479
}
480
+
481
+ /// Return true if the map contains a value for the specified key,
482
+ /// using equivalence
483
+ pure fn contains_key_equiv < Q : Hash + IterBytes + Equiv < K > > (
484
+ & self ,
485
+ key : & Q )
486
+ -> bool {
487
+ match self . bucket_for_key_equiv ( key) {
488
+ FoundEntry ( _) => { true }
489
+ TableFull | FoundHole ( _) => { false }
490
+ }
491
+ }
492
+
493
+ /// Return the value corresponding to the key in the map, using
494
+ /// equivalence
495
+ pure fn find_equiv < Q : Hash + IterBytes + Equiv < K > > ( & self , k : & Q )
496
+ -> Option < & self /V > {
497
+ match self . bucket_for_key_equiv ( k) {
498
+ FoundEntry ( idx) => Some ( self . value_for_bucket ( idx) ) ,
499
+ TableFull | FoundHole ( _) => None ,
500
+ }
501
+ }
453
502
}
454
503
455
504
impl < K : Hash + IterBytes + Eq , V : Eq > Eq for LinearMap < K , V > {
0 commit comments