@@ -70,7 +70,6 @@ use core::slice;
7070use core:: u32;
7171use std:: hash;
7272
73- use { Mutable , Set , MutableSet , MutableSeq } ;
7473use vec:: Vec ;
7574
7675type MatchWords < ' a > = Chain < MaskWords < ' a > , Skip < Take < Enumerate < Repeat < u32 > > > > > ;
@@ -755,6 +754,20 @@ impl Bitv {
755754 }
756755 self . set ( insert_pos, elem) ;
757756 }
757+
758+ /// Return the total number of bits in this vector
759+ #[ inline]
760+ pub fn len ( & self ) -> uint { self . nbits }
761+
762+ /// Returns true if there are no bits in this vector
763+ #[ inline]
764+ pub fn is_empty ( & self ) -> bool { self . len ( ) == 0 }
765+
766+ /// Clears all bits in this vector.
767+ #[ inline]
768+ pub fn clear ( & mut self ) {
769+ for w in self . storage . iter_mut ( ) { * w = 0u32 ; }
770+ }
758771}
759772
760773/// Transforms a byte-vector into a `Bitv`. Each byte becomes eight bits,
@@ -804,18 +817,6 @@ impl Default for Bitv {
804817 fn default ( ) -> Bitv { Bitv :: new ( ) }
805818}
806819
807- impl Collection for Bitv {
808- #[ inline]
809- fn len ( & self ) -> uint { self . nbits }
810- }
811-
812- impl Mutable for Bitv {
813- #[ inline]
814- fn clear ( & mut self ) {
815- for w in self . storage . iter_mut ( ) { * w = 0u32 ; }
816- }
817- }
818-
819820impl FromIterator < bool > for Bitv {
820821 fn from_iter < I : Iterator < bool > > ( iterator : I ) -> Bitv {
821822 let mut ret = Bitv :: new ( ) ;
@@ -1466,61 +1467,45 @@ impl BitvSet {
14661467 pub fn symmetric_difference_with ( & mut self , other : & BitvSet ) {
14671468 self . other_op ( other, |w1, w2| w1 ^ w2) ;
14681469 }
1469- }
1470-
1471- impl fmt:: Show for BitvSet {
1472- fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
1473- try!( write ! ( fmt, "{{" ) ) ;
1474- let mut first = true ;
1475- for n in self . iter ( ) {
1476- if !first {
1477- try!( write ! ( fmt, ", " ) ) ;
1478- }
1479- try!( write ! ( fmt, "{}" , n) ) ;
1480- first = false ;
1481- }
1482- write ! ( fmt, "}}" )
1483- }
1484- }
14851470
1486- impl < S : hash :: Writer > hash :: Hash < S > for BitvSet {
1487- fn hash ( & self , state : & mut S ) {
1488- for pos in self . iter ( ) {
1489- pos . hash ( state ) ;
1490- }
1471+ /// Return the number of set bits in this set.
1472+ # [ inline ]
1473+ pub fn len ( & self ) -> uint {
1474+ let & BitvSet ( ref bitv ) = self ;
1475+ bitv . storage . iter ( ) . fold ( 0 , |acc , & n| acc + n . count_ones ( ) )
14911476 }
1492- }
14931477
1494- impl Collection for BitvSet {
1478+ /// Returns whether there are no bits set in this set
14951479 #[ inline]
1496- fn len ( & self ) -> uint {
1480+ pub fn is_empty ( & self ) -> bool {
14971481 let & BitvSet ( ref bitv) = self ;
1498- bitv. storage . iter ( ) . fold ( 0 , |acc , & n| acc + n . count_ones ( ) )
1482+ bitv. storage . iter ( ) . all ( | & n| n == 0 )
14991483 }
1500- }
15011484
1502- impl Mutable for BitvSet {
1485+ /// Clears all bits in this set
15031486 #[ inline]
1504- fn clear ( & mut self ) {
1487+ pub fn clear ( & mut self ) {
15051488 let & BitvSet ( ref mut bitv) = self ;
15061489 bitv. clear ( ) ;
15071490 }
1508- }
15091491
1510- impl Set < uint > for BitvSet {
1492+ /// Returns `true` if this set contains the specified integer.
15111493 #[ inline]
1512- fn contains ( & self , value : & uint ) -> bool {
1494+ pub fn contains ( & self , value : & uint ) -> bool {
15131495 let & BitvSet ( ref bitv) = self ;
15141496 * value < bitv. nbits && bitv. get ( * value)
15151497 }
15161498
1499+ /// Returns `true` if the set has no elements in common with `other`.
1500+ /// This is equivalent to checking for an empty intersection.
15171501 #[ inline]
1518- fn is_disjoint ( & self , other : & BitvSet ) -> bool {
1502+ pub fn is_disjoint ( & self , other : & BitvSet ) -> bool {
15191503 self . intersection ( other) . next ( ) . is_none ( )
15201504 }
15211505
1506+ /// Returns `true` if the set is a subset of another.
15221507 #[ inline]
1523- fn is_subset ( & self , other : & BitvSet ) -> bool {
1508+ pub fn is_subset ( & self , other : & BitvSet ) -> bool {
15241509 let & BitvSet ( ref self_bitv) = self ;
15251510 let & BitvSet ( ref other_bitv) = other;
15261511
@@ -1531,14 +1516,15 @@ impl Set<uint> for BitvSet {
15311516 self_bitv. mask_words ( other_bitv. storage . len ( ) ) . all ( |( _, w) | w == 0 )
15321517 }
15331518
1519+ /// Returns `true` if the set is a superset of another.
15341520 #[ inline]
1535- fn is_superset ( & self , other : & BitvSet ) -> bool {
1521+ pub fn is_superset ( & self , other : & BitvSet ) -> bool {
15361522 other. is_subset ( self )
15371523 }
1538- }
15391524
1540- impl MutableSet < uint > for BitvSet {
1541- fn insert ( & mut self , value : uint ) -> bool {
1525+ /// Adds a value to the set. Returns `true` if the value was not already
1526+ /// present in the set.
1527+ pub fn insert ( & mut self , value : uint ) -> bool {
15421528 if self . contains ( & value) {
15431529 return false ;
15441530 }
@@ -1554,7 +1540,9 @@ impl MutableSet<uint> for BitvSet {
15541540 return true ;
15551541 }
15561542
1557- fn remove ( & mut self , value : & uint ) -> bool {
1543+ /// Removes a value from the set. Returns `true` if the value was
1544+ /// present in the set.
1545+ pub fn remove ( & mut self , value : & uint ) -> bool {
15581546 if !self . contains ( value) {
15591547 return false ;
15601548 }
@@ -1564,6 +1552,29 @@ impl MutableSet<uint> for BitvSet {
15641552 }
15651553}
15661554
1555+ impl fmt:: Show for BitvSet {
1556+ fn fmt ( & self , fmt : & mut fmt:: Formatter ) -> fmt:: Result {
1557+ try!( write ! ( fmt, "{{" ) ) ;
1558+ let mut first = true ;
1559+ for n in self . iter ( ) {
1560+ if !first {
1561+ try!( write ! ( fmt, ", " ) ) ;
1562+ }
1563+ try!( write ! ( fmt, "{}" , n) ) ;
1564+ first = false ;
1565+ }
1566+ write ! ( fmt, "}}" )
1567+ }
1568+ }
1569+
1570+ impl < S : hash:: Writer > hash:: Hash < S > for BitvSet {
1571+ fn hash ( & self , state : & mut S ) {
1572+ for pos in self . iter ( ) {
1573+ pos. hash ( state) ;
1574+ }
1575+ }
1576+ }
1577+
15671578/// An iterator for `BitvSet`.
15681579pub struct BitPositions < ' a > {
15691580 set : & ' a BitvSet ,
@@ -1643,7 +1654,6 @@ mod tests {
16431654 use std:: rand:: Rng ;
16441655 use test:: Bencher ;
16451656
1646- use { Set , Mutable , MutableSet , MutableSeq } ;
16471657 use bitv:: { Bitv , BitvSet , from_fn, from_bytes} ;
16481658 use bitv;
16491659 use vec:: Vec ;
0 commit comments