@@ -1555,7 +1555,7 @@ impl BitSet {
1555
1555
#[ inline]
1556
1556
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1557
1557
pub fn iter ( & self ) -> bit_set:: Iter {
1558
- SetIter { set : self , next_idx : 0 }
1558
+ SetIter ( BlockIter :: from_blocks ( self . bit_vec . blocks ( ) ) )
1559
1559
}
1560
1560
1561
1561
/// Iterator over each usize stored in `self` union `other`.
@@ -1580,13 +1580,11 @@ impl BitSet {
1580
1580
pub fn union < ' a > ( & ' a self , other : & ' a BitSet ) -> Union < ' a > {
1581
1581
fn or ( w1 : u32 , w2 : u32 ) -> u32 { w1 | w2 }
1582
1582
1583
- Union ( TwoBitPositions {
1584
- set : self ,
1585
- other : other,
1583
+ Union ( BlockIter :: from_blocks ( TwoBitPositions {
1584
+ set : self . bit_vec . blocks ( ) ,
1585
+ other : other. bit_vec . blocks ( ) ,
1586
1586
merge : or,
1587
- current_word : 0 ,
1588
- next_idx : 0
1589
- } )
1587
+ } ) )
1590
1588
}
1591
1589
1592
1590
/// Iterator over each usize stored in `self` intersect `other`.
@@ -1611,13 +1609,12 @@ impl BitSet {
1611
1609
pub fn intersection < ' a > ( & ' a self , other : & ' a BitSet ) -> Intersection < ' a > {
1612
1610
fn bitand ( w1 : u32 , w2 : u32 ) -> u32 { w1 & w2 }
1613
1611
let min = cmp:: min ( self . bit_vec . len ( ) , other. bit_vec . len ( ) ) ;
1614
- Intersection ( TwoBitPositions {
1615
- set : self ,
1616
- other : other,
1612
+
1613
+ Intersection ( BlockIter :: from_blocks ( TwoBitPositions {
1614
+ set : self . bit_vec . blocks ( ) ,
1615
+ other : other. bit_vec . blocks ( ) ,
1617
1616
merge : bitand,
1618
- current_word : 0 ,
1619
- next_idx : 0
1620
- } . take ( min) )
1617
+ } ) . take ( min) )
1621
1618
}
1622
1619
1623
1620
/// Iterator over each usize stored in the `self` setminus `other`.
@@ -1649,13 +1646,11 @@ impl BitSet {
1649
1646
pub fn difference < ' a > ( & ' a self , other : & ' a BitSet ) -> Difference < ' a > {
1650
1647
fn diff ( w1 : u32 , w2 : u32 ) -> u32 { w1 & !w2 }
1651
1648
1652
- Difference ( TwoBitPositions {
1653
- set : self ,
1654
- other : other,
1649
+ Difference ( BlockIter :: from_blocks ( TwoBitPositions {
1650
+ set : self . bit_vec . blocks ( ) ,
1651
+ other : other. bit_vec . blocks ( ) ,
1655
1652
merge : diff,
1656
- current_word : 0 ,
1657
- next_idx : 0
1658
- } )
1653
+ } ) )
1659
1654
}
1660
1655
1661
1656
/// Iterator over each usize stored in the symmetric difference of `self` and `other`.
@@ -1681,13 +1676,11 @@ impl BitSet {
1681
1676
pub fn symmetric_difference < ' a > ( & ' a self , other : & ' a BitSet ) -> SymmetricDifference < ' a > {
1682
1677
fn bitxor ( w1 : u32 , w2 : u32 ) -> u32 { w1 ^ w2 }
1683
1678
1684
- SymmetricDifference ( TwoBitPositions {
1685
- set : self ,
1686
- other : other,
1679
+ SymmetricDifference ( BlockIter :: from_blocks ( TwoBitPositions {
1680
+ set : self . bit_vec . blocks ( ) ,
1681
+ other : other. bit_vec . blocks ( ) ,
1687
1682
merge : bitxor,
1688
- current_word : 0 ,
1689
- next_idx : 0
1690
- } )
1683
+ } ) )
1691
1684
}
1692
1685
1693
1686
/// Unions in-place with the specified other bit vector.
@@ -1994,98 +1987,114 @@ impl hash::Hash for BitSet {
1994
1987
}
1995
1988
}
1996
1989
1997
- /// An iterator for `BitSet`.
1998
1990
#[ derive( Clone ) ]
1999
1991
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2000
- pub struct SetIter < ' a > {
2001
- set : & ' a BitSet ,
2002
- next_idx : usize
1992
+ struct BlockIter < T > where T : Iterator < Item =u32 > {
1993
+ head : u32 ,
1994
+ head_offset : usize ,
1995
+ tail : T ,
1996
+ }
1997
+
1998
+ impl < ' a , T > BlockIter < T > where T : Iterator < Item =u32 > {
1999
+ fn from_blocks ( mut blocks : T ) -> BlockIter < T > {
2000
+ let h = blocks. next ( ) . unwrap_or ( 0 ) ;
2001
+ BlockIter { tail : blocks, head : h, head_offset : 0 }
2002
+ }
2003
2003
}
2004
2004
2005
2005
/// An iterator combining two `BitSet` iterators.
2006
2006
#[ derive( Clone ) ]
2007
2007
struct TwoBitPositions < ' a > {
2008
- set : & ' a BitSet ,
2009
- other : & ' a BitSet ,
2008
+ set : Blocks < ' a > ,
2009
+ other : Blocks < ' a > ,
2010
2010
merge : fn ( u32 , u32 ) -> u32 ,
2011
- current_word : u32 ,
2012
- next_idx : usize
2013
2011
}
2014
2012
2013
+ /// An iterator for `BitSet`.
2014
+ #[ derive( Clone ) ]
2015
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2016
+ pub struct SetIter < ' a > ( BlockIter < Blocks < ' a > > ) ;
2015
2017
#[ derive( Clone ) ]
2016
2018
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2017
- pub struct Union < ' a > ( TwoBitPositions < ' a > ) ;
2019
+ pub struct Union < ' a > ( BlockIter < TwoBitPositions < ' a > > ) ;
2018
2020
#[ derive( Clone ) ]
2019
2021
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2020
- pub struct Intersection < ' a > ( Take < TwoBitPositions < ' a > > ) ;
2022
+ pub struct Intersection < ' a > ( Take < BlockIter < TwoBitPositions < ' a > > > ) ;
2021
2023
#[ derive( Clone ) ]
2022
2024
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2023
- pub struct Difference < ' a > ( TwoBitPositions < ' a > ) ;
2025
+ pub struct Difference < ' a > ( BlockIter < TwoBitPositions < ' a > > ) ;
2024
2026
#[ derive( Clone ) ]
2025
2027
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2026
- pub struct SymmetricDifference < ' a > ( TwoBitPositions < ' a > ) ;
2028
+ pub struct SymmetricDifference < ' a > ( BlockIter < TwoBitPositions < ' a > > ) ;
2027
2029
2028
2030
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2029
- impl < ' a > Iterator for SetIter < ' a > {
2031
+ impl < ' a , T > Iterator for BlockIter < T > where T : Iterator < Item = u32 > {
2030
2032
type Item = usize ;
2031
2033
2032
2034
fn next ( & mut self ) -> Option < usize > {
2033
- while self . next_idx < self . set . bit_vec . len ( ) {
2034
- let idx = self . next_idx ;
2035
- self . next_idx += 1 ;
2036
-
2037
- if self . set . contains ( & idx) {
2038
- return Some ( idx) ;
2035
+ while self . head == 0 {
2036
+ match self . tail . next ( ) {
2037
+ Some ( w) => self . head = w,
2038
+ None => return None
2039
2039
}
2040
+ self . head_offset += u32:: BITS ;
2040
2041
}
2041
2042
2042
- return None ;
2043
+ // from the current block, isolate the
2044
+ // LSB and subtract 1, producing k:
2045
+ // a block with a number of set bits
2046
+ // equal to the index of the LSB
2047
+ let k = ( self . head & ( !self . head + 1 ) ) - 1 ;
2048
+ // update block, removing the LSB
2049
+ self . head &= self . head - 1 ;
2050
+ // return offset + (index of LSB)
2051
+ Some ( self . head_offset + ( u32:: count_ones ( k) as usize ) )
2043
2052
}
2044
2053
2045
2054
#[ inline]
2046
2055
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2047
- ( 0 , Some ( self . set . bit_vec . len ( ) - self . next_idx ) )
2056
+ match self . tail . size_hint ( ) {
2057
+ ( _, Some ( h) ) => ( 0 , Some ( 1 + h * ( u32:: BITS as usize ) ) ) ,
2058
+ _ => ( 0 , None )
2059
+ }
2048
2060
}
2049
2061
}
2050
2062
2051
2063
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2052
2064
impl < ' a > Iterator for TwoBitPositions < ' a > {
2053
- type Item = usize ;
2054
-
2055
- fn next ( & mut self ) -> Option < usize > {
2056
- while self . next_idx < self . set . bit_vec . len ( ) ||
2057
- self . next_idx < self . other . bit_vec . len ( ) {
2058
- let bit_idx = self . next_idx % u32:: BITS ;
2059
- if bit_idx == 0 {
2060
- let s_bit_vec = & self . set . bit_vec ;
2061
- let o_bit_vec = & self . other . bit_vec ;
2062
- // Merging the two words is a bit of an awkward dance since
2063
- // one BitVec might be longer than the other
2064
- let word_idx = self . next_idx / u32:: BITS ;
2065
- let w1 = if word_idx < s_bit_vec. storage . len ( ) {
2066
- s_bit_vec. storage [ word_idx]
2067
- } else { 0 } ;
2068
- let w2 = if word_idx < o_bit_vec. storage . len ( ) {
2069
- o_bit_vec. storage [ word_idx]
2070
- } else { 0 } ;
2071
- self . current_word = ( self . merge ) ( w1, w2) ;
2072
- }
2073
-
2074
- self . next_idx += 1 ;
2075
- if self . current_word & ( 1 << bit_idx) != 0 {
2076
- return Some ( self . next_idx - 1 ) ;
2077
- }
2065
+ type Item = u32 ;
2066
+
2067
+ fn next ( & mut self ) -> Option < u32 > {
2068
+ match ( self . set . next ( ) , self . other . next ( ) ) {
2069
+ ( Some ( a) , Some ( b) ) => Some ( ( self . merge ) ( a, b) ) ,
2070
+ ( Some ( a) , None ) => Some ( ( self . merge ) ( a, 0 ) ) ,
2071
+ ( None , Some ( b) ) => Some ( ( self . merge ) ( 0 , b) ) ,
2072
+ _ => return None
2078
2073
}
2079
- return None ;
2080
2074
}
2081
2075
2082
2076
#[ inline]
2083
2077
fn size_hint ( & self ) -> ( usize , Option < usize > ) {
2084
- let cap = cmp:: max ( self . set . bit_vec . len ( ) , self . other . bit_vec . len ( ) ) ;
2085
- ( 0 , Some ( cap - self . next_idx ) )
2078
+ let ( a, au) = self . set . size_hint ( ) ;
2079
+ let ( b, bu) = self . other . size_hint ( ) ;
2080
+
2081
+ let upper = match ( au, bu) {
2082
+ ( Some ( au) , Some ( bu) ) => Some ( cmp:: max ( au, bu) ) ,
2083
+ _ => None
2084
+ } ;
2085
+
2086
+ ( cmp:: max ( a, b) , upper)
2086
2087
}
2087
2088
}
2088
2089
2090
+ #[ stable( feature = "rust1" , since = "1.0.0" ) ]
2091
+ impl < ' a > Iterator for SetIter < ' a > {
2092
+ type Item = usize ;
2093
+
2094
+ #[ inline] fn next ( & mut self ) -> Option < usize > { self . 0 . next ( ) }
2095
+ #[ inline] fn size_hint ( & self ) -> ( usize , Option < usize > ) { self . 0 . size_hint ( ) }
2096
+ }
2097
+
2089
2098
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
2090
2099
impl < ' a > Iterator for Union < ' a > {
2091
2100
type Item = usize ;
0 commit comments