array-rearrange
Advanced tools
Comparing version 2.0.0 to 2.1.0
28
index.js
@@ -7,17 +7,26 @@ 'use strict' | ||
function reorder(arr, index, stride) { | ||
var n = index.length; | ||
var n = index.length | ||
if (!stride) stride = 1; | ||
if (!stride) { | ||
stride = Math.floor(arr.length / index.length); | ||
} | ||
if (stride === 1) { | ||
for (var i=0; i<n; i++) { | ||
reorderSimple() | ||
} | ||
else { | ||
reorderStride(stride) | ||
} | ||
function reorderSimple () { | ||
for (var i=0; i < n; i++) { | ||
while (index[i] != i) { | ||
var id = index[i] | ||
var oldId = index[id]; | ||
index[id] = id; | ||
index[i] = oldId; | ||
var value = arr[id]; | ||
arr[id] = arr[i]; | ||
index[id] = id; | ||
index[i] = oldId; | ||
arr[i] = value; | ||
@@ -27,4 +36,5 @@ } | ||
} | ||
else { | ||
for (var i=0; i<n; i+=stride) { | ||
function reorderStride (stride) { | ||
for (var i=0; i < n; i+=stride) { | ||
while (index[i] != i) { | ||
@@ -31,0 +41,0 @@ var id = index[i] |
{ | ||
"name": "array-rearrange", | ||
"version": "2.0.0", | ||
"version": "2.1.0", | ||
"description": "Rearrange array elements by index array", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
11
test.js
@@ -17,1 +17,12 @@ 'use strict' | ||
assert.deepEqual(reorder([4,4, 3,3, 2,2, 1,1], [3,2,1,0], 2), [1,1, 2,2, 3,3, 4,4]) | ||
assert.deepEqual(reorder([4,4, 3,3, 2,2, 1,1], [3,2,1,0]), [1,1, 2,2, 3,3, 4,4]) | ||
// perf | ||
let N = 1e6 | ||
let a = Array.from({length: N}, Math.random) | ||
let idx = Array.from({length: N}, (v, i) => (N - i - 1)) | ||
console.time(1) | ||
reorder(a, idx) | ||
console.timeEnd(1) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
5571
108