Add xorshift to core::rand, which gave a 3x speedup for graph generation in the bfs code. Also, remove trailing white space.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
#[doc = "Random number generation"];
|
#[doc = "Random number generation"];
|
||||||
|
|
||||||
export rng, seed, seeded_rng, weighted, extensions;
|
export rng, seed, seeded_rng, weighted, extensions;
|
||||||
|
export xorshift, seeded_xorshift;
|
||||||
|
|
||||||
enum rctx {}
|
enum rctx {}
|
||||||
|
|
||||||
@@ -253,6 +254,35 @@ fn seeded_rng(seed: [u8]) -> rng {
|
|||||||
@rand_res(rustrt::rand_new_seeded(seed)) as rng
|
@rand_res(rustrt::rand_new_seeded(seed)) as rng
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type xorshift_state = {
|
||||||
|
mut x: u32,
|
||||||
|
mut y: u32,
|
||||||
|
mut z: u32,
|
||||||
|
mut w: u32
|
||||||
|
};
|
||||||
|
|
||||||
|
impl of rng for xorshift_state {
|
||||||
|
fn next() -> u32 {
|
||||||
|
let x = self.x;
|
||||||
|
let mut t = x ^ (x << 11);
|
||||||
|
self.x = self.y;
|
||||||
|
self.y = self.z;
|
||||||
|
self.z = self.w;
|
||||||
|
let w = self.w;
|
||||||
|
self.w = w ^ (w >> 19) ^ (t ^ (t >> 8));
|
||||||
|
self.w
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn xorshift() -> rng {
|
||||||
|
// constants taken from http://en.wikipedia.org/wiki/Xorshift
|
||||||
|
seeded_xorshift(123456789u32, 362436069u32, 521288629u32, 88675123u32)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn seeded_xorshift(x: u32, y: u32, z: u32, w: u32) -> rng {
|
||||||
|
{mut x: x, mut y: y, mut z: z, mut w: w} as rng
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
|
|||||||
@@ -129,7 +129,7 @@ fn alli<A: copy send>(xs: [A], f: fn~(uint, A) -> bool) -> bool {
|
|||||||
|
|
||||||
#[doc="Returns true if the function holds for any elements in the vector."]
|
#[doc="Returns true if the function holds for any elements in the vector."]
|
||||||
fn any<A: copy send>(xs: [A], f: fn~(A) -> bool) -> bool {
|
fn any<A: copy send>(xs: [A], f: fn~(A) -> bool) -> bool {
|
||||||
vec::any(map_slices(xs) {||
|
vec::any(map_slices(xs) {||
|
||||||
fn~(_base : uint, slice: [const A]/&, copy f) -> bool {
|
fn~(_base : uint, slice: [const A]/&, copy f) -> bool {
|
||||||
vec::any(slice, f)
|
vec::any(slice, f)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,7 @@ type graph = [[node_id]];
|
|||||||
type bfs_result = [node_id];
|
type bfs_result = [node_id];
|
||||||
|
|
||||||
fn make_edges(scale: uint, edgefactor: uint) -> [(node_id, node_id)] {
|
fn make_edges(scale: uint, edgefactor: uint) -> [(node_id, node_id)] {
|
||||||
let r = rand::rng();
|
let r = rand::xorshift();
|
||||||
|
|
||||||
fn choose_edge(i: node_id, j: node_id, scale: uint, r: rand::rng)
|
fn choose_edge(i: node_id, j: node_id, scale: uint, r: rand::rng)
|
||||||
-> (node_id, node_id) {
|
-> (node_id, node_id) {
|
||||||
@@ -247,12 +247,12 @@ fn pbfs(&&graph: arc::arc<graph>, key: node_id) -> bfs_result {
|
|||||||
white {
|
white {
|
||||||
let i = i as node_id;
|
let i = i as node_id;
|
||||||
|
|
||||||
let neighbors = (*graph)[i];
|
let neighbors = graph[i];
|
||||||
|
|
||||||
let mut color = white;
|
let mut color = white;
|
||||||
|
|
||||||
neighbors.each() {|k|
|
neighbors.each() {|k|
|
||||||
if is_gray((*colors)[k]) {
|
if is_gray(colors[k]) {
|
||||||
color = gray(k);
|
color = gray(k);
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user