@@ -23,31 +23,10 @@ use hash::Hash;
23
23
use prelude:: * ;
24
24
use to_bytes:: IterBytes ;
25
25
26
- pub trait SendMap < K : Eq Hash , V : Copy > {
27
- // FIXME(#3148) ^^^^ once find_ref() works, we can drop V:copy
28
-
29
- fn insert ( & mut self , k : K , +v : V ) -> bool ;
30
- fn remove ( & mut self , k : & K ) -> bool ;
31
- fn pop ( & mut self , k : & K ) -> Option < V > ;
32
- fn swap ( & mut self , k : K , +v : V ) -> Option < V > ;
33
- fn consume ( & mut self , f : fn ( K , V ) ) ;
34
- fn clear ( & mut self ) ;
35
- pure fn len ( & const self ) -> uint ;
36
- pure fn is_empty ( & const self ) -> bool ;
37
- pure fn contains_key ( & const self , k : & K ) -> bool ;
38
- pure fn each ( & self , blk : fn ( k : & K , v : & V ) -> bool ) ;
39
- pure fn each_key_ref ( & self , blk : fn ( k : & K ) -> bool ) ;
40
- pure fn each_value_ref ( & self , blk : fn ( v : & V ) -> bool ) ;
41
- pure fn find ( & const self , k : & K ) -> Option < V > ;
42
- pure fn get ( & const self , k : & K ) -> V ;
43
- pure fn find_ref ( & self , k : & K ) -> Option < & self /V > ;
44
- pure fn get_ref ( & self , k : & K ) -> & self /V ;
45
- }
46
-
47
26
/// Open addressing with linear probing.
48
27
pub mod linear {
49
28
use iter:: BaseIter ;
50
- use container:: Set ;
29
+ use container:: { Container , Mutable , Map , Set } ;
51
30
use cmp:: Eq ;
52
31
use cmp;
53
32
use hash:: Hash ;
@@ -279,7 +258,48 @@ pub mod linear {
279
258
}
280
259
}
281
260
282
- impl < K : Hash IterBytes Eq , V > LinearMap < K , V > {
261
+ impl < K : Hash IterBytes Eq , V > LinearMap < K , V > : Container {
262
+ pure fn len ( & self ) -> uint { self . size }
263
+ pure fn is_empty ( & self ) -> bool { self . len ( ) == 0 }
264
+ }
265
+
266
+ impl < K : Hash IterBytes Eq , V > LinearMap < K , V > : Mutable {
267
+ fn clear ( & mut self ) {
268
+ for uint:: range( 0 , self . buckets. len( ) ) |idx| {
269
+ self . buckets [ idx] = None ;
270
+ }
271
+ self . size = 0 ;
272
+ }
273
+ }
274
+
275
+ impl < K : Hash IterBytes Eq , V > LinearMap < K , V > : Map < K , V > {
276
+ pure fn contains_key ( & self , k : & K ) -> bool {
277
+ match self . bucket_for_key ( self . buckets , k) {
278
+ FoundEntry ( _) => { true }
279
+ TableFull | FoundHole ( _) => { false }
280
+ }
281
+ }
282
+
283
+ pure fn each ( & self , blk : fn ( k : & K , v : & V ) -> bool ) {
284
+ for vec:: each( self . buckets) |slot| {
285
+ let mut broke = false ;
286
+ do slot. iter |bucket| {
287
+ if !blk ( & bucket. key , & bucket. value ) {
288
+ broke = true ; // FIXME(#3064) just write "break;"
289
+ }
290
+ }
291
+ if broke { break ; }
292
+ }
293
+ }
294
+
295
+ pure fn each_key ( & self , blk : fn ( k : & K ) -> bool ) {
296
+ self . each ( |k, _v| blk ( k) )
297
+ }
298
+
299
+ pure fn each_value ( & self , blk : fn ( v : & V ) -> bool ) {
300
+ self . each ( |_k, v| blk ( v) )
301
+ }
302
+
283
303
fn insert ( & mut self , k : K , v : V ) -> bool {
284
304
if self . size >= self . resize_at {
285
305
// n.b.: We could also do this after searching, so
@@ -301,7 +321,9 @@ pub mod linear {
301
321
None => false ,
302
322
}
303
323
}
324
+ }
304
325
326
+ impl < K : Hash IterBytes Eq , V > LinearMap < K , V > {
305
327
fn pop ( & mut self , k : & K ) -> Option < V > {
306
328
let hash = k. hash_keyed ( self . k0 , self . k1 ) as uint ;
307
329
self . pop_internal ( hash, k)
@@ -347,29 +369,6 @@ pub mod linear {
347
369
}
348
370
}
349
371
350
- fn clear ( & mut self ) {
351
- for uint:: range( 0 , self . buckets. len( ) ) |idx| {
352
- self . buckets [ idx] = None ;
353
- }
354
- self . size = 0 ;
355
- }
356
-
357
- pure fn len ( & const self ) -> uint {
358
- self . size
359
- }
360
-
361
- pure fn is_empty ( & const self ) -> bool {
362
- self . len ( ) == 0
363
- }
364
-
365
- pure fn contains_key ( & const self ,
366
- k : & K ) -> bool {
367
- match self . bucket_for_key ( self . buckets , k) {
368
- FoundEntry ( _) => { true }
369
- TableFull | FoundHole ( _) => { false }
370
- }
371
- }
372
-
373
372
pure fn find_ref ( & self , k : & K ) -> Option < & self /V > {
374
373
match self . bucket_for_key ( self . buckets , k) {
375
374
FoundEntry ( idx) => {
@@ -396,26 +395,6 @@ pub mod linear {
396
395
None => fail fmt ! ( "No entry found for key: %?" , k) ,
397
396
}
398
397
}
399
-
400
- pure fn each ( & self , blk : fn ( k : & K , v : & V ) -> bool ) {
401
- for vec:: each( self . buckets) |slot| {
402
- let mut broke = false ;
403
- do slot. iter |bucket| {
404
- if !blk ( & bucket. key , & bucket. value ) {
405
- broke = true ; // FIXME(#3064) just write "break;"
406
- }
407
- }
408
- if broke { break ; }
409
- }
410
- }
411
-
412
- pure fn each_key ( & self , blk : fn ( k : & K ) -> bool ) {
413
- self . each ( |k, _v| blk ( k) )
414
- }
415
-
416
- pure fn each_value ( & self , blk : fn ( v : & V ) -> bool ) {
417
- self . each ( |_k, v| blk ( v) )
418
- }
419
398
}
420
399
421
400
impl < K : Hash IterBytes Eq , V : Copy > LinearMap < K , V > {
@@ -482,6 +461,15 @@ pub mod linear {
482
461
}
483
462
}
484
463
464
+ impl < T : Hash IterBytes Eq > LinearSet < T > : Container {
465
+ pure fn len ( & self ) -> uint { self . map . len ( ) }
466
+ pure fn is_empty ( & self ) -> bool { self . map . is_empty ( ) }
467
+ }
468
+
469
+ impl < T : Hash IterBytes Eq > LinearSet < T > : Mutable {
470
+ fn clear ( & mut self ) { self . map . clear ( ) }
471
+ }
472
+
485
473
impl < T : Hash IterBytes Eq > LinearSet < T > : Set < T > {
486
474
/// Return true if the set contains a value
487
475
pure fn contains ( & self , value : & T ) -> bool {
@@ -500,12 +488,6 @@ pub mod linear {
500
488
impl < T : Hash IterBytes Eq > LinearSet < T > {
501
489
/// Create an empty LinearSet
502
490
static fn new( ) -> LinearSet <T > { LinearSet { map: LinearMap ( ) } }
503
-
504
- /// Return the number of elements in the set
505
- pure fn len ( & self ) -> uint { self . map . len ( ) }
506
-
507
- /// Return true if the set contains no elements
508
- pure fn is_empty ( & self ) -> bool { self . map . is_empty ( ) }
509
491
}
510
492
}
511
493
0 commit comments