diff --git a/src/libcore/char.rs b/src/libcore/char.rs index 8403c6216082a..b656879e7a422 100644 --- a/src/libcore/char.rs +++ b/src/libcore/char.rs @@ -64,6 +64,7 @@ pub use is_XID_continue = unicode::derived_property::XID_Continue; * Indicates whether a character is in lower case, defined * in terms of the Unicode General Category 'Ll' */ +#[inline(always)] pub pure fn is_lowercase(c: char) -> bool { return unicode::general_category::Ll(c); } @@ -72,6 +73,7 @@ pub pure fn is_lowercase(c: char) -> bool { * Indicates whether a character is in upper case, defined * in terms of the Unicode General Category 'Lu'. */ +#[inline(always)] pub pure fn is_uppercase(c: char) -> bool { return unicode::general_category::Lu(c); } @@ -81,6 +83,7 @@ pub pure fn is_uppercase(c: char) -> bool { * terms of the Unicode General Categories 'Zs', 'Zl', 'Zp' * additional 'Cc'-category control codes in the range [0x09, 0x0d] */ +#[inline(always)] pub pure fn is_whitespace(c: char) -> bool { return ('\x09' <= c && c <= '\x0d') || unicode::general_category::Zs(c) @@ -93,6 +96,7 @@ pub pure fn is_whitespace(c: char) -> bool { * defined in terms of the Unicode General Categories 'Nd', 'Nl', 'No' * and the Derived Core Property 'Alphabetic'. */ +#[inline(always)] pub pure fn is_alphanumeric(c: char) -> bool { return unicode::derived_property::Alphabetic(c) || unicode::general_category::Nd(c) || @@ -101,11 +105,13 @@ pub pure fn is_alphanumeric(c: char) -> bool { } /// Indicates whether the character is an ASCII character +#[inline(always)] pub pure fn is_ascii(c: char) -> bool { c - ('\x7F' & c) == '\x00' } /// Indicates whether the character is numeric (Nd, Nl, or No) +#[inline(always)] pub pure fn is_digit(c: char) -> bool { return unicode::general_category::Nd(c) || unicode::general_category::Nl(c) || @@ -122,6 +128,7 @@ pub pure fn is_digit(c: char) -> bool { * 'b' or 'B', 11, etc. Returns none if the char does not * refer to a digit in the given radix. */ +#[inline] pub pure fn to_digit(c: char, radix: uint) -> Option { let val = match c { '0' .. '9' => c as uint - ('0' as uint), @@ -190,6 +197,7 @@ pub pure fn escape_default(c: char) -> ~str { * * -1 if a < b, 0 if a == b, +1 if a > b */ +#[inline(always)] pub pure fn cmp(a: char, b: char) -> int { return if b > a { -1 } else if b < a { 1 } diff --git a/src/libcore/clone.rs b/src/libcore/clone.rs index a0569348b2e5a..6fbcf2df45406 100644 --- a/src/libcore/clone.rs +++ b/src/libcore/clone.rs @@ -16,5 +16,6 @@ pub trait Clone { } impl (): Clone { + #[inline(always)] fn clone(&self) -> () { () } } diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 0f9e12058ba10..141b6f19ab436 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -53,26 +53,32 @@ pub trait Ord { pure fn gt(&self, other: &self) -> bool; } +#[inline(always)] pub pure fn lt(v1: &T, v2: &T) -> bool { (*v1).lt(v2) } +#[inline(always)] pub pure fn le(v1: &T, v2: &T) -> bool { (*v1).lt(v2) || (*v1).eq(v2) } +#[inline(always)] pub pure fn eq(v1: &T, v2: &T) -> bool { (*v1).eq(v2) } +#[inline(always)] pub pure fn ne(v1: &T, v2: &T) -> bool { (*v1).ne(v2) } +#[inline(always)] pub pure fn ge(v1: &T, v2: &T) -> bool { (*v1).ge(v2) } +#[inline(always)] pub pure fn gt(v1: &T, v2: &T) -> bool { (*v1).gt(v2) } diff --git a/src/libcore/either.rs b/src/libcore/either.rs index 4902edcedb102..c4f07db662180 100644 --- a/src/libcore/either.rs +++ b/src/libcore/either.rs @@ -28,6 +28,7 @@ pub enum Either { Right(U) } +#[inline(always)] pub fn either(f_left: fn(&T) -> V, f_right: fn(&U) -> V, value: &Either) -> V { /*! @@ -90,6 +91,7 @@ pub fn partition(eithers: ~[Either]) return (move lefts, move rights); } +#[inline(always)] pub pure fn flip(eith: Either) -> Either { //! Flips between left and right of a given either @@ -99,6 +101,7 @@ pub pure fn flip(eith: Either) -> Either { } } +#[inline(always)] pub pure fn to_result(eith: Either) -> Result { /*! @@ -114,18 +117,21 @@ pub pure fn to_result(eith: Either) } } +#[inline(always)] pub pure fn is_left(eith: &Either) -> bool { //! Checks whether the given value is a left match *eith { Left(_) => true, _ => false } } +#[inline(always)] pub pure fn is_right(eith: &Either) -> bool { //! Checks whether the given value is a right match *eith { Right(_) => true, _ => false } } +#[inline(always)] pub pure fn unwrap_left(eith: Either) -> T { //! Retrieves the value in the left branch. Fails if the either is Right. @@ -134,6 +140,7 @@ pub pure fn unwrap_left(eith: Either) -> T { } } +#[inline(always)] pub pure fn unwrap_right(eith: Either) -> U { //! Retrieves the value in the right branch. Fails if the either is Left. diff --git a/src/libcore/f32.rs b/src/libcore/f32.rs index 734cfc1108044..4c1cc890de9e4 100644 --- a/src/libcore/f32.rs +++ b/src/libcore/f32.rs @@ -105,38 +105,52 @@ pub const infinity: f32 = 1.0_f32/0.0_f32; pub const neg_infinity: f32 = -1.0_f32/0.0_f32; +#[inline(always)] pub pure fn is_NaN(f: f32) -> bool { f != f } +#[inline(always)] pub pure fn add(x: f32, y: f32) -> f32 { return x + y; } +#[inline(always)] pub pure fn sub(x: f32, y: f32) -> f32 { return x - y; } +#[inline(always)] pub pure fn mul(x: f32, y: f32) -> f32 { return x * y; } +#[inline(always)] pub pure fn div(x: f32, y: f32) -> f32 { return x / y; } +#[inline(always)] pub pure fn rem(x: f32, y: f32) -> f32 { return x % y; } +#[inline(always)] pub pure fn lt(x: f32, y: f32) -> bool { return x < y; } +#[inline(always)] pub pure fn le(x: f32, y: f32) -> bool { return x <= y; } +#[inline(always)] pub pure fn eq(x: f32, y: f32) -> bool { return x == y; } +#[inline(always)] pub pure fn ne(x: f32, y: f32) -> bool { return x != y; } +#[inline(always)] pub pure fn ge(x: f32, y: f32) -> bool { return x >= y; } +#[inline(always)] pub pure fn gt(x: f32, y: f32) -> bool { return x > y; } // FIXME (#1999): replace the predicates below with llvm intrinsics or // calls to the libmath macros in the rust runtime for performance. /// Returns true if `x` is a positive number, including +0.0f320 and +Infinity +#[inline(always)] pub pure fn is_positive(x: f32) -> bool { return x > 0.0f32 || (1.0f32/x) == infinity; } /// Returns true if `x` is a negative number, including -0.0f320 and -Infinity +#[inline(always)] pub pure fn is_negative(x: f32) -> bool { return x < 0.0f32 || (1.0f32/x) == neg_infinity; } @@ -145,6 +159,7 @@ pub pure fn is_negative(x: f32) -> bool * * This is the same as `f32::is_negative`. */ +#[inline(always)] pub pure fn is_nonpositive(x: f32) -> bool { return x < 0.0f32 || (1.0f32/x) == neg_infinity; } @@ -154,21 +169,25 @@ pub pure fn is_nonpositive(x: f32) -> bool { * * This is the same as `f32::is_positive`.) */ +#[inline(always)] pub pure fn is_nonnegative(x: f32) -> bool { return x > 0.0f32 || (1.0f32/x) == infinity; } /// Returns true if `x` is a zero number (positive or negative zero) +#[inline(always)] pub pure fn is_zero(x: f32) -> bool { return x == 0.0f32 || x == -0.0f32; } /// Returns true if `x`is an infinite number +#[inline(always)] pub pure fn is_infinite(x: f32) -> bool { return x == infinity || x == neg_infinity; } /// Returns true if `x`is a finite number +#[inline(always)] pub pure fn is_finite(x: f32) -> bool { return !(is_NaN(x) || is_infinite(x)); } @@ -219,45 +238,63 @@ pub mod consts { pub const ln_10: f32 = 2.30258509299404568401799145468436421_f32; } +#[inline(always)] pub pure fn signbit(x: f32) -> int { if is_negative(x) { return 1; } else { return 0; } } +#[inline(always)] pub pure fn logarithm(n: f32, b: f32) -> f32 { return log2(n) / log2(b); } #[cfg(notest)] impl f32 : cmp::Eq { + #[inline(always)] pure fn eq(&self, other: &f32) -> bool { (*self) == (*other) } + #[inline(always)] pure fn ne(&self, other: &f32) -> bool { (*self) != (*other) } } #[cfg(notest)] impl f32 : cmp::Ord { + #[inline(always)] pure fn lt(&self, other: &f32) -> bool { (*self) < (*other) } + #[inline(always)] pure fn le(&self, other: &f32) -> bool { (*self) <= (*other) } + #[inline(always)] pure fn ge(&self, other: &f32) -> bool { (*self) >= (*other) } + #[inline(always)] pure fn gt(&self, other: &f32) -> bool { (*self) > (*other) } } impl f32: num::Num { + #[inline(always)] pure fn add(&self, other: &f32) -> f32 { return *self + *other; } + #[inline(always)] pure fn sub(&self, other: &f32) -> f32 { return *self - *other; } + #[inline(always)] pure fn mul(&self, other: &f32) -> f32 { return *self * *other; } + #[inline(always)] pure fn div(&self, other: &f32) -> f32 { return *self / *other; } + #[inline(always)] pure fn modulo(&self, other: &f32) -> f32 { return *self % *other; } + #[inline(always)] pure fn neg(&self) -> f32 { return -*self; } + #[inline(always)] pure fn to_int(&self) -> int { return *self as int; } + #[inline(always)] static pure fn from_int(n: int) -> f32 { return n as f32; } } impl f32: num::Zero { + #[inline(always)] static pure fn zero() -> f32 { 0.0 } } impl f32: num::One { + #[inline(always)] static pure fn one() -> f32 { 1.0 } } diff --git a/src/libcore/f64.rs b/src/libcore/f64.rs index e0c47b509f1cb..cbcdc2e81e6ec 100644 --- a/src/libcore/f64.rs +++ b/src/libcore/f64.rs @@ -132,35 +132,49 @@ pub const infinity: f64 = 1.0_f64/0.0_f64; pub const neg_infinity: f64 = -1.0_f64/0.0_f64; +#[inline(always)] pub pure fn is_NaN(f: f64) -> bool { f != f } +#[inline(always)] pub pure fn add(x: f64, y: f64) -> f64 { return x + y; } +#[inline(always)] pub pure fn sub(x: f64, y: f64) -> f64 { return x - y; } +#[inline(always)] pub pure fn mul(x: f64, y: f64) -> f64 { return x * y; } +#[inline(always)] pub pure fn div(x: f64, y: f64) -> f64 { return x / y; } +#[inline(always)] pub pure fn rem(x: f64, y: f64) -> f64 { return x % y; } +#[inline(always)] pub pure fn lt(x: f64, y: f64) -> bool { return x < y; } +#[inline(always)] pub pure fn le(x: f64, y: f64) -> bool { return x <= y; } +#[inline(always)] pub pure fn eq(x: f64, y: f64) -> bool { return x == y; } +#[inline(always)] pub pure fn ne(x: f64, y: f64) -> bool { return x != y; } +#[inline(always)] pub pure fn ge(x: f64, y: f64) -> bool { return x >= y; } +#[inline(always)] pub pure fn gt(x: f64, y: f64) -> bool { return x > y; } /// Returns true if `x` is a positive number, including +0.0f640 and +Infinity +#[inline(always)] pub pure fn is_positive(x: f64) -> bool { return x > 0.0f64 || (1.0f64/x) == infinity; } /// Returns true if `x` is a negative number, including -0.0f640 and -Infinity +#[inline(always)] pub pure fn is_negative(x: f64) -> bool { return x < 0.0f64 || (1.0f64/x) == neg_infinity; } @@ -169,6 +183,7 @@ pub pure fn is_negative(x: f64) -> bool * * This is the same as `f64::is_negative`. */ +#[inline(always)] pub pure fn is_nonpositive(x: f64) -> bool { return x < 0.0f64 || (1.0f64/x) == neg_infinity; } @@ -178,21 +193,25 @@ pub pure fn is_nonpositive(x: f64) -> bool { * * This is the same as `f64::positive`. */ +#[inline(always)] pub pure fn is_nonnegative(x: f64) -> bool { return x > 0.0f64 || (1.0f64/x) == infinity; } /// Returns true if `x` is a zero number (positive or negative zero) +#[inline(always)] pub pure fn is_zero(x: f64) -> bool { return x == 0.0f64 || x == -0.0f64; } /// Returns true if `x`is an infinite number +#[inline(always)] pub pure fn is_infinite(x: f64) -> bool { return x == infinity || x == neg_infinity; } /// Returns true if `x`is a finite number +#[inline(always)] pub pure fn is_finite(x: f64) -> bool { return !(is_NaN(x) || is_infinite(x)); } @@ -243,45 +262,63 @@ pub mod consts { pub const ln_10: f64 = 2.30258509299404568401799145468436421_f64; } +#[inline(always)] pub pure fn signbit(x: f64) -> int { if is_negative(x) { return 1; } else { return 0; } } +#[inline(always)] pub pure fn logarithm(n: f64, b: f64) -> f64 { return log2(n) / log2(b); } #[cfg(notest)] impl f64 : cmp::Eq { + #[inline(always)] pure fn eq(&self, other: &f64) -> bool { (*self) == (*other) } + #[inline(always)] pure fn ne(&self, other: &f64) -> bool { (*self) != (*other) } } #[cfg(notest)] impl f64 : cmp::Ord { + #[inline(always)] pure fn lt(&self, other: &f64) -> bool { (*self) < (*other) } + #[inline(always)] pure fn le(&self, other: &f64) -> bool { (*self) <= (*other) } + #[inline(always)] pure fn ge(&self, other: &f64) -> bool { (*self) >= (*other) } + #[inline(always)] pure fn gt(&self, other: &f64) -> bool { (*self) > (*other) } } impl f64: num::Num { + #[inline(always)] pure fn add(&self, other: &f64) -> f64 { return *self + *other; } + #[inline(always)] pure fn sub(&self, other: &f64) -> f64 { return *self - *other; } + #[inline(always)] pure fn mul(&self, other: &f64) -> f64 { return *self * *other; } + #[inline(always)] pure fn div(&self, other: &f64) -> f64 { return *self / *other; } + #[inline(always)] pure fn modulo(&self, other: &f64) -> f64 { return *self % *other; } + #[inline(always)] pure fn neg(&self) -> f64 { return -*self; } + #[inline(always)] pure fn to_int(&self) -> int { return *self as int; } + #[inline(always)] static pure fn from_int(n: int) -> f64 { return n as f64; } } impl f64: num::Zero { + #[inline(always)] static pure fn zero() -> f64 { 0.0 } } impl f64: num::One { + #[inline(always)] static pure fn one() -> f64 { 1.0 } } diff --git a/src/libcore/float.rs b/src/libcore/float.rs index 92efe9c498120..4c79069ad0921 100644 --- a/src/libcore/float.rs +++ b/src/libcore/float.rs @@ -195,6 +195,7 @@ pub pure fn to_str_common(num: float, digits: uint, exact: bool) -> ~str { * * num - The float value * * digits - The number of significant digits */ +#[inline(always)] pub pure fn to_str_exact(num: float, digits: uint) -> ~str { to_str_common(num, digits, true) } @@ -215,6 +216,7 @@ pub fn test_to_str_exact_do_decimal() { * * num - The float value * * digits - The number of significant digits */ +#[inline(always)] pub pure fn to_str(num: float, digits: uint) -> ~str { to_str_common(num, digits, false) } @@ -400,30 +402,44 @@ pub pure fn pow_with_uint(base: uint, pow: uint) -> float { return total; } +#[inline(always)] pub pure fn is_positive(x: float) -> bool { f64::is_positive(x as f64) } +#[inline(always)] pub pure fn is_negative(x: float) -> bool { f64::is_negative(x as f64) } +#[inline(always)] pub pure fn is_nonpositive(x: float) -> bool { f64::is_nonpositive(x as f64) } +#[inline(always)] pub pure fn is_nonnegative(x: float) -> bool { f64::is_nonnegative(x as f64) } +#[inline(always)] pub pure fn is_zero(x: float) -> bool { f64::is_zero(x as f64) } +#[inline(always)] pub pure fn is_infinite(x: float) -> bool { f64::is_infinite(x as f64) } +#[inline(always)] pub pure fn is_finite(x: float) -> bool { f64::is_finite(x as f64) } +#[inline(always)] pub pure fn is_NaN(x: float) -> bool { f64::is_NaN(x as f64) } +#[inline(always)] pub pure fn abs(x: float) -> float { unsafe { f64::abs(x as f64) as float } } +#[inline(always)] pub pure fn sqrt(x: float) -> float { unsafe { f64::sqrt(x as f64) as float } } +#[inline(always)] pub pure fn atan(x: float) -> float { unsafe { f64::atan(x as f64) as float } } +#[inline(always)] pub pure fn sin(x: float) -> float { unsafe { f64::sin(x as f64) as float } } +#[inline(always)] pub pure fn cos(x: float) -> float { unsafe { f64::cos(x as f64) as float } } +#[inline(always)] pub pure fn tan(x: float) -> float { unsafe { f64::tan(x as f64) as float } } @@ -463,10 +479,12 @@ impl float: num::Num { } impl float: num::Zero { + #[inline(always)] static pure fn zero() -> float { 0.0 } } impl float: num::One { + #[inline(always)] static pure fn one() -> float { 1.0 } } diff --git a/src/libcore/int-template.rs b/src/libcore/int-template.rs index 4946a0997c1ed..a04610d199520 100644 --- a/src/libcore/int-template.rs +++ b/src/libcore/int-template.rs @@ -32,25 +32,42 @@ pub const bytes : uint = (inst::bits / 8); pub const min_value: T = (-1 as T) << (bits - 1); pub const max_value: T = min_value - 1 as T; +#[inline(always)] pub pure fn min(x: T, y: T) -> T { if x < y { x } else { y } } +#[inline(always)] pub pure fn max(x: T, y: T) -> T { if x > y { x } else { y } } +#[inline(always)] pub pure fn add(x: T, y: T) -> T { x + y } +#[inline(always)] pub pure fn sub(x: T, y: T) -> T { x - y } +#[inline(always)] pub pure fn mul(x: T, y: T) -> T { x * y } +#[inline(always)] pub pure fn div(x: T, y: T) -> T { x / y } +#[inline(always)] pub pure fn rem(x: T, y: T) -> T { x % y } +#[inline(always)] pub pure fn lt(x: T, y: T) -> bool { x < y } +#[inline(always)] pub pure fn le(x: T, y: T) -> bool { x <= y } +#[inline(always)] pub pure fn eq(x: T, y: T) -> bool { x == y } +#[inline(always)] pub pure fn ne(x: T, y: T) -> bool { x != y } +#[inline(always)] pub pure fn ge(x: T, y: T) -> bool { x >= y } +#[inline(always)] pub pure fn gt(x: T, y: T) -> bool { x > y } +#[inline(always)] pub pure fn is_positive(x: T) -> bool { x > 0 as T } +#[inline(always)] pub pure fn is_negative(x: T) -> bool { x < 0 as T } +#[inline(always)] pub pure fn is_nonpositive(x: T) -> bool { x <= 0 as T } +#[inline(always)] pub pure fn is_nonnegative(x: T) -> bool { x >= 0 as T } #[inline(always)] @@ -64,46 +81,64 @@ pub fn range(lo: T, hi: T, it: fn(T) -> bool) { } /// Computes the bitwise complement +#[inline(always)] pub pure fn compl(i: T) -> T { -1 as T ^ i } /// Computes the absolute value +#[inline(always)] pub pure fn abs(i: T) -> T { if is_negative(i) { -i } else { i } } #[cfg(notest)] impl T : Ord { + #[inline(always)] pure fn lt(&self, other: &T) -> bool { return (*self) < (*other); } + #[inline(always)] pure fn le(&self, other: &T) -> bool { return (*self) <= (*other); } + #[inline(always)] pure fn ge(&self, other: &T) -> bool { return (*self) >= (*other); } + #[inline(always)] pure fn gt(&self, other: &T) -> bool { return (*self) > (*other); } } #[cfg(notest)] impl T : Eq { + #[inline(always)] pure fn eq(&self, other: &T) -> bool { return (*self) == (*other); } + #[inline(always)] pure fn ne(&self, other: &T) -> bool { return (*self) != (*other); } } impl T: num::Num { + #[inline(always)] pure fn add(&self, other: &T) -> T { return *self + *other; } + #[inline(always)] pure fn sub(&self, other: &T) -> T { return *self - *other; } + #[inline(always)] pure fn mul(&self, other: &T) -> T { return *self * *other; } + #[inline(always)] pure fn div(&self, other: &T) -> T { return *self / *other; } + #[inline(always)] pure fn modulo(&self, other: &T) -> T { return *self % *other; } + #[inline(always)] pure fn neg(&self) -> T { return -*self; } + #[inline(always)] pure fn to_int(&self) -> int { return *self as int; } + #[inline(always)] static pure fn from_int(n: int) -> T { return n as T; } } impl T: num::Zero { + #[inline(always)] static pure fn zero() -> T { 0 } } impl T: num::One { + #[inline(always)] static pure fn one() -> T { 1 } } @@ -158,16 +193,19 @@ pub pure fn parse_bytes(buf: &[u8], radix: uint) -> Option { } /// Parse a string to an int +#[inline(always)] pub pure fn from_str(s: &str) -> Option { parse_bytes(str::to_bytes(s), 10u) } impl T : FromStr { + #[inline(always)] static pure fn from_str(s: &str) -> Option { from_str(s) } } /// Convert to a string in a given base +#[inline(always)] pub pure fn to_str(n: T, radix: uint) -> ~str { do to_str_bytes(n, radix) |slice| { do vec::as_imm_buf(slice) |p, len| { @@ -176,6 +214,7 @@ pub pure fn to_str(n: T, radix: uint) -> ~str { } } +#[inline(always)] pub pure fn to_str_bytes(n: T, radix: uint, f: fn(v: &[u8]) -> U) -> U { if n < 0 as T { uint::to_str_bytes(true, -n as uint, radix, f) @@ -185,6 +224,7 @@ pub pure fn to_str_bytes(n: T, radix: uint, f: fn(v: &[u8]) -> U) -> U { } /// Convert to a string +#[inline(always)] pub pure fn str(i: T) -> ~str { return to_str(i, 10u); } #[test] diff --git a/src/libcore/iter-trait.rs b/src/libcore/iter-trait.rs index a139daddcef11..942db1c5f2926 100644 --- a/src/libcore/iter-trait.rs +++ b/src/libcore/iter-trait.rs @@ -24,29 +24,38 @@ use option::Option; use self::inst::{IMPL_T, EACH, SIZE_HINT}; impl IMPL_T: iter::BaseIter { + #[inline(always)] pure fn each(&self, blk: fn(v: &A) -> bool) { EACH(self, blk) } + #[inline(always)] pure fn size_hint(&self) -> Option { SIZE_HINT(self) } } impl IMPL_T: iter::ExtendedIter { + #[inline(always)] pure fn eachi(&self, blk: fn(uint, v: &A) -> bool) { iter::eachi(self, blk) } + #[inline(always)] pure fn all(&self, blk: fn(&A) -> bool) -> bool { iter::all(self, blk) } + #[inline(always)] pure fn any(&self, blk: fn(&A) -> bool) -> bool { iter::any(self, blk) } + #[inline(always)] pure fn foldl(&self, b0: B, blk: fn(&B, &A) -> B) -> B { iter::foldl(self, move b0, blk) } + #[inline(always)] pure fn position(&self, f: fn(&A) -> bool) -> Option { iter::position(self, f) } + #[inline(always)] pure fn map_to_vec(&self, op: fn(&A) -> B) -> ~[B] { iter::map_to_vec(self, op) } + #[inline(always)] pure fn flat_map_to_vec>(&self, op: fn(&A) -> IB) -> ~[B] { iter::flat_map_to_vec(self, op) @@ -55,22 +64,29 @@ impl IMPL_T: iter::ExtendedIter { } impl IMPL_T: iter::EqIter { + #[inline(always)] pure fn contains(&self, x: &A) -> bool { iter::contains(self, x) } + #[inline(always)] pure fn count(&self, x: &A) -> uint { iter::count(self, x) } } impl IMPL_T: iter::CopyableIter { + #[inline(always)] pure fn filter_to_vec(&self, pred: fn(&A) -> bool) -> ~[A] { iter::filter_to_vec(self, pred) } + #[inline(always)] pure fn to_vec(&self) -> ~[A] { iter::to_vec(self) } + #[inline(always)] pure fn find(&self, f: fn(&A) -> bool) -> Option { iter::find(self, f) } } impl IMPL_T: iter::CopyableOrderedIter { + #[inline(always)] pure fn min(&self) -> A { iter::min(self) } + #[inline(always)] pure fn max(&self) -> A { iter::max(self) } } diff --git a/src/libcore/iter-trait/dlist.rs b/src/libcore/iter-trait/dlist.rs index eb395c57ae8cb..754b8f8b72fc8 100644 --- a/src/libcore/iter-trait/dlist.rs +++ b/src/libcore/iter-trait/dlist.rs @@ -47,6 +47,7 @@ mod inst { } } + #[inline(always)] pub pure fn SIZE_HINT(self: &IMPL_T) -> Option { Some(self.len()) } diff --git a/src/libcore/iter-trait/dvec.rs b/src/libcore/iter-trait/dvec.rs index 28d1cf03d46c7..af788989e9c0f 100644 --- a/src/libcore/iter-trait/dvec.rs +++ b/src/libcore/iter-trait/dvec.rs @@ -20,6 +20,7 @@ mod inst { * * Attempts to access this dvec during iteration will fail. */ + #[inline(always)] pub pure fn EACH(self: &IMPL_T, f: fn(v: &A) -> bool) { unsafe { do self.swap |v| { @@ -29,6 +30,7 @@ mod inst { } } + #[inline(always)] pub pure fn SIZE_HINT(self: &IMPL_T) -> Option { Some(self.len()) } diff --git a/src/libcore/iter-trait/option.rs b/src/libcore/iter-trait/option.rs index 2240db82f2953..986dbf7f3d186 100644 --- a/src/libcore/iter-trait/option.rs +++ b/src/libcore/iter-trait/option.rs @@ -14,6 +14,7 @@ mod inst { #[allow(non_camel_case_types)] pub type IMPL_T = Option; + #[inline(always)] pub pure fn EACH(self: &IMPL_T, f: fn(v: &A) -> bool) { match *self { None => (), @@ -21,6 +22,7 @@ mod inst { } } + #[inline(always)] pub pure fn SIZE_HINT(self: &IMPL_T) -> Option { match *self { None => Some(0), diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index 1e56b0f14afb7..4ea845543d1ac 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -88,6 +88,7 @@ pub trait Buildable { builder: fn(push: pure fn(A))) -> self; } +#[inline(always)] pub pure fn eachi>(self: &IA, blk: fn(uint, &A) -> bool) { let mut i = 0; @@ -97,6 +98,7 @@ pub pure fn eachi>(self: &IA, } } +#[inline(always)] pub pure fn all>(self: &IA, blk: fn(&A) -> bool) -> bool { for self.each |a| { @@ -105,6 +107,7 @@ pub pure fn all>(self: &IA, return true; } +#[inline(always)] pub pure fn any>(self: &IA, blk: fn(&A) -> bool) -> bool { for self.each |a| { @@ -113,6 +116,7 @@ pub pure fn any>(self: &IA, return false; } +#[inline(always)] pub pure fn filter_to_vec>( self: &IA, prd: fn(&A) -> bool) -> ~[A] { do vec::build_sized_opt(self.size_hint()) |push| { @@ -122,6 +126,7 @@ pub pure fn filter_to_vec>( } } +#[inline(always)] pub pure fn map_to_vec>(self: &IA, op: fn(&A) -> B) -> ~[B] { @@ -132,6 +137,7 @@ pub pure fn map_to_vec>(self: &IA, } } +#[inline(always)] pub pure fn flat_map_to_vec,IB:BaseIter>( self: &IA, op: fn(&A) -> IB) -> ~[B] { do vec::build |push| { @@ -143,6 +149,7 @@ pub pure fn flat_map_to_vec,IB:BaseIter>( } } +#[inline(always)] pub pure fn foldl>(self: &IA, b0: B, blk: fn(&B, &A) -> B) -> B { @@ -153,10 +160,12 @@ pub pure fn foldl>(self: &IA, b0: B, move b } +#[inline(always)] pub pure fn to_vec>(self: &IA) -> ~[A] { foldl::(self, ~[], |r, a| vec::append(copy (*r), ~[*a])) } +#[inline(always)] pub pure fn contains>(self: &IA, x: &A) -> bool { for self.each |a| { if *a == *x { return true; } @@ -164,6 +173,7 @@ pub pure fn contains>(self: &IA, x: &A) -> bool { return false; } +#[inline(always)] pub pure fn count>(self: &IA, x: &A) -> uint { do foldl(self, 0) |count, value| { if *value == *x { @@ -174,6 +184,7 @@ pub pure fn count>(self: &IA, x: &A) -> uint { } } +#[inline(always)] pub pure fn position>(self: &IA, f: fn(&A) -> bool) -> Option { @@ -189,6 +200,7 @@ pub pure fn position>(self: &IA, f: fn(&A) -> bool) // iter interface, such as would provide "reach" in addition to "each". as is, // it would have to be implemented with foldr, which is too inefficient. +#[inline(always)] pub pure fn repeat(times: uint, blk: fn() -> bool) { let mut i = 0; while i < times { @@ -197,6 +209,7 @@ pub pure fn repeat(times: uint, blk: fn() -> bool) { } } +#[inline(always)] pub pure fn min>(self: &IA) -> A { match do foldl::,IA>(self, None) |a, b| { match a { @@ -211,6 +224,7 @@ pub pure fn min>(self: &IA) -> A { } } +#[inline(always)] pub pure fn max>(self: &IA) -> A { match do foldl::,IA>(self, None) |a, b| { match a { @@ -225,6 +239,7 @@ pub pure fn max>(self: &IA) -> A { } } +#[inline(always)] pub pure fn find>(self: &IA, f: fn(&A) -> bool) -> Option { for self.each |i| { @@ -275,6 +290,7 @@ pub pure fn build_sized_opt>( // Functions that combine iteration and building /// Apply a function to each element of an iterable and return the results +#[inline(always)] pub fn map,U,BU: Buildable>(v: &IT, f: fn(&T) -> U) -> BU { do build_sized_opt(v.size_hint()) |push| { @@ -290,6 +306,7 @@ pub fn map,U,BU: Buildable>(v: &IT, f: fn(&T) -> U) * Creates a generic sequence of size `n_elts` and initializes the elements * to the value returned by the function `op`. */ +#[inline(always)] pub pure fn from_fn>(n_elts: uint, op: InitOp) -> BT { do Buildable::build_sized(n_elts) |push| { @@ -304,6 +321,7 @@ pub pure fn from_fn>(n_elts: uint, * Creates an immutable vector of size `n_elts` and initializes the elements * to the value `t`. */ +#[inline(always)] pub pure fn from_elem>(n_elts: uint, t: T) -> BT { do Buildable::build_sized(n_elts) |push| { diff --git a/src/libcore/managed.rs b/src/libcore/managed.rs index b334d24674308..8395526276c8e 100644 --- a/src/libcore/managed.rs +++ b/src/libcore/managed.rs @@ -35,6 +35,7 @@ pub mod raw { } +#[inline(always)] pub pure fn ptr_eq(a: @T, b: @T) -> bool { //! Determine if two shared boxes point to the same object unsafe { ptr::addr_of(&(*a)) == ptr::addr_of(&(*b)) } @@ -42,15 +43,21 @@ pub pure fn ptr_eq(a: @T, b: @T) -> bool { #[cfg(notest)] impl @const T : Eq { + #[inline(always)] pure fn eq(&self, other: &@const T) -> bool { *(*self) == *(*other) } + #[inline(always)] pure fn ne(&self, other: &@const T) -> bool { *(*self) != *(*other) } } #[cfg(notest)] impl @const T : Ord { + #[inline(always)] pure fn lt(&self, other: &@const T) -> bool { *(*self) < *(*other) } + #[inline(always)] pure fn le(&self, other: &@const T) -> bool { *(*self) <= *(*other) } + #[inline(always)] pure fn ge(&self, other: &@const T) -> bool { *(*self) >= *(*other) } + #[inline(always)] pure fn gt(&self, other: &@const T) -> bool { *(*self) > *(*other) } } diff --git a/src/libcore/nil.rs b/src/libcore/nil.rs index 5e306d4b57d7b..f3eb62a3a3ad0 100644 --- a/src/libcore/nil.rs +++ b/src/libcore/nil.rs @@ -22,15 +22,21 @@ use cmp::{Eq, Ord}; #[cfg(notest)] impl () : Eq { + #[inline(always)] pure fn eq(&self, _other: &()) -> bool { true } + #[inline(always)] pure fn ne(&self, _other: &()) -> bool { false } } #[cfg(notest)] impl () : Ord { + #[inline(always)] pure fn lt(&self, _other: &()) -> bool { false } + #[inline(always)] pure fn le(&self, _other: &()) -> bool { true } + #[inline(always)] pure fn ge(&self, _other: &()) -> bool { true } + #[inline(always)] pure fn gt(&self, _other: &()) -> bool { false } } diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 49ba10dfffbe7..414027bb77e6a 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -59,6 +59,7 @@ pub enum Option { Some(T), } +#[inline(always)] pub pure fn get(opt: Option) -> T { /*! Gets the value out of an option @@ -81,6 +82,7 @@ pub pure fn get(opt: Option) -> T { } } +#[inline(always)] pub pure fn get_ref(opt: &r/Option) -> &r/T { /*! Gets an immutable reference to the value inside an option. @@ -102,12 +104,14 @@ pub pure fn get_ref(opt: &r/Option) -> &r/T { } } +#[inline(always)] pub pure fn map(opt: &Option, f: fn(x: &T) -> U) -> Option { //! Maps a `some` value by reference from one type to another match *opt { Some(ref x) => Some(f(x)), None => None } } +#[inline(always)] pub pure fn map_consume(opt: Option, f: fn(v: T) -> U) -> Option { /*! @@ -117,6 +121,7 @@ pub pure fn map_consume(opt: Option, if opt.is_some() { Some(f(option::unwrap(move opt))) } else { None } } +#[inline(always)] pub pure fn chain(opt: Option, f: fn(t: T) -> Option) -> Option { /*! @@ -130,6 +135,7 @@ pub pure fn chain(opt: Option, } } +#[inline(always)] pub pure fn chain_ref(opt: &Option, f: fn(x: &T) -> Option) -> Option { /*! @@ -140,6 +146,7 @@ pub pure fn chain_ref(opt: &Option, match *opt { Some(ref x) => f(x), None => None } } +#[inline(always)] pub pure fn or(opta: Option, optb: Option) -> Option { /*! * Returns the leftmost some() value, or none if both are none. @@ -160,30 +167,35 @@ pub pure fn while_some(x: Option, blk: fn(v: T) -> Option) { } } +#[inline(always)] pub pure fn is_none(opt: &Option) -> bool { //! Returns true if the option equals `none` match *opt { None => true, Some(_) => false } } +#[inline(always)] pub pure fn is_some(opt: &Option) -> bool { //! Returns true if the option contains some value !is_none(opt) } +#[inline(always)] pub pure fn get_or_zero(opt: Option) -> T { //! Returns the contained value or zero (for this type) match opt { Some(copy x) => x, None => Zero::zero() } } +#[inline(always)] pub pure fn get_or_default(opt: Option, def: T) -> T { //! Returns the contained value or a default match opt { Some(copy x) => x, None => def } } +#[inline(always)] pub pure fn map_default(opt: &Option, def: U, f: fn(x: &T) -> U) -> U { //! Applies a function to the contained value or returns a default @@ -191,6 +203,7 @@ pub pure fn map_default(opt: &Option, def: U, match *opt { None => move def, Some(ref t) => f(t) } } +#[inline(always)] pub pure fn iter(opt: &Option, f: fn(x: &T)) { //! Performs an operation on the contained value by reference match *opt { None => (), Some(ref t) => f(t) } @@ -234,6 +247,7 @@ pub fn swap_unwrap(opt: &mut Option) -> T { unwrap(util::replace(opt, None)) } +#[inline(always)] pub pure fn expect(opt: Option, reason: &str) -> T { //! As unwrap, but with a specified failure message. match move opt { diff --git a/src/libcore/owned.rs b/src/libcore/owned.rs index 2f369e18fd152..fb14820113e00 100644 --- a/src/libcore/owned.rs +++ b/src/libcore/owned.rs @@ -18,15 +18,21 @@ use cmp::{Eq, Ord}; #[cfg(notest)] impl ~const T : Eq { + #[inline(always)] pure fn eq(&self, other: &~const T) -> bool { *(*self) == *(*other) } + #[inline(always)] pure fn ne(&self, other: &~const T) -> bool { *(*self) != *(*other) } } #[cfg(notest)] impl ~const T : Ord { + #[inline(always)] pure fn lt(&self, other: &~const T) -> bool { *(*self) < *(*other) } + #[inline(always)] pure fn le(&self, other: &~const T) -> bool { *(*self) <= *(*other) } + #[inline(always)] pure fn ge(&self, other: &~const T) -> bool { *(*self) >= *(*other) } + #[inline(always)] pure fn gt(&self, other: &~const T) -> bool { *(*self) > *(*other) } } diff --git a/src/libcore/ptr.rs b/src/libcore/ptr.rs index d8c3c74ba2e49..33770d727f929 100644 --- a/src/libcore/ptr.rs +++ b/src/libcore/ptr.rs @@ -108,9 +108,11 @@ pub pure fn null() -> *T { unsafe { cast::reinterpret_cast(&0u) } } pub pure fn mut_null() -> *mut T { unsafe { cast::reinterpret_cast(&0u) } } /// Returns true if the pointer is equal to the null pointer. +#[inline(always)] pub pure fn is_null(ptr: *const T) -> bool { ptr == null() } /// Returns true if the pointer is not equal to the null pointer. +#[inline(always)] pub pure fn is_not_null(ptr: *const T) -> bool { !is_null(ptr) } /** @@ -231,32 +233,38 @@ impl *mut T: Ptr { // Equality for pointers #[cfg(notest)] impl *const T : Eq { + #[inline(always)] pure fn eq(&self, other: &*const T) -> bool unsafe { let a: uint = cast::reinterpret_cast(&(*self)); let b: uint = cast::reinterpret_cast(&(*other)); return a == b; } + #[inline(always)] pure fn ne(&self, other: &*const T) -> bool { !(*self).eq(other) } } // Comparison for pointers #[cfg(notest)] impl *const T : Ord { + #[inline(always)] pure fn lt(&self, other: &*const T) -> bool unsafe { let a: uint = cast::reinterpret_cast(&(*self)); let b: uint = cast::reinterpret_cast(&(*other)); return a < b; } + #[inline(always)] pure fn le(&self, other: &*const T) -> bool unsafe { let a: uint = cast::reinterpret_cast(&(*self)); let b: uint = cast::reinterpret_cast(&(*other)); return a <= b; } + #[inline(always)] pure fn ge(&self, other: &*const T) -> bool unsafe { let a: uint = cast::reinterpret_cast(&(*self)); let b: uint = cast::reinterpret_cast(&(*other)); return a >= b; } + #[inline(always)] pure fn gt(&self, other: &*const T) -> bool unsafe { let a: uint = cast::reinterpret_cast(&(*self)); let b: uint = cast::reinterpret_cast(&(*other)); @@ -267,9 +275,11 @@ impl *const T : Ord { // Equality for region pointers #[cfg(notest)] impl &const T : Eq { + #[inline(always)] pure fn eq(&self, other: & &self/const T) -> bool { return *(*self) == *(*other); } + #[inline(always)] pure fn ne(&self, other: & &self/const T) -> bool { return *(*self) != *(*other); } @@ -278,15 +288,19 @@ impl &const T : Eq { // Comparison for region pointers #[cfg(notest)] impl &const T : Ord { + #[inline(always)] pure fn lt(&self, other: & &self/const T) -> bool { *(*self) < *(*other) } + #[inline(always)] pure fn le(&self, other: & &self/const T) -> bool { *(*self) <= *(*other) } + #[inline(always)] pure fn ge(&self, other: & &self/const T) -> bool { *(*self) >= *(*other) } + #[inline(always)] pure fn gt(&self, other: & &self/const T) -> bool { *(*self) > *(*other) } diff --git a/src/libcore/result.rs b/src/libcore/result.rs index 91f51908be8b6..9a09f1901ff7c 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -39,6 +39,7 @@ pub enum Result { * * If the result is an error */ +#[inline(always)] pub pure fn get(res: &Result) -> T { match *res { Ok(copy t) => t, @@ -55,6 +56,7 @@ pub pure fn get(res: &Result) -> T { * * If the result is an error */ +#[inline(always)] pub pure fn get_ref(res: &a/Result) -> &a/T { match *res { Ok(ref t) => t, @@ -71,6 +73,7 @@ pub pure fn get_ref(res: &a/Result) -> &a/T { * * If the result is not an error */ +#[inline(always)] pub pure fn get_err(res: &Result) -> U { match *res { Err(copy u) => u, @@ -79,6 +82,7 @@ pub pure fn get_err(res: &Result) -> U { } /// Returns true if the result is `ok` +#[inline(always)] pub pure fn is_ok(res: &Result) -> bool { match *res { Ok(_) => true, @@ -87,6 +91,7 @@ pub pure fn is_ok(res: &Result) -> bool { } /// Returns true if the result is `err` +#[inline(always)] pub pure fn is_err(res: &Result) -> bool { !is_ok(res) } @@ -97,6 +102,7 @@ pub pure fn is_err(res: &Result) -> bool { * `ok` result variants are converted to `either::right` variants, `err` * result variants are converted to `either::left`. */ +#[inline(always)] pub pure fn to_either(res: &Result) -> Either { match *res { @@ -119,6 +125,7 @@ pub pure fn to_either(res: &Result) * ok(parse_bytes(buf)) * } */ +#[inline(always)] pub pure fn chain(res: Result, op: fn(T) -> Result) -> Result { match move res { @@ -135,6 +142,7 @@ pub pure fn chain(res: Result, op: fn(T) * immediately returned. This function can be used to pass through a * successful result while handling an error. */ +#[inline(always)] pub pure fn chain_err( res: Result, op: fn(t: V) -> Result) @@ -159,6 +167,7 @@ pub pure fn chain_err( * print_buf(buf) * } */ +#[inline(always)] pub pure fn iter(res: &Result, f: fn(&T)) { match *res { Ok(ref t) => f(t), @@ -174,6 +183,7 @@ pub pure fn iter(res: &Result, f: fn(&T)) { * This function can be used to pass through a successful result while * handling an error. */ +#[inline(always)] pub pure fn iter_err(res: &Result, f: fn(&E)) { match *res { Ok(_) => (), @@ -195,6 +205,7 @@ pub pure fn iter_err(res: &Result, f: fn(&E)) { * parse_bytes(buf) * } */ +#[inline(always)] pub pure fn map(res: &Result, op: fn(&T) -> U) -> Result { match *res { @@ -211,6 +222,7 @@ pub pure fn map(res: &Result, op: fn(&T) -> U) * is immediately returned. This function can be used to pass through a * successful result while handling an error. */ +#[inline(always)] pub pure fn map_err(res: &Result, op: fn(&E) -> F) -> Result { match *res { @@ -289,6 +301,7 @@ impl Result { * assert incd == ~[2u, 3u, 4u]; * } */ +#[inline(always)] pub fn map_vec( ts: &[T], op: fn(&T) -> Result) -> Result<~[V],U> { @@ -302,6 +315,7 @@ pub fn map_vec( return Ok(move vs); } +#[inline(always)] pub fn map_opt( o_t: &Option, op: fn(&T) -> Result) -> Result,U> { @@ -323,6 +337,7 @@ pub fn map_opt( * used in 'careful' code contexts where it is both appropriate and easy * to accommodate an error like the vectors being of different lengths. */ +#[inline(always)] pub fn map_vec2(ss: &[S], ts: &[T], op: fn(&S,&T) -> Result) -> Result<~[V],U> { @@ -345,6 +360,7 @@ pub fn map_vec2(ss: &[S], ts: &[T], * error. This could be implemented using `map2()` but it is more efficient * on its own as no result vector is built. */ +#[inline(always)] pub fn iter_vec2(ss: &[S], ts: &[T], op: fn(&S,&T) -> Result<(),U>) -> Result<(),U> { diff --git a/src/libcore/to_str.rs b/src/libcore/to_str.rs index fb4ea48ca95cc..22e187d7768e6 100644 --- a/src/libcore/to_str.rs +++ b/src/libcore/to_str.rs @@ -25,67 +25,87 @@ use vec; pub trait ToStr { pub pure fn to_str() -> ~str; } impl int: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::int::str(self) } } impl i8: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::i8::str(self) } } impl i16: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::i16::str(self) } } impl i32: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::i32::str(self) } } impl i64: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::i64::str(self) } } impl uint: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::uint::str(self) } } impl u8: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::u8::str(self) } } impl u16: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::u16::str(self) } } impl u32: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::u32::str(self) } } impl u64: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::u64::str(self) } } impl float: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::float::to_str(self, 4u) } } impl f32: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::float::to_str(self as float, 4u) } } impl f64: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::float::to_str(self as float, 4u) } } impl bool: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::bool::to_str(self) } } impl (): ToStr { + #[inline(always)] pure fn to_str() -> ~str { ~"()" } } impl ~str: ToStr { + #[inline(always)] pure fn to_str() -> ~str { copy self } } impl &str: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::str::from_slice(self) } } impl @str: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ::str::from_slice(self) } } impl (A, B): ToStr { + #[inline(always)] pure fn to_str() -> ~str { let (a, b) = self; ~"(" + a.to_str() + ~", " + b.to_str() + ~")" } } impl (A, B, C): ToStr { + #[inline(always)] pure fn to_str() -> ~str { let (a, b, c) = self; ~"(" + a.to_str() + ~", " + b.to_str() + ~", " + c.to_str() + ~")" @@ -93,6 +113,7 @@ impl (A, B, C): ToStr { } impl ~[A]: ToStr { + #[inline(always)] pure fn to_str() -> ~str unsafe { // Bleh -- not really unsafe // push_str and push_char @@ -108,9 +129,11 @@ impl ~[A]: ToStr { } impl @A: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ~"@" + (*self).to_str() } } impl ~A: ToStr { + #[inline(always)] pure fn to_str() -> ~str { ~"~" + (*self).to_str() } } diff --git a/src/libcore/tuple.rs b/src/libcore/tuple.rs index 6ac7a1a04a397..c497a2f3c7f14 100644 --- a/src/libcore/tuple.rs +++ b/src/libcore/tuple.rs @@ -27,18 +27,21 @@ pub trait CopyableTuple { impl (T, U): CopyableTuple { /// Return the first element of self + #[inline(always)] pure fn first() -> T { let (t, _) = self; return t; } /// Return the second element of self + #[inline(always)] pure fn second() -> U { let (_, u) = self; return u; } /// Return the results of swapping the two elements of self + #[inline(always)] pure fn swap() -> (U, T) { let (t, u) = self; return (u, t); @@ -52,11 +55,13 @@ pub trait ImmutableTuple { } impl (T, U): ImmutableTuple { + #[inline(always)] pure fn first_ref(&self) -> &self/T { match *self { (ref t, _) => t, } } + #[inline(always)] pure fn second_ref(&self) -> &self/U { match *self { (_, ref u) => u, @@ -70,6 +75,7 @@ pub trait ExtendedTupleOps { } impl (&[A], &[B]): ExtendedTupleOps { + #[inline(always)] fn zip(&self) -> ~[(A, B)] { match *self { (ref a, ref b) => { @@ -78,6 +84,7 @@ impl (&[A], &[B]): ExtendedTupleOps { } } + #[inline(always)] fn map(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C] { match *self { (ref a, ref b) => { @@ -89,6 +96,7 @@ impl (&[A], &[B]): ExtendedTupleOps { impl (~[A], ~[B]): ExtendedTupleOps { + #[inline(always)] fn zip(&self) -> ~[(A, B)] { match *self { (ref a, ref b) => { @@ -97,6 +105,7 @@ impl (~[A], ~[B]): ExtendedTupleOps { } } + #[inline(always)] fn map(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C] { match *self { (ref a, ref b) => { @@ -108,6 +117,7 @@ impl (~[A], ~[B]): ExtendedTupleOps { #[cfg(notest)] impl (A, B) : Eq { + #[inline(always)] pure fn eq(&self, other: &(A, B)) -> bool { match (*self) { (ref self_a, ref self_b) => match other { @@ -117,11 +127,13 @@ impl (A, B) : Eq { } } } + #[inline(always)] pure fn ne(&self, other: &(A, B)) -> bool { !(*self).eq(other) } } #[cfg(notest)] impl (A, B) : Ord { + #[inline(always)] pure fn lt(&self, other: &(A, B)) -> bool { match (*self) { (ref self_a, ref self_b) => { @@ -136,13 +148,17 @@ impl (A, B) : Ord { } } } + #[inline(always)] pure fn le(&self, other: &(A, B)) -> bool { !(*other).lt(&(*self)) } + #[inline(always)] pure fn ge(&self, other: &(A, B)) -> bool { !(*self).lt(other) } + #[inline(always)] pure fn gt(&self, other: &(A, B)) -> bool { (*other).lt(&(*self)) } } #[cfg(notest)] impl (A, B, C) : Eq { + #[inline(always)] pure fn eq(&self, other: &(A, B, C)) -> bool { match (*self) { (ref self_a, ref self_b, ref self_c) => match other { @@ -153,11 +169,13 @@ impl (A, B, C) : Eq { } } } + #[inline(always)] pure fn ne(&self, other: &(A, B, C)) -> bool { !(*self).eq(other) } } #[cfg(notest)] impl (A, B, C) : Ord { + #[inline(always)] pure fn lt(&self, other: &(A, B, C)) -> bool { match (*self) { (ref self_a, ref self_b, ref self_c) => { @@ -174,8 +192,11 @@ impl (A, B, C) : Ord { } } } + #[inline(always)] pure fn le(&self, other: &(A, B, C)) -> bool { !(*other).lt(&(*self)) } + #[inline(always)] pure fn ge(&self, other: &(A, B, C)) -> bool { !(*self).lt(other) } + #[inline(always)] pure fn gt(&self, other: &(A, B, C)) -> bool { (*other).lt(&(*self)) } } diff --git a/src/libcore/uint-template.rs b/src/libcore/uint-template.rs index ad6f73e56c060..a7eb2f8072553 100644 --- a/src/libcore/uint-template.rs +++ b/src/libcore/uint-template.rs @@ -30,25 +30,42 @@ pub const bytes : uint = (inst::bits / 8); pub const min_value: T = 0 as T; pub const max_value: T = 0 as T - 1 as T; +#[inline(always)] pub pure fn min(x: T, y: T) -> T { if x < y { x } else { y } } +#[inline(always)] pub pure fn max(x: T, y: T) -> T { if x > y { x } else { y } } +#[inline(always)] pub pure fn add(x: T, y: T) -> T { x + y } +#[inline(always)] pub pure fn sub(x: T, y: T) -> T { x - y } +#[inline(always)] pub pure fn mul(x: T, y: T) -> T { x * y } +#[inline(always)] pub pure fn div(x: T, y: T) -> T { x / y } +#[inline(always)] pub pure fn rem(x: T, y: T) -> T { x % y } +#[inline(always)] pub pure fn lt(x: T, y: T) -> bool { x < y } +#[inline(always)] pub pure fn le(x: T, y: T) -> bool { x <= y } +#[inline(always)] pub pure fn eq(x: T, y: T) -> bool { x == y } +#[inline(always)] pub pure fn ne(x: T, y: T) -> bool { x != y } +#[inline(always)] pub pure fn ge(x: T, y: T) -> bool { x >= y } +#[inline(always)] pub pure fn gt(x: T, y: T) -> bool { x > y } +#[inline(always)] pub pure fn is_positive(x: T) -> bool { x > 0 as T } +#[inline(always)] pub pure fn is_negative(x: T) -> bool { x < 0 as T } +#[inline(always)] pub pure fn is_nonpositive(x: T) -> bool { x <= 0 as T } +#[inline(always)] pub pure fn is_nonnegative(x: T) -> bool { x >= 0 as T } #[inline(always)] @@ -62,41 +79,58 @@ pub pure fn range(lo: T, hi: T, it: fn(T) -> bool) { } /// Computes the bitwise complement +#[inline(always)] pub pure fn compl(i: T) -> T { max_value ^ i } #[cfg(notest)] impl T : Ord { + #[inline(always)] pure fn lt(&self, other: &T) -> bool { (*self) < (*other) } + #[inline(always)] pure fn le(&self, other: &T) -> bool { (*self) <= (*other) } + #[inline(always)] pure fn ge(&self, other: &T) -> bool { (*self) >= (*other) } + #[inline(always)] pure fn gt(&self, other: &T) -> bool { (*self) > (*other) } } #[cfg(notest)] impl T : Eq { + #[inline(always)] pure fn eq(&self, other: &T) -> bool { return (*self) == (*other); } + #[inline(always)] pure fn ne(&self, other: &T) -> bool { return (*self) != (*other); } } impl T: num::Num { + #[inline(always)] pure fn add(&self, other: &T) -> T { return *self + *other; } + #[inline(always)] pure fn sub(&self, other: &T) -> T { return *self - *other; } + #[inline(always)] pure fn mul(&self, other: &T) -> T { return *self * *other; } + #[inline(always)] pure fn div(&self, other: &T) -> T { return *self / *other; } + #[inline(always)] pure fn modulo(&self, other: &T) -> T { return *self % *other; } + #[inline(always)] pure fn neg(&self) -> T { return -*self; } + #[inline(always)] pure fn to_int(&self) -> int { return *self as int; } + #[inline(always)] static pure fn from_int(n: int) -> T { return n as T; } } impl T: num::Zero { + #[inline(always)] static pure fn zero() -> T { 0 } } impl T: num::One { + #[inline(always)] static pure fn one() -> T { 1 } } @@ -145,12 +179,14 @@ pub pure fn parse_bytes(buf: &[const u8], radix: uint) -> Option { } /// Parse a string to an int +#[inline(always)] pub pure fn from_str(s: &str) -> Option { parse_bytes(str::to_bytes(s), 10u) } impl T : FromStr { + #[inline(always)] static pure fn from_str(s: &str) -> Option { from_str(s) } } @@ -177,6 +213,7 @@ pub fn from_str_radix(buf: &str, radix: u64) -> Option { * * Fails if `radix` < 2 or `radix` > 16 */ +#[inline(always)] pub pure fn to_str(num: T, radix: uint) -> ~str { do to_str_bytes(false, num, radix) |slice| { do vec::as_imm_buf(slice) |p, len| { @@ -230,6 +267,7 @@ pub pure fn to_str_bytes(neg: bool, num: T, radix: uint, } /// Convert to a string +#[inline(always)] pub pure fn str(i: T) -> ~str { return to_str(i, 10u); } #[test]