All files main.js

98.62% Statements 143/145
93.33% Branches 14/15
100% Functions 2/2
98.62% Lines 143/145

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 1461x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 4x 4x 3x 3x 1x 1x 1x     4x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 9x 26x 26x 9x 9x 3x 3x 3x 9x 1x 1x 1x 1x 1x 1x 1x 5x 9x 2x 2x 9x 1x 1x 2x 2x 9x 1x 1x 1x 1x 1x  
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
'use strict';
 
// MODULES //
 
var strides2order = require( '@stdlib/ndarray/base/strides2order' );
var zeros = require( '@stdlib/array/base/zeros' );
var defaults = require( '@stdlib/ndarray/defaults' );
 
 
// VARIABLES //
 
var DEFAULT_ORDER = defaults.get( 'order' );
 
 
// FUNCTIONS //
 
/**
* Resolves a storage layout string from a layout integer.
*
* @private
* @param {NonNegativeInteger} layout - layout integer
* @returns {string} layout string
*
* @example
* var order = resolveString( 0 );
* // returns <string>
*
* @example
* var order = resolveString( 1 );
* // returns 'row-major'
*
* @example
* var order = resolveString( 2 );
* // returns 'column-major'
*
* @example
* var order = resolveString( 3 );
* // returns <string>
*/
function resolveString( layout ) {
	if ( layout === 1 ) {
		return 'row-major';
	}
	if ( layout === 2 ) {
		return 'column-major';
	}
	// layout === 0 || layout === 3
	return DEFAULT_ORDER;
}
 
 
// MAIN //
 
/**
* Resolves the most common underlying storage layout.
*
* @param {ArrayLikeObject<Collection>} strides - list of strides array
* @returns {string} storage layout
*
* @example
* var strides = [ [ 2, 1 ], [ 4, 1 ] ];
*
* var order = consensusOrder( strides );
* // returns 'row-major'
*
* @example
* var strides = [ [ 1, 2 ], [ 1, 4 ] ];
*
* var order = consensusOrder( strides );
* // returns 'column-major'
*
* @example
* var strides = [ [ 2, 1 ], [ 1, 4 ] ];
*
* var order = consensusOrder( strides );
* // returns <string>
*
* @example
* var strides = [ [ 1, 1 ], [ 1, 1 ] ];
*
* var order = consensusOrder( strides );
* // returns <string>
*/
function consensusOrder( strides ) {
	var tmp;
	var M;
	var N;
	var i;
 
	N = strides.length;
 
	// Initialize an array for grouping stride arrays according to memory layout:
	tmp = zeros( 4 ); // note: `4` corresponds to the number of possible return values from `strides2order`
	M = tmp.length;
 
	// Group stride arrays according to memory layout:
	for ( i = 0; i < N; i++ ) {
		tmp[ strides2order( strides[ i ] ) ] += 1;
	}
	// Determine which array should be used to determine the consensus order:
	if ( tmp[ 0 ] === N ) {
		// If all arrays are "disorganized", then fallback to the default order:
		return DEFAULT_ORDER;
	}
	if ( tmp[ 0 ] === N-1 ) {
		// If all but one array is "disorganized", find the "organized" array...
		for ( i = 1; i < M; i++ ) {
			if ( tmp[ i ] > 0 ) {
				return resolveString( i );
			}
		}
	}
	// We've got at least two "organized" arrays; find the layout which is most common...
	if ( tmp[ 1 ] > tmp[ 2 ] ) {
		return resolveString( 1 );
	}
	if ( tmp[ 1 ] < tmp[ 2 ] ) {
		return resolveString( 2 );
	}
	// tmp[ 1 ] === tmp[ 2 ]
	return DEFAULT_ORDER;
}
 
 
// EXPORTS //
 
module.exports = consensusOrder;