@@ -267,9 +267,7 @@ where R: Rng + ?Sized {
267267/// sometimes be useful to have the indices themselves so this is provided as
268268/// an alternative.
269269///
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.
273271///
274272/// Panics if `amount > length`.
275273#[ cfg( feature = "std" ) ]
@@ -300,9 +298,7 @@ where
300298///
301299/// This implementation uses the algorithm described by Efraimidis and Spirakis
302300/// 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.
306302///
307303/// Panics if `amount > length`.
308304#[ cfg( feature = "std" ) ]
@@ -347,63 +343,33 @@ where
347343 }
348344 impl < N > Eq for Element < N > { }
349345
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 ) ;
364352 }
365353
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 } ) ;
384356
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+ }
394359
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 ( ) ) ;
397366
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 ) ;
406371 }
372+ Ok ( IndexVec :: from ( result) )
407373}
408374
409375/// Randomly sample exactly `amount` indices from `0..length`, using Floyd's
0 commit comments