From 99a4d814b4346b0c65c59aca90264852846c5cd7 Mon Sep 17 00:00:00 2001 From: Taras gaidukov Date: Mon, 26 Feb 2018 13:24:07 +0200 Subject: [PATCH] Faster implementation of reshapeList --- src/tensor.js | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/tensor.js b/src/tensor.js index cfee72a..9194b75 100644 --- a/src/tensor.js +++ b/src/tensor.js @@ -154,18 +154,17 @@ function flattenList(list) { } function reshapeList(list, shape) { - // This modifies the list in place, given this is run on a temporary list; hence avoiding - // copying cost. - // Work from the inner-most dimension to outer-most, building up arrays of items matching // dimension length (this is essentially the inverse of the tensorToList implementation). for (let i = shape.length - 1; i > 0; i--) { let dimension = shape[i]; + const newList = []; - for (let j = 0; j < list.length; j++) { - let items = list.splice(j, dimension); - list.splice(j, 0, items); + for (let j = 0; j < list.length / dimension; j++) { + newList.push(list.slice(j * dimension, (j + 1) * dimension)); } + + list = newList; } return list;