time-to-botec

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

main.js (2027B)


      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 // FUNCTIONS //
     22 
     23 /**
     24 * Generates a stride array from an array shape (row-major).
     25 *
     26 * @private
     27 * @param {NonNegativeIntegerArray} shape - array shape
     28 * @returns {Array} array strides
     29 */
     30 function rowmajor( shape ) {
     31 	var ndims;
     32 	var out;
     33 	var s;
     34 	var i;
     35 
     36 	ndims = shape.length;
     37 	out = [];
     38 	for ( i = 0; i < ndims; i++ ) {
     39 		out.push( 0 );
     40 	}
     41 	s = 1;
     42 	for ( i = ndims-1; i >= 0; i-- ) {
     43 		out[ i ] = s;
     44 		s *= shape[ i ];
     45 	}
     46 	return out;
     47 }
     48 
     49 /**
     50 * Generates a stride array from an array shape (column-major).
     51 *
     52 * @private
     53 * @param {NonNegativeIntegerArray} shape - array shape
     54 * @returns {Array} array strides
     55 */
     56 function columnmajor( shape ) {
     57 	var out;
     58 	var s;
     59 	var i;
     60 
     61 	out = [];
     62 	s = 1;
     63 	for ( i = 0; i < shape.length; i++ ) {
     64 		out.push( s );
     65 		s *= shape[ i ];
     66 	}
     67 	return out;
     68 }
     69 
     70 
     71 // MAIN //
     72 
     73 /**
     74 * Generates a stride array from an array shape.
     75 *
     76 * @param {NonNegativeIntegerArray} shape - array shape
     77 * @param {string} order - specifies whether an array is row-major (C-style) or column-major (Fortran-style)
     78 * @returns {Array} array strides
     79 *
     80 * @example
     81 * var s = shape2strides( [ 3, 2 ], 'row-major' );
     82 * // returns [ 2, 1 ]
     83 *
     84 * s = shape2strides( [ 3, 2 ], 'column-major' );
     85 * // returns [ 1, 3 ]
     86 */
     87 function shape2strides( shape, order ) {
     88 	if ( order === 'column-major' ) {
     89 		return columnmajor( shape );
     90 	}
     91 	return rowmajor( shape );
     92 }
     93 
     94 
     95 // EXPORTS //
     96 
     97 module.exports = shape2strides;