// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.
// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Parity. If not, see .
//! Statistical functions and helpers.
use std::iter::FromIterator;
use std::ops::{Add, Sub, Deref, Div};
#[macro_use]
extern crate log;
/// Sorted corpus of data.
#[derive(Debug, Clone, PartialEq)]
pub struct Corpus(Vec);
impl From> for Corpus {
fn from(mut data: Vec) -> Self {
data.sort();
Corpus(data)
}
}
impl FromIterator for Corpus {
fn from_iter>(iterable: I) -> Self {
iterable.into_iter().collect::>().into()
}
}
impl Deref for Corpus {
type Target = [T];
fn deref(&self) -> &[T] { &self.0[..] }
}
impl Corpus {
/// Get the median element, if it exists.
pub fn median(&self) -> Option<&T> {
self.0.get(self.0.len() / 2)
}
/// Whether the corpus is empty.
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
/// Number of elements in the corpus.
pub fn len(&self) -> usize {
self.0.len()
}
}
impl Corpus
where T: Add