// Copyright 2015-2019 Parity Technologies (UK) Ltd.
// This file is part of Parity Ethereum.
// Parity Ethereum 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 Ethereum 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 Ethereum. 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 given percentile (approximated).
pub fn percentile(&self, val: usize) -> Option<&T> {
let len = self.0.len();
let x = val * len / 100;
let x = ::std::cmp::min(x, len);
if x == 0 {
return None;
}
self.0.get(x - 1)
}
/// 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