@@ -63,18 +63,14 @@ case, but slower in the long term as they do not use any caching of primes.
63
63
#![ doc( html_root_url = "https://wackywendell.github.io/primes/" ) ]
64
64
65
65
use std:: cmp:: Ordering :: { Equal , Greater , Less } ;
66
- #[ warn( non_camel_case_types) ]
67
- #[ warn( non_snake_case) ]
68
- #[ warn( unused_qualifications) ]
69
- #[ warn( non_upper_case_globals) ]
70
- #[ warn( missing_docs) ]
71
66
use std:: ops:: Index ;
72
67
use std:: slice;
73
68
74
69
/** A prime generator, using the Sieve of Eratosthenes.
75
70
76
71
Create with `let mut pset = PrimeSet::new()`, and then use `pset.iter()` to iterate over all primes.
77
72
**/
73
+ #[ derive( Default ) ]
78
74
pub struct PrimeSet {
79
75
lst : Vec < u64 > ,
80
76
}
@@ -98,7 +94,7 @@ impl PrimeSet {
98
94
let mut l: u64 = self . lst [ self . lst . len ( ) - 1 ] + 2 ;
99
95
let mut remainder = 0 ;
100
96
loop {
101
- for & n in self . lst . iter ( ) {
97
+ for & n in & self . lst {
102
98
remainder = l % n;
103
99
if remainder == 0 || n * n > l {
104
100
break ;
@@ -119,13 +115,17 @@ impl PrimeSet {
119
115
self . lst . len ( )
120
116
}
121
117
118
+ pub fn is_empty ( & self ) -> bool {
119
+ false
120
+ }
121
+
122
122
/// Return all primes found so far as a slice
123
- pub fn list < ' a > ( & ' a self ) -> & ' a [ u64 ] {
123
+ pub fn list ( & self ) -> & [ u64 ] {
124
124
& self . lst [ ..]
125
125
}
126
126
127
127
/// Iterator over all primes not yet found
128
- pub fn generator < ' a > ( & ' a mut self ) -> PrimeSetIter < ' a > {
128
+ pub fn generator ( & mut self ) -> PrimeSetIter {
129
129
let myn = self . len ( ) ;
130
130
PrimeSetIter {
131
131
p : self ,
@@ -136,7 +136,7 @@ impl PrimeSet {
136
136
137
137
/// Iterator over all primes, starting with 2. If you don't care about the "state" of the
138
138
/// PrimeSet, this is what you want!
139
- pub fn iter < ' a > ( & ' a mut self ) -> PrimeSetIter < ' a > {
139
+ pub fn iter ( & mut self ) -> PrimeSetIter {
140
140
PrimeSetIter {
141
141
p : self ,
142
142
n : 0 ,
@@ -149,7 +149,7 @@ impl PrimeSet {
149
149
//~ }
150
150
151
151
/// Iterator over just the primes found so far
152
- pub fn iter_vec < ' a > ( & ' a self ) -> slice:: Iter < ' a , u64 > {
152
+ pub fn iter_vec ( & self ) -> slice:: Iter < u64 > {
153
153
self . lst . iter ( )
154
154
}
155
155
@@ -166,6 +166,7 @@ impl PrimeSet {
166
166
/// Check if a number is prime
167
167
/// Note that this only requires primes up to n.sqrt() to be generated, and will generate
168
168
/// them as necessary on its own.
169
+ #[ cfg_attr( feature = "cargo-clippy" , allow( wrong_self_convention) ) ]
169
170
pub fn is_prime ( & mut self , n : u64 ) -> bool {
170
171
if n <= 1 {
171
172
return false ;
@@ -208,7 +209,7 @@ impl PrimeSet {
208
209
}
209
210
lim >>= 1 ;
210
211
}
211
- return Some ( ( base, self . lst [ base] ) ) ;
212
+ Some ( ( base, self . lst [ base] ) )
212
213
}
213
214
214
215
/// Get the nth prime, even if we haven't yet found it
@@ -257,9 +258,10 @@ impl<'a> Iterator for PrimeSetIter<'a> {
257
258
type Item = u64 ;
258
259
fn next ( & mut self ) -> Option < u64 > {
259
260
while self . n >= self . p . len ( ) {
260
- match self . expand {
261
- true => self . p . expand ( ) ,
262
- false => return None ,
261
+ if self . expand {
262
+ self . p . expand ( )
263
+ } else {
264
+ return None ;
263
265
}
264
266
}
265
267
self . n += 1 ;
@@ -282,7 +284,8 @@ fn firstfac(x: u64) -> u64 {
282
284
return n;
283
285
} ;
284
286
}
285
- return x;
287
+ // No factor found. It must be prime.
288
+ x
286
289
}
287
290
288
291
/// Find all prime factors of a number
@@ -302,7 +305,7 @@ pub fn factors(x: u64) -> Vec<u64> {
302
305
curn /= m
303
306
} ;
304
307
}
305
- return lst;
308
+ lst
306
309
}
307
310
308
311
/// Find all unique prime factors of a number
@@ -325,7 +328,7 @@ pub fn factors_uniq(x: u64) -> Vec<u64> {
325
328
break ;
326
329
}
327
330
}
328
- return lst;
331
+ lst
329
332
}
330
333
331
334
/// Test whether a number is prime. Checks every odd number up to sqrt(n).
0 commit comments