Skip to content

Commit 87c649f

Browse files
committed
---
yaml --- r: 56795 b: refs/heads/try c: 4b7d363 h: refs/heads/master i: 56793: 347cb45 56791: 6326ff4 v: v3
1 parent 057896e commit 87c649f

File tree

3 files changed

+177
-35
lines changed

3 files changed

+177
-35
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: c081ffbd1e845687202a975ea2e698b623e5722f
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 79a2b2eafc3c766cecec8a5f76317693bae9ed17
5-
refs/heads/try: 11f5f73b2b7b287cee16d300a10b1c50776c38d6
5+
refs/heads/try: 4b7d3634955ca6c7f4632d2c74ccc0137438e744
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libcore/iter.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -344,29 +344,3 @@ pub fn copy_seq<T:Copy,IT:BaseIter<T>,BT:Buildable<T>>(v: &IT) -> BT {
344344
for v.each |x| { push(*x); }
345345
}
346346
}
347-
348-
/**
349-
* Helper function to transform an internal iterator into an owned vector.
350-
*
351-
* # Example:
352-
*
353-
* ~~~
354-
* let v = ~[1, 2, 3];
355-
* let v2 = do iter_to_vec |f| { v.each(|e| f(*e)) };
356-
* if v != v2 { fail!() }
357-
* ~~~
358-
*/
359-
#[inline(always)]
360-
pub fn iter_to_vec<T>(pusher: &fn(it: &fn(T) -> bool)) -> ~[T] {
361-
let mut v = ~[];
362-
let pushf = |e| {v.push(e); true};
363-
pusher(pushf);
364-
v
365-
}
366-
367-
#[test]
368-
fn test_iter_to_vec() {
369-
let v = ~[1, 2, 3];
370-
let v2 = do iter_to_vec |f| { v.each(|e| f(*e)) };
371-
if v != v2 { fail!() }
372-
}

branches/try/src/libcore/rand.rs

Lines changed: 176 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,21 @@ pub struct Weighted<T> {
150150

151151
pub trait RngUtil {
152152
fn gen<T:Rand>(&self) -> T;
153-
/// Return a random int
153+
/**
154+
* Return a random int
155+
*
156+
* *Example*
157+
*
158+
* ~~~
159+
*
160+
* use core::rand::RngUtil;
161+
*
162+
* fn main() {
163+
* rng = rand::Rng();
164+
* println(fmt!("%d",rng.gen_int()));
165+
* }
166+
* ~~~
167+
*/
154168
fn gen_int(&self) -> int;
155169
fn gen_int_range(&self, start: int, end: int) -> int;
156170
/// Return a random i8
@@ -176,7 +190,21 @@ pub trait RngUtil {
176190
fn gen_u32(&self) -> u32;
177191
/// Return a random u64
178192
fn gen_u64(&self) -> u64;
179-
/// Return a random float in the interval [0,1]
193+
/**
194+
* Return random float in the interval [0,1]
195+
*
196+
* *Example*
197+
*
198+
* ~~~
199+
*
200+
* use core::rand::RngUtil;
201+
*
202+
* fn main() {
203+
* rng = rand::Rng();
204+
* println(fmt!("%f",rng.gen_float()));
205+
* }
206+
* ~~~
207+
*/
180208
fn gen_float(&self) -> float;
181209
/// Return a random f32 in the interval [0,1]
182210
fn gen_f32(&self) -> f32;
@@ -188,38 +216,178 @@ pub trait RngUtil {
188216
* Return a char randomly chosen from chars, failing if chars is empty
189217
*/
190218
fn gen_char_from(&self, chars: &str) -> char;
191-
/// Return a random bool
219+
/**
220+
* Return a random bool
221+
*
222+
* *Example*
223+
*
224+
* ~~~
225+
*
226+
* use core::rand::RngUtil;
227+
*
228+
* fn main() {
229+
* rng = rand::Rng();
230+
* println(fmt!("%b",rng.gen_bool()));
231+
* }
232+
* ~~~
233+
*/
192234
fn gen_bool(&self) -> bool;
193-
/// Return a bool with a 1 in n chance of true
235+
/**
236+
* Return a bool with a 1 in n chance of true
237+
*
238+
* *Example*
239+
*
240+
* ~~~
241+
*
242+
* use core::rand::RngUtil;
243+
*
244+
* fn main() {
245+
* rng = rand::Rng();
246+
* println(fmt!("%b",rng.gen_weighted_bool(3)));
247+
* }
248+
* ~~~
249+
*/
194250
fn gen_weighted_bool(&self, n: uint) -> bool;
195251
/**
196252
* Return a random string of the specified length composed of A-Z,a-z,0-9
253+
*
254+
* *Example*
255+
*
256+
* ~~~
257+
*
258+
* use core::rand::RngUtil;
259+
*
260+
* fn main() {
261+
* rng = rand::Rng();
262+
* println(rng.gen_str(8));
263+
* }
264+
* ~~~
197265
*/
198266
fn gen_str(&self, len: uint) -> ~str;
199-
/// Return a random byte string of the specified length
267+
/**
268+
* Return a random byte string of the specified length
269+
*
270+
* *Example*
271+
*
272+
* ~~~
273+
*
274+
* use core::rand::RngUtil;
275+
*
276+
* fn main() {
277+
* rng = rand::Rng();
278+
* println(fmt!("%?",rng.gen_bytes(8)));
279+
* }
280+
* ~~~
281+
*/
200282
fn gen_bytes(&self, len: uint) -> ~[u8];
201-
/// Choose an item randomly, failing if values is empty
283+
///
284+
/**
285+
* Choose an item randomly, failing if values is empty
286+
*
287+
* *Example*
288+
*
289+
* ~~~
290+
*
291+
* use core::rand::RngUtil;
292+
*
293+
* fn main() {
294+
* rng = rand::Rng();
295+
* println(fmt!("%d",rng.choose([1,2,4,8,16,32])));
296+
* }
297+
* ~~~
298+
*/
202299
fn choose<T:Copy>(&self, values: &[T]) -> T;
203300
/// Choose Some(item) randomly, returning None if values is empty
204301
fn choose_option<T:Copy>(&self, values: &[T]) -> Option<T>;
205302
/**
206303
* Choose an item respecting the relative weights, failing if the sum of
207304
* the weights is 0
305+
*
306+
* *Example*
307+
*
308+
* ~~~
309+
*
310+
* use core::rand::RngUtil;
311+
*
312+
* fn main() {
313+
* rng = rand::Rng();
314+
* let x = [rand::Weighted {weight: 4, item: 'a'}, rand::Weighted {weight: 2, item: 'b'}, rand::Weighted {weight: 2, item: 'c'}];
315+
* println(fmt!("%c",rng.choose_weighted(x)));
316+
* }
317+
* ~~~
208318
*/
209319
fn choose_weighted<T:Copy>(&self, v : &[Weighted<T>]) -> T;
210320
/**
211321
* Choose Some(item) respecting the relative weights, returning none if
212322
* the sum of the weights is 0
323+
*
324+
* *Example*
325+
*
326+
* ~~~
327+
*
328+
* use core::rand::RngUtil;
329+
*
330+
* fn main() {
331+
* rng = rand::Rng();
332+
* let x = [rand::Weighted {weight: 4, item: 'a'}, rand::Weighted {weight: 2, item: 'b'}, rand::Weighted {weight: 2, item: 'c'}];
333+
* println(fmt!("%?",rng.choose_weighted_option(x)));
334+
* }
335+
* ~~~
213336
*/
214337
fn choose_weighted_option<T:Copy>(&self, v: &[Weighted<T>]) -> Option<T>;
215338
/**
216339
* Return a vec containing copies of the items, in order, where
217340
* the weight of the item determines how many copies there are
341+
*
342+
* *Example*
343+
*
344+
* ~~~
345+
*
346+
* use core::rand::RngUtil;
347+
*
348+
* fn main() {
349+
* rng = rand::Rng();
350+
* let x = [rand::Weighted {weight: 4, item: 'a'}, rand::Weighted {weight: 2, item: 'b'}, rand::Weighted {weight: 2, item: 'c'}];
351+
* println(fmt!("%?",rng.weighted_vec(x)));
352+
* }
353+
* ~~~
218354
*/
219355
fn weighted_vec<T:Copy>(&self, v: &[Weighted<T>]) -> ~[T];
220-
/// Shuffle a vec
356+
/**
357+
* Shuffle a vec
358+
*
359+
* *Example*
360+
*
361+
* ~~~
362+
*
363+
* use core::rand::RngUtil;
364+
*
365+
* fn main() {
366+
* rng = rand::Rng();
367+
* println(fmt!("%?",rng.shuffle([1,2,3])));
368+
* }
369+
* ~~~
370+
*/
221371
fn shuffle<T:Copy>(&self, values: &[T]) -> ~[T];
222-
/// Shuffle a mutable vec in place
372+
/**
373+
* Shuffle a mutable vec in place
374+
*
375+
* *Example*
376+
*
377+
* ~~~
378+
*
379+
* use core::rand::RngUtil;
380+
*
381+
* fn main() {
382+
* rng = rand::Rng();
383+
* let mut y = [1,2,3];
384+
* rng.shuffle_mut(y);
385+
* println(fmt!("%?",y));
386+
* rng.shuffle_mut(y);
387+
* println(fmt!("%?",y));
388+
* }
389+
* ~~~
390+
*/
223391
fn shuffle_mut<T>(&self, values: &mut [T]);
224392
}
225393

0 commit comments

Comments
 (0)