Inlining methods/functions in core.
Also inlining some functions which take functions as arguments.
This commit is contained in:
parent
802d475190
commit
62f2749775
@ -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<uint> {
|
||||
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 }
|
||||
|
@ -16,5 +16,6 @@ pub trait Clone {
|
||||
}
|
||||
|
||||
impl (): Clone {
|
||||
#[inline(always)]
|
||||
fn clone(&self) -> () { () }
|
||||
}
|
||||
|
@ -53,26 +53,32 @@ pub trait Ord {
|
||||
pure fn gt(&self, other: &self) -> bool;
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn lt<T: Ord>(v1: &T, v2: &T) -> bool {
|
||||
(*v1).lt(v2)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn le<T: Ord Eq>(v1: &T, v2: &T) -> bool {
|
||||
(*v1).lt(v2) || (*v1).eq(v2)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn eq<T: Eq>(v1: &T, v2: &T) -> bool {
|
||||
(*v1).eq(v2)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn ne<T: Eq>(v1: &T, v2: &T) -> bool {
|
||||
(*v1).ne(v2)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn ge<T: Ord>(v1: &T, v2: &T) -> bool {
|
||||
(*v1).ge(v2)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn gt<T: Ord>(v1: &T, v2: &T) -> bool {
|
||||
(*v1).gt(v2)
|
||||
}
|
||||
|
@ -28,6 +28,7 @@ pub enum Either<T, U> {
|
||||
Right(U)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn either<T, U, V>(f_left: fn(&T) -> V,
|
||||
f_right: fn(&U) -> V, value: &Either<T, U>) -> V {
|
||||
/*!
|
||||
@ -90,6 +91,7 @@ pub fn partition<T, U>(eithers: ~[Either<T, U>])
|
||||
return (move lefts, move rights);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn flip<T, U>(eith: Either<T, U>) -> Either<U, T> {
|
||||
//! Flips between left and right of a given either
|
||||
|
||||
@ -99,6 +101,7 @@ pub pure fn flip<T, U>(eith: Either<T, U>) -> Either<U, T> {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn to_result<T, U>(eith: Either<T, U>)
|
||||
-> Result<U, T> {
|
||||
/*!
|
||||
@ -114,18 +117,21 @@ pub pure fn to_result<T, U>(eith: Either<T, U>)
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn is_left<T, U>(eith: &Either<T, U>) -> bool {
|
||||
//! Checks whether the given value is a left
|
||||
|
||||
match *eith { Left(_) => true, _ => false }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn is_right<T, U>(eith: &Either<T, U>) -> bool {
|
||||
//! Checks whether the given value is a right
|
||||
|
||||
match *eith { Right(_) => true, _ => false }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn unwrap_left<T,U>(eith: Either<T,U>) -> T {
|
||||
//! Retrieves the value in the left branch. Fails if the either is Right.
|
||||
|
||||
@ -134,6 +140,7 @@ pub pure fn unwrap_left<T,U>(eith: Either<T,U>) -> T {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn unwrap_right<T,U>(eith: Either<T,U>) -> U {
|
||||
//! Retrieves the value in the right branch. Fails if the either is Left.
|
||||
|
||||
|
@ -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 }
|
||||
}
|
||||
|
||||
|
@ -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 }
|
||||
}
|
||||
|
||||
|
@ -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 }
|
||||
}
|
||||
|
||||
|
@ -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<T> {
|
||||
}
|
||||
|
||||
/// Parse a string to an int
|
||||
#[inline(always)]
|
||||
pub pure fn from_str(s: &str) -> Option<T>
|
||||
{
|
||||
parse_bytes(str::to_bytes(s), 10u)
|
||||
}
|
||||
|
||||
impl T : FromStr {
|
||||
#[inline(always)]
|
||||
static pure fn from_str(s: &str) -> Option<T> { 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<U>(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<U>(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]
|
||||
|
@ -24,29 +24,38 @@ use option::Option;
|
||||
use self::inst::{IMPL_T, EACH, SIZE_HINT};
|
||||
|
||||
impl<A> IMPL_T<A>: iter::BaseIter<A> {
|
||||
#[inline(always)]
|
||||
pure fn each(&self, blk: fn(v: &A) -> bool) { EACH(self, blk) }
|
||||
#[inline(always)]
|
||||
pure fn size_hint(&self) -> Option<uint> { SIZE_HINT(self) }
|
||||
}
|
||||
|
||||
impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
|
||||
#[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<B>(&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<uint> {
|
||||
iter::position(self, f)
|
||||
}
|
||||
#[inline(always)]
|
||||
pure fn map_to_vec<B>(&self, op: fn(&A) -> B) -> ~[B] {
|
||||
iter::map_to_vec(self, op)
|
||||
}
|
||||
#[inline(always)]
|
||||
pure fn flat_map_to_vec<B,IB:BaseIter<B>>(&self, op: fn(&A) -> IB)
|
||||
-> ~[B] {
|
||||
iter::flat_map_to_vec(self, op)
|
||||
@ -55,22 +64,29 @@ impl<A> IMPL_T<A>: iter::ExtendedIter<A> {
|
||||
}
|
||||
|
||||
impl<A: Eq> IMPL_T<A>: iter::EqIter<A> {
|
||||
#[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<A: Copy> IMPL_T<A>: iter::CopyableIter<A> {
|
||||
#[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<A> {
|
||||
iter::find(self, f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<A: Copy Ord> IMPL_T<A>: iter::CopyableOrderedIter<A> {
|
||||
#[inline(always)]
|
||||
pure fn min(&self) -> A { iter::min(self) }
|
||||
#[inline(always)]
|
||||
pure fn max(&self) -> A { iter::max(self) }
|
||||
}
|
||||
|
||||
|
@ -47,6 +47,7 @@ mod inst {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
|
||||
Some(self.len())
|
||||
}
|
||||
|
@ -20,6 +20,7 @@ mod inst {
|
||||
*
|
||||
* Attempts to access this dvec during iteration will fail.
|
||||
*/
|
||||
#[inline(always)]
|
||||
pub pure fn EACH<A>(self: &IMPL_T<A>, f: fn(v: &A) -> bool) {
|
||||
unsafe {
|
||||
do self.swap |v| {
|
||||
@ -29,6 +30,7 @@ mod inst {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
|
||||
Some(self.len())
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ mod inst {
|
||||
#[allow(non_camel_case_types)]
|
||||
pub type IMPL_T<A> = Option<A>;
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn EACH<A>(self: &IMPL_T<A>, f: fn(v: &A) -> bool) {
|
||||
match *self {
|
||||
None => (),
|
||||
@ -21,6 +22,7 @@ mod inst {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn SIZE_HINT<A>(self: &IMPL_T<A>) -> Option<uint> {
|
||||
match *self {
|
||||
None => Some(0),
|
||||
|
@ -88,6 +88,7 @@ pub trait Buildable<A> {
|
||||
builder: fn(push: pure fn(A))) -> self;
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn eachi<A,IA:BaseIter<A>>(self: &IA,
|
||||
blk: fn(uint, &A) -> bool) {
|
||||
let mut i = 0;
|
||||
@ -97,6 +98,7 @@ pub pure fn eachi<A,IA:BaseIter<A>>(self: &IA,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn all<A,IA:BaseIter<A>>(self: &IA,
|
||||
blk: fn(&A) -> bool) -> bool {
|
||||
for self.each |a| {
|
||||
@ -105,6 +107,7 @@ pub pure fn all<A,IA:BaseIter<A>>(self: &IA,
|
||||
return true;
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn any<A,IA:BaseIter<A>>(self: &IA,
|
||||
blk: fn(&A) -> bool) -> bool {
|
||||
for self.each |a| {
|
||||
@ -113,6 +116,7 @@ pub pure fn any<A,IA:BaseIter<A>>(self: &IA,
|
||||
return false;
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn filter_to_vec<A:Copy,IA:BaseIter<A>>(
|
||||
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<A:Copy,IA:BaseIter<A>>(
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn map_to_vec<A,B,IA:BaseIter<A>>(self: &IA,
|
||||
op: fn(&A) -> B)
|
||||
-> ~[B] {
|
||||
@ -132,6 +137,7 @@ pub pure fn map_to_vec<A,B,IA:BaseIter<A>>(self: &IA,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn flat_map_to_vec<A,B,IA:BaseIter<A>,IB:BaseIter<B>>(
|
||||
self: &IA, op: fn(&A) -> IB) -> ~[B] {
|
||||
do vec::build |push| {
|
||||
@ -143,6 +149,7 @@ pub pure fn flat_map_to_vec<A,B,IA:BaseIter<A>,IB:BaseIter<B>>(
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn foldl<A,B,IA:BaseIter<A>>(self: &IA, b0: B,
|
||||
blk: fn(&B, &A) -> B)
|
||||
-> B {
|
||||
@ -153,10 +160,12 @@ pub pure fn foldl<A,B,IA:BaseIter<A>>(self: &IA, b0: B,
|
||||
move b
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn to_vec<A:Copy,IA:BaseIter<A>>(self: &IA) -> ~[A] {
|
||||
foldl::<A,~[A],IA>(self, ~[], |r, a| vec::append(copy (*r), ~[*a]))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn contains<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> bool {
|
||||
for self.each |a| {
|
||||
if *a == *x { return true; }
|
||||
@ -164,6 +173,7 @@ pub pure fn contains<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> bool {
|
||||
return false;
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn count<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> uint {
|
||||
do foldl(self, 0) |count, value| {
|
||||
if *value == *x {
|
||||
@ -174,6 +184,7 @@ pub pure fn count<A:Eq,IA:BaseIter<A>>(self: &IA, x: &A) -> uint {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn position<A,IA:BaseIter<A>>(self: &IA, f: fn(&A) -> bool)
|
||||
-> Option<uint>
|
||||
{
|
||||
@ -189,6 +200,7 @@ pub pure fn position<A,IA:BaseIter<A>>(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<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
|
||||
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
|
||||
match a {
|
||||
@ -211,6 +224,7 @@ pub pure fn min<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
|
||||
match do foldl::<A,Option<A>,IA>(self, None) |a, b| {
|
||||
match a {
|
||||
@ -225,6 +239,7 @@ pub pure fn max<A:Copy Ord,IA:BaseIter<A>>(self: &IA) -> A {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn find<A: Copy,IA:BaseIter<A>>(self: &IA,
|
||||
f: fn(&A) -> bool) -> Option<A> {
|
||||
for self.each |i| {
|
||||
@ -275,6 +290,7 @@ pub pure fn build_sized_opt<A,B: Buildable<A>>(
|
||||
// Functions that combine iteration and building
|
||||
|
||||
/// Apply a function to each element of an iterable and return the results
|
||||
#[inline(always)]
|
||||
pub fn map<T,IT: BaseIter<T>,U,BU: Buildable<U>>(v: &IT, f: fn(&T) -> U)
|
||||
-> BU {
|
||||
do build_sized_opt(v.size_hint()) |push| {
|
||||
@ -290,6 +306,7 @@ pub fn map<T,IT: BaseIter<T>,U,BU: Buildable<U>>(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<T,BT: Buildable<T>>(n_elts: uint,
|
||||
op: InitOp<T>) -> BT {
|
||||
do Buildable::build_sized(n_elts) |push| {
|
||||
@ -304,6 +321,7 @@ pub pure fn from_fn<T,BT: Buildable<T>>(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<T: Copy,BT: Buildable<T>>(n_elts: uint,
|
||||
t: T) -> BT {
|
||||
do Buildable::build_sized(n_elts) |push| {
|
||||
|
@ -35,6 +35,7 @@ pub mod raw {
|
||||
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn ptr_eq<T>(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<T>(a: @T, b: @T) -> bool {
|
||||
|
||||
#[cfg(notest)]
|
||||
impl<T:Eq> @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<T:Ord> @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) }
|
||||
}
|
||||
|
||||
|
@ -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 }
|
||||
}
|
||||
|
||||
|
@ -59,6 +59,7 @@ pub enum Option<T> {
|
||||
Some(T),
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn get<T: Copy>(opt: Option<T>) -> T {
|
||||
/*!
|
||||
Gets the value out of an option
|
||||
@ -81,6 +82,7 @@ pub pure fn get<T: Copy>(opt: Option<T>) -> T {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn get_ref<T>(opt: &r/Option<T>) -> &r/T {
|
||||
/*!
|
||||
Gets an immutable reference to the value inside an option.
|
||||
@ -102,12 +104,14 @@ pub pure fn get_ref<T>(opt: &r/Option<T>) -> &r/T {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn map<T, U>(opt: &Option<T>, f: fn(x: &T) -> U) -> Option<U> {
|
||||
//! 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<T, U>(opt: Option<T>,
|
||||
f: fn(v: T) -> U) -> Option<U> {
|
||||
/*!
|
||||
@ -117,6 +121,7 @@ pub pure fn map_consume<T, U>(opt: Option<T>,
|
||||
if opt.is_some() { Some(f(option::unwrap(move opt))) } else { None }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn chain<T, U>(opt: Option<T>,
|
||||
f: fn(t: T) -> Option<U>) -> Option<U> {
|
||||
/*!
|
||||
@ -130,6 +135,7 @@ pub pure fn chain<T, U>(opt: Option<T>,
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn chain_ref<T, U>(opt: &Option<T>,
|
||||
f: fn(x: &T) -> Option<U>) -> Option<U> {
|
||||
/*!
|
||||
@ -140,6 +146,7 @@ pub pure fn chain_ref<T, U>(opt: &Option<T>,
|
||||
match *opt { Some(ref x) => f(x), None => None }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn or<T>(opta: Option<T>, optb: Option<T>) -> Option<T> {
|
||||
/*!
|
||||
* Returns the leftmost some() value, or none if both are none.
|
||||
@ -160,30 +167,35 @@ pub pure fn while_some<T>(x: Option<T>, blk: fn(v: T) -> Option<T>) {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn is_none<T>(opt: &Option<T>) -> bool {
|
||||
//! Returns true if the option equals `none`
|
||||
|
||||
match *opt { None => true, Some(_) => false }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn is_some<T>(opt: &Option<T>) -> bool {
|
||||
//! Returns true if the option contains some value
|
||||
|
||||
!is_none(opt)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn get_or_zero<T: Copy Zero>(opt: Option<T>) -> 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<T: Copy>(opt: Option<T>, 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<T, U>(opt: &Option<T>, 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<T, U>(opt: &Option<T>, def: U,
|
||||
match *opt { None => move def, Some(ref t) => f(t) }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn iter<T>(opt: &Option<T>, 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<T>(opt: &mut Option<T>) -> T {
|
||||
unwrap(util::replace(opt, None))
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub pure fn expect<T>(opt: Option<T>, reason: &str) -> T {
|
||||
//! As unwrap, but with a specified failure message.
|
||||
match move opt {
|
||||
|
@ -18,15 +18,21 @@ use cmp::{Eq, Ord};
|
||||
|
||||
#[cfg(notest)]
|
||||
impl<T:Eq> ~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<T:Ord> ~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) }
|
||||
}
|
||||
|
||||
|
@ -108,9 +108,11 @@ pub pure fn null<T>() -> *T { unsafe { cast::reinterpret_cast(&0u) } }
|
||||
pub pure fn mut_null<T>() -> *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<T>(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<T>(ptr: *const T) -> bool { !is_null(ptr) }
|
||||
|
||||
/**
|
||||
@ -231,32 +233,38 @@ impl<T> *mut T: Ptr<T> {
|
||||
// Equality for pointers
|
||||
#[cfg(notest)]
|
||||
impl<T> *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<T> *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<T> *const T : Ord {
|
||||
// Equality for region pointers
|
||||
#[cfg(notest)]
|
||||
impl<T:Eq> &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<T:Eq> &const T : Eq {
|
||||
// Comparison for region pointers
|
||||
#[cfg(notest)]
|
||||
impl<T:Ord> &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)
|
||||
}
|
||||
|
@ -39,6 +39,7 @@ pub enum Result<T, U> {
|
||||
*
|
||||
* If the result is an error
|
||||
*/
|
||||
#[inline(always)]
|
||||
pub pure fn get<T: Copy, U>(res: &Result<T, U>) -> T {
|
||||
match *res {
|
||||
Ok(copy t) => t,
|
||||
@ -55,6 +56,7 @@ pub pure fn get<T: Copy, U>(res: &Result<T, U>) -> T {
|
||||
*
|
||||
* If the result is an error
|
||||
*/
|
||||
#[inline(always)]
|
||||
pub pure fn get_ref<T, U>(res: &a/Result<T, U>) -> &a/T {
|
||||
match *res {
|
||||
Ok(ref t) => t,
|
||||
@ -71,6 +73,7 @@ pub pure fn get_ref<T, U>(res: &a/Result<T, U>) -> &a/T {
|
||||
*
|
||||
* If the result is not an error
|
||||
*/
|
||||
#[inline(always)]
|
||||
pub pure fn get_err<T, U: Copy>(res: &Result<T, U>) -> U {
|
||||
match *res {
|
||||
Err(copy u) => u,
|
||||
@ -79,6 +82,7 @@ pub pure fn get_err<T, U: Copy>(res: &Result<T, U>) -> U {
|
||||
}
|
||||
|
||||
/// Returns true if the result is `ok`
|
||||
#[inline(always)]
|
||||
pub pure fn is_ok<T, U>(res: &Result<T, U>) -> bool {
|
||||
match *res {
|
||||
Ok(_) => true,
|
||||
@ -87,6 +91,7 @@ pub pure fn is_ok<T, U>(res: &Result<T, U>) -> bool {
|
||||
}
|
||||
|
||||
/// Returns true if the result is `err`
|
||||
#[inline(always)]
|
||||
pub pure fn is_err<T, U>(res: &Result<T, U>) -> bool {
|
||||
!is_ok(res)
|
||||
}
|
||||
@ -97,6 +102,7 @@ pub pure fn is_err<T, U>(res: &Result<T, U>) -> 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<T: Copy, U: Copy>(res: &Result<U, T>)
|
||||
-> Either<T, U> {
|
||||
match *res {
|
||||
@ -119,6 +125,7 @@ pub pure fn to_either<T: Copy, U: Copy>(res: &Result<U, T>)
|
||||
* ok(parse_bytes(buf))
|
||||
* }
|
||||
*/
|
||||
#[inline(always)]
|
||||
pub pure fn chain<T, U, V>(res: Result<T, V>, op: fn(T)
|
||||
-> Result<U, V>) -> Result<U, V> {
|
||||
match move res {
|
||||
@ -135,6 +142,7 @@ pub pure fn chain<T, U, V>(res: Result<T, V>, 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<T, U, V>(
|
||||
res: Result<T, V>,
|
||||
op: fn(t: V) -> Result<T, U>)
|
||||
@ -159,6 +167,7 @@ pub pure fn chain_err<T, U, V>(
|
||||
* print_buf(buf)
|
||||
* }
|
||||
*/
|
||||
#[inline(always)]
|
||||
pub pure fn iter<T, E>(res: &Result<T, E>, f: fn(&T)) {
|
||||
match *res {
|
||||
Ok(ref t) => f(t),
|
||||
@ -174,6 +183,7 @@ pub pure fn iter<T, E>(res: &Result<T, E>, 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<T, E>(res: &Result<T, E>, f: fn(&E)) {
|
||||
match *res {
|
||||
Ok(_) => (),
|
||||
@ -195,6 +205,7 @@ pub pure fn iter_err<T, E>(res: &Result<T, E>, f: fn(&E)) {
|
||||
* parse_bytes(buf)
|
||||
* }
|
||||
*/
|
||||
#[inline(always)]
|
||||
pub pure fn map<T, E: Copy, U: Copy>(res: &Result<T, E>, op: fn(&T) -> U)
|
||||
-> Result<U, E> {
|
||||
match *res {
|
||||
@ -211,6 +222,7 @@ pub pure fn map<T, E: Copy, U: Copy>(res: &Result<T, E>, 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<T: Copy, E, F: Copy>(res: &Result<T, E>, op: fn(&E) -> F)
|
||||
-> Result<T, F> {
|
||||
match *res {
|
||||
@ -289,6 +301,7 @@ impl<T, E: Copy> Result<T, E> {
|
||||
* assert incd == ~[2u, 3u, 4u];
|
||||
* }
|
||||
*/
|
||||
#[inline(always)]
|
||||
pub fn map_vec<T,U:Copy,V:Copy>(
|
||||
ts: &[T], op: fn(&T) -> Result<V,U>) -> Result<~[V],U> {
|
||||
|
||||
@ -302,6 +315,7 @@ pub fn map_vec<T,U:Copy,V:Copy>(
|
||||
return Ok(move vs);
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
pub fn map_opt<T,U:Copy,V:Copy>(
|
||||
o_t: &Option<T>, op: fn(&T) -> Result<V,U>) -> Result<Option<V>,U> {
|
||||
|
||||
@ -323,6 +337,7 @@ pub fn map_opt<T,U:Copy,V:Copy>(
|
||||
* 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<S,T,U:Copy,V:Copy>(ss: &[S], ts: &[T],
|
||||
op: fn(&S,&T) -> Result<V,U>) -> Result<~[V],U> {
|
||||
|
||||
@ -345,6 +360,7 @@ pub fn map_vec2<S,T,U:Copy,V:Copy>(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<S,T,U:Copy>(ss: &[S], ts: &[T],
|
||||
op: fn(&S,&T) -> Result<(),U>) -> Result<(),U> {
|
||||
|
||||
|
@ -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: ToStr Copy, B: ToStr Copy> (A, B): ToStr {
|
||||
#[inline(always)]
|
||||
pure fn to_str() -> ~str {
|
||||
let (a, b) = self;
|
||||
~"(" + a.to_str() + ~", " + b.to_str() + ~")"
|
||||
}
|
||||
}
|
||||
impl<A: ToStr Copy, B: ToStr Copy, C: ToStr Copy> (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: ToStr Copy, B: ToStr Copy, C: ToStr Copy> (A, B, C): ToStr {
|
||||
}
|
||||
|
||||
impl<A: ToStr> ~[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> ~[A]: ToStr {
|
||||
}
|
||||
|
||||
impl<A: ToStr> @A: ToStr {
|
||||
#[inline(always)]
|
||||
pure fn to_str() -> ~str { ~"@" + (*self).to_str() }
|
||||
}
|
||||
impl<A: ToStr> ~A: ToStr {
|
||||
#[inline(always)]
|
||||
pure fn to_str() -> ~str { ~"~" + (*self).to_str() }
|
||||
}
|
||||
|
||||
|
@ -27,18 +27,21 @@ pub trait CopyableTuple<T, U> {
|
||||
impl<T: Copy, U: Copy> (T, U): CopyableTuple<T, U> {
|
||||
|
||||
/// 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<T, U> {
|
||||
}
|
||||
|
||||
impl<T, U> (T, U): ImmutableTuple<T, U> {
|
||||
#[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<A,B> {
|
||||
}
|
||||
|
||||
impl<A: Copy, B: Copy> (&[A], &[B]): ExtendedTupleOps<A,B> {
|
||||
#[inline(always)]
|
||||
fn zip(&self) -> ~[(A, B)] {
|
||||
match *self {
|
||||
(ref a, ref b) => {
|
||||
@ -78,6 +84,7 @@ impl<A: Copy, B: Copy> (&[A], &[B]): ExtendedTupleOps<A,B> {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C] {
|
||||
match *self {
|
||||
(ref a, ref b) => {
|
||||
@ -89,6 +96,7 @@ impl<A: Copy, B: Copy> (&[A], &[B]): ExtendedTupleOps<A,B> {
|
||||
|
||||
impl<A: Copy, B: Copy> (~[A], ~[B]): ExtendedTupleOps<A,B> {
|
||||
|
||||
#[inline(always)]
|
||||
fn zip(&self) -> ~[(A, B)] {
|
||||
match *self {
|
||||
(ref a, ref b) => {
|
||||
@ -97,6 +105,7 @@ impl<A: Copy, B: Copy> (~[A], ~[B]): ExtendedTupleOps<A,B> {
|
||||
}
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn map<C>(&self, f: &fn(a: &A, b: &B) -> C) -> ~[C] {
|
||||
match *self {
|
||||
(ref a, ref b) => {
|
||||
@ -108,6 +117,7 @@ impl<A: Copy, B: Copy> (~[A], ~[B]): ExtendedTupleOps<A,B> {
|
||||
|
||||
#[cfg(notest)]
|
||||
impl<A: Eq, B: Eq> (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: Eq, B: Eq> (A, B) : Eq {
|
||||
}
|
||||
}
|
||||
}
|
||||
#[inline(always)]
|
||||
pure fn ne(&self, other: &(A, B)) -> bool { !(*self).eq(other) }
|
||||
}
|
||||
|
||||
#[cfg(notest)]
|
||||
impl<A: Ord, B: Ord> (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: Ord, B: Ord> (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: Eq, B: Eq, C: Eq> (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: Eq, B: Eq, C: Eq> (A, B, C) : Eq {
|
||||
}
|
||||
}
|
||||
}
|
||||
#[inline(always)]
|
||||
pure fn ne(&self, other: &(A, B, C)) -> bool { !(*self).eq(other) }
|
||||
}
|
||||
|
||||
#[cfg(notest)]
|
||||
impl<A: Ord, B: Ord, C: Ord> (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: Ord, B: Ord, C: Ord> (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)) }
|
||||
}
|
||||
|
||||
|
@ -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<T> {
|
||||
}
|
||||
|
||||
/// Parse a string to an int
|
||||
#[inline(always)]
|
||||
pub pure fn from_str(s: &str) -> Option<T>
|
||||
{
|
||||
parse_bytes(str::to_bytes(s), 10u)
|
||||
}
|
||||
|
||||
impl T : FromStr {
|
||||
#[inline(always)]
|
||||
static pure fn from_str(s: &str) -> Option<T> { from_str(s) }
|
||||
}
|
||||
|
||||
@ -177,6 +213,7 @@ pub fn from_str_radix(buf: &str, radix: u64) -> Option<u64> {
|
||||
*
|
||||
* 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<U>(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]
|
||||
|
Loading…
x
Reference in New Issue
Block a user