Permalink
Browse files

Move the knuth-fisher-yates shuffle implementation to the Random libr…

…ary. Update it to use the twister. Make Random a dependency of Query
  • Loading branch information...
1 parent 6b0ea74 commit 7d8585d2b1a7c434212d5a3c9ed308378157d0cb @Whiteknight Whiteknight committed Nov 25, 2011
Showing with 16 additions and 16 deletions.
  1. +1 −1 setup.winxed
  2. +1 −0 src/include/Random.winxed
  3. +1 −1 src/query/Provider.winxed
  4. +0 −14 src/query/Query.winxed
  5. +13 −0 src/unstable/random/Random.winxed
View
@@ -188,7 +188,7 @@ function setup_stable_libraries(var rosella)
);
// A library for working with aggregates
- setup_winxed_lib(rosella, "query", ["Core"],
+ setup_winxed_lib(rosella, "query", ["Core", "Random"],
"query/Provider",
"query/provider/Array",
"query/provider/Hash",
@@ -6,6 +6,7 @@ namespace Rosella.Random {
extern function __ln;
extern function default_uniform_random;
extern function default_normal_random;
+ extern function knuth_fisher_yates_shuffle;
}
function __load_random[anon, load, init]()
@@ -184,7 +184,7 @@ class Rosella.Query.Provider
function shuffle(var data)
{
var new_data = self.to_array(data);
- Rosella.Query.knuth_fisher_yates_shuffle(new_data);
+ Rosella.Random.knuth_fisher_yates_shuffle(new_data);
return new_data;
}
View
@@ -87,20 +87,6 @@ namespace Rosella.Query
}
}
- // Perform a random sort of an array in-place
- function knuth_fisher_yates_shuffle(var d)
- {
- int i = elements(d);
- var rand = new 'Integer';
- for (int i = elements(d) - 1; i > 0; i--) {
- rand.set_random(i + 1);
- int n = rand;
- var v = d[i];
- d[i] = d[n];
- d[n] = v;
- }
- }
-
// Install some common Query methods to the given type. Useful for
// built-in array and hash types.
function install_to_type(var type)
@@ -23,4 +23,17 @@ namespace Rosella.Random
{
return new Rosella.Random.BoxMullerNormal(center, stddev);
}
+
+ // Perform a random sort of an array in-place
+ function knuth_fisher_yates_shuffle(var d)
+ {
+ int i = elements(d);
+ var rand = default_uniform_random();
+ for (int i = elements(d) - 1; i > 0; i--) {
+ int n = rand.get_range(0, i + 1);
+ var v = d[i];
+ d[i] = d[n];
+ d[n] = v;
+ }
+ }
}

0 comments on commit 7d8585d

Please sign in to comment.