@@ -267,9 +267,7 @@ where R: Rng + ?Sized {
267
267
/// sometimes be useful to have the indices themselves so this is provided as
268
268
/// an alternative.
269
269
///
270
- /// This implementation uses `O(length + amount)` space and `O(length)` time
271
- /// if the "nightly" feature is enabled, or `O(length)` space and
272
- /// `O(length + amount * log length)` time otherwise.
270
+ /// This implementation uses `O(length + amount)` space and `O(length)` time.
273
271
///
274
272
/// Panics if `amount > length`.
275
273
#[ cfg( feature = "std" ) ]
@@ -300,9 +298,7 @@ where
300
298
///
301
299
/// This implementation uses the algorithm described by Efraimidis and Spirakis
302
300
/// in this paper: https://doi.org/10.1016/j.ipl.2005.11.003
303
- /// It uses `O(length + amount)` space and `O(length)` time if the
304
- /// "nightly" feature is enabled, or `O(length)` space and `O(length
305
- /// + amount * log length)` time otherwise.
301
+ /// It uses `O(length + amount)` space and `O(length)` time.
306
302
///
307
303
/// Panics if `amount > length`.
308
304
#[ cfg( feature = "std" ) ]
@@ -347,63 +343,33 @@ where
347
343
}
348
344
impl < N > Eq for Element < N > { }
349
345
350
- #[ cfg( feature = "nightly" ) ]
351
- {
352
- let mut candidates = Vec :: with_capacity ( length. as_usize ( ) ) ;
353
- let mut index = N :: zero ( ) ;
354
- while index < length {
355
- let weight = weight ( index. as_usize ( ) ) . into ( ) ;
356
- if !( weight >= 0. ) {
357
- return Err ( WeightedError :: InvalidWeight ) ;
358
- }
359
-
360
- let key = rng. gen :: < f64 > ( ) . powf ( 1.0 / weight) ;
361
- candidates. push ( Element { index, key } ) ;
362
-
363
- index += N :: one ( ) ;
346
+ let mut candidates = Vec :: with_capacity ( length. as_usize ( ) ) ;
347
+ let mut index = N :: zero ( ) ;
348
+ while index < length {
349
+ let weight = weight ( index. as_usize ( ) ) . into ( ) ;
350
+ if !( weight >= 0. ) {
351
+ return Err ( WeightedError :: InvalidWeight ) ;
364
352
}
365
353
366
- // Partially sort the array to find the `amount` elements with the greatest
367
- // keys. Do this by using `select_nth_unstable` to put the elements with
368
- // the *smallest* keys at the beginning of the list in `O(n)` time, which
369
- // provides equivalent information about the elements with the *greatest* keys.
370
- let ( _, mid, greater)
371
- = candidates. select_nth_unstable ( length. as_usize ( ) - amount. as_usize ( ) ) ;
372
-
373
- let mut result: Vec < N > = Vec :: with_capacity ( amount. as_usize ( ) ) ;
374
- result. push ( mid. index ) ;
375
- for element in greater {
376
- result. push ( element. index ) ;
377
- }
378
- Ok ( IndexVec :: from ( result) )
379
- }
380
-
381
- #[ cfg( not( feature = "nightly" ) ) ]
382
- {
383
- use alloc:: collections:: BinaryHeap ;
354
+ let key = rng. gen :: < f64 > ( ) . powf ( 1.0 / weight) ;
355
+ candidates. push ( Element { index, key } ) ;
384
356
385
- // Partially sort the array such that the `amount` elements with the largest
386
- // keys are first using a binary max heap.
387
- let mut candidates = BinaryHeap :: with_capacity ( length. as_usize ( ) ) ;
388
- let mut index = N :: zero ( ) ;
389
- while index < length {
390
- let weight = weight ( index. as_usize ( ) ) . into ( ) ;
391
- if !( weight >= 0. ) {
392
- return Err ( WeightedError :: InvalidWeight ) ;
393
- }
357
+ index += N :: one ( ) ;
358
+ }
394
359
395
- let key = rng. gen :: < f64 > ( ) . powf ( 1.0 / weight) ;
396
- candidates. push ( Element { index, key } ) ;
360
+ // Partially sort the array to find the `amount` elements with the greatest
361
+ // keys. Do this by using `select_nth_unstable` to put the elements with
362
+ // the *smallest* keys at the beginning of the list in `O(n)` time, which
363
+ // provides equivalent information about the elements with the *greatest* keys.
364
+ let ( _, mid, greater)
365
+ = candidates. select_nth_unstable ( length. as_usize ( ) - amount. as_usize ( ) ) ;
397
366
398
- index += N :: one ( ) ;
399
- }
400
-
401
- let mut result: Vec < N > = Vec :: with_capacity ( amount. as_usize ( ) ) ;
402
- while result. len ( ) < amount. as_usize ( ) {
403
- result. push ( candidates. pop ( ) . unwrap ( ) . index ) ;
404
- }
405
- Ok ( IndexVec :: from ( result) )
367
+ let mut result: Vec < N > = Vec :: with_capacity ( amount. as_usize ( ) ) ;
368
+ result. push ( mid. index ) ;
369
+ for element in greater {
370
+ result. push ( element. index ) ;
406
371
}
372
+ Ok ( IndexVec :: from ( result) )
407
373
}
408
374
409
375
/// Randomly sample exactly `amount` indices from `0..length`, using Floyd's
0 commit comments