mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-22 23:04:33 +00:00
core: add some inlining hints to methods/fns in rand.
This commit is contained in:
parent
30266a788f
commit
d4b934bdce
@ -53,6 +53,7 @@ pub trait Rand {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Rand for int {
|
impl Rand for int {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> int {
|
fn rand<R: Rng>(rng: &R) -> int {
|
||||||
if int::bits == 32 {
|
if int::bits == 32 {
|
||||||
rng.next() as int
|
rng.next() as int
|
||||||
@ -63,30 +64,35 @@ impl Rand for int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Rand for i8 {
|
impl Rand for i8 {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> i8 {
|
fn rand<R: Rng>(rng: &R) -> i8 {
|
||||||
rng.next() as i8
|
rng.next() as i8
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rand for i16 {
|
impl Rand for i16 {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> i16 {
|
fn rand<R: Rng>(rng: &R) -> i16 {
|
||||||
rng.next() as i16
|
rng.next() as i16
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rand for i32 {
|
impl Rand for i32 {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> i32 {
|
fn rand<R: Rng>(rng: &R) -> i32 {
|
||||||
rng.next() as i32
|
rng.next() as i32
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rand for i64 {
|
impl Rand for i64 {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> i64 {
|
fn rand<R: Rng>(rng: &R) -> i64 {
|
||||||
(rng.next() as i64 << 32) | rng.next() as i64
|
(rng.next() as i64 << 32) | rng.next() as i64
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rand for uint {
|
impl Rand for uint {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> uint {
|
fn rand<R: Rng>(rng: &R) -> uint {
|
||||||
if uint::bits == 32 {
|
if uint::bits == 32 {
|
||||||
rng.next() as uint
|
rng.next() as uint
|
||||||
@ -97,36 +103,42 @@ impl Rand for uint {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Rand for u8 {
|
impl Rand for u8 {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> u8 {
|
fn rand<R: Rng>(rng: &R) -> u8 {
|
||||||
rng.next() as u8
|
rng.next() as u8
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rand for u16 {
|
impl Rand for u16 {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> u16 {
|
fn rand<R: Rng>(rng: &R) -> u16 {
|
||||||
rng.next() as u16
|
rng.next() as u16
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rand for u32 {
|
impl Rand for u32 {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> u32 {
|
fn rand<R: Rng>(rng: &R) -> u32 {
|
||||||
rng.next()
|
rng.next()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rand for u64 {
|
impl Rand for u64 {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> u64 {
|
fn rand<R: Rng>(rng: &R) -> u64 {
|
||||||
(rng.next() as u64 << 32) | rng.next() as u64
|
(rng.next() as u64 << 32) | rng.next() as u64
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rand for float {
|
impl Rand for float {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> float {
|
fn rand<R: Rng>(rng: &R) -> float {
|
||||||
rng.gen::<f64>() as float
|
rng.gen::<f64>() as float
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rand for f32 {
|
impl Rand for f32 {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> f32 {
|
fn rand<R: Rng>(rng: &R) -> f32 {
|
||||||
rng.gen::<f64>() as f32
|
rng.gen::<f64>() as f32
|
||||||
}
|
}
|
||||||
@ -134,6 +146,7 @@ impl Rand for f32 {
|
|||||||
|
|
||||||
static scale : f64 = (u32::max_value as f64) + 1.0f64;
|
static scale : f64 = (u32::max_value as f64) + 1.0f64;
|
||||||
impl Rand for f64 {
|
impl Rand for f64 {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> f64 {
|
fn rand<R: Rng>(rng: &R) -> f64 {
|
||||||
let u1 = rng.next() as f64;
|
let u1 = rng.next() as f64;
|
||||||
let u2 = rng.next() as f64;
|
let u2 = rng.next() as f64;
|
||||||
@ -144,12 +157,14 @@ impl Rand for f64 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Rand for char {
|
impl Rand for char {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> char {
|
fn rand<R: Rng>(rng: &R) -> char {
|
||||||
rng.next() as char
|
rng.next() as char
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rand for bool {
|
impl Rand for bool {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> bool {
|
fn rand<R: Rng>(rng: &R) -> bool {
|
||||||
rng.next() & 1u32 == 1u32
|
rng.next() & 1u32 == 1u32
|
||||||
}
|
}
|
||||||
@ -163,6 +178,7 @@ macro_rules! tuple_impl {
|
|||||||
$( $tyvar : Rand ),*
|
$( $tyvar : Rand ),*
|
||||||
> Rand for ( $( $tyvar ),* , ) {
|
> Rand for ( $( $tyvar ),* , ) {
|
||||||
|
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(_rng: &R) -> ( $( $tyvar ),* , ) {
|
fn rand<R: Rng>(_rng: &R) -> ( $( $tyvar ),* , ) {
|
||||||
(
|
(
|
||||||
// use the $tyvar's to get the appropriate number of
|
// use the $tyvar's to get the appropriate number of
|
||||||
@ -177,7 +193,10 @@ macro_rules! tuple_impl {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rand for () { fn rand<R: Rng>(_: &R) -> () { () } }
|
impl Rand for () {
|
||||||
|
#[inline]
|
||||||
|
fn rand<R: Rng>(_: &R) -> () { () }
|
||||||
|
}
|
||||||
tuple_impl!{A}
|
tuple_impl!{A}
|
||||||
tuple_impl!{A, B}
|
tuple_impl!{A, B}
|
||||||
tuple_impl!{A, B, C}
|
tuple_impl!{A, B, C}
|
||||||
@ -190,6 +209,7 @@ tuple_impl!{A, B, C, D, E, F, G, H, I}
|
|||||||
tuple_impl!{A, B, C, D, E, F, G, H, I, J}
|
tuple_impl!{A, B, C, D, E, F, G, H, I, J}
|
||||||
|
|
||||||
impl<T:Rand> Rand for Option<T> {
|
impl<T:Rand> Rand for Option<T> {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> Option<T> {
|
fn rand<R: Rng>(rng: &R) -> Option<T> {
|
||||||
if rng.gen() {
|
if rng.gen() {
|
||||||
Some(rng.gen())
|
Some(rng.gen())
|
||||||
@ -200,10 +220,12 @@ impl<T:Rand> Rand for Option<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Rand> Rand for ~T {
|
impl<T: Rand> Rand for ~T {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> ~T { ~rng.gen() }
|
fn rand<R: Rng>(rng: &R) -> ~T { ~rng.gen() }
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: Rand> Rand for @T {
|
impl<T: Rand> Rand for @T {
|
||||||
|
#[inline]
|
||||||
fn rand<R: Rng>(rng: &R) -> @T { @rng.gen() }
|
fn rand<R: Rng>(rng: &R) -> @T { @rng.gen() }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -413,6 +435,7 @@ pub trait RngUtil {
|
|||||||
/// Extension methods for random number generators
|
/// Extension methods for random number generators
|
||||||
impl<R: Rng> RngUtil for R {
|
impl<R: Rng> RngUtil for R {
|
||||||
/// Return a random value for a Rand type
|
/// Return a random value for a Rand type
|
||||||
|
#[inline(always)]
|
||||||
fn gen<T: Rand>(&self) -> T {
|
fn gen<T: Rand>(&self) -> T {
|
||||||
Rand::rand(self)
|
Rand::rand(self)
|
||||||
}
|
}
|
||||||
@ -675,6 +698,7 @@ pub impl IsaacRng {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Refills the output buffer (`self.rsl`)
|
/// Refills the output buffer (`self.rsl`)
|
||||||
|
#[inline]
|
||||||
priv fn isaac(&self) {
|
priv fn isaac(&self) {
|
||||||
self.c += 1;
|
self.c += 1;
|
||||||
// abbreviations
|
// abbreviations
|
||||||
@ -731,6 +755,9 @@ impl Rng for IsaacRng {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// An [Xorshift random number
|
||||||
|
/// generator](http://en.wikipedia.org/wiki/Xorshift). Not suitable for
|
||||||
|
/// cryptographic purposes.
|
||||||
pub struct XorShiftRng {
|
pub struct XorShiftRng {
|
||||||
priv mut x: u32,
|
priv mut x: u32,
|
||||||
priv mut y: u32,
|
priv mut y: u32,
|
||||||
@ -739,6 +766,7 @@ pub struct XorShiftRng {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Rng for XorShiftRng {
|
impl Rng for XorShiftRng {
|
||||||
|
#[inline]
|
||||||
pub fn next(&self) -> u32 {
|
pub fn next(&self) -> u32 {
|
||||||
let x = self.x;
|
let x = self.x;
|
||||||
let t = x ^ (x << 11);
|
let t = x ^ (x << 11);
|
||||||
@ -788,6 +816,7 @@ fn tls_rng_state(_v: @IsaacRng) {}
|
|||||||
* seeded by the system. Intended to be used in method chaining style, ie
|
* seeded by the system. Intended to be used in method chaining style, ie
|
||||||
* `task_rng().gen::<int>()`.
|
* `task_rng().gen::<int>()`.
|
||||||
*/
|
*/
|
||||||
|
#[inline]
|
||||||
pub fn task_rng() -> @IsaacRng {
|
pub fn task_rng() -> @IsaacRng {
|
||||||
let r : Option<@IsaacRng>;
|
let r : Option<@IsaacRng>;
|
||||||
unsafe {
|
unsafe {
|
||||||
@ -807,6 +836,7 @@ pub fn task_rng() -> @IsaacRng {
|
|||||||
|
|
||||||
// Allow direct chaining with `task_rng`
|
// Allow direct chaining with `task_rng`
|
||||||
impl<R: Rng> Rng for @R {
|
impl<R: Rng> Rng for @R {
|
||||||
|
#[inline(always)]
|
||||||
fn next(&self) -> u32 { (**self).next() }
|
fn next(&self) -> u32 { (**self).next() }
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -814,6 +844,7 @@ impl<R: Rng> Rng for @R {
|
|||||||
* Returns a random value of a Rand type, using the task's random number
|
* Returns a random value of a Rand type, using the task's random number
|
||||||
* generator.
|
* generator.
|
||||||
*/
|
*/
|
||||||
|
#[inline]
|
||||||
pub fn random<T: Rand>() -> T {
|
pub fn random<T: Rand>() -> T {
|
||||||
task_rng().gen()
|
task_rng().gen()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user