Merge branch 'shuffle-subset' of github.com:PrajitR/d3 into shuffle-subset
This commit is contained in:
Коммит
fcd87f6b34
|
@ -163,11 +163,15 @@
|
||||||
return d3_ascending(f(d), x);
|
return d3_ascending(f(d), x);
|
||||||
} : f);
|
} : f);
|
||||||
};
|
};
|
||||||
d3.shuffle = function(array) {
|
d3.shuffle = function(array, i0, i1) {
|
||||||
var m = array.length, t, i;
|
if ((m = arguments.length) < 3) {
|
||||||
|
i1 = array.length;
|
||||||
|
if (m < 2) i0 = 0;
|
||||||
|
}
|
||||||
|
var m = i1 - i0, t, i;
|
||||||
while (m) {
|
while (m) {
|
||||||
i = Math.random() * m-- | 0;
|
i = Math.random() * m-- | 0;
|
||||||
t = array[m], array[m] = array[i], array[i] = t;
|
t = array[m + i0], array[m + i0] = array[i + i0], array[i + i0] = t;
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
};
|
};
|
||||||
|
|
Различия файлов скрыты, потому что одна или несколько строк слишком длинны
|
@ -1,8 +1,9 @@
|
||||||
d3.shuffle = function(array) {
|
d3.shuffle = function(array, i0, i1) {
|
||||||
var m = array.length, t, i;
|
if ((m = arguments.length) < 3) { i1 = array.length; if (m < 2) i0 = 0; }
|
||||||
|
var m = i1 - i0, t, i;
|
||||||
while (m) {
|
while (m) {
|
||||||
i = Math.random() * m-- | 0;
|
i = Math.random() * m-- | 0;
|
||||||
t = array[m], array[m] = array[i], array[i] = t;
|
t = array[m + i0], array[m + i0] = array[i + i0], array[i + i0] = t;
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
};
|
};
|
||||||
|
|
|
@ -0,0 +1,43 @@
|
||||||
|
var vows = require("vows"),
|
||||||
|
load = require("../load"),
|
||||||
|
assert = require("../assert"),
|
||||||
|
seedrandom = require("seedrandom");
|
||||||
|
|
||||||
|
var suite = vows.describe("d3.shuffle");
|
||||||
|
|
||||||
|
var _random;
|
||||||
|
|
||||||
|
suite.addBatch({
|
||||||
|
"shuffle": {
|
||||||
|
topic: load("arrays/shuffle").sandbox({Math: Math}).expression("d3.shuffle"),
|
||||||
|
"(using seedrandom)": {
|
||||||
|
topic: function(random) {
|
||||||
|
_random = Math.random;
|
||||||
|
return random;
|
||||||
|
},
|
||||||
|
"shuffles an array in-place": function(shuffle) {
|
||||||
|
Math.seedrandom("a random seed.");
|
||||||
|
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||||
|
assert.strictEqual(shuffle(array), array);
|
||||||
|
assert.deepEqual(array, [6, 4, 7, 9, 3, 1, 5, 8, 0, 2]);
|
||||||
|
},
|
||||||
|
"shuffles a subset of an array from the specified start": function(shuffle) {
|
||||||
|
Math.seedrandom("a random seed.");
|
||||||
|
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||||
|
assert.strictEqual(shuffle(array, 4), array);
|
||||||
|
assert.deepEqual(array, [0, 1, 2, 3, 9, 7, 6, 8, 4, 5]);
|
||||||
|
},
|
||||||
|
"shuffles a subset of an array between the specified start and end": function(shuffle) {
|
||||||
|
Math.seedrandom("a random seed.");
|
||||||
|
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||||
|
assert.strictEqual(shuffle(array, 3, 8), array);
|
||||||
|
assert.deepEqual(array, [0, 1, 2, 5, 7, 6, 3, 4, 8, 9]);
|
||||||
|
},
|
||||||
|
teardown: function() {
|
||||||
|
Math.random = _random;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
suite.export(module);
|
|
@ -5,6 +5,8 @@ var vows = require("vows"),
|
||||||
|
|
||||||
var suite = vows.describe("d3.random");
|
var suite = vows.describe("d3.random");
|
||||||
|
|
||||||
|
var _random;
|
||||||
|
|
||||||
// Testing a random number generator is a bit more complicated than testing
|
// Testing a random number generator is a bit more complicated than testing
|
||||||
// deterministic code, so we use different techniques.
|
// deterministic code, so we use different techniques.
|
||||||
//
|
//
|
||||||
|
|
Загрузка…
Ссылка в новой задаче