time-to-botec

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

main.js (1844B)


      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 abs = require( '@stdlib/math/base/special/abs' );
     24 
     25 
     26 // MAIN //
     27 
     28 /**
     29 * Determines the order of a multidimensional array based on a provided stride array.
     30 *
     31 * @param {IntegerArray} strides - stride array
     32 * @returns {integer} order
     33 *
     34 * @example
     35 * var strides2order = require( '@stdlib/ndarray/base/strides2order' );
     36 *
     37 * var order = strides2order( [ 2, 1 ] );
     38 * // returns 1
     39 *
     40 * order = strides2order( [ 1, 2 ] );
     41 * // returns 2
     42 *
     43 * order = strides2order( [ 1, 1, 1 ] );
     44 * // returns 3
     45 *
     46 * order = strides2order( [ 2, 3, 1 ] );
     47 * // returns 0
     48 */
     49 function strides2order( strides ) {
     50 	var column;
     51 	var ndims;
     52 	var row;
     53 	var s1;
     54 	var s2;
     55 	var i;
     56 
     57 	ndims = strides.length;
     58 	if ( ndims === 0 ) {
     59 		return 0|0; // 'none'
     60 	}
     61 	column = true;
     62 	row = true;
     63 
     64 	s1 = abs( strides[ 0 ] );
     65 	for ( i = 1; i < ndims; i++ ) {
     66 		s2 = abs( strides[ i ] );
     67 		if ( column && s2 < s1 ) {
     68 			column = false;
     69 		} else if ( row && s2 > s1 ) {
     70 			row = false;
     71 		}
     72 		if ( row || column ) {
     73 			s1 = s2;
     74 		} else {
     75 			return 0|0; // 'none'
     76 		}
     77 	}
     78 	if ( row && column ) {
     79 		return 3|0; // 'both'
     80 	}
     81 	if ( row ) {
     82 		return 1|0; // 'row-major'
     83 	}
     84 	return 2|0; // 'column-major'
     85 }
     86 
     87 
     88 // EXPORTS //
     89 
     90 module.exports = strides2order;