1
- // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1
+ // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2
2
// file at the top-level directory of this distribution and at
3
3
// http://rust-lang.org/COPYRIGHT.
4
4
//
@@ -46,6 +46,8 @@ pub trait SendMap<K:Eq Hash, V: Copy> {
46
46
47
47
/// Open addressing with linear probing.
48
48
pub mod linear {
49
+ use iter:: BaseIter ;
50
+ use container:: Set ;
49
51
use cmp:: Eq ;
50
52
use cmp;
51
53
use hash:: Hash ;
@@ -442,7 +444,7 @@ pub mod linear {
442
444
}
443
445
}
444
446
445
- impl < K : Hash IterBytes Eq , V : Eq > LinearMap < K , V > : cmp :: Eq {
447
+ impl < K : Hash IterBytes Eq , V : Eq > LinearMap < K , V > : Eq {
446
448
pure fn eq ( & self , other : & LinearMap < K , V > ) -> bool {
447
449
if self . len ( ) != other. len ( ) { return false ; }
448
450
@@ -460,6 +462,47 @@ pub mod linear {
460
462
!self . eq ( other)
461
463
}
462
464
}
465
+
466
+ pub struct LinearSet < T : Hash IterBytes Eq > {
467
+ priv map: LinearMap < T , ( ) >
468
+ }
469
+
470
+ impl < T : Hash IterBytes Eq > LinearSet < T > : BaseIter < T > {
471
+ /// Visit all values in order
472
+ pure fn each ( & self , f : fn ( & T ) -> bool ) { self . map . each_key ( f) }
473
+ pure fn size_hint ( & self ) -> Option < uint > { Some ( self . len ( ) ) }
474
+ }
475
+
476
+ impl < T : Hash IterBytes Eq > LinearSet < T > : Eq {
477
+ pure fn eq ( & self , other : & LinearSet < T > ) -> bool { self . map == other. map }
478
+ pure fn ne ( & self , other : & LinearSet < T > ) -> bool { self . map != other. map }
479
+ }
480
+
481
+ impl < T : Hash IterBytes Eq > LinearSet < T > : Set < T > {
482
+ /// Return true if the set contains a value
483
+ pure fn contains ( & self , value : & T ) -> bool {
484
+ self . map . contains_key ( value)
485
+ }
486
+
487
+ /// Add a value to the set. Return true if the value was not already
488
+ /// present in the set.
489
+ fn insert ( & mut self , value : T ) -> bool { self . map . insert ( value, ( ) ) }
490
+
491
+ /// Remove a value from the set. Return true if the value was
492
+ /// present in the set.
493
+ fn remove ( & mut self , value : & T ) -> bool { self . map . remove ( value) }
494
+ }
495
+
496
+ impl < T : Hash IterBytes Eq > LinearSet < T > {
497
+ /// Create an empty LinearSet
498
+ static fn new( ) -> LinearSet <T > { LinearSet { map: LinearMap ( ) } }
499
+
500
+ /// Return the number of elements in the set
501
+ pure fn len ( & self ) -> uint { self . map . len ( ) }
502
+
503
+ /// Return true if the set contains no elements
504
+ pure fn is_empty ( & self ) -> bool { self . map . is_empty ( ) }
505
+ }
463
506
}
464
507
465
508
#[ test]
0 commit comments