time-to-botec

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

main.js (1660B)


      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 * Returns array iteration order.
     25 *
     26 * ## Notes
     27 *
     28 * -   Return value key:
     29 *
     30 *     -   `0`: unordered (i.e., strides of mixed sign; e.g., `[ 9, -3, 1 ]`)
     31 *     -   `1`: ordered left-to-right (i.e., all nonnegative strides)
     32 *     -   `-1`: ordered right-to-left (i.e., all negative strides)
     33 *
     34 * @param {IntegerArray} strides - stride array
     35 * @returns {integer} iteration order
     36 *
     37 * @example
     38 * var o = iterationOrder( [ 2, 1 ] );
     39 * // returns 1
     40 *
     41 * o = iterationOrder( [ -2, 1 ] );
     42 * // returns 0
     43 *
     44 * o = iterationOrder( [ -2, -1 ] );
     45 * // returns -1
     46 */
     47 function iterationOrder( strides ) {
     48 	var cnt;
     49 	var i;
     50 
     51 	cnt = 0;
     52 	for ( i = 0; i < strides.length; i++ ) {
     53 		if ( strides[ i ] < 0 ) {
     54 			cnt += 1;
     55 		}
     56 	}
     57 	if ( cnt === 0 ) {
     58 		// All nonnegative strides:
     59 		return 1|0; // asm-type annotation
     60 	}
     61 	if ( cnt === strides.length ) {
     62 		// All negative strides:
     63 		return -1|0; // asm-type annotation
     64 	}
     65 	// Strides of mixed signs:
     66 	return 0|0; // asm-type annotation
     67 }
     68 
     69 
     70 // EXPORTS //
     71 
     72 module.exports = iterationOrder;