use super::DataError;
#[derive(Clone, Debug, PartialEq)]
pub struct SampleWeights {
values: Vec<f32>,
total: f64,
}
impl SampleWeights {
pub fn new(values: Vec<f32>) -> Result<Self, DataError> {
if values.is_empty() {
return Err(DataError::EmptySampleWeights);
}
let mut total = 0.0_f64;
for (index, &value) in values.iter().enumerate() {
if !value.is_finite() {
return Err(DataError::NonFiniteSampleWeight { index });
}
if value < 0.0 {
return Err(DataError::NegativeSampleWeight { index });
}
total += f64::from(value);
}
if total <= 0.0 {
return Err(DataError::ZeroTotalSampleWeight);
}
Ok(Self { values, total })
}
#[inline]
pub fn len(&self) -> usize {
self.values.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.values.is_empty()
}
#[inline]
pub fn as_slice(&self) -> &[f32] {
&self.values
}
#[inline]
pub fn get(&self, index: usize) -> Option<f32> {
self.values.get(index).copied()
}
#[inline]
pub const fn total(&self) -> f64 {
self.total
}
#[inline]
pub fn into_values(self) -> Vec<f32> {
self.values
}
}