@@ -108,17 +108,19 @@ pub mod linear {
108
108
}
109
109
110
110
#[ inline( always) ]
111
- pure fn bucket_for_key ( & self , k : & K ) -> SearchResult {
111
+ pure fn bucket_for_key ( & self , buckets : & [ Option < Bucket < K , V > > ] ,
112
+ k : & K ) -> SearchResult {
112
113
let hash = k. hash_keyed ( self . k0 , self . k1 ) as uint ;
113
- self . bucket_for_key_with_hash ( hash, k)
114
+ self . bucket_for_key_with_hash ( buckets , hash, k)
114
115
}
115
116
116
117
#[ inline( always) ]
117
118
pure fn bucket_for_key_with_hash ( & self ,
119
+ buckets : & [ Option < Bucket < K , V > > ] ,
118
120
hash : uint ,
119
121
k : & K ) -> SearchResult {
120
122
let _ = for self . bucket_sequence( hash) |i| {
121
- match self . buckets [ i] {
123
+ match buckets[ i] {
122
124
Some ( ref bkt) => if bkt. hash == hash && * k == bkt. key {
123
125
return FoundEntry ( i) ;
124
126
} ,
@@ -155,19 +157,11 @@ pub mod linear {
155
157
}
156
158
}
157
159
158
- #[ inline( always) ]
159
- pure fn value_for_bucket ( & self , idx : uint ) -> & self /V {
160
- match self . buckets [ idx] {
161
- Some ( ref bkt) => & bkt. value ,
162
- None => die ! ( ~"LinearMap :: find: internal logic error"),
163
- }
164
- }
165
-
166
160
/// Inserts the key value pair into the buckets.
167
161
/// Assumes that there will be a bucket.
168
162
/// True if there was no previous entry with that key
169
163
fn insert_internal ( & mut self , hash : uint , k : K , v : V ) -> bool {
170
- match self.bucket_for_key_with_hash(hash, &k) {
164
+ match self . bucket_for_key_with_hash ( self . buckets , hash, & k) {
171
165
TableFull => { die ! ( ~"Internal logic error"); }
172
166
FoundHole(idx) => {
173
167
debug!(" insert fresh ( %?->%?) at idx %?, hash %?",
@@ -202,7 +196,8 @@ pub mod linear {
202
196
//
203
197
// I found this explanation elucidating:
204
198
// http://www.maths.lse.ac.uk/Courses/MA407/del-hash.pdf
205
- let mut idx = match self.bucket_for_key_with_hash(hash, k) {
199
+ let mut idx = match self.bucket_for_key_with_hash(self.buckets,
200
+ hash, k) {
206
201
TableFull | FoundHole(_) => return None,
207
202
FoundEntry(idx) => idx
208
203
};
@@ -278,7 +273,7 @@ pub mod linear {
278
273
impl <K: Hash IterBytes Eq, V> LinearMap<K, V>: Map<K, V> {
279
274
/// Return true if the map contains a value for the specified key
280
275
pure fn contains_key(&self, k: &K) -> bool {
281
- match self.bucket_for_key(k) {
276
+ match self.bucket_for_key(self.buckets, k) {
282
277
FoundEntry(_) => {true}
283
278
TableFull | FoundHole(_) => {false}
284
279
}
@@ -296,9 +291,20 @@ pub mod linear {
296
291
297
292
/// Return the value corresponding to the key in the map
298
293
pure fn find(&self, k: &K) -> Option<&self/V> {
299
- match self.bucket_for_key(k) {
300
- FoundEntry(idx) => Some(self.value_for_bucket(idx)),
301
- TableFull | FoundHole(_) => None,
294
+ match self.bucket_for_key(self.buckets, k) {
295
+ FoundEntry(idx) => {
296
+ match self.buckets[idx] {
297
+ Some(ref bkt) => {
298
+ Some(&bkt.value)
299
+ }
300
+ None => {
301
+ die!(~" LinearMap :: find: internal logic error")
302
+ }
303
+ }
304
+ }
305
+ TableFull | FoundHole(_) => {
306
+ None
307
+ }
302
308
}
303
309
}
304
310
@@ -358,63 +364,6 @@ pub mod linear {
358
364
old_value
359
365
}
360
366
361
- /// Return the value corresponding to the key in the map, or insert
362
- /// and return the value if it doesn't exist.
363
- fn find_or_insert(&mut self, k: K, v: V) -> &self/V {
364
- if self.size >= self.resize_at {
365
- // n.b.: We could also do this after searching, so
366
- // that we do not resize if this call to insert is
367
- // simply going to update a key in place. My sense
368
- // though is that it's worse to have to search through
369
- // buckets to find the right spot twice than to just
370
- // resize in this corner case.
371
- self.expand();
372
- }
373
-
374
- let hash = k.hash_keyed(self.k0, self.k1) as uint;
375
- let idx = match self.bucket_for_key_with_hash(hash, &k) {
376
- TableFull => die!(~" Internal logic error"),
377
- FoundEntry(idx) => idx,
378
- FoundHole(idx) => {
379
- self.buckets[idx] = Some(Bucket{hash: hash, key: k,
380
- value: v});
381
- self.size += 1;
382
- idx
383
- },
384
- };
385
-
386
- self.value_for_bucket(idx)
387
- }
388
-
389
- /// Return the value corresponding to the key in the map, or create,
390
- /// insert, and return a new value if it doesn't exist.
391
- fn find_or_insert_with(&mut self, k: K, f: fn(&K) -> V) -> &self/V {
392
- if self.size >= self.resize_at {
393
- // n.b.: We could also do this after searching, so
394
- // that we do not resize if this call to insert is
395
- // simply going to update a key in place. My sense
396
- // though is that it's worse to have to search through
397
- // buckets to find the right spot twice than to just
398
- // resize in this corner case.
399
- self.expand();
400
- }
401
-
402
- let hash = k.hash_keyed(self.k0, self.k1) as uint;
403
- let idx = match self.bucket_for_key_with_hash(hash, &k) {
404
- TableFull => die!(~" Internal logic error"),
405
- FoundEntry(idx) => idx,
406
- FoundHole(idx) => {
407
- let v = f(&k);
408
- self.buckets[idx] = Some(Bucket{hash: hash, key: k,
409
- value: v});
410
- self.size += 1;
411
- idx
412
- },
413
- };
414
-
415
- self.value_for_bucket(idx)
416
- }
417
-
418
367
fn consume(&mut self, f: fn(K, V)) {
419
368
let mut buckets = ~[];
420
369
self.buckets <-> buckets;
@@ -572,7 +521,7 @@ mod test_map {
572
521
use uint;
573
522
574
523
#[ test]
575
- pub fn test_insert ( ) {
524
+ pub fn inserts ( ) {
576
525
let mut m = LinearMap :: new ( ) ;
577
526
assert m. insert ( 1 , 2 ) ;
578
527
assert m. insert ( 2 , 4 ) ;
@@ -581,7 +530,7 @@ mod test_map {
581
530
}
582
531
583
532
#[ test]
584
- pub fn test_insert_overwrite ( ) {
533
+ pub fn overwrite ( ) {
585
534
let mut m = LinearMap :: new ( ) ;
586
535
assert m. insert ( 1 , 2 ) ;
587
536
assert * m. get ( & 1 ) == 2 ;
@@ -590,7 +539,7 @@ mod test_map {
590
539
}
591
540
592
541
#[ test]
593
- pub fn test_insert_conflicts ( ) {
542
+ pub fn conflicts ( ) {
594
543
let mut m = linear:: linear_map_with_capacity ( 4 ) ;
595
544
assert m. insert ( 1 , 2 ) ;
596
545
assert m. insert ( 5 , 3 ) ;
@@ -601,7 +550,7 @@ mod test_map {
601
550
}
602
551
603
552
#[ test]
604
- pub fn test_conflict_remove ( ) {
553
+ pub fn conflict_remove ( ) {
605
554
let mut m = linear:: linear_map_with_capacity ( 4 ) ;
606
555
assert m. insert ( 1 , 2 ) ;
607
556
assert m. insert ( 5 , 3 ) ;
@@ -612,7 +561,7 @@ mod test_map {
612
561
}
613
562
614
563
#[ test]
615
- pub fn test_is_empty ( ) {
564
+ pub fn empty ( ) {
616
565
let mut m = linear:: linear_map_with_capacity ( 4 ) ;
617
566
assert m. insert ( 1 , 2 ) ;
618
567
assert !m. is_empty ( ) ;
@@ -621,37 +570,23 @@ mod test_map {
621
570
}
622
571
623
572
#[ test]
624
- pub fn test_pop ( ) {
573
+ pub fn pops ( ) {
625
574
let mut m = LinearMap :: new ( ) ;
626
575
m. insert ( 1 , 2 ) ;
627
576
assert m. pop ( & 1 ) == Some ( 2 ) ;
628
577
assert m. pop ( & 1 ) == None ;
629
578
}
630
579
631
580
#[ test]
632
- pub fn test_swap ( ) {
581
+ pub fn swaps ( ) {
633
582
let mut m = LinearMap :: new ( ) ;
634
583
assert m. swap ( 1 , 2 ) == None ;
635
584
assert m. swap ( 1 , 3 ) == Some ( 2 ) ;
636
585
assert m. swap ( 1 , 4 ) == Some ( 3 ) ;
637
586
}
638
587
639
588
#[ test]
640
- pub fn test_find_or_insert ( ) {
641
- let mut m = LinearMap :: new :: < int , int > ( ) ;
642
- assert m. find_or_insert ( 1 , 2 ) == & 2 ;
643
- assert m. find_or_insert ( 1 , 3 ) == & 2 ;
644
- }
645
-
646
- #[ test]
647
- pub fn test_find_or_insert_with ( ) {
648
- let mut m = LinearMap :: new :: < int , int > ( ) ;
649
- assert m. find_or_insert_with ( 1 , |_| 2 ) == & 2 ;
650
- assert m. find_or_insert_with ( 1 , |_| 3 ) == & 2 ;
651
- }
652
-
653
- #[ test]
654
- pub fn test_consume ( ) {
589
+ pub fn consumes ( ) {
655
590
let mut m = LinearMap :: new ( ) ;
656
591
assert m. insert ( 1 , 2 ) ;
657
592
assert m. insert ( 2 , 3 ) ;
@@ -666,7 +601,7 @@ mod test_map {
666
601
}
667
602
668
603
#[ test]
669
- pub fn test_iterate ( ) {
604
+ pub fn iterate ( ) {
670
605
let mut m = linear:: linear_map_with_capacity ( 4 ) ;
671
606
for uint:: range( 0 , 32 ) |i| {
672
607
assert m. insert( i, i* 2 ) ;
@@ -680,7 +615,7 @@ mod test_map {
680
615
}
681
616
682
617
#[ test]
683
- pub fn test_find ( ) {
618
+ pub fn find ( ) {
684
619
let mut m = LinearMap :: new ( ) ;
685
620
assert m. find ( & 1 ) . is_none ( ) ;
686
621
m. insert ( 1 , 2 ) ;
0 commit comments