main.js (1776B)
1 /** 2 * @license Apache-2.0 3 * 4 * Copyright (c) 2018 The Stdlib Authors. 5 * 6 * Licensed under the Apache License, Version 2.0 (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, software 13 * distributed under the License is distributed on an "AS IS" BASIS, 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 'use strict'; 20 21 // MODULES // 22 23 var recurse = require( './recurse.js' ); 24 25 26 // MAIN // 27 28 /** 29 * Converts an ndarray buffer to a generic array (which may include nested arrays). 30 * 31 * @param {(ArrayLikeObject|TypedArray|Buffer)} buffer - data buffer 32 * @param {NonNegativeIntegerArray} shape - array shape 33 * @param {IntegerArray} strides - array strides 34 * @param {NonNegativeInteger} offset - index offset 35 * @param {string} order - specifies whether an array is row-major (C-style) or column-major (Fortran-style) 36 * @returns {(EmptyArray|Array|Array<Array>)} array (which may include nested arrays) 37 * 38 * @example 39 * var buffer = [ 1, 2, 3, 4 ]; 40 * var shape = [ 2, 2 ]; 41 * var order = 'row-major'; 42 * var strides = [ 2, 1 ]; 43 * var offset = 0; 44 * 45 * var out = ndarray2array( buffer, shape, strides, offset, order ); 46 * // returns [ [ 1, 2 ], [ 3, 4 ] ] 47 */ 48 function ndarray2array( buffer, shape, strides, offset, order ) { 49 var i; 50 if ( shape.length === 0 ) { 51 return []; 52 } 53 for ( i = 0; i < shape.length; i++ ) { 54 if ( shape[ i ] === 0 ) { 55 return []; 56 } 57 } 58 return recurse( buffer, shape, strides, offset, order, 0 ); 59 } 60 61 62 // EXPORTS // 63 64 module.exports = ndarray2array;