auto merge of #6036 : huonw/rust/core-less-at, r=nikomatsakis
From a cursory `git grep` this removes the last part of `core` that requires on `@` (other than `io` and the task local data section). It renames `RandRes` to ~~StdRng~~ `IsaacRng` and `XorShiftState` to `XorShiftRng` as well as moving their constructors to static methods. To go with this, it adds `rng()` which is designed to be used when the programmer just wants a random number generator, without caring about which exact algorithm is being used. It also removes all the `gen_int`, `gen_uint`, `gen_char` (etc) methods on `RngUtil` (by moving the defintions to the actual `Rand` instances). The replacement is using `RngUtil::gen`, either type-inferred or with an annotation (`rng.gen::<uint>()`). I tried to have the `Rng` and `RngUtil` traits exported by `core::prelude` (since `core::rand` (except for `random()`) is useless without them), but this caused [an explosion of (seemingly unrelated) `error: unresolved import`'s](https://gist.github.com/5451839).
This commit is contained in:
commit
e26f992d5e
20 changed files with 399 additions and 540 deletions
|
|
@ -15,6 +15,7 @@ use std::time;
|
|||
use std::treemap::TreeMap;
|
||||
use core::hashmap::{HashMap, HashSet};
|
||||
use core::trie::TrieMap;
|
||||
use core::rand::Rng;
|
||||
|
||||
fn timed(label: &str, f: &fn()) {
|
||||
let start = time::precise_time_s();
|
||||
|
|
@ -102,7 +103,7 @@ fn main() {
|
|||
let mut rand = vec::with_capacity(n_keys);
|
||||
|
||||
{
|
||||
let rng = core::rand::seeded_rng([1, 1, 1, 1, 1, 1, 1]);
|
||||
let rng = core::rand::IsaacRng::new_seeded([1, 1, 1, 1, 1, 1, 1]);
|
||||
let mut set = HashSet::new();
|
||||
while set.len() != n_keys {
|
||||
let next = rng.next() as uint;
|
||||
|
|
|
|||
|
|
@ -12,7 +12,6 @@ extern mod std;
|
|||
use core::hashmap::HashSet;
|
||||
use std::bitv::BitvSet;
|
||||
use std::treemap::TreeSet;
|
||||
use core::io::WriterUtil;
|
||||
|
||||
struct Results {
|
||||
sequential_ints: float,
|
||||
|
|
@ -32,7 +31,7 @@ fn timed(result: &mut float, op: &fn()) {
|
|||
}
|
||||
|
||||
pub impl Results {
|
||||
fn bench_int<T:Set<uint>>(&mut self, rng: @rand::Rng, num_keys: uint,
|
||||
fn bench_int<T:Set<uint>, R: rand::Rng>(&mut self, rng: &R, num_keys: uint,
|
||||
rand_cap: uint, f: &fn() -> T) {
|
||||
{
|
||||
let mut set = f();
|
||||
|
|
@ -70,8 +69,8 @@ pub impl Results {
|
|||
}
|
||||
}
|
||||
|
||||
fn bench_str<T:Set<~str>>(&mut self, rng: @rand::Rng, num_keys: uint,
|
||||
f: &fn() -> T) {
|
||||
fn bench_str<T:Set<~str>, R: rand::Rng>(&mut self, rng: &R, num_keys: uint,
|
||||
f: &fn() -> T) {
|
||||
{
|
||||
let mut set = f();
|
||||
do timed(&mut self.sequential_strings) {
|
||||
|
|
@ -156,25 +155,25 @@ fn main() {
|
|||
let max = 200000;
|
||||
|
||||
{
|
||||
let rng = rand::seeded_rng(seed);
|
||||
let rng = rand::IsaacRng::new_seeded(seed);
|
||||
let mut results = empty_results();
|
||||
results.bench_int(rng, num_keys, max, || HashSet::new::<uint>());
|
||||
results.bench_str(rng, num_keys, || HashSet::new::<~str>());
|
||||
results.bench_int(&rng, num_keys, max, || HashSet::new::<uint>());
|
||||
results.bench_str(&rng, num_keys, || HashSet::new::<~str>());
|
||||
write_results("core::hashmap::HashSet", &results);
|
||||
}
|
||||
|
||||
{
|
||||
let rng = rand::seeded_rng(seed);
|
||||
let rng = rand::IsaacRng::new_seeded(seed);
|
||||
let mut results = empty_results();
|
||||
results.bench_int(rng, num_keys, max, || TreeSet::new::<uint>());
|
||||
results.bench_str(rng, num_keys, || TreeSet::new::<~str>());
|
||||
results.bench_int(&rng, num_keys, max, || TreeSet::new::<uint>());
|
||||
results.bench_str(&rng, num_keys, || TreeSet::new::<~str>());
|
||||
write_results("std::treemap::TreeSet", &results);
|
||||
}
|
||||
|
||||
{
|
||||
let rng = rand::seeded_rng(seed);
|
||||
let rng = rand::IsaacRng::new_seeded(seed);
|
||||
let mut results = empty_results();
|
||||
results.bench_int(rng, num_keys, max, || BitvSet::new());
|
||||
results.bench_int(&rng, num_keys, max, || BitvSet::new());
|
||||
write_results("std::bitv::BitvSet", &results);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,8 +13,6 @@
|
|||
extern mod std;
|
||||
|
||||
use std::time::precise_time_s;
|
||||
|
||||
use core::io::{Reader, ReaderUtil};
|
||||
use core::rand::RngUtil;
|
||||
|
||||
macro_rules! bench (
|
||||
|
|
@ -71,13 +69,13 @@ fn read_line() {
|
|||
}
|
||||
|
||||
fn vec_plus() {
|
||||
let r = rand::Rng();
|
||||
let r = rand::rng();
|
||||
|
||||
let mut v = ~[];
|
||||
let mut i = 0;
|
||||
while i < 1500 {
|
||||
let rv = vec::from_elem(r.gen_uint_range(0, i + 1), i);
|
||||
if r.gen_bool() {
|
||||
if r.gen() {
|
||||
v += rv;
|
||||
}
|
||||
else {
|
||||
|
|
@ -88,13 +86,13 @@ fn vec_plus() {
|
|||
}
|
||||
|
||||
fn vec_append() {
|
||||
let r = rand::Rng();
|
||||
let r = rand::rng();
|
||||
|
||||
let mut v = ~[];
|
||||
let mut i = 0;
|
||||
while i < 1500 {
|
||||
let rv = vec::from_elem(r.gen_uint_range(0, i + 1), i);
|
||||
if r.gen_bool() {
|
||||
if r.gen() {
|
||||
v = vec::append(v, rv);
|
||||
}
|
||||
else {
|
||||
|
|
@ -105,12 +103,12 @@ fn vec_append() {
|
|||
}
|
||||
|
||||
fn vec_push_all() {
|
||||
let r = rand::Rng();
|
||||
let r = rand::rng();
|
||||
|
||||
let mut v = ~[];
|
||||
for uint::range(0, 1500) |i| {
|
||||
let mut rv = vec::from_elem(r.gen_uint_range(0, i + 1), i);
|
||||
if r.gen_bool() {
|
||||
if r.gen() {
|
||||
v.push_all(rv);
|
||||
}
|
||||
else {
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@ use std::time;
|
|||
use std::deque::Deque;
|
||||
use std::par;
|
||||
use core::hashmap::{HashMap, HashSet};
|
||||
use core::io::WriterUtil;
|
||||
use core::int::abs;
|
||||
use core::rand::RngUtil;
|
||||
|
||||
|
|
@ -34,9 +33,9 @@ type graph = ~[~[node_id]];
|
|||
type bfs_result = ~[node_id];
|
||||
|
||||
fn make_edges(scale: uint, edgefactor: uint) -> ~[(node_id, node_id)] {
|
||||
let r = rand::xorshift();
|
||||
let r = rand::XorShiftRng::new();
|
||||
|
||||
fn choose_edge(i: node_id, j: node_id, scale: uint, r: @rand::Rng)
|
||||
fn choose_edge<R: rand::Rng>(i: node_id, j: node_id, scale: uint, r: &R)
|
||||
-> (node_id, node_id) {
|
||||
|
||||
let A = 0.57;
|
||||
|
|
@ -51,7 +50,7 @@ fn make_edges(scale: uint, edgefactor: uint) -> ~[(node_id, node_id)] {
|
|||
let j = j * 2i64;
|
||||
let scale = scale - 1u;
|
||||
|
||||
let x = r.gen_float();
|
||||
let x = r.gen::<float>();
|
||||
|
||||
if x < A {
|
||||
choose_edge(i, j, scale, r)
|
||||
|
|
@ -75,7 +74,7 @@ fn make_edges(scale: uint, edgefactor: uint) -> ~[(node_id, node_id)] {
|
|||
}
|
||||
|
||||
do vec::from_fn((1u << scale) * edgefactor) |_i| {
|
||||
choose_edge(0i64, 0i64, scale, r)
|
||||
choose_edge(0i64, 0i64, scale, &r)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -105,7 +104,7 @@ fn make_graph(N: uint, edges: ~[(node_id, node_id)]) -> graph {
|
|||
|
||||
fn gen_search_keys(graph: &[~[node_id]], n: uint) -> ~[node_id] {
|
||||
let mut keys = HashSet::new();
|
||||
let r = rand::Rng();
|
||||
let r = rand::rng();
|
||||
|
||||
while keys.len() < n {
|
||||
let k = r.gen_uint_range(0u, graph.len());
|
||||
|
|
|
|||
|
|
@ -13,8 +13,8 @@ fn lerp(a: f32, b: f32, v: f32) -> f32 { a * (1.0 - v) + b * v }
|
|||
#[inline(always)]
|
||||
fn smooth(v: f32) -> f32 { v * v * (3.0 - 2.0 * v) }
|
||||
|
||||
fn random_gradient(r: @Rng) -> Vec2 {
|
||||
let v = r.gen_float() * float::consts::pi * 2.0;
|
||||
fn random_gradient<R:Rng>(r: &R) -> Vec2 {
|
||||
let v = 2.0 * float::consts::pi * r.gen();
|
||||
Vec2 {
|
||||
x: float::cos(v) as f32,
|
||||
y: float::sin(v) as f32,
|
||||
|
|
@ -33,9 +33,9 @@ struct Noise2DContext {
|
|||
|
||||
pub impl Noise2DContext {
|
||||
fn new() -> Noise2DContext {
|
||||
let r = rand::Rng();
|
||||
let r = rand::rng();
|
||||
let mut rgradients = [ Vec2 { x: 0.0, y: 0.0 }, ..256 ];
|
||||
for int::range(0, 256) |i| { rgradients[i] = random_gradient(r); }
|
||||
for int::range(0, 256) |i| { rgradients[i] = random_gradient(&r); }
|
||||
let mut permutations = [ 0, ..256 ];
|
||||
for int::range(0, 256) |i| { permutations[i] = i; }
|
||||
r.shuffle_mut(permutations);
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@
|
|||
* http://shootout.alioth.debian.org/
|
||||
*/
|
||||
extern mod std;
|
||||
use core::io::WriterUtil;
|
||||
use core::rand::Rng;
|
||||
|
||||
fn LINE_LENGTH() -> uint { return 60u; }
|
||||
|
||||
|
|
@ -63,7 +63,7 @@ fn make_random_fasta(wr: @io::Writer,
|
|||
genelist: ~[AminoAcids],
|
||||
n: int) {
|
||||
wr.write_line(~">" + id + ~" " + desc);
|
||||
let rng = @mut MyRandom {last: rand::Rng().next()};
|
||||
let rng = @mut MyRandom {last: rand::rng().next()};
|
||||
let mut op: ~str = ~"";
|
||||
for uint::range(0u, n as uint) |_i| {
|
||||
str::push_char(&mut op, select_random(myrandom_next(rng, 100u32),
|
||||
|
|
|
|||
|
|
@ -54,6 +54,7 @@ fn runtest2(f: extern fn(), frame_backoff: u32, last_stk: *u8) -> u32 {
|
|||
}
|
||||
|
||||
pub fn main() {
|
||||
use core::rand::Rng;
|
||||
let fns = ~[
|
||||
calllink01,
|
||||
calllink02,
|
||||
|
|
@ -61,7 +62,7 @@ pub fn main() {
|
|||
calllink09,
|
||||
calllink10
|
||||
];
|
||||
let rng = rand::Rng();
|
||||
let rng = rand::rng();
|
||||
for fns.each |f| {
|
||||
let f = *f;
|
||||
let sz = rng.next() % 256u32 + 256u32;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue