Skip to content

Commit

Permalink
auto merge of #19050 : japaric/rust/moar-dst, r=aturon
Browse files Browse the repository at this point in the history
  • Loading branch information
bors committed Nov 18, 2014
2 parents 9c96a79 + d50e80f commit f637f1c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/librustc/middle/trans/cabi_x86_64.rs
Expand Up @@ -62,12 +62,12 @@ impl RegClass {
}
}

trait ClassList {
trait ClassList for Sized? {
fn is_pass_byval(&self) -> bool;
fn is_ret_bysret(&self) -> bool;
}

impl<'a> ClassList for &'a [RegClass] {
impl ClassList for [RegClass] {
fn is_pass_byval(&self) -> bool {
if self.len() == 0 { return false; }

Expand Down
4 changes: 2 additions & 2 deletions src/librustc/middle/trans/llrepr.rs
Expand Up @@ -12,11 +12,11 @@ use middle::trans::context::CrateContext;
use middle::trans::type_::Type;
use llvm::ValueRef;

pub trait LlvmRepr {
pub trait LlvmRepr for Sized? {
fn llrepr(&self, ccx: &CrateContext) -> String;
}

impl<'a, T:LlvmRepr> LlvmRepr for &'a [T] {
impl<T:LlvmRepr> LlvmRepr for [T] {
fn llrepr(&self, ccx: &CrateContext) -> String {
let reprs: Vec<String> = self.iter().map(|t| t.llrepr(ccx)).collect();
format!("[{}]", reprs.connect(","))
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/util/ppaux.rs
Expand Up @@ -37,7 +37,7 @@ use syntax::{ast, ast_util};
use syntax::owned_slice::OwnedSlice;

/// Produces a string suitable for debugging output.
pub trait Repr {
pub trait Repr for Sized? {
fn repr(&self, tcx: &ctxt) -> String;
}

Expand Down Expand Up @@ -578,9 +578,9 @@ impl Repr for () {
}
}

impl<'a,T:Repr> Repr for &'a T {
impl<'a, Sized? T:Repr> Repr for &'a T {
fn repr(&self, tcx: &ctxt) -> String {
(&**self).repr(tcx)
Repr::repr(*self, tcx)
}
}

Expand All @@ -600,9 +600,9 @@ fn repr_vec<T:Repr>(tcx: &ctxt, v: &[T]) -> String {
vec_map_to_string(v, |t| t.repr(tcx))
}

impl<'a, T:Repr> Repr for &'a [T] {
impl<T:Repr> Repr for [T] {
fn repr(&self, tcx: &ctxt) -> String {
repr_vec(tcx, *self)
repr_vec(tcx, self)
}
}

Expand Down
18 changes: 9 additions & 9 deletions src/libsyntax/ext/quote.rs
Expand Up @@ -90,14 +90,14 @@ pub mod rt {
*/

// FIXME: Move this trait to pprust and get rid of *_to_str?
pub trait ToSource {
pub trait ToSource for Sized? {
// Takes a thing and generates a string containing rust code for it.
fn to_source(&self) -> String;
}

// FIXME (Issue #16472): This should go away after ToToken impls
// are revised to go directly to token-trees.
trait ToSourceWithHygiene : ToSource {
trait ToSourceWithHygiene for Sized? : ToSource {
// Takes a thing and generates a string containing rust code
// for it, encoding Idents as special byte sequences to
// maintain hygiene across serialization and deserialization.
Expand Down Expand Up @@ -150,15 +150,15 @@ pub mod rt {

macro_rules! impl_to_source_slice(
($t:ty, $sep:expr) => (
impl<'a> ToSource for &'a [$t] {
impl ToSource for [$t] {
fn to_source(&self) -> String {
slice_to_source($sep, *self)
slice_to_source($sep, self)
}
}

impl<'a> ToSourceWithHygiene for &'a [$t] {
impl ToSourceWithHygiene for [$t] {
fn to_source_with_hygiene(&self) -> String {
slice_to_source_with_hygiene($sep, *self)
slice_to_source_with_hygiene($sep, self)
}
}
)
Expand Down Expand Up @@ -200,14 +200,14 @@ pub mod rt {
}
}

impl<'a> ToSource for &'a str {
impl ToSource for str {
fn to_source(&self) -> String {
let lit = dummy_spanned(ast::LitStr(
token::intern_and_get_ident(*self), ast::CookedStr));
token::intern_and_get_ident(self), ast::CookedStr));
pprust::lit_to_string(&lit)
}
}
impl<'a> ToSourceWithHygiene for &'a str {
impl ToSourceWithHygiene for str {
fn to_source_with_hygiene(&self) -> String {
self.to_source()
}
Expand Down
56 changes: 28 additions & 28 deletions src/libtest/stats.rs
Expand Up @@ -38,7 +38,7 @@ fn local_sort<T: Float>(v: &mut [T]) {
}

/// Trait that provides simple descriptive statistics on a univariate set of numeric samples.
pub trait Stats <T: FloatMath + FromPrimitive>{
pub trait Stats <T: FloatMath + FromPrimitive> for Sized? {

/// Sum of the samples.
///
Expand All @@ -47,24 +47,24 @@ pub trait Stats <T: FloatMath + FromPrimitive>{
/// ["Adaptive Precision Floating-Point Arithmetic and Fast Robust Geometric Predicates"]
/// (http://www.cs.cmu.edu/~quake-papers/robust-arithmetic.ps)
/// *Discrete & Computational Geometry 18*, 3 (Oct 1997), 305-363, Shewchuk J.R.
fn sum(self) -> T;
fn sum(&self) -> T;

/// Minimum value of the samples.
fn min(self) -> T;
fn min(&self) -> T;

/// Maximum value of the samples.
fn max(self) -> T;
fn max(&self) -> T;

/// Arithmetic mean (average) of the samples: sum divided by sample-count.
///
/// See: https://en.wikipedia.org/wiki/Arithmetic_mean
fn mean(self) -> T;
fn mean(&self) -> T;

/// Median of the samples: value separating the lower half of the samples from the higher half.
/// Equal to `self.percentile(50.0)`.
///
/// See: https://en.wikipedia.org/wiki/Median
fn median(self) -> T;
fn median(&self) -> T;

/// Variance of the samples: bias-corrected mean of the squares of the differences of each
/// sample from the sample mean. Note that this calculates the _sample variance_ rather than the
Expand All @@ -73,21 +73,21 @@ pub trait Stats <T: FloatMath + FromPrimitive>{
/// than `n`.
///
/// See: https://en.wikipedia.org/wiki/Variance
fn var(self) -> T;
fn var(&self) -> T;

/// Standard deviation: the square root of the sample variance.
///
/// Note: this is not a robust statistic for non-normal distributions. Prefer the
/// `median_abs_dev` for unknown distributions.
///
/// See: https://en.wikipedia.org/wiki/Standard_deviation
fn std_dev(self) -> T;
fn std_dev(&self) -> T;

/// Standard deviation as a percent of the mean value. See `std_dev` and `mean`.
///
/// Note: this is not a robust statistic for non-normal distributions. Prefer the
/// `median_abs_dev_pct` for unknown distributions.
fn std_dev_pct(self) -> T;
fn std_dev_pct(&self) -> T;

/// Scaled median of the absolute deviations of each sample from the sample median. This is a
/// robust (distribution-agnostic) estimator of sample variability. Use this in preference to
Expand All @@ -96,10 +96,10 @@ pub trait Stats <T: FloatMath + FromPrimitive>{
/// deviation.
///
/// See: http://en.wikipedia.org/wiki/Median_absolute_deviation
fn median_abs_dev(self) -> T;
fn median_abs_dev(&self) -> T;

/// Median absolute deviation as a percent of the median. See `median_abs_dev` and `median`.
fn median_abs_dev_pct(self) -> T;
fn median_abs_dev_pct(&self) -> T;

/// Percentile: the value below which `pct` percent of the values in `self` fall. For example,
/// percentile(95.0) will return the value `v` such that 95% of the samples `s` in `self`
Expand All @@ -108,21 +108,21 @@ pub trait Stats <T: FloatMath + FromPrimitive>{
/// Calculated by linear interpolation between closest ranks.
///
/// See: http://en.wikipedia.org/wiki/Percentile
fn percentile(self, pct: T) -> T;
fn percentile(&self, pct: T) -> T;

/// Quartiles of the sample: three values that divide the sample into four equal groups, each
/// with 1/4 of the data. The middle value is the median. See `median` and `percentile`. This
/// function may calculate the 3 quartiles more efficiently than 3 calls to `percentile`, but
/// is otherwise equivalent.
///
/// See also: https://en.wikipedia.org/wiki/Quartile
fn quartiles(self) -> (T,T,T);
fn quartiles(&self) -> (T,T,T);

/// Inter-quartile range: the difference between the 25th percentile (1st quartile) and the 75th
/// percentile (3rd quartile). See `quartiles`.
///
/// See also: https://en.wikipedia.org/wiki/Interquartile_range
fn iqr(self) -> T;
fn iqr(&self) -> T;
}

/// Extracted collection of all the summary statistics of a sample set.
Expand Down Expand Up @@ -163,9 +163,9 @@ impl<T: FloatMath + FromPrimitive> Summary<T> {
}
}

impl<'a, T: FloatMath + FromPrimitive> Stats<T> for &'a [T] {
impl<T: FloatMath + FromPrimitive> Stats<T> for [T] {
// FIXME #11059 handle NaN, inf and overflow
fn sum(self) -> T {
fn sum(&self) -> T {
let mut partials = vec![];

for &mut x in self.iter() {
Expand Down Expand Up @@ -198,26 +198,26 @@ impl<'a, T: FloatMath + FromPrimitive> Stats<T> for &'a [T] {
partials.iter().fold(zero, |p, q| p + *q)
}

fn min(self) -> T {
fn min(&self) -> T {
assert!(self.len() != 0);
self.iter().fold(self[0], |p, q| p.min(*q))
}

fn max(self) -> T {
fn max(&self) -> T {
assert!(self.len() != 0);
self.iter().fold(self[0], |p, q| p.max(*q))
}

fn mean(self) -> T {
fn mean(&self) -> T {
assert!(self.len() != 0);
self.sum() / FromPrimitive::from_uint(self.len()).unwrap()
}

fn median(self) -> T {
fn median(&self) -> T {
self.percentile(FromPrimitive::from_uint(50).unwrap())
}

fn var(self) -> T {
fn var(&self) -> T {
if self.len() < 2 {
Float::zero()
} else {
Expand All @@ -235,16 +235,16 @@ impl<'a, T: FloatMath + FromPrimitive> Stats<T> for &'a [T] {
}
}

fn std_dev(self) -> T {
fn std_dev(&self) -> T {
self.var().sqrt()
}

fn std_dev_pct(self) -> T {
fn std_dev_pct(&self) -> T {
let hundred = FromPrimitive::from_uint(100).unwrap();
(self.std_dev() / self.mean()) * hundred
}

fn median_abs_dev(self) -> T {
fn median_abs_dev(&self) -> T {
let med = self.median();
let abs_devs: Vec<T> = self.iter().map(|&v| (med - v).abs()).collect();
// This constant is derived by smarter statistics brains than me, but it is
Expand All @@ -253,18 +253,18 @@ impl<'a, T: FloatMath + FromPrimitive> Stats<T> for &'a [T] {
abs_devs.as_slice().median() * number
}

fn median_abs_dev_pct(self) -> T {
fn median_abs_dev_pct(&self) -> T {
let hundred = FromPrimitive::from_uint(100).unwrap();
(self.median_abs_dev() / self.median()) * hundred
}

fn percentile(self, pct: T) -> T {
fn percentile(&self, pct: T) -> T {
let mut tmp = self.to_vec();
local_sort(tmp.as_mut_slice());
percentile_of_sorted(tmp.as_slice(), pct)
}

fn quartiles(self) -> (T,T,T) {
fn quartiles(&self) -> (T,T,T) {
let mut tmp = self.to_vec();
local_sort(tmp.as_mut_slice());
let first = FromPrimitive::from_uint(25).unwrap();
Expand All @@ -276,7 +276,7 @@ impl<'a, T: FloatMath + FromPrimitive> Stats<T> for &'a [T] {
(a,b,c)
}

fn iqr(self) -> T {
fn iqr(&self) -> T {
let (a,_,c) = self.quartiles();
c - a
}
Expand Down

0 comments on commit f637f1c

Please sign in to comment.