mod read;
use std::iter::Iterator;
use crate::statistics::Statistic;
#[derive(Debug, Clone, Default)]
pub struct Sample<T> {
pub data: Vec<T>,
}
impl<T> Sample<T> {
pub fn new(data: Vec<T>) -> Self {
Self { data }
}
pub fn len(&self) -> usize {
self.data.len()
}
pub fn is_empty(&self) -> bool {
self.data.is_empty()
}
pub fn estimate<Output, D>(&self, statistic: impl Statistic<Self, Output>) -> Output
where
Self: AsRef<[T]>,
T: Clone,
{
statistic.compute(&self)
}
}
impl<T> FromIterator<T> for Sample<T> {
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self {
Sample::new(iter.into_iter().collect())
}
}
impl<T> IntoIterator for Sample<T> {
type Item = T;
type IntoIter = std::vec::IntoIter<T>;
fn into_iter(self) -> Self::IntoIter {
self.data.into_iter()
}
}
pub trait SamplingIterator: Iterator {
fn sample(self, n: usize) -> Sample<Self::Item>
where
Self: Sized,
{
self.take(n).collect()
}
}
impl<I: Iterator> SamplingIterator for I {}
impl<T> AsRef<[T]> for Sample<T> {
fn as_ref(&self) -> &[T] { &self.data }
}