Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

array_shuffle - optimization of duplicated code #57

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 20 additions & 39 deletions scripts/yyVariable.js
Original file line number Diff line number Diff line change
Expand Up @@ -682,27 +682,27 @@ function array_sort( _array, _typeofSort )
} // end else
} // end array_sort

function array_shuffle( _array )
function array_shuffle_common( _array, _return_copy )
{
var ret = undefined;
if (Array.isArray(_array)) {

ret = _array.slice();
var currentIndex = ret.length, temporaryValue, randomIndex;
ret = (_return_copy === true) ? _array.slice() : _array;

// While there remain elements to shuffle...
while (0 !== currentIndex) {
var currentIndex = ret.length, temporaryValue, randomIndex;

// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;
// While there remain elements to shuffle...
while (0 !== currentIndex) {

// And swap it with the current element.
temporaryValue = ret[currentIndex];
ret[currentIndex] = ret[randomIndex];
ret[randomIndex] = temporaryValue;
}
// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;

// And swap it with the current element.
temporaryValue = ret[currentIndex];
ret[currentIndex] = ret[randomIndex];
ret[randomIndex] = temporaryValue;
}

} // end if
else {
Expand All @@ -711,33 +711,14 @@ function array_shuffle( _array )
return ret;
} // end array_shuffle

function array_shuffle_ext( _array )
function array_shuffle( _array )
{
var ret = undefined;
if (Array.isArray(_array)) {

ret = _array;
var currentIndex = ret.length, temporaryValue, randomIndex;

// While there remain elements to shuffle...
while (0 !== currentIndex) {

// Pick a remaining element...
randomIndex = Math.floor(Math.random() * currentIndex);
currentIndex -= 1;

// And swap it with the current element.
temporaryValue = ret[currentIndex];
ret[currentIndex] = ret[randomIndex];
ret[randomIndex] = temporaryValue;
}

return array_shuffle_common( _array, true);
} // end array_shuffle

} // end if
else {
yyError( "argument0 is not an array");
} // end else
return ret;
function array_shuffle_ext( _array )
{
return array_shuffle_common( _array, false);
} // end array_shuffle_ext


Expand Down Expand Up @@ -2501,4 +2482,4 @@ function variable_struct_remove( _id, _var)
} // end if
} // end for
} // end if
}
}