Skip to content

Commit 0b1a0d0

Browse files
committed
std::rand: move the Rand impls into a separate file for neatness.
1 parent 6f4ec72 commit 0b1a0d0

File tree

2 files changed

+201
-167
lines changed

2 files changed

+201
-167
lines changed

src/libstd/rand/mod.rs

Lines changed: 2 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
22
// file at the top-level directory of this distribution and at
33
// http://rust-lang.org/COPYRIGHT.
44
//
@@ -45,14 +45,11 @@ fn main () {
4545

4646
use cast;
4747
use container::Container;
48-
use int;
4948
use iter::{Iterator, range};
5049
use local_data;
5150
use prelude::*;
5251
use str;
53-
use u32;
5452
use u64;
55-
use uint;
5653
use vec;
5754
use os::getenv;
5855

@@ -64,6 +61,7 @@ pub mod isaac;
6461
pub mod os;
6562
pub mod reader;
6663
pub mod reseeding;
64+
mod rand_impls;
6765

6866
/// A type that can be randomly generated using an Rng
6967
pub trait Rand {
@@ -72,169 +70,6 @@ pub trait Rand {
7270
fn rand<R: Rng>(rng: &mut R) -> Self;
7371
}
7472

75-
impl Rand for int {
76-
#[inline]
77-
fn rand<R: Rng>(rng: &mut R) -> int {
78-
if int::bits == 32 {
79-
rng.gen::<i32>() as int
80-
} else {
81-
rng.gen::<i64>() as int
82-
}
83-
}
84-
}
85-
86-
impl Rand for i8 {
87-
#[inline]
88-
fn rand<R: Rng>(rng: &mut R) -> i8 {
89-
rng.next_u32() as i8
90-
}
91-
}
92-
93-
impl Rand for i16 {
94-
#[inline]
95-
fn rand<R: Rng>(rng: &mut R) -> i16 {
96-
rng.next_u32() as i16
97-
}
98-
}
99-
100-
impl Rand for i32 {
101-
#[inline]
102-
fn rand<R: Rng>(rng: &mut R) -> i32 {
103-
rng.next_u32() as i32
104-
}
105-
}
106-
107-
impl Rand for i64 {
108-
#[inline]
109-
fn rand<R: Rng>(rng: &mut R) -> i64 {
110-
rng.next_u64() as i64
111-
}
112-
}
113-
114-
impl Rand for uint {
115-
#[inline]
116-
fn rand<R: Rng>(rng: &mut R) -> uint {
117-
if uint::bits == 32 {
118-
rng.gen::<u32>() as uint
119-
} else {
120-
rng.gen::<u64>() as uint
121-
}
122-
}
123-
}
124-
125-
impl Rand for u8 {
126-
#[inline]
127-
fn rand<R: Rng>(rng: &mut R) -> u8 {
128-
rng.next_u32() as u8
129-
}
130-
}
131-
132-
impl Rand for u16 {
133-
#[inline]
134-
fn rand<R: Rng>(rng: &mut R) -> u16 {
135-
rng.next_u32() as u16
136-
}
137-
}
138-
139-
impl Rand for u32 {
140-
#[inline]
141-
fn rand<R: Rng>(rng: &mut R) -> u32 {
142-
rng.next_u32()
143-
}
144-
}
145-
146-
impl Rand for u64 {
147-
#[inline]
148-
fn rand<R: Rng>(rng: &mut R) -> u64 {
149-
rng.next_u64()
150-
}
151-
}
152-
153-
impl Rand for f32 {
154-
#[inline]
155-
fn rand<R: Rng>(rng: &mut R) -> f32 {
156-
rng.gen::<f64>() as f32
157-
}
158-
}
159-
160-
static SCALE : f64 = (u32::max_value as f64) + 1.0f64;
161-
impl Rand for f64 {
162-
#[inline]
163-
fn rand<R: Rng>(rng: &mut R) -> f64 {
164-
let u1 = rng.next_u32() as f64;
165-
let u2 = rng.next_u32() as f64;
166-
let u3 = rng.next_u32() as f64;
167-
168-
((u1 / SCALE + u2) / SCALE + u3) / SCALE
169-
}
170-
}
171-
172-
impl Rand for bool {
173-
#[inline]
174-
fn rand<R: Rng>(rng: &mut R) -> bool {
175-
rng.gen::<u8>() & 1 == 1
176-
}
177-
}
178-
179-
macro_rules! tuple_impl {
180-
// use variables to indicate the arity of the tuple
181-
($($tyvar:ident),* ) => {
182-
// the trailing commas are for the 1 tuple
183-
impl<
184-
$( $tyvar : Rand ),*
185-
> Rand for ( $( $tyvar ),* , ) {
186-
187-
#[inline]
188-
fn rand<R: Rng>(_rng: &mut R) -> ( $( $tyvar ),* , ) {
189-
(
190-
// use the $tyvar's to get the appropriate number of
191-
// repeats (they're not actually needed)
192-
$(
193-
_rng.gen::<$tyvar>()
194-
),*
195-
,
196-
)
197-
}
198-
}
199-
}
200-
}
201-
202-
impl Rand for () {
203-
#[inline]
204-
fn rand<R: Rng>(_: &mut R) -> () { () }
205-
}
206-
tuple_impl!{A}
207-
tuple_impl!{A, B}
208-
tuple_impl!{A, B, C}
209-
tuple_impl!{A, B, C, D}
210-
tuple_impl!{A, B, C, D, E}
211-
tuple_impl!{A, B, C, D, E, F}
212-
tuple_impl!{A, B, C, D, E, F, G}
213-
tuple_impl!{A, B, C, D, E, F, G, H}
214-
tuple_impl!{A, B, C, D, E, F, G, H, I}
215-
tuple_impl!{A, B, C, D, E, F, G, H, I, J}
216-
217-
impl<T:Rand> Rand for Option<T> {
218-
#[inline]
219-
fn rand<R: Rng>(rng: &mut R) -> Option<T> {
220-
if rng.gen() {
221-
Some(rng.gen())
222-
} else {
223-
None
224-
}
225-
}
226-
}
227-
228-
impl<T: Rand> Rand for ~T {
229-
#[inline]
230-
fn rand<R: Rng>(rng: &mut R) -> ~T { ~rng.gen() }
231-
}
232-
233-
impl<T: Rand + 'static> Rand for @T {
234-
#[inline]
235-
fn rand<R: Rng>(rng: &mut R) -> @T { @rng.gen() }
236-
}
237-
23873
/// A value with a particular weight compared to other values
23974
pub struct Weighted<T> {
24075
/// The numerical weight of this item

src/libstd/rand/rand_impls.rs

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
// Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! The implementations of `Rand` for the built-in types.
12+
13+
use char;
14+
use int;
15+
use option::{Option, Some, None};
16+
use rand::{Rand,Rng};
17+
use u32;
18+
use uint;
19+
20+
impl Rand for int {
21+
#[inline]
22+
fn rand<R: Rng>(rng: &mut R) -> int {
23+
if int::bits == 32 {
24+
rng.gen::<i32>() as int
25+
} else {
26+
rng.gen::<i64>() as int
27+
}
28+
}
29+
}
30+
31+
impl Rand for i8 {
32+
#[inline]
33+
fn rand<R: Rng>(rng: &mut R) -> i8 {
34+
rng.next_u32() as i8
35+
}
36+
}
37+
38+
impl Rand for i16 {
39+
#[inline]
40+
fn rand<R: Rng>(rng: &mut R) -> i16 {
41+
rng.next_u32() as i16
42+
}
43+
}
44+
45+
impl Rand for i32 {
46+
#[inline]
47+
fn rand<R: Rng>(rng: &mut R) -> i32 {
48+
rng.next_u32() as i32
49+
}
50+
}
51+
52+
impl Rand for i64 {
53+
#[inline]
54+
fn rand<R: Rng>(rng: &mut R) -> i64 {
55+
rng.next_u64() as i64
56+
}
57+
}
58+
59+
impl Rand for uint {
60+
#[inline]
61+
fn rand<R: Rng>(rng: &mut R) -> uint {
62+
if uint::bits == 32 {
63+
rng.gen::<u32>() as uint
64+
} else {
65+
rng.gen::<u64>() as uint
66+
}
67+
}
68+
}
69+
70+
impl Rand for u8 {
71+
#[inline]
72+
fn rand<R: Rng>(rng: &mut R) -> u8 {
73+
rng.next_u32() as u8
74+
}
75+
}
76+
77+
impl Rand for u16 {
78+
#[inline]
79+
fn rand<R: Rng>(rng: &mut R) -> u16 {
80+
rng.next_u32() as u16
81+
}
82+
}
83+
84+
impl Rand for u32 {
85+
#[inline]
86+
fn rand<R: Rng>(rng: &mut R) -> u32 {
87+
rng.next_u32()
88+
}
89+
}
90+
91+
impl Rand for u64 {
92+
#[inline]
93+
fn rand<R: Rng>(rng: &mut R) -> u64 {
94+
rng.next_u64()
95+
}
96+
}
97+
98+
impl Rand for f32 {
99+
#[inline]
100+
fn rand<R: Rng>(rng: &mut R) -> f32 {
101+
rng.gen::<f64>() as f32
102+
}
103+
}
104+
105+
static SCALE : f64 = (u32::max_value as f64) + 1.0f64;
106+
impl Rand for f64 {
107+
#[inline]
108+
fn rand<R: Rng>(rng: &mut R) -> f64 {
109+
let u1 = rng.next_u32() as f64;
110+
let u2 = rng.next_u32() as f64;
111+
let u3 = rng.next_u32() as f64;
112+
113+
((u1 / SCALE + u2) / SCALE + u3) / SCALE
114+
}
115+
}
116+
117+
118+
impl Rand for char {
119+
#[inline]
120+
fn rand<R: Rng>(rng: &mut R) -> char {
121+
// a char is 21 bits
122+
static CHAR_MASK: u32 = 0x001f_ffff;
123+
loop {
124+
// Rejection sampling. About 0.2% of numbers with at most
125+
// 21-bits are invalid codepoints (surrogates), so this
126+
// will succeed first go almost every time.
127+
match char::from_u32(rng.next_u32() & CHAR_MASK) {
128+
Some(c) => return c,
129+
None => {}
130+
}
131+
}
132+
}
133+
}
134+
135+
impl Rand for bool {
136+
#[inline]
137+
fn rand<R: Rng>(rng: &mut R) -> bool {
138+
rng.gen::<u8>() & 1 == 1
139+
}
140+
}
141+
142+
macro_rules! tuple_impl {
143+
// use variables to indicate the arity of the tuple
144+
($($tyvar:ident),* ) => {
145+
// the trailing commas are for the 1 tuple
146+
impl<
147+
$( $tyvar : Rand ),*
148+
> Rand for ( $( $tyvar ),* , ) {
149+
150+
#[inline]
151+
fn rand<R: Rng>(_rng: &mut R) -> ( $( $tyvar ),* , ) {
152+
(
153+
// use the $tyvar's to get the appropriate number of
154+
// repeats (they're not actually needed)
155+
$(
156+
_rng.gen::<$tyvar>()
157+
),*
158+
,
159+
)
160+
}
161+
}
162+
}
163+
}
164+
165+
impl Rand for () {
166+
#[inline]
167+
fn rand<R: Rng>(_: &mut R) -> () { () }
168+
}
169+
tuple_impl!{A}
170+
tuple_impl!{A, B}
171+
tuple_impl!{A, B, C}
172+
tuple_impl!{A, B, C, D}
173+
tuple_impl!{A, B, C, D, E}
174+
tuple_impl!{A, B, C, D, E, F}
175+
tuple_impl!{A, B, C, D, E, F, G}
176+
tuple_impl!{A, B, C, D, E, F, G, H}
177+
tuple_impl!{A, B, C, D, E, F, G, H, I}
178+
tuple_impl!{A, B, C, D, E, F, G, H, I, J}
179+
180+
impl<T:Rand> Rand for Option<T> {
181+
#[inline]
182+
fn rand<R: Rng>(rng: &mut R) -> Option<T> {
183+
if rng.gen() {
184+
Some(rng.gen())
185+
} else {
186+
None
187+
}
188+
}
189+
}
190+
191+
impl<T: Rand> Rand for ~T {
192+
#[inline]
193+
fn rand<R: Rng>(rng: &mut R) -> ~T { ~rng.gen() }
194+
}
195+
196+
impl<T: Rand + 'static> Rand for @T {
197+
#[inline]
198+
fn rand<R: Rng>(rng: &mut R) -> @T { @rng.gen() }
199+
}

0 commit comments

Comments
 (0)