Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add an implementation of the knuth-fisher-yates shuffle, using the ne…

…w set_random method in Parrot
  • Loading branch information...
commit d61a7a27914d5525cf76f9d60a5a9593b16e01cf 1 parent 3f78e50
@Whiteknight authored
View
7 src/query/Provider.winxed
@@ -162,6 +162,13 @@ class Rosella.Query.Provider
return new_data;
}
+ function shuffle(var data)
+ {
+ var new_data = self.to_array(data);
+ Rosella.Query.knuth_fisher_yates_shuffle(new_data);
+ return new_data;
+ }
+
// Return the list of keys of the aggregate
function keys(var data)
{
View
13 src/query/Query.winxed
@@ -86,6 +86,19 @@ namespace Rosella.Query
}
}
+ 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;
+ }
+ }
+
function install_to_type(var type)
{
var type_class = Rosella.get_type_class(type);
View
6 src/query/Queryable.winxed
@@ -104,6 +104,12 @@ class Rosella.Query.Queryable
return self.as_queryable(result);
}
+ function shuffle()
+ {
+ var result = self.provider.shuffle(self.data);
+ return self.as_queryable(result);
+ }
+
// Return a count of elements
function count(var func [optional], int has_func [opt_flag])
{
Please sign in to comment.
Something went wrong with that request. Please try again.