1+ extern  crate  bit_vector; 
12extern  crate  num; 
23
34use  std:: cmp; 
45use  std:: env; 
56use  std:: fmt:: Display ; 
67use  std:: str:: FromStr ; 
78
9+ use  bit_vector:: { BitVector , BitSliceMut } ; 
10+ 
811use  num:: Bounded ; 
912
1013macro_rules!  err_exit { 
@@ -22,6 +25,8 @@ macro_rules! err_exit {
2225    ) 
2326} 
2427
28+ type  SieveStorage  = usize ; 
29+ 
2530fn  main ( )  { 
2631    let  requested_threads:  usize  = parse_command_line_argument ( 1 ,  "threads" ) ; 
2732    let  max_prime:  usize  = parse_command_line_argument ( 2 ,  "max_prime" ) ; 
@@ -33,7 +38,7 @@ fn main() {
3338        max_prime :  max_prime
3439    } ; 
3540    println ! ( "{:?}" ,  sieve) ; 
36-     println ! ( "{:?}" ,  sieve. calculate_indices ( std :: mem :: size_of :: < usize > ( )   *   8 ) ) ; 
41+     println ! ( "{:?}" ,  sieve. find_primes ( ) ) ; 
3742} 
3843
3944fn  parse_command_line_argument < T :  FromStr  + Bounded  + Display > ( position :  usize ,  name :  & str )  -> T  { 
@@ -58,7 +63,26 @@ struct Sieve {
5863} 
5964
6065impl  Sieve  { 
61-     fn  calculate_indices ( & self ,  storage_size :  usize )  -> Vec < usize >  { 
66+     fn  find_primes ( & self )  -> SieveResult  { 
67+         let  mut  bit_vector = BitVector :: with_capacity ( self . max_prime  + 1 ,  true ) ; 
68+ 
69+         { 
70+             let  indices = self . calculate_indices ( ) ; 
71+             let  bit_slices = self . split_into_bit_slices ( & mut  bit_vector,  & indices) ; 
72+ 
73+             println ! ( "{:?}" ,  bit_slices) ; 
74+         } 
75+ 
76+         //TODO implement methods to get the bit vector or print its contents on this 
77+         SieveResult  { 
78+             threads :  self . threads , 
79+             max_prime :  self . max_prime , 
80+             bit_vector :  bit_vector
81+         } 
82+     } 
83+ 
84+     fn  calculate_indices ( & self )  -> Vec < usize >  { 
85+         let  storage_size = std:: mem:: size_of :: < SieveStorage > ( )  *  8 ; 
6286        let  numbers_per_segment = ( self . max_prime  + 1 )  / self . threads ; 
6387
6488        let  mut  indices = vec ! [ ] ;         
@@ -81,4 +105,28 @@ impl Sieve {
81105
82106        indices
83107    } 
108+ 
109+     fn  split_into_bit_slices < ' a > ( & self ,  bit_vector :  & ' a  mut  BitVector < SieveStorage > ,  indices :  & [ usize ] )  -> Vec < BitSliceMut < ' a ,  SieveStorage > >  { 
110+         let  mut  bit_slices = vec ! [ ] ; 
111+ 
112+         //TODO request as_bit_slice and as_bit_slice_mut methods? 
113+         bit_slices. push ( bit_vector. split_at_mut ( 0 ) . 1 ) ; 
114+         let  mut  split_indices = 0 ; 
115+         for  index in  indices { 
116+             let  last_slice = bit_slices. pop ( ) . unwrap ( ) ; 
117+             let  ( new_slice,  remainder)  = last_slice. split_at_mut ( index - split_indices) ; 
118+             split_indices = * index; 
119+             bit_slices. push ( new_slice) ; 
120+             bit_slices. push ( remainder) ; 
121+         } 
122+ 
123+         bit_slices
124+     } 
125+ } 
126+ 
127+ #[ derive( Debug ) ]  
128+ struct  SieveResult  { 
129+     threads :  usize , 
130+     max_prime :  usize , 
131+     bit_vector :  BitVector < SieveStorage > 
84132} 
0 commit comments