<commit_before>use {Distribution, Generator};

/// A categorical distribution.
#[derive(Clone)]
pub struct Categorical {
    /// The size of the probabilty vector.
    pub k: usize,
    /// The probability vector.
    pub p: Vec<f64>,
}

fn is_prob_vec(p: &[f64]) -> bool {
    let mut sum = 0.;
    for &p_i in p.iter() {
        if p_i < 0.|| p_i > 1. { return false; }
        sum += p_i;
    }
    (sum - 1.).abs() <= 1e-12
}

impl Categorical {
    /// Create a Categorical distribution with success probability `p`.
    ///
    /// # Panics
    ///
    /// Panics if `p < 0` or `p > 1`.
    #[inline]
    pub fn new(p: &[f64]) -> Categorical {
        debug_assert!(is_prob_vec(p), "Categorical::new() is called with p not a probabilty vector");
        Categorical { k: p.len(),  p: p.to_vec()}
    }
}<commit_msg>Moved the probability vector check inside the debug scope<commit_after>use {Distribution, Generator};

/// A categorical distribution.
#[derive(Clone)]
pub struct Categorical {
    /// The size of the probabilty vector.
    pub k: usize,
    /// The probability vector.
    pub p: Vec<f64>,
}

impl Categorical {
    /// Create a categorical distribution with success probability `p`.
    ///
    /// # Panics
    ///
    /// Panics if `p[i] < 0` or `p[i] > 1` for any `i = 0..k`, or if
    /// `sum(p) != 1`.
    #[inline]
    pub fn new(p: &[f64]) -> Categorical {
        debug_assert!({
            fn is_prob_vec(p: &[f64]) -> bool {
                // Check if p is a probability vector.
                let mut sum = 0.;
                for &p_i in p.iter() {
                    if p_i < 0.|| p_i > 1. { return false; }
                    sum += p_i;
                }
                (sum - 1.).abs() <= 1e-12
            }
            is_prob_vec(&p)
        }, "Categorical::new() is called with p not a probabilty vector");

        Categorical { k: p.len(),  p: p.to_vec()}
    }
}
{'fraction_non_alphanumeric': 0.15709812108559498, 'fraction_numerical': 0.016701461377870562, 'mean_word_length': 1.7622478386167146, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4418705', 'n_tokens_mistral': 677, 'n_tokens_neox': 646, 'n_words': 200}