Skip to content

Commit

Permalink
Feature gate choose_multiple_weighted on std
Browse files Browse the repository at this point in the history
  • Loading branch information
dhardy committed Jan 25, 2021
1 parent 22dec87 commit fa615ef
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
11 changes: 7 additions & 4 deletions src/seq/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ use alloc::collections::BTreeSet;
#[cfg(feature = "std")] use std::collections::HashSet;

#[cfg(feature = "alloc")]
use crate::distributions::{uniform::SampleUniform, Distribution, Uniform, WeightedError};
use crate::distributions::{uniform::SampleUniform, Distribution, Uniform};
#[cfg(feature = "std")]
use crate::distributions::WeightedError;
use crate::Rng;

#[cfg(feature = "serde1")]
Expand Down Expand Up @@ -270,6 +272,8 @@ where R: Rng + ?Sized {
/// `O(length + amount * log length)` time otherwise.
///
/// Panics if `amount > length`.
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
pub fn sample_weighted<R, F, X>(
rng: &mut R, length: usize, weight: F, amount: usize,
) -> Result<IndexVec, WeightedError>
Expand Down Expand Up @@ -301,6 +305,7 @@ where
/// + amount * log length)` time otherwise.
///
/// Panics if `amount > length`.
#[cfg(feature = "std")]
fn sample_efraimidis_spirakis<R, F, X, N>(
rng: &mut R, length: N, weight: F, amount: N,
) -> Result<IndexVec, WeightedError>
Expand Down Expand Up @@ -375,9 +380,6 @@ where

#[cfg(not(feature = "nightly"))]
{
#[cfg(all(feature = "alloc", not(feature = "std")))]
use crate::alloc::collections::BinaryHeap;
#[cfg(feature = "std")]
use std::collections::BinaryHeap;

// Partially sort the array such that the `amount` elements with the largest
Expand Down Expand Up @@ -619,6 +621,7 @@ mod test {
assert_eq!(v1, v2);
}

#[cfg(feature = "std")]
#[test]
fn test_sample_weighted() {
let seed_rng = crate::test::rng;
Expand Down
12 changes: 8 additions & 4 deletions src/seq/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,11 @@ pub trait SliceRandom {
/// println!("{:?}", choices.choose_multiple_weighted(&mut rng, 2, |item| item.1).unwrap().collect::<Vec<_>>());
/// ```
/// [`choose_multiple`]: SliceRandom::choose_multiple
#[cfg(feature = "alloc")]
//
// Note: this is feature-gated on std due to usage of f64::powf.
// If necessary, we may use alloc+libm as an alternative (see PR #1089).
#[cfg(feature = "std")]
#[cfg_attr(doc_cfg, doc(cfg(feature = "std")))]
fn choose_multiple_weighted<R, F, X>(
&self, rng: &mut R, amount: usize, weight: F,
) -> Result<SliceChooseIter<Self, Self::Item>, WeightedError>
Expand Down Expand Up @@ -556,7 +560,7 @@ impl<T> SliceRandom for [T] {
Ok(&mut self[distr.sample(rng)])
}

#[cfg(feature = "alloc")]
#[cfg(feature = "std")]
fn choose_multiple_weighted<R, F, X>(
&self, rng: &mut R, amount: usize, weight: F,
) -> Result<SliceChooseIter<Self, Self::Item>, WeightedError>
Expand Down Expand Up @@ -1228,7 +1232,7 @@ mod test {
}

#[test]
#[cfg(feature = "alloc")]
#[cfg(feature = "std")]
fn test_multiple_weighted_edge_cases() {
use super::*;

Expand Down Expand Up @@ -1308,7 +1312,7 @@ mod test {
}

#[test]
#[cfg(feature = "alloc")]
#[cfg(feature = "std")]
fn test_multiple_weighted_distributions() {
use super::*;

Expand Down

0 comments on commit fa615ef

Please sign in to comment.