mirror of
https://gitea.wildfiregames.com/0ad/0ad.git
synced 2026-07-20 22:45:58 +00:00
e95f4e9744
Delete clone globalscripts function introduced by 9f47ed536d.
Rename the better supported deepcopy function to clone.
Delete unused Vector2D and Vector3D clone prototype functions that can
just use clone if needed.
Differential Revision: https://code.wildfiregames.com/D870
Reviewed By: leper
This was SVN commit r20125.
62 lines
1.1 KiB
JavaScript
62 lines
1.1 KiB
JavaScript
/**
|
|
* "Inside-out" implementation of Fisher-Yates shuffle
|
|
*/
|
|
function shuffleArray(source)
|
|
{
|
|
if (!source.length)
|
|
return [];
|
|
|
|
let result = [source[0]];
|
|
for (let i = 1; i < source.length; ++i)
|
|
{
|
|
let j = randIntInclusive(0, i);
|
|
result[i] = result[j];
|
|
result[j] = source[i];
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/**
|
|
* Generates each permutation of the given array and runs the callback function on it.
|
|
* Creating arrays with all permutations of the given array has a bad memory footprint.
|
|
* Algorithm by B. R. Heap. Changes the input array.
|
|
*/
|
|
function heapsPermute(array, callback)
|
|
{
|
|
let c = new Array(array.length).fill(0);
|
|
|
|
callback(clone(array));
|
|
|
|
let i = 0;
|
|
while (i < array.length)
|
|
{
|
|
if (c[i] < i)
|
|
{
|
|
let swapIndex = i % 2 ? c[i] : 0;
|
|
let swapValue = clone(array[swapIndex]);
|
|
array[swapIndex] = array[i];
|
|
array[i] = swapValue;
|
|
|
|
callback(clone(array));
|
|
|
|
++c[i];
|
|
i = 0;
|
|
}
|
|
else
|
|
{
|
|
c[i] = 0;
|
|
++i;
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Removes prefixing path from a path or filename, leaving just the file's name (with extension)
|
|
*
|
|
* ie. a/b/c/file.ext -> file.ext
|
|
*/
|
|
function basename(path)
|
|
{
|
|
return path.slice(path.lastIndexOf("/") + 1);
|
|
}
|