time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

recurse.js (1573B)


      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 // MAIN //
     22 
     23 /**
     24 * Recursively converts an ndarray to a generic array.
     25 *
     26 * @private
     27 * @param {(ArrayLikeObject|TypedArray|Buffer)} buffer - data buffer
     28 * @param {NonNegativeIntegerArray} shape - array shape
     29 * @param {IntegerArray} strides - array strides
     30 * @param {NonNegativeInteger} offset - index offset
     31 * @param {string} order - specifies whether an array is row-major (C-style) or column-major (Fortran-style)
     32 * @param {NonNegativeInteger} dim - dimension
     33 * @returns {(Array|Array<Array>)} output array
     34 */
     35 function recurse( buffer, shape, strides, offset, order, dim ) {
     36 	var stride;
     37 	var item;
     38 	var out;
     39 	var n;
     40 	var i;
     41 
     42 	if ( dim >= shape.length ) {
     43 		return buffer[ offset ];
     44 	}
     45 	out = [];
     46 
     47 	n = shape[ dim ];
     48 	stride = strides[ dim ];
     49 
     50 	for ( i = 0; i < n; i++ ) {
     51 		item = recurse( buffer, shape, strides, offset, order, dim+1 );
     52 		out.push( item );
     53 		offset += stride;
     54 	}
     55 	return out;
     56 }
     57 
     58 
     59 // EXPORTS //
     60 
     61 module.exports = recurse;