Skip to content

Commit

Permalink
Add an implementation of the knuth-fisher-yates shuffle, using the ne…
Browse files Browse the repository at this point in the history
…w set_random method in Parrot
  • Loading branch information
Whiteknight committed Oct 29, 2011
1 parent 3f78e50 commit d61a7a2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/query/Provider.winxed
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ class Rosella.Query.Provider
return new_data; 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 // Return the list of keys of the aggregate
function keys(var data) function keys(var data)
{ {
Expand Down
13 changes: 13 additions & 0 deletions src/query/Query.winxed
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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) function install_to_type(var type)
{ {
var type_class = Rosella.get_type_class(type); var type_class = Rosella.get_type_class(type);
Expand Down
6 changes: 6 additions & 0 deletions src/query/Queryable.winxed
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -104,6 +104,12 @@ class Rosella.Query.Queryable
return self.as_queryable(result); return self.as_queryable(result);
} }


function shuffle()
{
var result = self.provider.shuffle(self.data);
return self.as_queryable(result);
}

// Return a count of elements // Return a count of elements
function count(var func [optional], int has_func [opt_flag]) function count(var func [optional], int has_func [opt_flag])
{ {
Expand Down

0 comments on commit d61a7a2

Please sign in to comment.