1
- // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
1
+ // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2
2
// file at the top-level directory of this distribution and at
3
3
// http://rust-lang.org/COPYRIGHT.
4
4
//
@@ -45,14 +45,11 @@ fn main () {
45
45
46
46
use cast;
47
47
use container:: Container ;
48
- use int;
49
48
use iter:: { Iterator , range} ;
50
49
use local_data;
51
50
use prelude:: * ;
52
51
use str;
53
- use u32;
54
52
use u64;
55
- use uint;
56
53
use vec;
57
54
use os:: getenv;
58
55
@@ -64,6 +61,7 @@ pub mod isaac;
64
61
pub mod os;
65
62
pub mod reader;
66
63
pub mod reseeding;
64
+ mod rand_impls;
67
65
68
66
/// A type that can be randomly generated using an Rng
69
67
pub trait Rand {
@@ -72,169 +70,6 @@ pub trait Rand {
72
70
fn rand < R : Rng > ( rng : & mut R ) -> Self ;
73
71
}
74
72
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
-
238
73
/// A value with a particular weight compared to other values
239
74
pub struct Weighted < T > {
240
75
/// The numerical weight of this item
0 commit comments